|
1 | | -from typing import List |
| 1 | +from typing import List, Dict, Any |
2 | 2 | from pathlib import Path |
3 | 3 | from pprint import pformat |
4 | | - |
| 4 | +import logging |
5 | 5 | from .doc_gen import DocGen |
6 | 6 |
|
| 7 | +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
7 | 8 |
|
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: |
12 | 21 | docgen_root = Path(root) |
| 22 | + if not docgen_root.exists(): |
| 23 | + logging.warning(f"Root directory {docgen_root} does not exist.") |
| 24 | + return {} |
| 25 | + |
13 | 26 | doc_gen = base.clone().for_root(docgen_root) |
14 | 27 | doc_gen.collect_snippets() |
15 | | - print(f"Root {docgen_root.name}") |
| 28 | + |
16 | 29 | 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}") |
24 | 30 | 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 | + |
26 | 54 | return all_stats |
27 | 55 |
|
| 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) |
28 | 83 |
|
29 | 84 | if __name__ == "__main__": |
30 | 85 | from sys import argv |
31 | 86 |
|
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