From 878693383e823b2dfb200b99b3b7d7a14162ea68 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 22 Jun 2026 14:14:05 +0200 Subject: [PATCH] feat(cli): add watch option to stubs command --- midas/cli/commands/stubs.py | 59 ++++++++++++++++++++++++++++++------- pyproject.toml | 6 +++- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/midas/cli/commands/stubs.py b/midas/cli/commands/stubs.py index 98b3cd4..a5267c9 100644 --- a/midas/cli/commands/stubs.py +++ b/midas/cli/commands/stubs.py @@ -1,27 +1,64 @@ import ast +import time from pathlib import Path from typing import TextIO +import black import click +from watchdog.events import DirModifiedEvent, FileModifiedEvent, FileSystemEventHandler +from watchdog.observers import Observer from midas.checker.checker import TypeChecker from midas.generator.stubs import StubsGenerator -@click.command(help="Generate stubs from Midas definitions") -@click.argument("file", type=click.File("r")) -@click.option("-o", "--output", type=click.File("w"), default="-") -def stubs( - file: TextIO, - output: TextIO, -): - source_path: Path = Path(file.name).resolve() - +def generate_stubs(in_path: Path, out_path: Path): checker = TypeChecker() - checker.import_midas(source_path) + checker.import_midas(in_path) generator = StubsGenerator(checker.types) module: ast.Module = generator.generate_stubs() module = ast.fix_missing_locations(module) - output.write(ast.unparse(module)) + output: str = ast.unparse(module) + output = black.format_str(output, mode=black.Mode(is_pyi=True)) + + out_path.write_text(output) + + +class Handler(FileSystemEventHandler): + def __init__(self, in_path: Path, out_path: Path) -> None: + super().__init__() + self.in_path: Path = in_path + self.out_path: Path = out_path + + def on_modified(self, event: DirModifiedEvent | FileModifiedEvent) -> None: + generate_stubs(self.in_path, self.out_path) + + +@click.command(help="Generate stubs from Midas definitions") +@click.argument("file", type=click.File("r")) +@click.option("-o", "--output", type=click.File("w"), default="-") +@click.option("-w", "--watch", is_flag=True) +def stubs( + file: TextIO, + output: TextIO, + watch: bool, +): + source_path: Path = Path(file.name).resolve() + out_path: Path = Path(output.name).resolve() + generate_stubs(source_path, out_path) + + if watch: + print(f"Watching {source_path}...") + print("Press CTRL+C to stop") + handler = Handler(source_path, out_path) + observer = Observer() + observer.schedule(handler, str(source_path)) + observer.start() + try: + while True: + time.sleep(1) + except KeyboardInterrupt: + observer.stop() + observer.join() diff --git a/pyproject.toml b/pyproject.toml index 69a9f7e..48f3b21 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,11 @@ authors = [ { name = "Louis Heredero", email = "louis.heredero@students.hevs.ch" }, ] classifiers = ["Programming Language :: Python :: 3"] -dependencies = ["click>=8.4.1"] +dependencies = [ + "black>=26.5.1", + "click>=8.4.1", + "watchdog>=6.0.0", +] [project.urls] Homepage = "https://git.kbk28.ch/HEL/midas"