|
1 | 1 | import abc |
| 2 | +import fnmatch |
2 | 3 | import locale |
3 | 4 | import logging |
4 | 5 | import os |
5 | 6 | import pathlib |
| 7 | +import posixpath |
| 8 | +import re |
| 9 | +import sys |
6 | 10 | from functools import cached_property |
7 | 11 |
|
8 | | -# pylint: disable-next=import-private-name |
9 | | -from pathlib import Path, _PosixFlavour # type: ignore |
| 12 | +from pathlib import Path |
10 | 13 | from urllib.parse import quote_from_bytes as urlquote_from_bytes |
11 | 14 | from io import BytesIO, StringIO |
12 | 15 |
|
|
17 | 20 | logger = logging.getLogger(__name__) |
18 | 21 |
|
19 | 22 |
|
20 | | -class _DatabricksFlavour(_PosixFlavour): |
| 23 | +class _DatabricksFlavour: |
| 24 | + # adapted from pathlib._Flavour, where we ignore support for drives, as we |
| 25 | + # don't have that concept in Databricks. We also ignore support for Windows |
| 26 | + # paths, as we only support POSIX paths in Databricks. |
| 27 | + |
| 28 | + sep = '/' |
| 29 | + altsep = '' |
| 30 | + has_drv = False |
| 31 | + pathmod = posixpath |
| 32 | + is_supported = True |
| 33 | + |
21 | 34 | def __init__(self, ws: WorkspaceClient): |
22 | | - super().__init__() |
| 35 | + self.join = self.sep.join |
23 | 36 | self._ws = ws |
24 | 37 |
|
25 | | - def make_uri(self, path): |
| 38 | + def parse_parts(self, parts: list[str]) -> tuple[str, str, list[str]]: |
| 39 | + # adapted from pathlib._Flavour.parse_parts, |
| 40 | + # where we ignore support for drives, as we |
| 41 | + # don't have that concept in Databricks |
| 42 | + parsed = [] |
| 43 | + drv = root = '' |
| 44 | + for part in reversed(parts): |
| 45 | + if not part: |
| 46 | + continue |
| 47 | + drv, root, rel = self.splitroot(part) |
| 48 | + if self.sep not in rel: |
| 49 | + if rel and rel != '.': |
| 50 | + parsed.append(sys.intern(rel)) |
| 51 | + continue |
| 52 | + for part_ in reversed(rel.split(self.sep)): |
| 53 | + if part_ and part_ != '.': |
| 54 | + parsed.append(sys.intern(part_)) |
| 55 | + if drv or root: |
| 56 | + parsed.append(drv + root) |
| 57 | + parsed.reverse() |
| 58 | + return drv, root, parsed |
| 59 | + |
| 60 | + @staticmethod |
| 61 | + def join_parsed_parts( |
| 62 | + drv: str, |
| 63 | + root: str, |
| 64 | + parts: list[str], |
| 65 | + _, |
| 66 | + root2: str, |
| 67 | + parts2: list[str], |
| 68 | + ) -> tuple[str, str, list[str]]: |
| 69 | + # adapted from pathlib.PurePosixPath, where we ignore support for drives, |
| 70 | + # as we don't have that concept in Databricks |
| 71 | + if root2: |
| 72 | + return drv, root2, [drv + root2] + parts2[1:] |
| 73 | + return drv, root, parts + parts2 |
| 74 | + |
| 75 | + @staticmethod |
| 76 | + def splitroot(part, sep=sep) -> tuple[str, str, str]: |
| 77 | + if part and part[0] == sep: |
| 78 | + stripped_part = part.lstrip(sep) |
| 79 | + if len(part) - len(stripped_part) == 2: |
| 80 | + return '', sep * 2, stripped_part |
| 81 | + return '', sep, stripped_part |
| 82 | + return '', '', part |
| 83 | + |
| 84 | + @staticmethod |
| 85 | + def casefold(value: str) -> str: |
| 86 | + return value |
| 87 | + |
| 88 | + @staticmethod |
| 89 | + def casefold_parts(parts: list[str]) -> list[str]: |
| 90 | + return parts |
| 91 | + |
| 92 | + @staticmethod |
| 93 | + def compile_pattern(pattern: str): |
| 94 | + return re.compile(fnmatch.translate(pattern)).fullmatch |
| 95 | + |
| 96 | + @staticmethod |
| 97 | + def is_reserved(_) -> bool: |
| 98 | + return False |
| 99 | + |
| 100 | + def make_uri(self, path) -> str: |
26 | 101 | return self._ws.config.host + '#workspace' + urlquote_from_bytes(bytes(path)) |
27 | 102 |
|
28 | 103 | def __repr__(self): |
|
0 commit comments