dexter has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/37785?usp=email )
Change subject: runtime: add method to lookup a file by name without selecting it ......................................................................
runtime: add method to lookup a file by name without selecting it
In some cases it might come in handy to be able to lookup a random file in the file system tree before actually selecting it. This would be very useful in situations where we need to check the presence of the file or if we need to check certain file attributes before performing some task.
Related: OS#6092 Change-Id: I6b6121e749cea843163659e1a26bb3893c032e29 --- M pySim/runtime.py 1 file changed, 36 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/85/37785/1
diff --git a/pySim/runtime.py b/pySim/runtime.py index 08371ae..5ffbee6 100644 --- a/pySim/runtime.py +++ b/pySim/runtime.py @@ -234,6 +234,42 @@ node = node.parent return None
+ def get_file_by_name(self, name: str) -> CardFile: + """Obtain the file object from the file system tree by its name without actually selecting the file. + + Returns: + CardFile() instance or None""" + + # handling of entire paths with multiple directories/elements + if '/' in name: + pathlist = name.split('/') + # treat /DF.GSM/foo like MF/DF.GSM/foo + if pathlist[0] == '': + pathlist[0] = 'MF' + else: + pathlist = [name] + + # start in the current working directory (we can still + # select any ADF and the MF from here, so those will be + # among the selectables). + file = self.get_cwd() + + for p in pathlist: + # Look for the next file in the path list + selectables = file.get_selectables().items() + file = None + for selectable in selectables: + if selectable[1].name == name: + file = selectable[1] + break + + # When we hit none, then the given path must be invalid + if file is None: + return None + + # Return the file object found at the tip of the path + return file + def interpret_sw(self, sw: str): """Interpret a given status word relative to the currently selected application or the underlying card profile.