Skip to content

Commit

Permalink
chore: urn key format
Browse files Browse the repository at this point in the history
  • Loading branch information
SusanPhevos committed Dec 25, 2024
1 parent d5373e5 commit 51ff17f
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions test/check_rule_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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<str>]]"""
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):
Expand Down Expand Up @@ -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':
Expand All @@ -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']):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 51ff17f

Please sign in to comment.