From 10e9e97724721d1ced82e2ce118b70304a18b1ba Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 3 Aug 2024 23:26:45 +0200 Subject: [PATCH] pySim.esim.saip.templates: Add expand_default_value() method This method can be used to expand the default value pattern of the file system template for the file to the specified (record, file) length. Change-Id: Id3eb16910c0bdfa572294e14ca1cd44ca95ca69f --- pySim/esim/saip/templates.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/pySim/esim/saip/templates.py b/pySim/esim/saip/templates.py index f1fd9751..eff28c27 100644 --- a/pySim/esim/saip/templates.py +++ b/pySim/esim/saip/templates.py @@ -17,7 +17,7 @@ from typing import * from copy import deepcopy -from pySim.utils import all_subclasses +from pySim.utils import all_subclasses, h2b from pySim.filesystem import Path import pySim.esim.saip.oid as OID @@ -51,6 +51,10 @@ class FileTemplate: # initialize empty self.parent = None self.children = [] + if self.default_val: + length = self._default_value_len() or 100 + # run the method once to verify the pattern can be processed + self.expand_default_value_pattern(length) def __str__(self) -> str: return "FileTemplate(%s)" % (self.name) @@ -85,6 +89,34 @@ class FileTemplate: if path[1].lower() == c.name.lower(): return c.get_file_by_path(path[1:]) + def _default_value_len(self): + if self.file_type in ['TR']: + return self.file_size + elif self.file_type in ['LF', 'CY']: + return self.rec_len + + def expand_default_value_pattern(self, length: Optional[int] = None) -> Optional[bytes]: + """Expand the default value pattern to the specified length.""" + if not length: + length = self._default_value_len() + if not length: + raise ValueError("%s does not have a default length" % self) + if not self.default_val: + return None + if not '...' in self.default_val: + return h2b(self.default_val) + l = self.default_val.split('...') + if len(l) != 2: + raise ValueError("Pattern '%s' contains more than one ..." % self.default_val) + prefix = h2b(l[0]) + suffix = h2b(l[1]) + pad_len = length - len(prefix) - len(suffix) + if pad_len <= 0: + ret = prefix + suffix + return ret[:length] + return prefix + prefix[-1:] * pad_len + suffix + + class ProfileTemplate: """Representation of a SimAlliance/TCA Profile Template. Each Template is identified by its OID and consists of a number of file definitions. We implement each profile template as a class derived from this