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