Add scraper script for КлассПакета extraction
This commit is contained in:
+46
@@ -0,0 +1,46 @@
|
|||||||
|
import os
|
||||||
|
import re
|
||||||
|
import argparse
|
||||||
|
from typing import List, Tuple
|
||||||
|
|
||||||
|
PATTERN = re.compile(r"(КлассПакета_\w+)\s*=\s*(\d+)\s*;")
|
||||||
|
|
||||||
|
def scan_directory(directory: str) -> List[Tuple[str, int, str]]:
|
||||||
|
seen = {}
|
||||||
|
for root, _, files in os.walk(directory):
|
||||||
|
for fname in files:
|
||||||
|
fpath = os.path.join(root, fname)
|
||||||
|
try:
|
||||||
|
with open(fpath, "r", encoding="utf-8") as f:
|
||||||
|
for line in f:
|
||||||
|
m = PATTERN.search(line)
|
||||||
|
if m:
|
||||||
|
name, value = m.group(1), int(m.group(2))
|
||||||
|
if name not in seen:
|
||||||
|
seen[name] = (value, f"{name} = {value};")
|
||||||
|
except (UnicodeDecodeError, PermissionError):
|
||||||
|
pass
|
||||||
|
return [(k, v[0], v[1]) for k, v in seen.items()]
|
||||||
|
|
||||||
|
def write_markdown_table(results: List[Tuple[str, int, str]], output_path: str) -> None:
|
||||||
|
with open(output_path, "w", encoding="utf-8") as f:
|
||||||
|
f.write("| КлассПакета | Значение | Присваивание |\n")
|
||||||
|
f.write("|------------|----------|-------------|\n")
|
||||||
|
for name, value, assignment in sorted(results, key=lambda x: x[0]):
|
||||||
|
f.write(f"| {name} | {value} | `{assignment}` |\n")
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(description="Extract КлассПакета values to a markdown table.")
|
||||||
|
parser.add_argument("directory", help="Directory to scan recursively")
|
||||||
|
parser.add_argument("-o", "--output", default="result.md", help="Output markdown file (default: result.md)")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if not os.path.isdir(args.directory):
|
||||||
|
parser.error(f"Directory not found: {args.directory}")
|
||||||
|
|
||||||
|
results = scan_directory(args.directory)
|
||||||
|
write_markdown_table(results, args.output)
|
||||||
|
print(f"Written {len(results)} entries to {args.output}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user