@@ -36,6 +36,15 @@ class FileHeaderChecker(BaseChecker):
36
36
'help' : 'The file header that should be on top of a file.' ,
37
37
}
38
38
),
39
+ (
40
+ 'file-header-path' ,
41
+ {
42
+ 'default' : None ,
43
+ 'type' : 'string' ,
44
+ 'metavar' : '<file>' ,
45
+ 'help' : 'The path to the file that contains the header.' ,
46
+ }
47
+ ),
39
48
(
40
49
'file-header-ignore-empty-files' ,
41
50
{
@@ -47,27 +56,39 @@ class FileHeaderChecker(BaseChecker):
47
56
),
48
57
)
49
58
50
- def process_module (self , node ):
51
- """Process the astroid node stream."""
52
- if self .config .file_header :
53
- content = None
54
- with node .stream () as stream :
55
- # Explicit decoding required by python 3
56
- content = stream .read ().decode ('utf-8' )
59
+ def __init__ (self , linter = None ):
60
+ super (FileHeaderChecker , self ).__init__ (linter = linter )
61
+ self .pattern = None
62
+ self .header = None
57
63
58
- if self .config .file_header_ignore_empty_files and not content :
59
- return
64
+ def open (self ):
65
+ self .header = self .config .file_header
66
+ if not self .header and self .config .file_header_path :
67
+ with open (self .config .file_header_path , 'r' ) as f :
68
+ self .header = f .read ()
60
69
70
+ if self .header :
61
71
if sys .version_info [0 ] < 3 :
62
- pattern = re .compile (
63
- r'\A' + self .config .file_header , re .LOCALE | re .MULTILINE )
72
+ opts = re .LOCALE | re .MULTILINE
64
73
else :
65
- # The use of re.LOCALE is discouraged in python 3
66
- pattern = re .compile (
67
- r'\A' + self .config .file_header , re .MULTILINE )
74
+ opts = re .MULTILINE
75
+ self .pattern = re .compile (r'\A' + self .header , opts )
76
+
77
+ def process_module (self , node ):
78
+ """Process the astroid node stream."""
79
+
80
+ if self .pattern is None :
81
+ return
82
+
83
+ content = None
84
+ with node .stream () as stream :
85
+ # Explicit decoding required by python 3
86
+ content = stream .read ().decode ('utf-8' )
87
+
88
+ if self .config .file_header_ignore_empty_files and not content :
89
+ return
68
90
69
- matches = pattern .findall (content )
91
+ matches = self . pattern .findall (content )
70
92
71
- if len (matches ) != 1 :
72
- self .add_message ('invalid-file-header' , 1 ,
73
- args = self .config .file_header )
93
+ if len (matches ) != 1 :
94
+ self .add_message ('invalid-file-header' , 1 , args = self .header )
0 commit comments