-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_generator.py
45 lines (37 loc) · 1.65 KB
/
plugin_generator.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
import re
import textwrap
from pathlib import Path
def render_template(template_path, template_kwargs):
def make_replacer(template_kwargs):
def replacer(match: re.Match) -> str:
key = match.group(1)
return str(template_kwargs[key])
return replacer
with open(template_path, "r") as template:
template_str = template.read()
return re.sub(
r"{{\s*([\w_]+)\s*}}", make_replacer(template_kwargs), template_str
)
def generate_plugin(
command_name: str, command_shortcut: str, emoji_dir: str, emoji_load_limit: int
):
base_plugin_name = "universal-emoji"
script_t_name = f"{base_plugin_name}.template.js"
command_t_name = f"{base_plugin_name}-command.template.ini"
command_rendered_name = Path(f"{base_plugin_name}.autogenerated.ini")
autogenerated_warning = "WARNING: this file is generated automatically. Changes will not persist. Change the template file or generator code itself to persist changes"
script_kwargs = {
"emoji_dir": emoji_dir,
"emoji_load_limit": emoji_load_limit,
"autogenerated_warning": autogenerated_warning,
}
rendered_script = render_template(script_t_name, script_kwargs)
command_kwargs = {
"command": textwrap.indent(rendered_script, " ").replace("\"", "\\\""),
"command_name": command_name,
"command_shortcut": command_shortcut,
}
rendered_command = render_template(command_t_name, command_kwargs)
with open(command_rendered_name, "w") as render_file:
render_file.write(rendered_command)
print(f"saved command to {command_rendered_name.absolute().as_posix()}")