Belle II Software  light-2205-abys
online_book_statistics.py
1 #!/usr/bin/env python3
2 
3 
10 
11 """This script calculates some numbers about the online book, e.g. number of
12 exercises etc.
13 """
14 
15 from pathlib import Path
16 import os
17 import pprint
18 import re
19 
20 
21 class Statistics:
22  def __init__(self):
23  self.code_inclusions: int = 0
24  self.hints: int = 0
25  self.solutions: int = 0
26  self.exercises: int = 0
27  self.overview_boxesoverview_boxes = 0
28  self.key_pointskey_points = 0
29  self.figuresfigures = 0
30  self.characterscharacters = 0
31 
32  def print_summary(self):
33  pprint.pprint(self.__dict__)
34 
35 
37  # Dictionary keys must match attributes of Statistics class
38  regexes = dict(
39  code_inclusions=re.compile("(code-block\\s*::)|(literalinclude\\s*::)"),
40  hints=re.compile(":class:.*hint"),
41  solutions=re.compile(":class:.*solution"),
42  exercises=re.compile(":class:.*exercise"),
43  overview_boxes=re.compile(":class:.*overview"),
44  key_points=re.compile(":class:.*key-points"),
45  figures=re.compile("figure\\s*::")
46  )
47 
48  def __init__(self):
49  self.statisticsstatistics = Statistics()
50 
51  def read_rst_file(self, path: Path):
52  text = path.read_text()
53  self.statisticsstatistics.characters += len(text)
54  for line in text.split("\n"):
55  for key, regex in self.regexesregexes.items():
56  if regex.findall(line):
57  setattr(
58  self.statisticsstatistics, key, getattr(self.statisticsstatistics, key) + 1
59  )
60 
61  def walk_directory(self, path: Path):
62  print(f"Walking {path}")
63  for root, _, files in os.walk(path):
64  for file in files:
65  path = Path(root) / file
66  if path.suffix == ".rst":
67  self.read_rst_fileread_rst_file(path)
68 
69 
70 def main():
71  this_dir = Path(__file__).resolve().parent
72  sv = StatisticsVisitor()
73  sv.walk_directory(this_dir)
74  sv.statistics.print_summary()
75 
76 
77 if __name__ == "__main__":
78  main()
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:75