Create a new mapping in the virtual filesystem. Args: vpath: a virtual path to bind binding: a target to use whenever the bound virtual path is referenced. such a target can be either a path on the host filesystem, an object instance or a class. the beha
(self, vpath: QlPath, binding: Union[QlPath, QlFsMappedObject, Callable], *, force: bool = False)
| 187 | raise TypeError(path) |
| 188 | |
| 189 | def add_mapping(self, vpath: QlPath, binding: Union[QlPath, QlFsMappedObject, Callable], *, force: bool = False) -> None: |
| 190 | """Create a new mapping in the virtual filesystem. |
| 191 | |
| 192 | Args: |
| 193 | vpath: a virtual path to bind |
| 194 | |
| 195 | binding: a target to use whenever the bound virtual path is referenced. such a target can be |
| 196 | either a path on the host filesystem, an object instance or a class. the behavior of the mapping |
| 197 | is determined by the bound object type: |
| 198 | [*] a string: bind a path on the host filesystem (e.g. "/dev/urandom"). use with caution! |
| 199 | [*] an object: bind an object instance which will be returned each time the virtual path is opened |
| 200 | [*] a class: bind a class that will be instantiated each time the virtual path is opened |
| 201 | |
| 202 | force: when set to `True`, re-mapping an existing vpath becomes possible. In such case, the |
| 203 | old mapping will be discarded |
| 204 | |
| 205 | Raises: |
| 206 | `KeyError`: in case the specified vpath has already been mapped (default behavior). |
| 207 | """ |
| 208 | |
| 209 | vpath = self.__fspath(vpath) |
| 210 | absvpath = self.path.virtual_abspath(vpath) |
| 211 | |
| 212 | if self.has_mapping(absvpath) and not force: |
| 213 | raise KeyError(f'mapping already exists: "{absvpath}"') |
| 214 | |
| 215 | if isinstance(binding, (str, bytes, PathLike)): |
| 216 | binding = self.__fspath(binding) |
| 217 | |
| 218 | self._mapping[absvpath] = binding |
| 219 | |
| 220 | def remove_mapping(self, vpath: QlPath) -> None: |
| 221 | """Remove a mapping from the fs mapper. |
no test coverage detected