From 51ff17f1bfc3b6d16e759477411907d02c2c63db Mon Sep 17 00:00:00 2001 From: LiShuzhen Date: Wed, 25 Dec 2024 21:13:23 +0800 Subject: [PATCH] chore: urn key format --- test/check_rule_format.py | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/test/check_rule_format.py b/test/check_rule_format.py index c347703..30952a8 100644 --- a/test/check_rule_format.py +++ b/test/check_rule_format.py @@ -24,6 +24,11 @@ ROOT_PATH, '../custom_components/xiaomi_home/miot/specs/custom_service.json') +BOOL_TRANS_URN_KEY_COLON_NUM: int = 4 +CUSTOM_SERVICE_URN_KEY_COLON_NUM: int = 5 +MULTI_LANG_URN_KEY_COLON_NUM: int = 5 +SPEC_FILTER_URN_KEY_COLON_NUM: int = 5 + def load_json_file(file_path: str) -> Optional[dict]: try: @@ -93,11 +98,18 @@ def nested_3_dict_str_str(d: dict) -> bool: return False return True +def urn_key(d: dict, cnt: int) -> bool: + for k in d.keys(): + if cnt != k.count(':'): + return False + return True def spec_filter(d: dict) -> bool: """restricted format: dict[str, dict[str, list]]""" if not dict_str_dict(d): return False + if not urn_key(d, SPEC_FILTER_URN_KEY_COLON_NUM): + return False for value in d.values(): for k, v in value.items(): if not isinstance(k, str) or not isinstance(v, list): @@ -194,6 +206,8 @@ def custom_service(d: dict) -> bool: """restricted format: dict[str, dict[str, Any]]""" if not dict_str_dict(d): return False + if not urn_key(d, CUSTOM_SERVICE_URN_KEY_COLON_NUM): + return False for v in d.values(): for key, value in v.items(): if key=='new': @@ -218,6 +232,8 @@ def bool_trans(d: dict) -> bool: return False if 'data' not in d or 'translate' not in d: return False + if not urn_key(d['data'], BOOL_TRANS_URN_KEY_COLON_NUM): + return False if not dict_str_str(d['data']): return False if not nested_3_dict_str_str(d['translate']): @@ -234,6 +250,13 @@ def bool_trans(d: dict) -> bool: return False return True +def multi_lang(d: dict) -> bool: + """dict[str, dict[str, dict[str, dict[str, str]] ] ]""" + if not nested_3_dict_str_str(d): + return False + if not urn_key(d, MULTI_LANG_URN_KEY_COLON_NUM): + return False + return True def compare_dict_structure(dict1: dict, dict2: dict) -> bool: if not isinstance(dict1, dict) or not isinstance(dict2, dict): @@ -268,13 +291,13 @@ def sort_bool_trans(file_path: str): def sort_multi_lang(file_path: str): - multi_lang: dict = load_json_file(file_path=file_path) - multi_lang = dict(sorted(multi_lang.items())) - for urn, trans in multi_lang.items(): - multi_lang[urn] = dict(sorted(trans.items())) - for lang, spec in multi_lang[urn].items(): - multi_lang[urn][lang] = dict(sorted(spec.items())) - return multi_lang + lang_data: dict = load_json_file(file_path=file_path) + lang_data = dict(sorted(lang_data.items())) + for urn, trans in lang_data.items(): + lang_data[urn] = dict(sorted(trans.items())) + for lang, spec in lang_data[urn].items(): + lang_data[urn][lang] = dict(sorted(spec.items())) + return lang_data def sort_spec_filter(file_path: str): @@ -311,7 +334,7 @@ def test_spec_filter(): def test_multi_lang(): data: dict = load_json_file(SPEC_MULTI_LANG_FILE) assert data, f'load {SPEC_MULTI_LANG_FILE} failed' - assert nested_3_dict_str_str(data), f'{SPEC_MULTI_LANG_FILE} format error' + assert multi_lang(data), f'{SPEC_MULTI_LANG_FILE} format error' @pytest.mark.github