Skip to content

Commit 5130533

Browse files
sh4869myint
authored andcommitted
reflected exclude option when use glob (#44)
* apply exclude option when use glob * add unit test for is_exclude_file
1 parent 9fe7aae commit 5130533

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

autoflake.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ def fix_code(source, additional_imports=None, expand_star_imports=False,
615615
remove_duplicate_keys=remove_duplicate_keys,
616616
remove_unused_variables=remove_unused_variables,
617617
ignore_init_module_imports=ignore_init_module_imports,
618-
))))
618+
))))
619619

620620
if filtered_source == source:
621621
break
@@ -647,7 +647,7 @@ def fix_file(filename, args, standard_out):
647647
remove_duplicate_keys=args.remove_duplicate_keys,
648648
remove_unused_variables=args.remove_unused_variables,
649649
ignore_init_module_imports=ignore_init_module_imports,
650-
)
650+
)
651651

652652
if original_source != filtered_source:
653653
if args.in_place:
@@ -745,18 +745,25 @@ def is_python_file(filename):
745745
return True
746746

747747

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

752752
if base_name.startswith('.'):
753-
return False
753+
return True
754754

755755
for pattern in exclude:
756756
if fnmatch.fnmatch(base_name, pattern):
757-
return False
757+
return True
758758
if fnmatch.fnmatch(filename, pattern):
759-
return False
759+
return True
760+
return False
761+
762+
763+
def match_file(filename, exclude):
764+
"""Return True if file is okay for modifying/recursing."""
765+
if is_exclude_file(filename, exclude):
766+
return False
760767

761768
if not os.path.isdir(filename) and not is_python_file(filename):
762769
return False
@@ -777,7 +784,8 @@ def find_files(filenames, recursive, exclude):
777784
if match_file(os.path.join(root, d),
778785
exclude)]
779786
else:
780-
yield name
787+
if not is_exclude_file(name, exclude):
788+
yield name
781789

782790

783791
def _main(argv, standard_out, standard_error):

test_autoflake.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,23 @@ def test_is_python_file(self):
11911191
self.assertFalse(autoflake.is_python_file(os.devnull))
11921192
self.assertFalse(autoflake.is_python_file('/bin/bash'))
11931193

1194+
def test_is_exclude_file(self):
1195+
self.assertTrue(autoflake.is_exclude_file(
1196+
"1.py", ["test*", "1*"]))
1197+
1198+
self.assertFalse(autoflake.is_exclude_file(
1199+
"2.py", ["test*", "1*"]))
1200+
1201+
# folder glob
1202+
self.assertTrue(autoflake.is_exclude_file(
1203+
"test/test.py", ["test/**.py"]))
1204+
1205+
self.assertTrue(autoflake.is_exclude_file(
1206+
"test/auto_test.py", ["test/*_test.py"]))
1207+
1208+
self.assertFalse(autoflake.is_exclude_file(
1209+
"test/auto_auto.py", ["test/*_test.py"]))
1210+
11941211
def test_match_file(self):
11951212
with temporary_file('', suffix='.py', prefix='.') as filename:
11961213
self.assertFalse(autoflake.match_file(filename, exclude=[]),

0 commit comments

Comments
 (0)