Belle II Software development
online_book_statistics.py
1#!/usr/bin/env python3
2
3
10
11"""
12This script calculates some numbers about the online book, e.g. number of
13exercises etc.
14"""
15
16from pathlib import Path
17import os
18import pprint
19import re
20
21
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
61
62 def read_rst_file(self, path: Path):
63 """Read rst file"""
64 text = path.read_text()
65 self.statistics.characters += len(text)
66 for line in text.split("\n"):
67 for key, regex in self.regexes.items():
68 if regex.findall(line):
69 setattr(
70 self.statistics, key, getattr(self.statistics, 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_file(path)
81
82
83def main():
84 this_dir = Path(__file__).resolve().parent
86 sv.walk_directory(this_dir)
87 sv.statistics.print_summary()
88
89
90if __name__ == "__main__":
91 main()
dict regexes
Dictionary keys must match attributes of Statistics class.
Definition: main.py:1