Change in pysim[master]: filesystem: fix various issues found by mypy

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Fri Apr 2 19:05:24 UTC 2021


laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/23581 )


Change subject: filesystem: fix various issues found by mypy
......................................................................

filesystem: fix various issues found by mypy

Change-Id: Ib4de80451614712bdf5377a3a5b86156008e2c42
---
M pySim/filesystem.py
M pySim/utils.py
2 files changed, 30 insertions(+), 26 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/81/23581/1

diff --git a/pySim/filesystem.py b/pySim/filesystem.py
index 1c2a886..be0aaf6 100644
--- a/pySim/filesystem.py
+++ b/pySim/filesystem.py
@@ -31,7 +31,7 @@
 from cmd2 import CommandSet, with_default_category, with_argparser
 import argparse
 
-from typing import Optional, Iterable, List, Any
+from typing import cast, Optional, Iterable, List, Any, Dict, Tuple
 
 from pySim.utils import sw_match, h2b, b2h, is_hex
 from pySim.exceptions import *
@@ -82,17 +82,19 @@
         else:
             return self.fid
 
-    def fully_qualified_path(self, prefer_name:bool=True):
+    def fully_qualified_path(self, prefer_name:bool=True) -> List[str]:
         """Return fully qualified path to file as list of FID or name strings.
 
         Args:
             prefer_name : Preferably build path of names; fall-back to FIDs as required
         """
-        if self.parent != self:
+        if self.parent and self.parent != self:
             ret = self.parent.fully_qualified_path(prefer_name)
         else:
             ret = []
-        ret.append(self._path_element(prefer_name))
+        elem = self._path_element(prefer_name)
+        if elem:
+            ret.append(elem)
         return ret
 
     def get_mf(self) -> Optional['CardMF']:
@@ -101,11 +103,11 @@
             return None
         # iterate towards the top. MF has parent == self
         node = self
-        while node.parent != node:
+        while node.parent and node.parent != node:
             node = node.parent
-        return node
+        return cast(CardMF, node)
 
-    def _get_self_selectables(self, alias:str=None, flags = []) -> dict:
+    def _get_self_selectables(self, alias:str=None, flags = []) -> Dict[str, 'CardFile']:
         """Return a dict of {'identifier': self} tuples.
 
         Args:
@@ -124,7 +126,7 @@
             sels.update({self.name: self})
         return sels
 
-    def get_selectables(self, flags = []) -> dict:
+    def get_selectables(self, flags = []) -> Dict[str, 'CardFile']:
         """Return a dict of {'identifier': File} that is selectable from the current file.
 
         Args:
@@ -140,7 +142,8 @@
             sels = self._get_self_selectables('.', flags)
         # we can always select our parent
         if flags == [] or 'PARENT' in flags:
-            sels = self.parent._get_self_selectables('..', flags)
+            if self.parent:
+                sels = self.parent._get_self_selectables('..', flags)
         # if we have a MF, we can always select its applications
         if flags == [] or 'MF' in flags:
             mf = self.get_mf()
@@ -149,22 +152,22 @@
                 sels.update(mf.get_app_selectables(flags = flags))
         return sels
 
-    def get_selectable_names(self, flags = []) -> dict:
+    def get_selectable_names(self, flags = []) -> List[str]:
         """Return a dict of {'identifier': File} that is selectable from the current file.
 
         Args:
             flags : Specify which selectables to return 'FIDS' and/or 'NAMES';
                     If not specified, all selectables will be returned.
         Returns:
-            dict containing all selectable items. Key is identifier (string), value
-            a reference to a CardFile (or derived class) instance.
+            list containing all selectable names.
         """
         sels = self.get_selectables(flags)
-        return sels.keys()
+        return list(sels.keys())
 
     def decode_select_response(self, data_hex:str):
         """Decode the response to a SELECT command."""
-        return self.parent.decode_select_response(data_hex)
+        if self.parent:
+            return self.parent.decode_select_response(data_hex)
 
 
 class CardDF(CardFile):
@@ -241,7 +244,7 @@
                 sels.update({x.name: x for x in self.children.values() if x.name})
         return sels
 
-    def lookup_file_by_name(self, name:str) -> Optional[CardFile]:
+    def lookup_file_by_name(self, name:Optional[str]) -> Optional[CardFile]:
         """Find a file with given name within current DF."""
         if name == None:
             return None
@@ -250,12 +253,12 @@
                 return i
         return None
 
-    def lookup_file_by_sfid(self, sfid:str) -> Optional[CardFile]:
+    def lookup_file_by_sfid(self, sfid:Optional[str]) -> Optional[CardFile]:
         """Find a file with given short file ID within current DF."""
         if sfid == None:
             return None
         for i in self.children.values():
-            if i.sfid == int(sfid):
+            if i.sfid == int(str(sfid)):
                 return i
         return None
 
@@ -332,8 +335,9 @@
     def __init__(self, aid:str, **kwargs):
         super().__init__(**kwargs)
         self.aid = aid           # Application Identifier
-        if self.parent:
-            self.parent.add_application(self)
+        mf = self.get_mf()
+        if mf:
+            mf.add_application(self)
 
     def __str__(self):
         return "ADF(%s)" % (self.aid)
@@ -644,7 +648,7 @@
         method = getattr(self, '_encode_record_bin', None)
         if callable(method):
             raw_bin_data = method(abstract_data)
-            return h2b(raw_bin_data)
+            return b2h(raw_bin_data)
         raise NotImplementedError
 
     def encode_record_bin(self, abstract_data:dict) -> bytearray:
@@ -683,8 +687,8 @@
     We add a special class for those, so the user only has to provide encoder/decoder functions
     for a record, while this class takes care of split / merge of records.
     """
-    def __init__(self, fid:str, sfid:str=None, name:str=None, desc:str=None,
-                 parent:Optional[CardDF]=None, rec_len:int=None, size={1,None}):
+    def __init__(self, fid:str, rec_len:int, sfid:str=None, name:str=None, desc:str=None,
+                 parent:Optional[CardDF]=None, size={1,None}):
         """
         Args:
             fid : File Identifier (4 hex digits)
@@ -757,7 +761,7 @@
             return method(abstract_data)
         method = getattr(self, '_encode_record_bin', None)
         if callable(method):
-            return h2b(method(abstract_data))
+            return b2h(method(abstract_data))
         raise NotImplementedError
 
     def encode_record_bin(self, abstract_data:dict) -> bytearray:
@@ -1040,8 +1044,8 @@
             rec_nr : Record number to read
             data_hex : Abstract data to be written
         """
-        hex_data = self.selected_file.encode_record_hex(data)
-        return self.update_record(self, rec_nr, data_hex)
+        data_hex = self.selected_file.encode_record_hex(data)
+        return self.update_record(rec_nr, data_hex)
 
 
 
diff --git a/pySim/utils.py b/pySim/utils.py
index 607526c..75106d8 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -803,7 +803,7 @@
 
 	return None
 
-def sw_match(sw:str, pattern:str) -> str:
+def sw_match(sw:str, pattern:str) -> bool:
 	"""Match given SW against given pattern."""
 	# Create a masked version of the returned status word
 	sw_lower = sw.lower()

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/23581
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ib4de80451614712bdf5377a3a5b86156008e2c42
Gerrit-Change-Number: 23581
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210402/c818fc33/attachment.htm>


More information about the gerrit-log mailing list