-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate.py
120 lines (107 loc) · 4.42 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os, json, re
directory = os.path.dirname(os.path.realpath(__file__))
regex = '(:) ((((\w+)(\.))?(%s))|((")(%s)("))|((\')(%s)(\'))) (=) ([bBrRuU]?f?)("{3})'
# ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
# 1 5 6 7 9 10 11 13 14 15 16 17 18
begin_captures = {
"1": {"name": "punctuation.separator.colon.python"},
"5": {"name": "source.python"},
"6": {"name": "punctuation.separator.period.python"},
"7": {"name": "meta.attribute.python"},
"9": {"name": "string.quoted.single.python"},
"10": {"name": "string.quoted.single.python"},
"11": {"name": "string.quoted.single.python"},
"13": {"name": "string.quoted.single.python"},
"14": {"name": "string.quoted.single.python"},
"15": {"name": "string.quoted.single.python"},
"16": {"name": "keyword.operator.assignment.python"},
"17": {"name": "storage.type.string.python"},
"18": {"name": "string.quoted.multi.python"},
}
def make_vs_code_extension(languages):
package_file_path = os.path.join(
directory, "vscode-python-inline-source", "package.json"
)
readme_file_path = os.path.join(
directory, "vscode-python-inline-source", "README.md"
)
add_supported_languages_to_readme(languages, readme_file_path)
syntax_file_path = os.path.join(
directory,
"vscode-python-inline-source",
"syntaxes",
"python-inline-source.json",
)
with open(package_file_path, "r") as f:
package = json.load(f)
with open(syntax_file_path, "r") as f:
syntax = json.load(f)
embedded_languages = {}
patterns = []
for langname, options in languages.items():
embedded_languages[options["contentName"]] = langname
patterns.append(
{
"contentName": options["contentName"],
"begin": regex % ((options["match"],) * 3),
"beginCaptures": begin_captures,
"end": '("{3})',
"endCaptures": {"1": {"name": "string.quoted.multi.python"}},
"patterns": [{"include": options["include"]}],
}
)
package["contributes"]["grammars"][0]["embeddedLanguages"] = embedded_languages
syntax["patterns"] = patterns
with open(package_file_path, 'w') as f:
json.dump(package, f, indent=4)
with open(syntax_file_path, 'w') as f:
json.dump(syntax, f, indent=4)
def make_python_types(languages):
sourcetypes_file_path = os.path.join(
directory, "sourcetypes", "sourcetypes.py"
)
readme_file_path = os.path.join(
directory, "sourcetypes", "README.md"
)
add_supported_languages_to_readme(languages, readme_file_path)
with open(sourcetypes_file_path, 'w') as f:
f.writelines(
[
"try:\n",
" from typing import Annotated\n",
"except ImportError:\n",
" from typing_extensions import Annotated\n",
"\n",
"source_code = Annotated[str, 'source_code']\n",
"\n",
]
)
for langname, options in languages.items():
f.write(f"{langname} = Annotated[source_code, '{langname}']\n")
for alias in options["match"].split("|"):
if alias != langname:
f.write(f"{alias} = {langname}\n")
f.write("\n")
def add_supported_languages_to_readme(languages, readme_file_path):
with open(readme_file_path, "r") as f:
readme = f.read()
readme_text = []
for langname, options in languages.items():
readme_text_line = f"\n- `{langname}`"
aliases = [f"`{m}`" for m in options['match'].split("|") if m != langname]
if aliases:
if len(aliases) > 1:
aliases[-1] = f"and {aliases[-1]}"
readme_text_line = f"{readme_text_line} (aliased as {', '.join(aliases)})"
readme_text.append(readme_text_line)
readme = re.sub(r"(## Supported Languages\n)[^#]*", fr"\1{''.join(readme_text)}\n\n", readme)
with open(readme_file_path, 'w') as f:
f.write(readme)
def main():
with open(os.path.join(directory, "languages.json"), "r") as f:
languages = json.load(f)
make_vs_code_extension(languages)
make_python_types(languages)
add_supported_languages_to_readme(languages, os.path.join(directory, "README.md"))
if __name__ == "__main__":
main()