Use replace_if_needed() for at commands

This commit is contained in:
Kovid Goyal 2022-09-06 18:51:23 +05:30
parent d703cb51cd
commit d0efe00449
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -3,7 +3,6 @@
import io import io
import json import json
import os
from contextlib import contextmanager, suppress from contextlib import contextmanager, suppress
from typing import Dict, Iterator, List, Set, Tuple, Union from typing import Dict, Iterator, List, Set, Tuple, Union
@ -46,6 +45,7 @@ def replace(template: str, **kw: str) -> str:
# }}} # }}}
# rc command wrappers {{{
json_field_types: Dict[str, str] = { json_field_types: Dict[str, str] = {
'bool': 'bool', 'str': 'string', 'list.str': '[]string', 'dict.str': 'map[string]string', 'float': 'float64', 'int': 'int', 'bool': 'bool', 'str': 'string', 'list.str': '[]string', 'dict.str': 'map[string]string', 'float': 'float64', 'int': 'int',
'scroll_amount': 'interface{}', 'spacing': 'interface{}', 'colors': 'interface{}', 'scroll_amount': 'interface{}', 'spacing': 'interface{}', 'colors': 'interface{}',
@ -91,7 +91,7 @@ def render_alias_map(alias_map: Dict[str, Tuple[str, ...]]) -> str:
return amap return amap
def build_go_code(name: str, cmd: RemoteCommand, seq: OptionSpecSeq, template: str) -> str: def go_code_for_remote_command(name: str, cmd: RemoteCommand, seq: OptionSpecSeq, template: str) -> str:
template = '\n' + template[len('//go:build exclude'):] template = '\n' + template[len('//go:build exclude'):]
NO_RESPONSE_BASE = 'false' NO_RESPONSE_BASE = 'false'
af: List[str] = [] af: List[str] = []
@ -171,6 +171,7 @@ def build_go_code(name: str, cmd: RemoteCommand, seq: OptionSpecSeq, template: s
STREAM_WANTED='true' if cmd.reads_streaming_data else 'false', STREAM_WANTED='true' if cmd.reads_streaming_data else 'false',
) )
return ans return ans
# }}}
# Constants {{{ # Constants {{{
@ -239,19 +240,22 @@ def update_at_commands() -> None:
for name in all_command_names(): for name in all_command_names():
cmd = command_for_name(name) cmd = command_for_name(name)
opts = parse_option_spec(cmd.options_spec or '\n\n')[0] opts = parse_option_spec(cmd.options_spec or '\n\n')[0]
code = build_go_code(name, cmd, opts, template) code = go_code_for_remote_command(name, cmd, opts, template)
dest = f'tools/cmd/at/cmd_{name}_generated.go' dest = f'tools/cmd/at/cmd_{name}_generated.go'
if os.path.exists(dest): with replace_if_needed(dest) as f:
os.remove(dest)
with open(dest, 'w') as f:
f.write(code) f.write(code)
def update_completion() -> None:
pass
def main() -> None: def main() -> None:
with replace_if_needed('constants_generated.go') as f: with replace_if_needed('constants_generated.go') as f:
f.write(generate_constants()) f.write(generate_constants())
with replace_if_needed('tools/utils/style/color-names_generated.go') as f: with replace_if_needed('tools/utils/style/color-names_generated.go') as f:
f.write(generate_color_names()) f.write(generate_color_names())
update_completion()
update_at_commands() update_at_commands()
print(json.dumps(changed, indent=2)) print(json.dumps(changed, indent=2))