| 1 |
# collect_stats.py |
| 2 |
# |
| 3 |
# Copyright 2026 James Westman <[email protected]> |
| 4 |
# |
| 5 |
# This file is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU Lesser General Public License as |
| 7 |
# published by the Free Software Foundation; either version 3 of the |
| 8 |
# License, or (at your option) any later version. |
| 9 |
# |
| 10 |
# This file is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
# Lesser General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU Lesser General Public |
| 16 |
# License along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
# SPDX-License-Identifier: LGPL-3.0-or-later |
| 19 |
|
| 20 |
""" |
| 21 |
Collects statistics about property, signal, and child object usage from a sample of blueprint files to help improve |
| 22 |
completion suggestions. |
| 23 |
|
| 24 |
Usage: |
| 25 |
./blueprint-compiler.py tools collect-stats <path-to-blueprint-regression-tests> |
| 26 |
""" |
| 27 |
|
| 28 |
import json |
| 29 |
import os |
| 30 |
from collections import defaultdict |
| 31 |
from dataclasses import dataclass, field |
| 32 |
|
| 33 |
from ..ast_utils import AstNode |
| 34 |
from ..language import Child, Object, Property, Signal, Template |
| 35 |
from ..parser import parse |
| 36 |
from ..tokenizer import tokenize |
| 37 |
|
| 38 |
MAX_ITEMS = 5 |
| 39 |
MIN_OCCURRENCES = 5 |
| 40 |
|
| 41 |
|
| 42 |
@dataclass |
| 43 |
class ClassStats: |
| 44 |
usages: dict[str, int] = field(default_factory=lambda: defaultdict(int)) |
| 45 |
|
| 46 |
def summarize(self): |
| 47 |
often_enough = [x for x in self.usages.items() if x[1] >= MIN_OCCURRENCES] |
| 48 |
if len(often_enough) == 0: |
| 49 |
return None |
| 50 |
|
| 51 |
most_frequent = sorted(often_enough, key=lambda x: x[1], reverse=True) |
| 52 |
return [x[0] for x in most_frequent[:MAX_ITEMS]] |
| 53 |
|
| 54 |
|
| 55 |
def collect_stats(dir: str): |
| 56 |
class_stats: defaultdict[str, ClassStats] = defaultdict(lambda: ClassStats()) |
| 57 |
|
| 58 |
def walk_ast(ast: AstNode): |
| 59 |
if isinstance(ast, Object) and not isinstance(ast, Template): |
| 60 |
if ( |
| 61 |
not ast.class_name.is_extern |
| 62 |
and ast.class_name.gir_type is not None |
| 63 |
and not ast.class_name.gir_type.deprecated |
| 64 |
): |
| 65 |
class_name = ast.class_name.gir_type.full_name |
| 66 |
class_stat = class_stats[class_name] |
| 67 |
for prop in ast.content.children[Property]: |
| 68 |
class_stat.usages["p:" + prop.name] += 1 |
| 69 |
for signal in ast.content.children[Signal]: |
| 70 |
class_stat.usages["s:" + signal.full_name] += 1 |
| 71 |
for child in ast.content.children[Child]: |
| 72 |
if not child.object.class_name.is_extern: |
| 73 |
child_class_name = child.object.class_name.gir_type.full_name |
| 74 |
class_stat.usages["c:" + child_class_name] += 1 |
| 75 |
|
| 76 |
for child in ast.children: |
| 77 |
walk_ast(child) |
| 78 |
|
| 79 |
for root, _, files in os.walk(dir): |
| 80 |
for fname in files: |
| 81 |
if not fname.endswith(".blp"): |
| 82 |
continue |
| 83 |
|
| 84 |
path = os.path.join(root, fname) |
| 85 |
with open(path, "r") as f: |
| 86 |
content = f.read() |
| 87 |
|
| 88 |
tokens = tokenize(content) |
| 89 |
ast, errors, warnings = parse(tokens) |
| 90 |
if errors is not None or ast is None: |
| 91 |
continue |
| 92 |
|
| 93 |
walk_ast(ast) |
| 94 |
|
| 95 |
results = {k: v.summarize() for k, v in class_stats.items()} |
| 96 |
results = {k: v for k, v in results.items() if v is not None} |
| 97 |
|
| 98 |
with open(os.path.join(os.path.dirname(__file__), "stats.json"), "w") as f: |
| 99 |
json.dump(results, f, indent=2, sort_keys=True) |