neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/39745?usp=email )
Change subject: [5/7] personalization: refactor AlgorithmID ......................................................................
[5/7] personalization: refactor AlgorithmID
Refactor AlgorithmID to the new ConfigurableParameter implementation style.
Note from the future: AlgorithmID so far takes "raw" int values, but will turn to be an "enum" parameter with predefined meaningful strings in I71c2ec1b753c66cb577436944634f32792353240
Change-Id: I6296fdcfd5d2ed313c4aade57ff43cc362375848 --- M pySim/esim/saip/personalization.py 1 file changed, 28 insertions(+), 6 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/45/39745/1
diff --git a/pySim/esim/saip/personalization.py b/pySim/esim/saip/personalization.py index 36baf43..34486bd 100644 --- a/pySim/esim/saip/personalization.py +++ b/pySim/esim/saip/personalization.py @@ -428,7 +428,34 @@ keyReference = 0x0B
-class AlgoConfig(ConfigurableParameter): +class AlgorithmID(DecimalParam): + key = 'algorithmID' + allow_len = 1 + + @classmethod + def validate_val(cls, val): + val = super().validate_val(val) + val = int(val) + valid = (1, 2, 3) + if val not in valid: + raise ValueError(f'Invalid algorithmID {val!r}, must be one of {valid}') + return val + + @classmethod + def apply_val(cls, pes: ProfileElementSequence, val): + found = 0 + for pe in pes.get_pes_for_type('akaParameter'): + algoConfiguration = pe.decoded['algoConfiguration'] + if algoConfiguration[0] != 'algoParameter': + continue + algoConfiguration[1][cls.key] = val + found += 1 + if not found: + raise ValueError('input template UPP has unexpected structure:' + f' {cls.name} cannot find algoParameter with key={cls.key}') + + +class AlgoConfig(ConfigurableParameter, metaclass=ClassVarMeta): """Configurable Algorithm parameter.""" key = None def validate(self): @@ -446,8 +473,3 @@ pass class Opc(AlgoConfig, key='opc'): pass -class AlgorithmID(AlgoConfig, key='algorithmID'): - def validate(self): - if self.input_value not in [1, 2, 3]: - raise ValueError('Invalid algorithmID %s' % (self.input_value)) - self.value = self.input_value