eric6/Plugins/CheckerPlugins/CodeStyleChecker/PathLib/PathlibChecker.py

changeset 8198
1c765dc90c21
parent 8166
bd5cd5858503
child 8207
d359172d11be
equal deleted inserted replaced
8197:9037d09ed87c 8198:1c765dc90c21
7 Module implementing the checker for functions that can be replaced by use of 7 Module implementing the checker for functions that can be replaced by use of
8 the pathlib module. 8 the pathlib module.
9 """ 9 """
10 10
11 import ast 11 import ast
12 import sys 12 import copy
13 13
14 14
15 class PathlibChecker(object): 15 class PathlibChecker(object):
16 """ 16 """
17 Class implementing a checker for functions that can be replaced by use of 17 Class implementing a checker for functions that can be replaced by use of
63 "open": "P301", 63 "open": "P301",
64 64
65 "py.path.local": "P401", 65 "py.path.local": "P401",
66 } 66 }
67 67
68 def __init__(self, source, filename, selected, ignored, expected, repeat): 68 def __init__(self, source, filename, tree, selected, ignored, expected,
69 repeat):
69 """ 70 """
70 Constructor 71 Constructor
71 72
72 @param source source code to be checked 73 @param source source code to be checked
73 @type list of str 74 @type list of str
74 @param filename name of the source file 75 @param filename name of the source file
75 @type str 76 @type str
77 @param tree AST tree of the source code
78 @type ast.Module
76 @param selected list of selected codes 79 @param selected list of selected codes
77 @type list of str 80 @type list of str
78 @param ignored list of codes to be ignored 81 @param ignored list of codes to be ignored
79 @type list of str 82 @type list of str
80 @param expected list of expected codes 83 @param expected list of expected codes
86 self.__ignore = ('',) if selected else tuple(ignored) 89 self.__ignore = ('',) if selected else tuple(ignored)
87 self.__expected = expected[:] 90 self.__expected = expected[:]
88 self.__repeat = repeat 91 self.__repeat = repeat
89 self.__filename = filename 92 self.__filename = filename
90 self.__source = source[:] 93 self.__source = source[:]
94 self.__tree = copy.deepcopy(tree)
91 95
92 # statistics counters 96 # statistics counters
93 self.counters = {} 97 self.counters = {}
94 98
95 # collection of detected errors 99 # collection of detected errors
145 "code": code, 149 "code": code,
146 "args": args, 150 "args": args,
147 } 151 }
148 ) 152 )
149 153
150 def __reportInvalidSyntax(self):
151 """
152 Private method to report a syntax error.
153 """
154 exc_type, exc = sys.exc_info()[:2]
155 if len(exc.args) > 1:
156 offset = exc.args[1]
157 if len(offset) > 2:
158 offset = offset[1:3]
159 else:
160 offset = (1, 0)
161 self.__error(offset[0] - 1, offset[1] or 0,
162 'M901', exc_type.__name__, exc.args[0])
163
164 def __generateTree(self):
165 """
166 Private method to generate an AST for our source.
167
168 @return generated AST
169 @rtype ast.AST
170 """
171 return ast.parse("".join(self.__source), self.__filename)
172
173 def run(self): 154 def run(self):
174 """ 155 """
175 Public method to check the given source against functions 156 Public method to check the given source against functions
176 to be replaced by 'pathlib' equivalents. 157 to be replaced by 'pathlib' equivalents.
177 """ 158 """
179 # don't do anything, if essential data is missing 160 # don't do anything, if essential data is missing
180 return 161 return
181 162
182 if not self.__checkCodes: 163 if not self.__checkCodes:
183 # don't do anything, if no codes were selected 164 # don't do anything, if no codes were selected
184 return
185
186 try:
187 self.__tree = self.__generateTree()
188 except (SyntaxError, TypeError):
189 self.__reportInvalidSyntax()
190 return 165 return
191 166
192 visitor = PathlibVisitor(self.__checkForReplacement) 167 visitor = PathlibVisitor(self.__checkForReplacement)
193 visitor.visit(self.__tree) 168 visitor.visit(self.__tree)
194 169

eric ide

mercurial