Skip to content

Fixes #402 and fixes #53 add ignore_dir_prefix option original credit @nandoflorestan #563

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

Closed
wants to merge 2 commits into from

Conversation

andy-pi
Copy link

@andy-pi andy-pi commented Mar 2, 2018

Babel currently ignores directories prefixed . and _
Variously listed as feature / bug since this behavior is not explicit
To exclude directories now use --ignore_dir_prefix option and specify a prefix, e.g. _

based on the following by @nandoflorestan
nandoflorestan@9d2e0ba#diff-870e23eed5eae4756d0fe90bb4799c81L896

@codecov-io
Copy link

codecov-io commented Mar 2, 2018

Codecov Report

Merging #563 into master will decrease coverage by 0.08%.
The diff coverage is 50%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #563      +/-   ##
==========================================
- Coverage   90.23%   90.15%   -0.09%     
==========================================
  Files          24       24              
  Lines        4045     4052       +7     
==========================================
+ Hits         3650     3653       +3     
- Misses        395      399       +4
Impacted Files Coverage Δ
babel/messages/extract.py 94.26% <40%> (-1.01%) ⬇️
babel/messages/frontend.py 86.13% <66.66%> (-0.13%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1378a3d...042ed06. Read the comment docs.

@andy-pi
Copy link
Author

andy-pi commented Mar 2, 2018

@akx I'm a first time contributor, looking to improve my python / open source skills doing a bit of bug fixing. Can you please help with the codecov check issue - I cannot see it showing any error apart from the fact that the test coverage percentage has reduced - obviously, since I added a few lines. I don't know how to add a test for this bugfix or even if it is needed, or if it would increase the percentage enough to pass anyway. Any help appreciated.

@andy-pi
Copy link
Author

andy-pi commented Mar 8, 2018

This issue for codecov also shows the same problem: https://github.com/codecov/support/issues/135

Copy link
Member

@akx akx left a comment

Choose a reason for hiding this comment

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

Thank you for the contribution! I've commented on some possibly problematic bits, and I would love to see some tests for the next iteration. However, I'm a little hesitant to merge this change, because I feel it's not quite enough.

I still think it would be better to generalize the directory filter to be a function that gets called for each dirname (or better yet, also with dirpath, for more advanced use cases), which would default to something like lambda subdir: not (subdir.startswith('.') or subdir.startswith('_')), to retain the current behavior. (The command line API would generate a function like this from the user's input.)
This makes more advanced use cases possible, while still being straightforward to use in a simple (prefix matching, what-have-you) way.

Would you feel up to developing that? :)

if ignore_dir_prefixes:
for prefix in ignore_dir_prefixes:
if subdir.startswith(prefix):
dirnames.remove(subdir)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's safe to modify dirnames as it's being iterated over.
This is the reason the original code used a list comprehension to reassign the whole list in one fell swoop.

Copy link
Member

Choose a reason for hiding this comment

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

Also, once you've removed subdir, you should break out of the loop here; if multiple prefixes match the same subdir, remove will raise ValueError: list.remove(x): x not in list.

@@ -63,7 +63,8 @@ def _strip(line):

def extract_from_dir(dirname=None, method_map=DEFAULT_MAPPING,
options_map=None, keywords=DEFAULT_KEYWORDS,
comment_tags=(), callback=None, strip_comment_tags=False):
comment_tags=(), callback=None, strip_comment_tags=False,
ignore_dir_prefixes=None):
Copy link
Member

Choose a reason for hiding this comment

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

ignore_dir_prefixes should probably default to ('.', '_'), to retain the original behavior.

if not (subdir.startswith('.') or subdir.startswith('_'))
]
for subdir in dirnames:
if ignore_dir_prefixes:
Copy link
Member

Choose a reason for hiding this comment

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

If ignore_dir_prefixes defaults to an empty tuple, this check is unnecessary. Also, it should be done outside the for subdir... loop anyway, as there's no way the prefix list will change during that iteration.

@@ -313,6 +313,8 @@ class extract_messages(Command):
'files or directories with commas(,)'), # TODO: Support repetition of this argument
('input-dirs=', None, # TODO (3.x): Remove me.
'alias for input-paths (does allow files as well as directories).'),
('ignore-dir-prefixes=', None,
'Bypass directories whose name start with PREFIX. You can specify this more than once.'),
Copy link
Member

Choose a reason for hiding this comment

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

How is specifying this more than once handled? All I see is attempting to split a string by ,.

@andy-pi
Copy link
Author

andy-pi commented May 28, 2018

@akx Thanks for reviewing. Yes I would like to develop this, and I'll take on board your suggestions. I'm pretty inexperienced as a python dev - so not really sure how to write tests so I'd need some help with that - would you be able to suggest something?

@akx
Copy link
Member

akx commented May 28, 2018

@andy-pi You can look at the tests that Babel currently ships :) With py.test, the test framework we use, the gist of it is that tests are functions named test_<something> within test_<something>.py files and use the assert statement to, well, assert things about what the code does. This is a simple test for the message extraction frontend (CLI), for instance.

For this feature I'd like to see, at the very least, tests that

  • ensure that . and _ paths get ignored by default (as things work now)
  • ensure that an user of the API can pass in a custom filter function which gets correctly used
  • ensure that an user of the CLI can pass in multiple exclusion paths (or prefixes, or globs, however you end up implementing that) and they work

You may want to refactor the current code a little for testability; for instance, the for ... os.walk loop that this code concerns could be wholesale extracted into a new small method that can then be unit tested.

HTH!

@andy-pi
Copy link
Author

andy-pi commented May 28, 2018

@akx ok thanks. I will look at it and get to work!

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.

4 participants