Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list: colorize output #3351

Merged
merged 1 commit into from
Mar 9, 2020
Merged

Conversation

gurobokum
Copy link
Contributor

@gurobokum gurobokum commented Feb 17, 2020

Close #3347

  • ❗ Have you followed the guidelines in the Contributing to DVC list?

  • πŸ“– Check this box if this PR does not require documentation updates, or if it does and you have created a separate PR in dvc.org with such updates (or at least opened an issue about it in that repo). Please link below to your PR (or issue) in the dvc.org repo.

  • ❌ Have you checked DeepSource, CodeClimate, and other sanity checks below? We consider their findings recommendatory and don't expect everything to be addressed. Please review them carefully and fix those that actually improve code or fix bugs.

Thank you for the contribution - we'll try to review it as soon as possible. πŸ™

@gurobokum gurobokum force-pushed the dvc_list_colorize branch 2 times, most recently from ceafb40 to 5dcca67 Compare February 17, 2020 20:08
setup.py Outdated Show resolved Hide resolved
dvc/utils/ls_colors.py Outdated Show resolved Hide resolved
dvc/utils/ls_colors.py Outdated Show resolved Hide resolved
dvc/repo/ls.py Outdated Show resolved Hide resolved
dvc/command/ls.py Outdated Show resolved Hide resolved
dvc/repo/ls.py Outdated Show resolved Hide resolved
dvc/utils/ls_colors.py Outdated Show resolved Hide resolved
@efiop
Copy link
Contributor

efiop commented Feb 18, 2020

@JIoJIaJIu Btw, we've been thinking about --show-json for dvc ls to make it useful in automated scenarios. How about we make repo.ls return a dict/list of dicts that would contain an info dict for each path? That info dict would tell if that path is an output, directory, etc. That info then could be processed and colorized properly in dvc/command/ls.py. And we'll get a cheap dvc ls ... --show-json in the future πŸ™‚

