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

reflected exclude option when use glob #44

Merged
merged 2 commits into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions autoflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ def fix_code(source, additional_imports=None, expand_star_imports=False,
remove_duplicate_keys=remove_duplicate_keys,
remove_unused_variables=remove_unused_variables,
ignore_init_module_imports=ignore_init_module_imports,
))))
))))

if filtered_source == source:
break
Expand Down Expand Up @@ -647,7 +647,7 @@ def fix_file(filename, args, standard_out):
remove_duplicate_keys=args.remove_duplicate_keys,
remove_unused_variables=args.remove_unused_variables,
ignore_init_module_imports=ignore_init_module_imports,
)
)

if original_source != filtered_source:
if args.in_place:
Expand Down Expand Up @@ -745,18 +745,25 @@ def is_python_file(filename):
return True


def match_file(filename, exclude):
"""Return True if file is okay for modifying/recursing."""
def is_exclude_file(filename, exclude):
"""Return True if file matches exclude pattern."""
base_name = os.path.basename(filename)

if base_name.startswith('.'):
return False
return True

for pattern in exclude:
if fnmatch.fnmatch(base_name, pattern):
return False
return True
if fnmatch.fnmatch(filename, pattern):
return False
return True
return False


def match_file(filename, exclude):
"""Return True if file is okay for modifying/recursing."""
if is_exclude_file(filename, exclude):
return False

if not os.path.isdir(filename) and not is_python_file(filename):
return False
Expand All @@ -777,7 +784,8 @@ def find_files(filenames, recursive, exclude):
if match_file(os.path.join(root, d),
exclude)]
else:
yield name
if not is_exclude_file(name, exclude):
yield name


def _main(argv, standard_out, standard_error):
Expand Down
17 changes: 17 additions & 0 deletions test_autoflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,23 @@ def test_is_python_file(self):
self.assertFalse(autoflake.is_python_file(os.devnull))
self.assertFalse(autoflake.is_python_file('/bin/bash'))

def test_is_exclude_file(self):
self.assertTrue(autoflake.is_exclude_file(
"1.py", ["test*", "1*"]))

self.assertFalse(autoflake.is_exclude_file(
"2.py", ["test*", "1*"]))

# folder glob
self.assertTrue(autoflake.is_exclude_file(
"test/test.py", ["test/**.py"]))

self.assertTrue(autoflake.is_exclude_file(
"test/auto_test.py", ["test/*_test.py"]))

self.assertFalse(autoflake.is_exclude_file(
"test/auto_auto.py", ["test/*_test.py"]))

def test_match_file(self):
with temporary_file('', suffix='.py', prefix='.') as filename:
self.assertFalse(autoflake.match_file(filename, exclude=[]),
Expand Down