|
| 1 | +"""UPath implementation for DVCFileSystem. |
| 2 | +
|
| 3 | +This provides a `pathlib.Path` like interface to |
| 4 | +work with DVCFileSystem. |
| 5 | +
|
| 6 | +Examples |
| 7 | +-------- |
| 8 | +
|
| 9 | +>>> from upath import UPath |
| 10 | +
|
| 11 | +>>> local = UPath("dvc://path/to/local/repo") |
| 12 | +>>> https = UPath("dvc+https://github.com/iterative/example-get-started", rev="main") |
| 13 | +>>> ssh = UPath("dvc+ssh://git@github.com:iterative/example-get-started.git") |
| 14 | +""" |
| 15 | + |
| 16 | +from urllib.parse import urlsplit |
| 17 | + |
| 18 | +from upath import UPath |
| 19 | + |
| 20 | + |
| 21 | +class DVCPath(UPath): |
| 22 | + @classmethod |
| 23 | + def _transform_init_args(cls, args, protocol, storage_options): |
| 24 | + if not args: |
| 25 | + args = ("/",) |
| 26 | + elif ( |
| 27 | + args |
| 28 | + and "url" not in storage_options |
| 29 | + and protocol in {"dvc+http", "dvc+https", "dvc+ssh"} |
| 30 | + ): |
| 31 | + url, *rest = args |
| 32 | + url = urlsplit(str(url)) |
| 33 | + proto = protocol.split("+")[1] |
| 34 | + if proto == "ssh": |
| 35 | + base_url = url.netloc + url.path |
| 36 | + else: |
| 37 | + base_url = url._replace(scheme=proto).geturl() |
| 38 | + storage_options["url"] = base_url |
| 39 | + # Assume the given path is a root url |
| 40 | + args = ("/", *rest) |
| 41 | + return super()._transform_init_args(args, "dvc", storage_options) |
| 42 | + |
| 43 | + def __str__(self): |
| 44 | + s = super().__str__() |
| 45 | + if url := self.storage_options.get("url"): |
| 46 | + return s.replace("dvc://", f"dvc+{url}", 1) |
| 47 | + return s |
| 48 | + |
| 49 | + def with_segments(self, *pathsegments): |
| 50 | + obj = super().with_segments(*pathsegments) |
| 51 | + # cache filesystem, as dvcfs does not cache filesystem |
| 52 | + # caveat: any joinpath operation will instantiate filesystem |
| 53 | + obj._fs_cached = self.fs |
| 54 | + return obj |
0 commit comments