laforge has uploaded this change for review.

View Change

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
---
M pySim/esim/saip/templates.py
1 file changed, 45 insertions(+), 1 deletion(-)

git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/41/37741/1
diff --git a/pySim/esim/saip/templates.py b/pySim/esim/saip/templates.py
index f1fd975..eff28c2 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 @@
# 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 @@
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

To view, visit change 37741. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Id3eb16910c0bdfa572294e14ca1cd44ca95ca69f
Gerrit-Change-Number: 37741
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge@osmocom.org>
Gerrit-MessageType: newchange