All defs are now typed

This commit is contained in:
Kovid Goyal
2020-03-14 14:37:11 +05:30
parent d3f37eeba4
commit 5956277863
5 changed files with 272 additions and 191 deletions

View File

@@ -4,12 +4,12 @@
import subprocess
from collections import defaultdict
from typing import DefaultDict, Dict, FrozenSet, List, Tuple, Union
from typing import Any, DefaultDict, Dict, FrozenSet, List, Tuple, Union
KeymapType = Dict[str, Tuple[str, Union[FrozenSet[str], str]]]
def resolve_keys(keymap: KeymapType):
def resolve_keys(keymap: KeymapType) -> DefaultDict[str, List[str]]:
ans: DefaultDict[str, List[str]] = defaultdict(list)
for ch, (attr, atype) in keymap.items():
if isinstance(atype, str) and atype in ('int', 'uint'):
@@ -20,7 +20,7 @@ def resolve_keys(keymap: KeymapType):
return ans
def enum(keymap: KeymapType):
def enum(keymap: KeymapType) -> str:
lines = []
for ch, (attr, atype) in keymap.items():
lines.append(f"{attr}='{ch}'")
@@ -31,7 +31,7 @@ def enum(keymap: KeymapType):
'''.format(',\n'.join(lines))
def parse_key(keymap: KeymapType):
def parse_key(keymap: KeymapType) -> str:
lines = []
for attr, atype in keymap.values():
vs = atype.upper() if isinstance(atype, str) and atype in ('uint', 'int') else 'FLAG'
@@ -39,7 +39,7 @@ def parse_key(keymap: KeymapType):
return ' \n'.join(lines)
def parse_flag(keymap: KeymapType, type_map, command_class):
def parse_flag(keymap: KeymapType, type_map: Dict[str, Any], command_class: str) -> str:
lines = []
for ch in type_map['flag']:
attr, allowed_values = keymap[ch]
@@ -57,14 +57,14 @@ def parse_flag(keymap: KeymapType, type_map, command_class):
return ' \n'.join(lines)
def parse_number(keymap: KeymapType):
def parse_number(keymap: KeymapType) -> Tuple[str, str]:
int_keys = [f'I({attr})' for attr, atype in keymap.values() if atype == 'int']
uint_keys = [f'U({attr})' for attr, atype in keymap.values() if atype == 'uint']
return '; '.join(int_keys), '; '.join(uint_keys)
def cmd_for_report(report_name, keymap: KeymapType, type_map, payload_allowed):
def group(atype, conv):
def cmd_for_report(report_name: str, keymap: KeymapType, type_map: Dict[str, Any], payload_allowed: bool) -> str:
def group(atype: str, conv: str) -> Tuple[str, str]:
flag_fmt, flag_attrs = [], []
cv = {'flag': 'c', 'int': 'i', 'uint': 'I'}[atype]
for ch in type_map[atype]:
@@ -89,7 +89,15 @@ def cmd_for_report(report_name, keymap: KeymapType, type_map, payload_allowed):
return '\n'.join(ans)
def generate(function_name, callback_name, report_name, keymap: KeymapType, command_class, initial_key='a', payload_allowed=True):
def generate(
function_name: str,
callback_name: str,
report_name: str,
keymap: KeymapType,
command_class: str,
initial_key: str = 'a',
payload_allowed: bool = True
) -> str:
type_map = resolve_keys(keymap)
keys_enum = enum(keymap)
handle_key = parse_key(keymap)
@@ -230,7 +238,7 @@ static inline void
'''
def write_header(text, path):
def write_header(text: str, path: str) -> None:
with open(path, 'w') as f:
print(f'// This file is generated by {__file__} do not edit!', file=f, end='\n\n')
print('#pragma once', file=f)
@@ -238,7 +246,7 @@ def write_header(text, path):
subprocess.check_call(['clang-format', '-i', path])
def graphics_parser():
def graphics_parser() -> None:
flag = frozenset
keymap: KeymapType = {
'a': ('action', flag('tTqpd')),