filesystem: add flags to filter selectables

When requesting what DF/EF/ADF are selectable it is useful to have some
control of what we do not want in the resulting list.

Change-Id: Idb50a512bfdbfdf2e98f2ce0e89928cb0ff19f5e
Related: OS#4963
This commit is contained in:
Philipp Maier
2021-02-25 16:48:10 +01:00
committed by laforge
parent 3b51f436a4
commit 786f781a5f

View File

@@ -86,33 +86,38 @@ class CardFile(object):
node = node.parent node = node.parent
return node return node
def _get_self_selectables(self, alias=None): def _get_self_selectables(self, alias=None, flags = []):
"""Return a dict of {'identifier': self} tuples""" """Return a dict of {'identifier': self} tuples"""
sels = {} sels = {}
if alias: if alias:
sels.update({alias: self}) sels.update({alias: self})
if self.fid: if self.fid and (flags == [] or 'FIDS' in flags):
sels.update({self.fid: self}) sels.update({self.fid: self})
if self.name: if self.name and (flags == [] or 'NAMES' in flags):
sels.update({self.name: self}) sels.update({self.name: self})
return sels return sels
def get_selectables(self): def get_selectables(self, flags = []):
"""Return a dict of {'identifier': File} that is selectable from the current file.""" """Return a dict of {'identifier': File} that is selectable from the current file."""
sels = {}
# we can always select ourself # we can always select ourself
sels = self._get_self_selectables('.') if flags == [] or 'SELF' in flags:
sels = self._get_self_selectables('.', flags)
# we can always select our parent # we can always select our parent
sels = self.parent._get_self_selectables('..') if flags == [] or 'PARENT' in flags:
sels = self.parent._get_self_selectables('..', flags)
# if we have a MF, we can always select its applications # if we have a MF, we can always select its applications
mf = self.get_mf() if flags == [] or 'MF' in flags:
if mf: mf = self.get_mf()
sels.update(mf._get_self_selectables()) if mf:
sels.update(mf.get_app_selectables()) sels.update(mf._get_self_selectables(flags = flags))
if flags == [] or 'APPS' in flags:
sels.update(mf.get_app_selectables(flags))
return sels return sels
def get_selectable_names(self): def get_selectable_names(self, flags = []):
"""Return a list of strings for all identifiers that are selectable from the current file.""" """Return a list of strings for all identifiers that are selectable from the current file."""
sels = self.get_selectables() sels = self.get_selectables(flags)
return sels.keys() return sels.keys()
def decode_select_response(self, data_hex): def decode_select_response(self, data_hex):
@@ -158,12 +163,14 @@ class CardDF(CardFile):
for child in children: for child in children:
self.add_file(child, ignore_existing) self.add_file(child, ignore_existing)
def get_selectables(self): def get_selectables(self, flags = []):
"""Get selectable (DF/EF names) from current DF""" """Get selectable (DF/EF names) from current DF"""
# global selectables + our children # global selectables + our children
sels = super().get_selectables() sels = super().get_selectables(flags)
sels.update({x.fid: x for x in self.children.values() if x.fid}) if flags == [] or 'FIDS' in flags:
sels.update({x.name: x for x in self.children.values() if x.name}) sels.update({x.fid: x for x in self.children.values() if x.fid})
if flags == [] or 'NAMES' in flags:
sels.update({x.name: x for x in self.children.values() if x.name})
return sels return sels
def lookup_file_by_name(self, name): def lookup_file_by_name(self, name):
@@ -216,16 +223,20 @@ class CardMF(CardDF):
"""Get list of completions (AID names)""" """Get list of completions (AID names)"""
return [x.name for x in self.applications] return [x.name for x in self.applications]
def get_selectables(self): def get_selectables(self, flags = []):
"""Get list of completions (DF/EF/ADF names) from current DF""" """Get list of completions (DF/EF/ADF names) from current DF"""
sels = super().get_selectables() sels = super().get_selectables(flags)
sels.update(self.get_app_selectables()) if flags == [] or 'APPS' in flags:
sels.update(self.get_app_selectables(flags))
return sels return sels
def get_app_selectables(self): def get_app_selectables(self, flags = []):
# applications by AID + name """Get applications by AID + name"""
sels = {x.aid: x for x in self.applications.values()} sels = {}
sels.update({x.name: x for x in self.applications.values() if x.name}) if flags == [] or 'FIDS' in flags:
sels.update({x.aid: x for x in self.applications.values()})
if flags == [] or 'NAMES' in flags:
sels.update({x.name: x for x in self.applications.values() if x.name})
return sels return sels
def decode_select_response(self, data_hex): def decode_select_response(self, data_hex):
@@ -261,10 +272,10 @@ class CardEF(CardFile):
def __str__(self): def __str__(self):
return "EF(%s)" % (super().__str__()) return "EF(%s)" % (super().__str__())
def get_selectables(self): def get_selectables(self, flags = []):
"""Get list of completions (EF names) from current DF""" """Get list of completions (EF names) from current DF"""
#global selectable names + those of the parent DF #global selectable names + those of the parent DF
sels = super().get_selectables() sels = super().get_selectables(flags)
sels.update({x.name:x for x in self.parent.children.values() if x != self}) sels.update({x.name:x for x in self.parent.children.values() if x != self})
return sels return sels