| 62 | |
| 63 | |
| 64 | class QlFsMapper: |
| 65 | |
| 66 | def __init__(self, path: QlOsPath): |
| 67 | self._mapping: MutableMapping[str, Any] = {} |
| 68 | self.path = path |
| 69 | |
| 70 | def __contains__(self, vpath: str) -> bool: |
| 71 | # canonicalize the path first |
| 72 | absvpath = self.path.virtual_abspath(vpath) |
| 73 | |
| 74 | return absvpath in self._mapping |
| 75 | |
| 76 | def has_mapping(self, vpath: str) -> bool: |
| 77 | """Check whether a specific virtrual path has a binding. |
| 78 | |
| 79 | Args: |
| 80 | vpath: virtual path name to check |
| 81 | |
| 82 | Returns: `True` if the specified virtual path has been bound, `False` otherwise. |
| 83 | """ |
| 84 | |
| 85 | return vpath in self |
| 86 | |
| 87 | def __len__(self) -> int: |
| 88 | return len(self._mapping) |
| 89 | |
| 90 | def mapping_count(self) -> int: |
| 91 | """Count of currently existing bindings. |
| 92 | """ |
| 93 | |
| 94 | return len(self) |
| 95 | |
| 96 | def __open_mapped(self, absvpath: str, opener: Callable, *args) -> Any: |
| 97 | """Internal method user for opening an existing mapped object. |
| 98 | |
| 99 | Args: |
| 100 | absvpath: absolute virtual path name |
| 101 | opener: a method to use to open the target host path |
| 102 | *args: arguments to the opener method |
| 103 | """ |
| 104 | |
| 105 | mapped = self._mapping[absvpath] |
| 106 | |
| 107 | # mapped to a file name on the host file system |
| 108 | if isinstance(mapped, str): |
| 109 | obj = opener(mapped, *args) |
| 110 | |
| 111 | # mapped to a class or a method |
| 112 | elif callable(mapped): |
| 113 | obj = mapped() |
| 114 | |
| 115 | # mapped to another kind of object |
| 116 | else: |
| 117 | obj = mapped |
| 118 | |
| 119 | return obj |
| 120 | |
| 121 | def __open_new(self, absvpath: str, opener: Callable, *args) -> Any: |