33import glob
44import re
55import sys
6+ import warnings
67from collections import namedtuple
78from itertools import chain
89
1617 r"(?:\n|^)diff --git a\/.* b\/.*(?:\n|$)" )
1718
1819
19- Test = namedtuple ('Test' , ('name' , 'pattern' , 'hint' , 'filename' , 'error' ))
20+ Test = namedtuple (
21+ 'Test' , (
22+ 'name' ,
23+ 'pattern' ,
24+ 'hint' ,
25+ 'file_pattern' ,
26+ 'filename' ,
27+ 'error' ,
28+ )
29+ )
2030
2131
22- def parse_args ():
32+ def parse_args (args ):
2333 parser = argparse .ArgumentParser ()
2434 parser .add_argument (
2535 'files' ,
@@ -42,19 +52,29 @@ def parse_args():
4252 action = 'store_true' ,
4353 help = 'Analyze content from git diff.'
4454 )
45- return parser .parse_args ()
55+ return parser .parse_args (args = args )
4656
4757
4858def load_config (path ):
4959 with open (path ) as fs :
5060 for test in yaml .safe_load (fs ):
51- filename = test .get ('filename' , ['*' ])
52- if not isinstance (filename , list ):
53- filename = list (filename )
61+ filename = test .get ('filename' )
62+ if filename :
63+ warnings .warn (
64+ "The glob style 'filename' configuration attribute has been"
65+ " deprecated in favor of a new RegEx based 'filePattern' attribute."
66+ " 'filename' support will be removed in relint version 2.0." ,
67+ DeprecationWarning
68+ )
69+ if not isinstance (filename , list ):
70+ filename = list (filename )
71+ file_pattern = test .get ('filePattern' , '.*' )
72+ file_pattern = re .compile (file_pattern )
5473 yield Test (
5574 name = test ['name' ],
5675 pattern = re .compile (test ['pattern' ], re .MULTILINE ),
5776 hint = test .get ('hint' ),
77+ file_pattern = file_pattern ,
5878 filename = filename ,
5979 error = test .get ('error' , True )
6080 )
@@ -68,10 +88,16 @@ def lint_file(filename, tests):
6888 pass
6989 else :
7090 for test in tests :
71- if any (fnmatch .fnmatch (filename , fp ) for fp in test .filename ):
72- for match in test .pattern .finditer (content ):
73- line_number = match .string [:match .start ()].count ('\n ' ) + 1
74- yield filename , test , match , line_number
91+ if test .filename :
92+ if any (fnmatch .fnmatch (filename , fp ) for fp in test .filename ):
93+ for match in test .pattern .finditer (content ):
94+ line_number = match .string [:match .start ()].count ('\n ' ) + 1
95+ yield filename , test , match , line_number
96+ else :
97+ if test .file_pattern .match (filename ):
98+ for match in test .pattern .finditer (content ):
99+ line_number = match .string [:match .start ()].count ('\n ' ) + 1
100+ yield filename , test , match , line_number
75101
76102
77103def parse_line_numbers (output ):
@@ -183,8 +209,8 @@ def parse_diff(output):
183209 return changed_content
184210
185211
186- def main ():
187- args = parse_args ()
212+ def main (args = sys . argv ):
213+ args = parse_args (args )
188214 paths = {
189215 path
190216 for file in args .files
@@ -207,5 +233,9 @@ def main():
207233 exit (exit_code )
208234
209235
236+ if not sys .warnoptions :
237+ warnings .simplefilter ("default" )
238+
239+
210240if __name__ == '__main__' :
211241 main ()
0 commit comments