|
| 1 | +from keyword import iskeyword |
1 | 2 | from os import chdir |
| 3 | +from pathlib import Path |
| 4 | +from re import fullmatch, sub |
| 5 | +from sys import executable, version_info |
2 | 6 |
|
3 | 7 | from .client.functions import FunctionCollection, keys |
4 | 8 | from .client.globals import Globals, GlobalVar |
|
38 | 42 |
|
39 | 43 |
|
40 | 44 | def generate_static_completion() -> None: |
41 | | - from keyword import iskeyword |
42 | | - from pathlib import Path |
43 | | - from re import fullmatch |
44 | | - from subprocess import run |
| 45 | + from mypy.stubgen import Options, generate_stubs |
45 | 46 |
|
46 | | - ident = r'[a-zA-Z_][a-zA-Z0-9_]*' |
| 47 | + base = Path(__file__).parent.absolute() / 'client' |
| 48 | + annotation = base / 'workspace.pyi' |
| 49 | + |
| 50 | + try: |
| 51 | + annotation.unlink() |
| 52 | + except FileNotFoundError: |
| 53 | + pass |
| 54 | + |
| 55 | + chdir(base) |
| 56 | + |
| 57 | + o = Options( |
| 58 | + (version_info.major, version_info.minor), |
| 59 | + no_import=True, |
| 60 | + doc_dir='', |
| 61 | + search_path=[], |
| 62 | + interpreter=executable, |
| 63 | + parse_only=False, |
| 64 | + ignore_errors=False, |
| 65 | + include_private=False, |
| 66 | + output_dir='.', |
| 67 | + modules=['workspace'], |
| 68 | + packages=[], |
| 69 | + files=[], |
| 70 | + verbose=True, |
| 71 | + quiet=False, |
| 72 | + export_less=False, |
| 73 | + ) |
47 | 74 |
|
48 | | - client = Path(__file__).parent.absolute() / 'client' |
49 | | - chdir(client) |
50 | | - run(['stubgen', 'workspace.py', '-o', '.']) |
| 75 | + generate_stubs(o) |
| 76 | + |
| 77 | + ident = r'[a-zA-Z_][a-zA-Z0-9_]*' |
51 | 78 |
|
52 | 79 | ws = Workspace.open() |
53 | | - with open('workspace.pyi', 'a') as fout: |
| 80 | + |
| 81 | + text = annotation.read_text() |
| 82 | + text = sub(r' {4}[a-z][a-zA-Z]+: FunctionCollection\n', '', text) |
| 83 | + annotation.write_text(text) |
| 84 | + |
| 85 | + with open(annotation, 'a') as fout: |
54 | 86 | for key, value in ws.__dict__.items(): |
55 | 87 | if not isinstance(value, FunctionCollection): |
56 | 88 | continue |
|
0 commit comments