feat(cli): add watch option to stubs command

This commit is contained in:
2026-06-22 14:14:05 +02:00
parent 0b91de75a8
commit 878693383e
2 changed files with 53 additions and 12 deletions

View File

@@ -1,27 +1,64 @@
import ast import ast
import time
from pathlib import Path from pathlib import Path
from typing import TextIO from typing import TextIO
import black
import click import click
from watchdog.events import DirModifiedEvent, FileModifiedEvent, FileSystemEventHandler
from watchdog.observers import Observer
from midas.checker.checker import TypeChecker from midas.checker.checker import TypeChecker
from midas.generator.stubs import StubsGenerator from midas.generator.stubs import StubsGenerator
@click.command(help="Generate stubs from Midas definitions") def generate_stubs(in_path: Path, out_path: Path):
@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()
checker = TypeChecker() checker = TypeChecker()
checker.import_midas(source_path) checker.import_midas(in_path)
generator = StubsGenerator(checker.types) generator = StubsGenerator(checker.types)
module: ast.Module = generator.generate_stubs() module: ast.Module = generator.generate_stubs()
module = ast.fix_missing_locations(module) 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()

View File

@@ -8,7 +8,11 @@ authors = [
{ name = "Louis Heredero", email = "louis.heredero@students.hevs.ch" }, { name = "Louis Heredero", email = "louis.heredero@students.hevs.ch" },
] ]
classifiers = ["Programming Language :: Python :: 3"] 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] [project.urls]
Homepage = "https://git.kbk28.ch/HEL/midas" Homepage = "https://git.kbk28.ch/HEL/midas"