2020
2121
2222import argparse
23+ import fnmatch
2324import os
2425import re
2526import sys
@@ -49,7 +50,8 @@ def is_extensionless(path):
4950def matches_extension (path , extension ):
5051 """
5152 Returns True if path has the given extension, or if
52- the last path component matches the extension.
53+ the last path component matches the extension. Supports
54+ Unix glob matching.
5355
5456 >>> matches_extension("./www/profile.php", "php")
5557 True
@@ -66,19 +68,29 @@ def matches_extension(path, extension):
6668 else :
6769 # If the is an extension, drop the leading period and
6870 # compare it to the extension.
69- return ext [1 :] == extension
71+ return fnmatch . fnmatch ( ext [1 :], extension )
7072
7173
72- def path_filter (extensions = None , exclude_paths = None ):
74+ def path_filter (extensions , exclude_paths = None ):
7375 """
74- Returns a function (useful as the path_filter field of a Query instance)
75- that returns True iff the path it is given has an extension one of the
76- file extensions specified in `extensions`, an array of strings.
76+ Returns a function that returns True if a filepath is acceptable.
77+
78+ @param extensions An array of strings. Specifies what file
79+ extensions should be accepted by the
80+ filter. If None, we default to the Unix glob
81+ `*` and match every file extension.
82+ @param exclude_paths An array of strings which represents filepaths
83+ that should never be accepted by the filter.
84+
85+ @return function A filter function that will only return True
86+ when a filepath is acceptable under the above
87+ conditions.
7788
7889 >>> map(path_filter(extensions=['js', 'php']),
7990 ... ['./profile.php', './q.jjs'])
8091 [True, False]
81- >>> map(path_filter(exclude_paths=['html']),
92+ >>> map(path_filter(extensions=['*'],
93+ ... exclude_paths=['html']),
8294 ... ['./html/x.php', './lib/y.js'])
8395 [False, True]
8496 >>> map(path_filter(extensions=['js', 'BUILD']),
@@ -88,11 +100,9 @@ def path_filter(extensions=None, exclude_paths=None):
88100 exclude_paths = exclude_paths or []
89101
90102 def the_filter (path ):
91- if extensions :
92- if not any (
93- matches_extension (path , extension ) for extension in extensions
94- ):
95- return False
103+ if not any (matches_extension (path , extension )
104+ for extension in extensions ):
105+ return False
96106 if exclude_paths :
97107 for excluded in exclude_paths :
98108 if path .startswith (
@@ -917,9 +927,11 @@ def _parse_command_line():
917927 'or a percentage of the way through, '
918928 'just before which to end.' )
919929
920- parser .add_argument ('--extensions' , action = 'store' , type = str ,
930+ parser .add_argument ('--extensions' , action = 'store' ,
931+ default = '*' , type = str ,
921932 help = 'A comma-delimited list of file extensions '
922- 'to process.' )
933+ 'to process. Also supports Unix pattern '
934+ 'matching.' )
923935 parser .add_argument ('--include-extensionless' , action = 'store_true' ,
924936 help = 'If set, this will check files without '
925937 'an extension, along with any matching file '
@@ -950,12 +962,6 @@ def _parse_command_line():
950962
951963 arguments = parser .parse_args ()
952964
953- if (
954- arguments .extensions is None
955- ) and (arguments .include_extensionless is False ):
956- parser .print_usage ()
957- sys .exit (0 )
958-
959965 yes_to_all = arguments .accept_all
960966
961967 query_options = {}
@@ -969,12 +975,12 @@ def _parse_command_line():
969975 query_options ['root_directory' ] = arguments .d
970976 query_options ['inc_extensionless' ] = arguments .include_extensionless
971977
972- if arguments .extensions is not None or arguments . exclude_paths is not None :
973- query_options [ 'path_filter' ] = (
974- path_filter ( extensions = arguments . extensions . split ( ',' )
975- if arguments . extensions is not None else None ,
976- exclude_paths = arguments .exclude_paths .split (',' )
977- if arguments . exclude_paths is not None else None ) )
978+ if arguments .exclude_paths is not None :
979+ exclude_paths = arguments . exclude_paths . split ( ',' )
980+ else :
981+ exclude_paths = None
982+ query_options [ 'path_filter' ] = path_filter ( arguments .extensions .split (',' ),
983+ exclude_paths )
978984
979985 options = {}
980986 options ['query' ] = Query (** query_options )
0 commit comments