Virtual to host path manipulations helper.
| 14 | |
| 15 | |
| 16 | class QlOsPath: |
| 17 | """Virtual to host path manipulations helper. |
| 18 | """ |
| 19 | |
| 20 | def __init__(self, rootfs: str, cwd: str, emulos: QL_OS) -> None: |
| 21 | """Initialize a path manipulation object. |
| 22 | |
| 23 | Args: |
| 24 | rootfs : host path to serve as the virtual root directory |
| 25 | cwd : virtual current working directory |
| 26 | emuls : emulated operating system |
| 27 | """ |
| 28 | |
| 29 | nt_path_os = (QL_OS.WINDOWS, QL_OS.DOS) |
| 30 | posix_path_os = QL_OS_POSIX |
| 31 | |
| 32 | # rootfs is a local directory on the host, and expected to exist |
| 33 | self._rootfs_path = Path(rootfs).resolve(strict=True) |
| 34 | |
| 35 | # determine how virtual paths should be handled |
| 36 | if emulos in nt_path_os: |
| 37 | self.PureVirtualPath = PureWindowsPath |
| 38 | |
| 39 | elif emulos in posix_path_os: |
| 40 | self.PureVirtualPath = PurePosixPath |
| 41 | |
| 42 | else: |
| 43 | raise ValueError(f'unexpected os type: {emulos}') |
| 44 | |
| 45 | self.cwd = cwd |
| 46 | |
| 47 | # <TEMPORARY> |
| 48 | # temporary aliases for backward compatibility |
| 49 | self.transform_to_relative_path = self.virtual_abspath |
| 50 | self.transform_to_real_path = self.virtual_to_host_path |
| 51 | # </TEMPORARY> |
| 52 | |
| 53 | @staticmethod |
| 54 | def __strip_parent_refs(path: AnyPurePath) -> AnyPurePath: |
| 55 | """Strip leading parent dir references, if any. |
| 56 | """ |
| 57 | |
| 58 | if path.parts: |
| 59 | pardir = r'..' |
| 60 | |
| 61 | while path.parts[0] == pardir: |
| 62 | path = path.relative_to(pardir) |
| 63 | |
| 64 | return path |
| 65 | |
| 66 | @property |
| 67 | def root(self) -> str: |
| 68 | return str(self._cwd_anchor) |
| 69 | |
| 70 | @property |
| 71 | def cwd(self) -> str: |
| 72 | return str(self._cwd_anchor / self._cwd_vpath) |
| 73 |
no outgoing calls