|
| 1 | +import abc |
| 2 | +from typing import BinaryIO, Iterable, Text |
| 3 | +from typing import runtime_checkable, Protocol |
| 4 | + |
| 5 | + |
| 6 | +class ResourceReader(metaclass=abc.ABCMeta): |
| 7 | + """Abstract base class for loaders to provide resource reading support.""" |
| 8 | + |
| 9 | + @abc.abstractmethod |
| 10 | + def open_resource(self, resource: Text) -> BinaryIO: |
| 11 | + """Return an opened, file-like object for binary reading. |
| 12 | +
|
| 13 | + The 'resource' argument is expected to represent only a file name. |
| 14 | + If the resource cannot be found, FileNotFoundError is raised. |
| 15 | + """ |
| 16 | + # This deliberately raises FileNotFoundError instead of |
| 17 | + # NotImplementedError so that if this method is accidentally called, |
| 18 | + # it'll still do the right thing. |
| 19 | + raise FileNotFoundError |
| 20 | + |
| 21 | + @abc.abstractmethod |
| 22 | + def resource_path(self, resource: Text) -> Text: |
| 23 | + """Return the file system path to the specified resource. |
| 24 | +
|
| 25 | + The 'resource' argument is expected to represent only a file name. |
| 26 | + If the resource does not exist on the file system, raise |
| 27 | + FileNotFoundError. |
| 28 | + """ |
| 29 | + # This deliberately raises FileNotFoundError instead of |
| 30 | + # NotImplementedError so that if this method is accidentally called, |
| 31 | + # it'll still do the right thing. |
| 32 | + raise FileNotFoundError |
| 33 | + |
| 34 | + @abc.abstractmethod |
| 35 | + def is_resource(self, path: Text) -> bool: |
| 36 | + """Return True if the named 'path' is a resource. |
| 37 | +
|
| 38 | + Files are resources, directories are not. |
| 39 | + """ |
| 40 | + raise FileNotFoundError |
| 41 | + |
| 42 | + @abc.abstractmethod |
| 43 | + def contents(self) -> Iterable[str]: |
| 44 | + """Return an iterable of entries in `package`.""" |
| 45 | + raise FileNotFoundError |
| 46 | + |
| 47 | + |
| 48 | +@runtime_checkable |
| 49 | +class Traversable(Protocol): |
| 50 | + """ |
| 51 | + An object with a subset of pathlib.Path methods suitable for |
| 52 | + traversing directories and opening files. |
| 53 | + """ |
| 54 | + |
| 55 | + @abc.abstractmethod |
| 56 | + def iterdir(self): |
| 57 | + """ |
| 58 | + Yield Traversable objects in self |
| 59 | + """ |
| 60 | + |
| 61 | + def read_bytes(self): |
| 62 | + """ |
| 63 | + Read contents of self as bytes |
| 64 | + """ |
| 65 | + with self.open('rb') as strm: |
| 66 | + return strm.read() |
| 67 | + |
| 68 | + def read_text(self, encoding=None): |
| 69 | + """ |
| 70 | + Read contents of self as text |
| 71 | + """ |
| 72 | + with self.open(encoding=encoding) as strm: |
| 73 | + return strm.read() |
| 74 | + |
| 75 | + @abc.abstractmethod |
| 76 | + def is_dir(self) -> bool: |
| 77 | + """ |
| 78 | + Return True if self is a directory |
| 79 | + """ |
| 80 | + |
| 81 | + @abc.abstractmethod |
| 82 | + def is_file(self) -> bool: |
| 83 | + """ |
| 84 | + Return True if self is a file |
| 85 | + """ |
| 86 | + |
| 87 | + @abc.abstractmethod |
| 88 | + def joinpath(self, child): |
| 89 | + """ |
| 90 | + Return Traversable child in self |
| 91 | + """ |
| 92 | + |
| 93 | + def __truediv__(self, child): |
| 94 | + """ |
| 95 | + Return Traversable child in self |
| 96 | + """ |
| 97 | + return self.joinpath(child) |
| 98 | + |
| 99 | + @abc.abstractmethod |
| 100 | + def open(self, mode='r', *args, **kwargs): |
| 101 | + """ |
| 102 | + mode may be 'r' or 'rb' to open as text or binary. Return a handle |
| 103 | + suitable for reading (same as pathlib.Path.open). |
| 104 | +
|
| 105 | + When opening as text, accepts encoding parameters such as those |
| 106 | + accepted by io.TextIOWrapper. |
| 107 | + """ |
| 108 | + |
| 109 | + @abc.abstractproperty |
| 110 | + def name(self) -> str: |
| 111 | + """ |
| 112 | + The base name of this object without any parent references. |
| 113 | + """ |
| 114 | + |
| 115 | + |
| 116 | +class TraversableResources(ResourceReader): |
| 117 | + """ |
| 118 | + The required interface for providing traversable |
| 119 | + resources. |
| 120 | + """ |
| 121 | + |
| 122 | + @abc.abstractmethod |
| 123 | + def files(self): |
| 124 | + """Return a Traversable object for the loaded package.""" |
| 125 | + |
| 126 | + def open_resource(self, resource): |
| 127 | + return self.files().joinpath(resource).open('rb') |
| 128 | + |
| 129 | + def resource_path(self, resource): |
| 130 | + raise FileNotFoundError(resource) |
| 131 | + |
| 132 | + def is_resource(self, path): |
| 133 | + return self.files().joinpath(path).is_file() |
| 134 | + |
| 135 | + def contents(self): |
| 136 | + return (item.name for item in self.files().iterdir()) |
0 commit comments