DataViews/CodeMetrics.py

changeset 4566
a2e8f3c420ec
parent 4425
2f51c66b3120
child 4631
5c1a96925da4
equal deleted inserted replaced
4564:a6480863adfb 4566:a2e8f3c420ec
83 toktype = KEYWORD 83 toktype = KEYWORD
84 self.__addToken(toktype, toktext, srow, scol, line) 84 self.__addToken(toktype, toktext, srow, scol, line)
85 else: 85 else:
86 self.__addToken(toktype, toktext, srow, scol, line) 86 self.__addToken(toktype, toktext, srow, scol, line)
87 except tokenize.TokenError as msg: 87 except tokenize.TokenError as msg:
88 print("Token Error: {0}".format(str(msg))) 88 print("Token Error: {0}".format(str(msg))) # __IGNORE_WARNING__
89 return 89 return
90 90
91 return 91 return
92 92
93 def __addToken(self, toktype, toktext, srow, scol, line): 93 def __addToken(self, toktype, toktext, srow, scol, line):
171 @param value the increment (int) 171 @param value the increment (int)
172 """ 172 """
173 for id, level, row in self.active: 173 for id, level, row in self.active:
174 counters = self.counters.setdefault(id, {}) 174 counters = self.counters.setdefault(id, {})
175 counters[key] = counters.setdefault(key, 0) + value 175 counters[key] = counters.setdefault(key, 0) + value
176
177 def dump(self):
178 """
179 Public method used to format and print the collected statistics.
180 """
181 label_len = 79 - len(spacer) - 6 * 6
182 print(spacer + "FUNCTION / CLASS".ljust(label_len) +
183 " START END LINES NLOC COMM EMPTY")
184 for id in self.identifiers + ['TOTAL ']:
185 label = id
186 counters = self.counters.get(id, {})
187 msg = spacer + label.ljust(label_len)
188
189 for key in ('start', 'end', 'lines', 'nloc', 'comments', 'empty'):
190 if counters.get(key, 0):
191 msg += " {0:d}".format(counters[key])
192 else:
193 msg += " " * 6
194
195 print(msg)
196 176
197 def getCounter(self, id, key): 177 def getCounter(self, id, key):
198 """ 178 """
199 Public method used to get a specific counter value. 179 Public method used to get a specific counter value.
200 180
270 summarize(total, 'empty lines', stats.getCounter('TOTAL ', 'empty')) 250 summarize(total, 'empty lines', stats.getCounter('TOTAL ', 'empty'))
271 summarize(total, 'non-commentary lines', 251 summarize(total, 'non-commentary lines',
272 stats.getCounter('TOTAL ', 'nloc')) 252 stats.getCounter('TOTAL ', 'nloc'))
273 253
274 return stats 254 return stats
275
276
277 def main():
278 """
279 Module main function used when called as a script.
280
281 Loop over all files given on the command line and collect the individual
282 and overall source code statistics.
283 """
284 import sys
285
286 files = sys.argv[1:]
287
288 if not files:
289 sys.exit(1)
290
291 total = {}
292
293 summarize(total, 'files', len(files))
294 for file in files:
295 print(file)
296 stats = analyze(file, total)
297 stats.dump()
298
299 print("\nSummary")
300 for key in ['files', 'lines', 'bytes', 'comments',
301 'empty lines', 'non-commentary lines']:
302 print(key.ljust(20) + "{0:d}".format(total[key]))
303
304 sys.exit(0)
305
306 if __name__ == "__main__":
307 main()

eric ide

mercurial