From 3b33fe57a65c9fcb96dd18b1d3e5496304a00dda Mon Sep 17 00:00:00 2001 From: Benjamin Moeller Date: Sat, 17 Aug 2024 11:53:57 +0200 Subject: [PATCH 1/2] 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 a0da2fdf15303745c8172b8265cf9cb05011a0a5 Mon Sep 17 00:00:00 2001 From: Benjamin Moeller Date: Sat, 17 Aug 2024 12:01:40 +0200 Subject: [PATCH 2/2] load_dic_file fixed empty lines --- software/script/chameleon_cli_unit.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index bfac55f6..14df8376 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 is None: - 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 @@ -2495,7 +2492,7 @@ def on_exec(self, args: argparse.Namespace): print(f"{CY if enabled[fwslot]['hf'] else C0}{hf_tag_type}{C0}") else: print("undef") - if (not args.short) and enabled[fwslot]['hf']: + if (not args.short) and enabled[fwslot]['hf'] and hf_tag_type != TagSpecificType.UNDEFINED: if current != slot: self.cmd.set_active_slot(slot) current = slot @@ -2546,7 +2543,7 @@ def on_exec(self, args: argparse.Namespace): print(f"{CY if enabled[fwslot]['lf'] else C0}{lf_tag_type}{C0}") else: print("undef") - if (not args.short) and enabled[fwslot]['lf']: + if (not args.short) and enabled[fwslot]['lf'] and lf_tag_type != TagSpecificType.UNDEFINED: if current != slot: self.cmd.set_active_slot(slot) current = slot