Skip to content

Commit 954b202

Browse files
authored
Update stats.py
1 parent 3861986 commit 954b202

File tree

1 file changed

+74
-16
lines changed

1 file changed

+74
-16
lines changed
Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,90 @@
1-
from typing import List
1+
from typing import List, Dict, Any
22
from pathlib import Path
33
from pprint import pformat
4-
4+
import logging
55
from .doc_gen import DocGen
66

7+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
78

8-
def main(roots: List[str]):
9-
base = DocGen.empty()
10-
all_stats = []
11-
for root in roots:
9+
def process_root(root: str, base: DocGen) -> Dict[str, Any]:
10+
"""
11+
Process a single root directory & collect example stats.
12+
13+
Args:
14+
root (str): The root dir to process.
15+
base (DocGen): Instance of DocGen class.
16+
17+
Returns:
18+
dict: Stats for the given root.
19+
"""
20+
try:
1221
docgen_root = Path(root)
22+
if not docgen_root.exists():
23+
logging.warning(f"Root directory {docgen_root} does not exist.")
24+
return {}
25+
1326
doc_gen = base.clone().for_root(docgen_root)
1427
doc_gen.collect_snippets()
15-
print(f"Root {docgen_root.name}")
28+
1629
stats = doc_gen.stats()
17-
print(f"SDKs {stats['sdks']}")
18-
print(f"Services {stats['services']}")
19-
print(f"Examples {stats['examples']}")
20-
print(f"Version {stats['versions']}")
21-
print(f"Snippets {stats['snippets']}")
22-
genai = pformat(dict(stats["genai"]))
23-
print(f"GenAI {genai}")
2430
stats['root'] = docgen_root.name
25-
all_stats.append(stats)
31+
return stats
32+
except Exception as e:
33+
logging.error(f"Error processing root {root}: {e}")
34+
return {}
35+
36+
def collect_stats(roots: List[str]) -> List[Dict[str, Any]]:
37+
"""
38+
Collects stats for a list of root directories.
39+
40+
Args:
41+
roots (List[str]): A list of root directories as strings.
42+
43+
Returns:
44+
List[Dict[str, Any]]: A list of dictionaries with collected stats for each root.
45+
"""
46+
base = DocGen.empty()
47+
all_stats = []
48+
49+
for root in roots:
50+
stats = process_root(root, base)
51+
if stats:
52+
all_stats.append(stats)
53+
2654
return all_stats
2755

56+
def print_stats(stats: Dict[str, Any]):
57+
"""
58+
Prints stats in a formatted manner.
59+
60+
Args:
61+
stats (dict): A dictionary containing collected stats.
62+
"""
63+
logging.info(f"Root: {stats['root']}")
64+
logging.info(f"SDKs: {stats['sdks']}")
65+
logging.info(f"Services: {stats['services']}")
66+
logging.info(f"Examples: {stats['examples']}")
67+
logging.info(f"Version: {stats['versions']}")
68+
logging.info(f"Snippets: {stats['snippets']}")
69+
genai = pformat(dict(stats.get("genai", {})))
70+
logging.info(f"GenAI: {genai}")
71+
72+
def main(roots: List[str]):
73+
"""
74+
Main function that collects stats for each root directory and prints the results.
75+
76+
Args:
77+
roots (List[str]): A list of root directory paths as strings.
78+
"""
79+
all_stats = collect_stats(roots)
80+
81+
for stats in all_stats:
82+
print_stats(stats)
2883

2984
if __name__ == "__main__":
3085
from sys import argv
3186

32-
main(argv[1:])
87+
if len(argv) < 2:
88+
logging.error("No root directories provided. Usage: script.py <root1>")
89+
else:
90+
main(argv[1:])

0 commit comments

Comments
 (0)