dvc/command/ls.py Outdated Show resolved Hide resolved
dvc/command/ls.py Outdated Show resolved Hide resolved
dvc/path_info.py Outdated Show resolved Hide resolved
dvc/command/ls.py Outdated Show resolved Hide resolved
dvc/repo/ls.py Outdated Show resolved Hide resolved
@efiop efiop changed the title api: colorize dvc list list: colorize output Feb 19, 2020
@gurobokum gurobokum requested review from efiop and Suor February 20, 2020 02:38
@gurobokum gurobokum changed the title list: colorize output [WIP[ list: colorize output Feb 20, 2020
@gurobokum gurobokum changed the title [WIP[ list: colorize output [WIP] list: colorize output Feb 20, 2020
dvc/command/ls/ls_colors.py Outdated Show resolved Hide resolved
@gurobokum gurobokum changed the title [WIP] list: colorize output list: colorize output Feb 20, 2020
@gurobokum gurobokum force-pushed the dvc_list_colorize branch 2 times, most recently from cd07166 to d4b0194 Compare February 20, 2020 18:48
dvc/repo/ls.py Outdated Show resolved Hide resolved
dvc/repo/ls.py Outdated Show resolved Hide resolved
dvc/repo/ls.py Outdated Show resolved Hide resolved
Copy link
Contributor

@efiop efiop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks amazing! πŸ”₯

Just a few minor comments left.

dvc/repo/ls.py Show resolved Hide resolved
dvc/command/ls/__init__.py Outdated Show resolved Hide resolved
dvc/repo/ls.py Outdated Show resolved Hide resolved
dvc/repo/ls.py Outdated Show resolved Hide resolved
dvc/repo/ls.py Outdated Show resolved Hide resolved
tests/func/test_ls.py Outdated Show resolved Hide resolved
dvc/command/ls/ls_colors.py Outdated Show resolved Hide resolved
tests/func/test_ls.py Outdated Show resolved Hide resolved
tests/unit/command/ls/test_ls_colors.py Outdated Show resolved Hide resolved
@efiop efiop requested review from pared and skshetry February 25, 2020 13:36
dvc/command/ls/__init__.py Outdated Show resolved Hide resolved
dvc/command/ls/__init__.py Outdated Show resolved Hide resolved
dvc/command/ls/__init__.py Outdated Show resolved Hide resolved
dvc/command/ls/ls_colors.py Outdated Show resolved Hide resolved
tests/func/test_ls.py Outdated Show resolved Hide resolved
Copy link
Contributor

@pared pared left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! One comment, more of a question, than a suggestion.

dvc/command/ls/ls_colors.py Outdated Show resolved Hide resolved
Copy link
Contributor

@Suor Suor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of bugs, some small optimization and cosmetics.

dvc/command/ls/ls_colors.py Outdated Show resolved Hide resolved
dvc/repo/ls.py Outdated Show resolved Hide resolved
dvc/repo/ls.py Outdated Show resolved Hide resolved
dvc/repo/ls.py Show resolved Hide resolved
dvc/repo/ls.py Outdated Show resolved Hide resolved
dvc/repo/ls.py Show resolved Hide resolved
from dvc.compat import fspath

if out:
isdir = out.is_dir_checksum if out.checksum else False
Copy link
Contributor

@Suor Suor Feb 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no checksum it might still be dir, I would write a note here about this edge case. Now the code looks quirky for no apparent reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I got if out.checksum is missed than out.is_dir_checksum fails
Do you have thoughts how can it be improved?

Copy link
Contributor

@Suor Suor Feb 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it fails, we don't really know whether that is a dir. We may just say about this limitation in a comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't really know whether that is a dir

In what case the out can be dir but doesn't have checksum?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Suor it was an open question - could you please provide the case when it can happen?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a user edits a .dvc file and deletes the output checksum there. This is not as weird as it might sound, people may want to do that to recalc the checksum from the actual dir they have.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I can imagine that user don't see folder when has been working on dvc file and doesn't understand what's going on, but it's really specific case. I can handle it in the #3394. What do you think @efiop?

tests/unit/command/ls/test_ls_colors.py Show resolved Hide resolved
Copy link
Member

@skshetry skshetry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. πŸ”₯ Just a few suggestions.

Comment on lines +14 to +25
def _prettify(entries, with_color=False):
if with_color:
ls_colors = LsColors()
fmt = ls_colors.format
else:

def fmt(entry):
return entry["path"]

return [fmt(entry) for entry in entries]


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def _prettify(entries, with_color=False):
if with_color:
ls_colors = LsColors()
fmt = ls_colors.format
else:
def fmt(entry):
return entry["path"]
return [fmt(entry) for entry in entries]
def _prettify(entries, with_color=False):
def fmt(entry):
return entry["path"]
if with_color:
ls_colors = LsColors()
fmt = ls_colors.format
return [fmt(entry) for entry in entries]

Or, maybe, even use a lambda?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've remembered why I did it as it is
with lambda there is an E371 style error

def _prettify(entries, with_color=False):
    if with_color:
        ls_colors = LsColors()
        fmt = ls_colors.format
    else:
        fmt = lambda entry: entry["path"]

    return [fmt(entry) for entry in entries]
dvc/command/ls/__init__.py:17:5: E731 do not assign a lambda expression, use a def

Unfortunately with you suggestion I am getting F811

def _prettify(entries, with_color=False):
     def fmt(entry):
        return entry["path"]

    if with_color:
        ls_colors = LsColors()
        fmt = ls_colors.format
    return [fmt(entry) for entry in entries]
dvc/command/ls/__init__.py:20:9: F811 redefinition of unused 'fmt' from line 15

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JIoJIaJIu, It's fine to ignore redefinition warnings (it's what I suggested). But, I'm fine as-is, it's only a suggestion.

Comment on lines +4 to +14
def colorize(ls_colors):
def _colorize(f, spec=""):
fs_path = {
"path": f,
"isexec": "e" in spec,
"isdir": "d" in spec,
"isout": "o" in spec,
}
return ls_colors.format(fs_path)

return _colorize
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be one less redirection if we just did following on all of the tests:

ls_colors.format({"path": f, "isexec": True})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @skshetry for the point!
The changes came from the comment #3351 (comment) in terms of making tests more readable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are other keys mandatory? If they aren't, I don't really think there's any need for redirection.

If they are, you can just:

shape = {
  "isexec": False,
  "isdir": False,
  "isout": False,
}
ls_colors.format({"path": f, **shape, "isexec": True})

But, I'm fine with it as well. But, one less redirection would be great.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are other keys mandatory?

Nope, there are not. In fact before the comment my tests were as you described

I don't see the problem with additional step here (redirection you mean). I think it makes sense not to add additional functionality related to tests only, but let it keep here cause it's thin enough

@efiop
Copy link
Contributor

efiop commented Mar 5, 2020

@JIoJIaJIu @skshetry So what about the changes/questions above?

@gurobokum
Copy link
Contributor Author

@JIoJIaJIu @skshetry So what about the changes/questions above?

@efiop I've provided my thoughts, I think we can keep the PR in the last shape if you don't mind

@skshetry
Copy link
Member

skshetry commented Mar 6, 2020

@efiop, I have already approved. It was just a few suggestions. I'm fine as-is.

@efiop efiop merged commit 02cd520 into iterative:master Mar 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

colorize output for command dvc list
6 participants