From 3b33fe57a65c9fcb96dd18b1d3e5496304a00dda Mon Sep 17 00:00:00 2001 From: Benjamin Moeller Date: Sat, 17 Aug 2024 11:53:57 +0200 Subject: [PATCH 1/3] Add function 'load_dic_file' als POC --- software/script/chameleon_cli_unit.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index 6bcf9e9b..bfac55f6 100644 --- a/software/script/chameleon_cli_unit.py +++ b/software/script/chameleon_cli_unit.py @@ -52,6 +52,14 @@ def check_tools(): print(f'{CR}Warning, tools {", ".join(missing_tools)} not found. ' f'Corresponding commands will not work as intended.{C0}') +dict_keys = set() +def load_dic_file(filename, keys): + #print("Read directory from", filename) ## debug + for dict_key in filename.readlines(): + if not dict_key.startswith("#") and not dict_key is None: + dict_keys.add(dict_key) + + return dict_keys class BaseCLIUnit: def __init__(self): @@ -943,8 +951,11 @@ def on_exec(self, args: argparse.Namespace): return if args.import_dic is not None: - if not load_dic_file(args.import_dic, keys): - return + for key in load_dic_file(args.import_dic, keys): + if not re.match(r'^[a-fA-F0-9]{12}$', key): + print(f' - {CR}Key should in hex[12] format, invalid key is ignored{C0}, key = "{key}"') + continue + keys.add(bytes.fromhex(key)) if len(keys) == 0: print(f' - {CR}No keys{C0}') From 336d8f2001be0f2ce07bae67a5420a371b7d2c62 Mon Sep 17 00:00:00 2001 From: Benjamin Moeller Date: Sat, 17 Aug 2024 12:01:40 +0200 Subject: [PATCH 2/3] load_dic_file fixed empty lines --- software/script/chameleon_cli_unit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index bfac55f6..3564fb23 100644 --- a/software/script/chameleon_cli_unit.py +++ b/software/script/chameleon_cli_unit.py @@ -56,7 +56,7 @@ def check_tools(): def load_dic_file(filename, keys): #print("Read directory from", filename) ## debug for dict_key in filename.readlines(): - if not dict_key.startswith("#") and not dict_key is None: + if not dict_key.startswith("#") and not dict_key.isspace(): dict_keys.add(dict_key) return dict_keys From 251c13d998f9cce046ffdd6eda4c00b3aed0d799 Mon Sep 17 00:00:00 2001 From: Benjamin Moeller Date: Sat, 17 Aug 2024 17:37:20 +0200 Subject: [PATCH 3/3] Delete function, rewrite logic in call. --- software/script/chameleon_cli_unit.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index 3564fb23..f7f91aca 100644 --- a/software/script/chameleon_cli_unit.py +++ b/software/script/chameleon_cli_unit.py @@ -52,14 +52,6 @@ def check_tools(): print(f'{CR}Warning, tools {", ".join(missing_tools)} not found. ' f'Corresponding commands will not work as intended.{C0}') -dict_keys = set() -def load_dic_file(filename, keys): - #print("Read directory from", filename) ## debug - for dict_key in filename.readlines(): - if not dict_key.startswith("#") and not dict_key.isspace(): - dict_keys.add(dict_key) - - return dict_keys class BaseCLIUnit: def __init__(self): @@ -951,12 +943,17 @@ def on_exec(self, args: argparse.Namespace): return if args.import_dic is not None: - for key in load_dic_file(args.import_dic, keys): - if not re.match(r'^[a-fA-F0-9]{12}$', key): + for key in args.import_dic.readlines(): + if key.startswith("#"): # ignore comments + pass + elif key.isspace(): # ignore empty lines + pass + elif re.match(r'^[a-fA-F0-9]{12}$', key): # take only this key format + keys.add(bytes.fromhex(key)) + else: # in case of another format, a conversion is needed print(f' - {CR}Key should in hex[12] format, invalid key is ignored{C0}, key = "{key}"') - continue - keys.add(bytes.fromhex(key)) - + continue + if len(keys) == 0: print(f' - {CR}No keys{C0}') return