card_key_provider: add a static method to parse --column-keys args

The contents of the --column-keys arguments are currently parsed
in init_card_key_provider. Let's add a static method in
CardKeyFieldCryptor to simplify re-usage of the CardKeyFieldCryptor

Related: SYS#6959
This commit is contained in:
Philipp Maier
2026-05-07 14:18:16 +02:00
parent 3a713ca2a6
commit 191113baee

View File

@@ -140,6 +140,23 @@ class CardKeyFieldCryptor:
arg_parser.add_argument('--csv-column-key', metavar='FIELD:AES_KEY_HEX', default=[], action='append', arg_parser.add_argument('--csv-column-key', metavar='FIELD:AES_KEY_HEX', default=[], action='append',
help=argparse.SUPPRESS, dest='column_key') help=argparse.SUPPRESS, dest='column_key')
@staticmethod
def transport_keys_from_opts(opts: argparse.Namespace) -> dict:
"""
Transport keys are passed via the commandline using the '--column-key' option. Each column requires a
dedicated transport key. This method can be used to extract the column keys parameters from the commandline
options into a dict that can be directly passed to the construtor with the transport_keys argument.
Args:
opts: parsed commandline options (Namespace)
"""
transport_keys = {}
for par in opts.column_key:
name, key = par.split(':')
transport_keys[name] = key
return transport_keys
class CardKeyProvider(abc.ABC): class CardKeyProvider(abc.ABC):
"""Base class, not containing any concrete implementation.""" """Base class, not containing any concrete implementation."""
@@ -343,10 +360,7 @@ def argparse_add_card_key_provider_args(arg_parser: argparse.ArgumentParser):
def init_card_key_provider(opts: argparse.Namespace): def init_card_key_provider(opts: argparse.Namespace):
"""Initialize card key provider depending on the user provided commandline options""" """Initialize card key provider depending on the user provided commandline options"""
column_keys = {} transport_keys = CardKeyFieldCryptor.transport_keys_from_opts(opts)
for par in opts.column_key:
name, key = par.split(':')
column_keys[name] = key
card_key_field_cryptor = CardKeyFieldCryptor(transport_keys) card_key_field_cryptor = CardKeyFieldCryptor(transport_keys)
if os.path.isfile(os.path.expanduser(opts.csv)): if os.path.isfile(os.path.expanduser(opts.csv)):
card_key_provider_register(CardKeyProviderCsv(os.path.expanduser(opts.csv), card_key_field_cryptor)) card_key_provider_register(CardKeyProviderCsv(os.path.expanduser(opts.csv), card_key_field_cryptor))