|
14 | 14 | from ._abc import Loader |
15 | 15 | import abc |
16 | 16 | import warnings |
17 | | -from typing import BinaryIO, Iterable, Text |
18 | | -from typing import Protocol, runtime_checkable |
| 17 | + |
| 18 | +# for compatibility with Python 3.10 |
| 19 | +from .resources.abc import ResourceReader, Traversable, TraversableResources |
| 20 | + |
| 21 | + |
| 22 | +__all__ = [ |
| 23 | + 'Loader', 'Finder', 'MetaPathFinder', 'PathEntryFinder', |
| 24 | + 'ResourceLoader', 'InspectLoader', 'ExecutionLoader', |
| 25 | + 'FileLoader', 'SourceLoader', |
| 26 | + |
| 27 | + # for compatibility with Python 3.10 |
| 28 | + 'ResourceReader', 'Traversable', 'TraversableResources', |
| 29 | +] |
19 | 30 |
|
20 | 31 |
|
21 | 32 | def _register(abstract_cls, *classes): |
@@ -307,136 +318,3 @@ def set_data(self, path, data): |
307 | 318 | """ |
308 | 319 |
|
309 | 320 | _register(SourceLoader, machinery.SourceFileLoader) |
310 | | - |
311 | | - |
312 | | -class ResourceReader(metaclass=abc.ABCMeta): |
313 | | - """Abstract base class for loaders to provide resource reading support.""" |
314 | | - |
315 | | - @abc.abstractmethod |
316 | | - def open_resource(self, resource: Text) -> BinaryIO: |
317 | | - """Return an opened, file-like object for binary reading. |
318 | | -
|
319 | | - The 'resource' argument is expected to represent only a file name. |
320 | | - If the resource cannot be found, FileNotFoundError is raised. |
321 | | - """ |
322 | | - # This deliberately raises FileNotFoundError instead of |
323 | | - # NotImplementedError so that if this method is accidentally called, |
324 | | - # it'll still do the right thing. |
325 | | - raise FileNotFoundError |
326 | | - |
327 | | - @abc.abstractmethod |
328 | | - def resource_path(self, resource: Text) -> Text: |
329 | | - """Return the file system path to the specified resource. |
330 | | -
|
331 | | - The 'resource' argument is expected to represent only a file name. |
332 | | - If the resource does not exist on the file system, raise |
333 | | - FileNotFoundError. |
334 | | - """ |
335 | | - # This deliberately raises FileNotFoundError instead of |
336 | | - # NotImplementedError so that if this method is accidentally called, |
337 | | - # it'll still do the right thing. |
338 | | - raise FileNotFoundError |
339 | | - |
340 | | - @abc.abstractmethod |
341 | | - def is_resource(self, path: Text) -> bool: |
342 | | - """Return True if the named 'path' is a resource. |
343 | | -
|
344 | | - Files are resources, directories are not. |
345 | | - """ |
346 | | - raise FileNotFoundError |
347 | | - |
348 | | - @abc.abstractmethod |
349 | | - def contents(self) -> Iterable[str]: |
350 | | - """Return an iterable of entries in `package`.""" |
351 | | - raise FileNotFoundError |
352 | | - |
353 | | - |
354 | | -@runtime_checkable |
355 | | -class Traversable(Protocol): |
356 | | - """ |
357 | | - An object with a subset of pathlib.Path methods suitable for |
358 | | - traversing directories and opening files. |
359 | | - """ |
360 | | - |
361 | | - @abc.abstractmethod |
362 | | - def iterdir(self): |
363 | | - """ |
364 | | - Yield Traversable objects in self |
365 | | - """ |
366 | | - |
367 | | - def read_bytes(self): |
368 | | - """ |
369 | | - Read contents of self as bytes |
370 | | - """ |
371 | | - with self.open('rb') as strm: |
372 | | - return strm.read() |
373 | | - |
374 | | - def read_text(self, encoding=None): |
375 | | - """ |
376 | | - Read contents of self as text |
377 | | - """ |
378 | | - with self.open(encoding=encoding) as strm: |
379 | | - return strm.read() |
380 | | - |
381 | | - @abc.abstractmethod |
382 | | - def is_dir(self) -> bool: |
383 | | - """ |
384 | | - Return True if self is a directory |
385 | | - """ |
386 | | - |
387 | | - @abc.abstractmethod |
388 | | - def is_file(self) -> bool: |
389 | | - """ |
390 | | - Return True if self is a file |
391 | | - """ |
392 | | - |
393 | | - @abc.abstractmethod |
394 | | - def joinpath(self, child): |
395 | | - """ |
396 | | - Return Traversable child in self |
397 | | - """ |
398 | | - |
399 | | - def __truediv__(self, child): |
400 | | - """ |
401 | | - Return Traversable child in self |
402 | | - """ |
403 | | - return self.joinpath(child) |
404 | | - |
405 | | - @abc.abstractmethod |
406 | | - def open(self, mode='r', *args, **kwargs): |
407 | | - """ |
408 | | - mode may be 'r' or 'rb' to open as text or binary. Return a handle |
409 | | - suitable for reading (same as pathlib.Path.open). |
410 | | -
|
411 | | - When opening as text, accepts encoding parameters such as those |
412 | | - accepted by io.TextIOWrapper. |
413 | | - """ |
414 | | - |
415 | | - @abc.abstractproperty |
416 | | - def name(self) -> str: |
417 | | - """ |
418 | | - The base name of this object without any parent references. |
419 | | - """ |
420 | | - |
421 | | - |
422 | | -class TraversableResources(ResourceReader): |
423 | | - """ |
424 | | - The required interface for providing traversable |
425 | | - resources. |
426 | | - """ |
427 | | - |
428 | | - @abc.abstractmethod |
429 | | - def files(self): |
430 | | - """Return a Traversable object for the loaded package.""" |
431 | | - |
432 | | - def open_resource(self, resource): |
433 | | - return self.files().joinpath(resource).open('rb') |
434 | | - |
435 | | - def resource_path(self, resource): |
436 | | - raise FileNotFoundError(resource) |
437 | | - |
438 | | - def is_resource(self, path): |
439 | | - return self.files().joinpath(path).is_file() |
440 | | - |
441 | | - def contents(self): |
442 | | - return (item.name for item in self.files().iterdir()) |
0 commit comments