| 1 |
""" |
| 2 |
Simple implementation of JoinablePath, for use in pathlib tests. |
| 3 |
""" |
| 4 |
|
| 5 |
import ntpath |
| 6 |
import os.path |
| 7 |
import posixpath |
| 8 |
|
| 9 |
from . import is_pypi |
| 10 |
|
| 11 |
if is_pypi: |
| 12 |
from pathlib_abc import _JoinablePath |
| 13 |
else: |
| 14 |
from pathlib.types import _JoinablePath |
| 15 |
|
| 16 |
|
| 17 |
class LexicalPath(_JoinablePath): |
| 18 |
__slots__ = ('_segments',) |
| 19 |
parser = os.path |
| 20 |
|
| 21 |
def __init__(self, *pathsegments): |
| 22 |
self._segments = pathsegments |
| 23 |
|
| 24 |
def __hash__(self): |
| 25 |
return hash(str(self)) |
| 26 |
|
| 27 |
def __eq__(self, other): |
| 28 |
if not isinstance(other, LexicalPath): |
| 29 |
return NotImplemented |
| 30 |
return str(self) == str(other) |
| 31 |
|
| 32 |
def __str__(self): |
| 33 |
if not self._segments: |
| 34 |
return '' |
| 35 |
return self.parser.join(*self._segments) |
| 36 |
|
| 37 |
def __repr__(self): |
| 38 |
return f'{type(self).__name__}({str(self)!r})' |
| 39 |
|
| 40 |
def with_segments(self, *pathsegments): |
| 41 |
return type(self)(*pathsegments) |
| 42 |
|
| 43 |
|
| 44 |
class LexicalPosixPath(LexicalPath): |
| 45 |
__slots__ = () |
| 46 |
parser = posixpath |
| 47 |
|
| 48 |
|
| 49 |
class LexicalWindowsPath(LexicalPath): |
| 50 |
__slots__ = () |
| 51 |
parser = ntpath |