Debian Codegraph @

texmf-dist/source/support/optexcount/counter.py

raw
1 import keywords as kw
2 import word_iterator as wi
3 import header as hd
4 import color_print as cp
5
6
7 class Counter:
8 """
9 Class that analyzes a source code and counts word counts
10 """
11
12 def __init__(self, filename, color_print=False, verb_char=None):
13 self.word_iter = wi.WordIterator(filename) # source of words that will be analyzed
14 if verb_char is not None:
15 self.word_iter.add_separator(verb_char)
16 self.verb_char = verb_char # used for inline verbatim text
17 self.color_print = color_print # input text will be printed(with colors)
18 self.printer = cp.Printer()
19 self.regular_words_count = 0
20 self.header_words_count = 0
21 self.caption_words_count = 0
22 self.figure_float_count = 0
23 self.math_inline_count = 0
24 self.math_count = 0
25 self.all_headers = []
26 """
27 Three possible types of __context:
28 1) regular-text (implicit)
29 2) headers
30 3) captions
31 """
32 self.__context = "regular-text"
33
34 def run(self):
35 """
36 Main method of the class
37 Loads all words and initializes the analysis
38 """
39 pair = self.word_iter.read()
40 while pair is not None and pair[0] != "\\bye":
41 self.__process_word(pair)
42 pair = self.word_iter.read()
43 if pair is not None:
44 self.print_keyword(pair)
45 self.print_irrelevant_word(("", "\n"))
46
47 def print_result(self):
48 """
49 Generates formatted output of the counter
50 """
51 print("Text words summary: " + str(self.regular_words_count))
52 print("Header words summary: " + str(self.header_words_count))
53 print("Caption and notes words summary: " + str(self.caption_words_count))
54 print("Headers summary: " + str(len(self.all_headers)))
55 print("Figure/float count: " + str(self.figure_float_count))
56 print("Inline math formulae count: " + str(self.math_inline_count))
57 print("Math formulae count: " + str(self.math_count))
58 print("Subcounts: (header-words-count + text-words-count + caption-words-count)")
59 for header in self.all_headers:
60 print(header)
61
62 def print_counted_word(self, pair):
63 """
64 Prints word that was counted. The color depends on category of the word.
65 """
66 if not self.color_print:
67 return
68 if self.__context == "regular-text":
69 self.printer.blue(pair[0]+pair[1])
70 elif self.__context == "header":
71 self.printer.cyan(pair[0]+pair[1])
72 elif self.__context == "caption":
73 self.printer.green(pair[0]+pair[1])
74
75 def print_keyword(self, pair):
76 """
77 Prints keyword - word with red color
78 """
79 if not self.color_print:
80 return
81 self.printer.red(pair[0]+pair[1])
82
83 def print_irrelevant_word(self, pair):
84 """
85 Prints words that wasn't counted and are not important for our functionality, like commentary words etc...
86 """
87 if not self.color_print:
88 return
89 self.printer.white(pair[0]+pair[1])
90
91 def __process_word(self, pair):
92 """
93 Decides how to treat with word from argument
94 """
95 if len(pair[0]) == 0 or pair[0] == "\n": # empty word or newline
96 self.print_irrelevant_word(pair)
97 elif pair[0] == "%":
98 self.print_irrelevant_word(pair)
99 self.__skip_commentary()
100 elif pair[0] == "{":
101 self.print_irrelevant_word(pair)
102 self.__load_curly_brackets()
103 elif pair[0] == "$":
104 self.print_keyword(pair)
105 self.__load_inline_formulae()
106 elif pair[0] == "$$":
107 self.print_keyword(pair)
108 self.__load_formulae()
109 elif self.verb_char is not None and pair[0] == self.verb_char:
110 self.print_keyword(pair)
111 self.__load_verbatim(self.verb_char)
112 elif self.__is_keyword(pair[0]):
113 self.__process_keyword(pair)
114 else:
115 self.__process_text_word(pair)
116
117 def __process_keyword(self, pair):
118 """
119 Treats with keywords.
120 Calls action on known keywords that are important for the counter
121 If keyword is unknown it is skipped
122 """
123 word, arg = self.__split_keyword(pair[0])
124 self.print_keyword((word, ''))
125 if arg is None:
126 self.print_irrelevant_word(('', pair[1]))
127 else:
128 self.print_irrelevant_word((arg, pair[1]))
129 if word == '\\tit':
130 self.all_headers.append(hd.Header("title"))
131 self.__load_header()
132 elif word == '\\chap':
133 self.all_headers.append(hd.Header("chapter"))
134 self.__skip_brackets("[", "]")
135 self.__load_header()
136 elif word == '\\sec':
137 self.all_headers.append(hd.Header("section"))
138 self.__skip_brackets("[", "]")
139 self.__load_header()
140 elif word == '\\secc':
141 self.all_headers.append(hd.Header("subsection"))
142 self.__skip_brackets("[", "]")
143 self.__load_header()
144 elif word == '\\begitems':
145 self.__load_list()
146 elif word == '\\caption':
147 self.__skip_brackets("[", "]")
148 self.__load_caption()
149 elif word == '\\fnote' or word == '\\fnotetext' or word == '\\mnote':
150 self.__load_footnote()
151 elif word in kw.keywords_list:
152 if word in kw.floats_keywords:
153 self.figure_float_count += 1
154 self.__read_arguments(word)
155 elif word == '\\begtt':
156 self.__skip_commentary()
157 self.__load_verbatim()
158 elif word == '\\verbchar' or word == '\\activettchar': # keywords with same functionality
159 self.__set_verb_char(word, arg, pair[1])
160 elif word == '\\code':
161 self.__load_code_verbatim()
162 elif word in kw.logos:
163 self.__load_logo(word, arg, pair[1])
164 else:
165 pass # skip unknown keywords
166
167 def __process_text_word(self, pair):
168 """
169 Increases word counts based on the word location - word context
170 """
171 # word with only one character that is not alphanumeric won't be counted as word
172 if len(pair[0]) == 0:
173 self.print_irrelevant_word(pair)
174 elif len(pair[0]) == 1 and not pair[0].isalnum():
175 self.print_irrelevant_word(pair)
176 elif self.__context == "regular-text":
177 self.print_counted_word(pair)
178 self.regular_words_count += 1
179 if len(self.all_headers):
180 self.all_headers[-1].add_text_word()
181 elif self.__context == "header":
182 self.print_counted_word(pair)
183 self.header_words_count += 1
184 self.all_headers[-1].add_header_word(pair[0])
185 elif self.__context == "caption":
186 self.print_counted_word(pair)
187 self.caption_words_count += 1
188 if len(self.all_headers):
189 self.all_headers[-1].add_caption_word()
190
191 @staticmethod
192 def __is_keyword(word):
193 """
194 Decides whether word is keyword or not
195 """
196 if len(word) >= 3:
197 if word[0] == '\\' and word[1].isalpha() and word[2].isalpha():
198 return True
199 return False
200
201 @staticmethod
202 def __split_keyword(word):
203 """
204 Splits word to two part - keyword and its argument
205 For example:
206 '\verbchar"' will be splitted into pair: '\verbchar', '"'
207 """
208 for i in range(1, len(word)):
209 if not word[i].isalpha():
210 return word[:i], word[i:]
211 return word, None
212
213 def __load_header(self):
214 """
215 Loads header
216 It reads the source code until new line occurs.
217 In case of ^^J at the end of the line it reads until next new line occurs.
218 """
219 orig_context = self.__context
220 self.__context = 'header'
221 pair = self.word_iter.read()
222 skip_new_line = False
223 while pair is not None and pair[0] != "\\bye":
224 if not len(pair[0]):
225 self.print_irrelevant_word(pair)
226 elif pair[0] == "\n":
227 self.print_irrelevant_word(pair)
228 if not skip_new_line:
229 self.__context = orig_context
230 return
231 skip_new_line = False
232 elif pair[0] == "^^J":
233 self.print_irrelevant_word(pair)
234 skip_new_line = True
235 else:
236 self.__process_word(pair)
237 skip_new_line = False
238 pair = self.word_iter.read()
239 self.__context = orig_context
240 if pair is not None:
241 self.__process_keyword(pair)
242
243 def __load_list(self):
244 """
245 Loads list - words that are surrounded by '\begitems' and '\enditems'
246 """
247 pair = self.word_iter.read()
248 while pair is not None and pair[0] != "\\enditems":
249 if pair[0] != "*":
250 self.__process_word(pair)
251 else:
252 self.print_irrelevant_word(pair)
253 pair = self.word_iter.read()
254 if pair is None:
255 raise Exception("No list ending found - \\enditems")
256 else:
257 self.print_keyword(pair)
258
259 def __load_caption(self):
260 """
261 Loads caption - words until EOL
262 """
263 orig_context = self.__context
264 self.__context = 'caption'
265 pair = self.word_iter.read()
266 while pair is not None and pair[0] != "\\bye":
267 if pair[0] == "\n":
268 self.print_irrelevant_word(pair)
269 self.__context = orig_context
270 return
271 else:
272 self.__process_word(pair)
273 pair = self.word_iter.read()
274 self.__context = orig_context
275 if pair is not None:
276 self.print_keyword(pair)
277
278 def __load_footnote(self):
279 """
280 Loads footnote(block in curly brackets)
281 """
282 orig_context = self.__context
283 self.__context = 'caption'
284 pair = self.word_iter.read()
285 if pair is None:
286 raise Exception("No opening curly bracket found!")
287 while len(pair[0]) == 0 or pair[0].isspace():
288 self.print_irrelevant_word(pair)
289 pair = self.word_iter.read()
290 if pair[0] != "{":
291 self.__context = orig_context
292 self.word_iter.push_back(pair)
293 return
294 self.print_irrelevant_word(pair)
295 self.__load_curly_brackets()
296 self.__context = orig_context
297
298 def __load_formulae(self):
299 """
300 Loads math formulae($$ as separator)
301 """
302 pair = self.word_iter.read()
303 while pair is not None and pair[0] != "$$":
304 self.print_irrelevant_word(pair)
305 pair = self.word_iter.read()
306 if pair is None:
307 raise Exception("No end of math formulae found!")
308 else:
309 self.print_keyword(pair)
310 self.math_count += 1
311
312 def __load_inline_formulae(self):
313 """
314 Loads inline math formulae($ as separator)
315 """
316 pair = self.word_iter.read()
317 while pair is not None and pair[0] != "$":
318 self.print_irrelevant_word(pair)
319 pair = self.word_iter.read()
320 self.math_inline_count += 1
321 if pair is None:
322 raise Exception("No end of inline math formulae found!")
323 else:
324 self.print_keyword(pair)
325
326 def __load_verbatim(self, ending="\\endtt", keyword_print=True):
327 """
328 Reads words until ending(param) word occurs.
329 These words are processed as regular word(not keywords etc...)
330 """
331 pair = self.word_iter.read()
332 while pair is not None and ending != pair[0]:
333 self.__process_text_word(pair)
334 pair = self.word_iter.read()
335 if pair is None:
336 raise Exception("Verbatim text not terminated!")
337 else:
338 if keyword_print:
339 self.print_keyword(pair)
340 else:
341 self.print_irrelevant_word(pair)
342
343 def __load_code_verbatim(self):
344 """
345 In case of verbatim using "\code" keyword
346 """
347 pair = self.word_iter.read()
348 while len(pair[0]) == 0 or pair[0].isspace():
349 self.print_irrelevant_word(pair)
350 pair = self.word_iter.read()
351 if pair[0] != "{":
352 raise Exception("\\Code must be followed be opening curly bracket('{')!")
353 self.print_irrelevant_word(pair)
354 self.__load_verbatim("}", False)
355
356 def __load_logo(self, logo, arg, sep):
357 """
358 Checks whether logo keyword is followed by slash etc...
359 """
360 if arg != '/' or len(sep) > 0:
361 pass
362 elif self.__context == "regular-text":
363 self.regular_words_count += 1
364 if len(self.all_headers):
365 self.all_headers[-1].add_text_word()
366 elif self.__context == "header":
367 self.header_words_count += 1
368 self.all_headers[-1].add_header_word(logo)
369 elif self.__context == "caption":
370 self.caption_words_count += 1
371 if len(self.all_headers):
372 self.all_headers[-1].add_caption_word()
373
374 def __set_verb_char(self, word, arg, sep):
375 """
376 Loads new character for inline verbatim
377 """
378 if arg is not None and len(arg) == 1:
379 self.verb_char = arg
380 self.word_iter.add_separator(arg)
381 elif arg is None and sep == '':
382 pair = self.word_iter.read()
383 if len(pair[0]) != 1:
384 raise Exception("Invalid use of " + word)
385 else:
386 self.print_irrelevant_word(pair)
387 self.verb_char = pair[0]
388 self.word_iter.add_separator(pair[0])
389 else:
390 raise Exception("Invalid use of " + word)
391
392 def __read_arguments(self, word):
393 """
394 Loads from keywords file what arguments has got particular keyword from argument
395 These arguments are skipped - not important for our purpose
396 """
397 params = kw.keywords_list[word]
398 for p in params:
399 if p == "O":
400 pass # this case is managed by '__process_keyword' method
401 elif p == "W":
402 self.__obligatory_argument()
403 elif p == "S":
404 self.__skip_brackets("[", "]")
405 elif p == "P":
406 self.__skip_brackets("(", ")")
407 elif p == "C":
408 self.__skip_brackets("{", "}")
409 else: # unknown specifier - no argument expected
410 pass
411
412 def __obligatory_argument(self):
413 """
414 Just reads another word. In case of the end of source file Exception is thrown
415 """
416 pair = self.word_iter.read()
417 if pair is None or pair[0] == "\\bye":
418 raise Exception("No obligatory argument found")
419 self.print_irrelevant_word(pair)
420 return pair
421
422 def __load_curly_brackets(self):
423 """
424 Loads block of source code in curly brackets
425 """
426 pair = self.word_iter.read()
427 while pair is not None and pair[0] != "}":
428 self.__process_word(pair)
429 pair = self.word_iter.read()
430 if pair is None:
431 raise Exception("No closing bracket ('}') found.")
432 else:
433 self.print_irrelevant_word(pair)
434
435 def __skip_brackets(self, opening, closing):
436 """
437 Skips block of source code in brackets opening-closing
438 """
439 pair = self.word_iter.read()
440 bracket_count = 0
441 while True:
442 if pair is None:
443 raise Exception("No closing bracket ('" + closing + "') found.")
444 elif pair[0] == opening:
445 bracket_count += 1
446 self.print_irrelevant_word(pair)
447 elif pair[0] == closing:
448 bracket_count -= 1
449 self.print_irrelevant_word(pair)
450 if bracket_count <= 0:
451 if pair[0] != closing:
452 self.word_iter.push_back(pair)
453 break
454 elif pair[0] != opening and pair[0] != closing:
455 self.print_irrelevant_word(pair)
456 pair = self.word_iter.read()
457
458 def __skip_commentary(self):
459 """
460 Skips entire line
461 """
462 pair = self.word_iter.read()
463 while pair is not None and pair[0] != "\n":
464 self.print_irrelevant_word(pair)
465 pair = self.word_iter.read()
466 self.print_irrelevant_word(pair)