8000 Move Traversable to abc module. Closes #87. · jaraco/cpython@bd53527 · GitHub
[go: up one dir, main page]

Skip to content

Commit bd53527

Browse files
committed
Move Traversable to abc module. Closes python#87.
1 parent a4cf54d commit bd53527

File tree

3 files changed

+67
-67
lines changed

3 files changed

+67
-67
lines changed

importlib_resources/_py3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def read_text(package: Package,
132132
return fp.read()
133133

134134

135-
def files(package: Package) -> trees.Traversable:
135+
def files(package: Package) -> resources_abc.Traversable:
136136
"""
137137
Get a Traversable resource from a package
138138
"""

importlib_resources/abc.py

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from __future__ import absolute_import
22

3+
import abc
4+
35
from ._compat import ABC, FileNotFoundError
4-
from abc import abstractmethod
56

67
# We use mypy's comment syntax here since this file must be compatible with
78
# both Python 2 and 3.
@@ -15,7 +16,7 @@
1516
class ResourceReader(ABC):
1617
"""Abstract base class for loaders to provide resource reading support."""
1718

18-
@abstractmethod
19+
@abc.abstractmethod
1920
def open_resource(self, resource):
2021
# type: (Text) -> BinaryIO
2122
"""Return an opened, file-like object for binary reading.
@@ -28,7 +29,7 @@ def open_resource(self, resource):
2829
# it'll still do the right thing.
2930
raise FileNotFoundError
3031

31-
@abstractmethod
32+
@abc.abstractmethod
3233
def resource_path(self, resource):
3334
# type: (Text) -> Text
3435
"""Return the file system path to the specified resource.
@@ -42,7 +43,7 @@ def resource_path(self, resource):
4243
# it'll still do the right thing.
4344
raise FileNotFoundError
4445

45-
@abstractmethod
46+
@abc.abstractmethod
4647
def is_resource(self, path):
4748
# type: (Text) -> bool
4849
"""Return True if the named 'path' is a resource.
@@ -51,8 +52,67 @@ def is_resource(self, path):
5152
"""
5253
raise FileNotFoundError
5354

54-
@abstractmethod
55+
@abc.abstractmethod
5556
def contents(self):
5657
# type: () -> Iterable[str]
5758
"""Return an iterable of entries in `package`."""
5859
raise FileNotFoundError
60+
61+
62+
class Traversable(ABC):
63+
"""
64+
An object with a subset of pathlib.Path methods suitable for
65+
traversing directories and opening files.
66+
"""
67+
68+
@abc.abstractmethod
69+
def iterdir(self):
70+
"""
71+
Yield Traversable objects in self
72+
"""
73+
74+
@abc.abstractmethod
75+
def read_bytes(self):
76+
"""
77+
Read contents of self as bytes
78+
"""
79+
80+
@abc.abstractmethod
81+
def read_text(self, encoding=None):
82+
"""
83+
Read contents of self as bytes
84+
"""
85+
86+
@abc.abstractmethod
87+
def is_dir(self):
88+
"""
89+
Return True if self is a dir
90+
"""
91+
92+
@abc.abstractmethod
93+
def is_file(self):
94+
"""
95+
Return True if self is a file
96+
"""
97+
98+
@abc.abstractmethod
99+
def joinpath(self, child):
100+
"""
101+
Return Traversable child in self
102+
"""
103+
104+
@abc.abstractmethod
105+
def __truediv__(self, child):
106+
"""
107+
Return Traversable child in self
108+
"""
109+
110+
@abc.abstractmethod
111+
def open(self, mode='r', *args, **kwargs):
112+
"""
113+
mode may be 'r' or 'rb' to open as text or binary. Return a handle
114+
suitable for reading (same as pathlib.Path.open).
115+
116+
When opening as text, accepts encoding parameters such as those
117+
accepted by io.TextIOWrapper.
118+
"""

importlib_resources/trees.py

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,15 @@
11
from __future__ import absolute_import
22

33
import os
4-
import abc
54
import tempfile
65
import contextlib
76

87
from ._compat import (
9-
ABC, Path, package_spec, FileNotFoundError, ZipPath,
8+
Path, package_spec, FileNotFoundError, ZipPath,
109
singledispatch,
1110
)
1211

1312

14-
class Traversable(ABC):
15-
"""
16-
An object with a subset of pathlib.Path methods suitable for
17-
traversing directories and opening files.
18-
"""
19-
20-
@abc.abstractmethod
21-
def iterdir(self):
22-
"""
23-
Yield Traversable objects in self
24-
"""
25-
26-
@abc.abstractmethod
27-
def read_bytes(self):
28-
"""
29-
Read contents of self as bytes
30-
"""
31-
32-
@abc.abstractmethod
33-
def read_text(self, encoding=None):
34-
"""
35-
Read contents of self as bytes
36-
"""
37-
38-
@abc.abstractmethod
39-
def is_dir(self):
40-
"""
41-
Return True if self is a dir
42-
"""
43-
44-
@abc.abstractmethod
45-
def is_file(self):
46-
"""
47-
Return True if self is a file
48-
"""
49-
50-
@abc.abstractmethod
51-
def joinpath(self, child):
52-
"""
53-
Return Traversable child in self
54-
"""
55-
56-
@abc.abstractmethod
57-
def __truediv__(self, child):
58-
"""
59-
Return Traversable child in self
60-
"""
61-
62-
@abc.abstractmethod
63-
def open(self, mode='r', *args, **kwargs):
64-
"""
65-
mode may be 'r' or 'rb' to open as text or binary. Return a handle
66-
suitable for reading (same as pathlib.Path.open).
67-
68-
When opening as text, accepts encoding parameters such as those
69-
accepted by io.TextIOWrapper.
70-
"""
71-
72-
7313
def from_package(package):
7414
"""Return a Traversable object for the given package"""
7515
spec = package_spec(package)

0 commit comments

Comments
 (0)
0