filesystem: use sort path when selecting an application

The method build_select_path_to uses the internal file system tree model
to find the path to a given file. This works the same for applications
(ADF) as it works for normal files (EF/DF). However, an application can
be selected anytime from any location in the filesystem tree. There is
no need to select a specific path leading to that application first.
This means that if there is an ADF somewhere in the resulting
inter_path, we may clip everything before that ADF.

Related: OS#5418
Change-Id: I838a99bb47afc73b4274baecb04fff31abf7b2e2
diff --git a/pySim/filesystem.py b/pySim/filesystem.py
index d39162c..35863a8 100644
--- a/pySim/filesystem.py
+++ b/pySim/filesystem.py
@@ -136,6 +136,16 @@
         return ret
 
     def build_select_path_to(self, target: 'CardFile') -> Optional[List['CardFile']]:
+
+        # special-case handling for applications. Applications may be selected
+        # any time from any location. If there is an ADF somewhere in the path,
+        # we may clip everything before that ADF.
+        def clip_path(inter_path):
+            for i in reversed(range(0, len(inter_path))):
+                if isinstance(inter_path[i], CardADF):
+                    return inter_path[i:]
+            return inter_path
+
         """Build the relative sequence of files we need to traverse to get from us to 'target'."""
         # special-case handling for selecting MF while we MF is selected
         if target == target.get_mf():
@@ -152,7 +162,7 @@
                     for te2 in target_fqpath[i+1:]:
                         inter_path.append(te2)
                     # we found our common ancestor
-                    return inter_path[1:]
+                    return clip_path(inter_path[1:])
         return None
 
     def get_mf(self) -> Optional['CardMF']: