Skip to content

Commit 4eb6788

Browse files
committed
Move Multiplexed to its own module
1 parent 6934f74 commit 4eb6788

File tree

2 files changed

+52
-50
lines changed

2 files changed

+52
-50
lines changed

importlib_resources/abc.py

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from __future__ import absolute_import
22

33
import abc
4-
import itertools
54

6-
from ._compat import ABC, FileNotFoundError, suppress
5+
from ._compat import ABC, FileNotFoundError
76

87
# Use mypy's comment syntax for Python 2 compatibility
98
try:
@@ -117,54 +116,6 @@ def open(self, mode='r', *args, **kwargs):
117116
"""
118117

119118

120-
class Multiplexed(Traversable):
121-
"""
122-
Given a series of Traversable objects, implement a merged
123-
version of the interface across all objects. Useful for
124-
namespace packages which may be multihomed at a single
125-
name.
126-
"""
127-
128-
def __init__(self, *paths):
129-
self._paths = paths
130-
131-
def iterdir(self):
132-
return itertools.chain.from_iterable(
133-
path.iterdir() for path in self._paths)
134-
135-
def read_bytes(self):
136-
return self.open(mode='rb').read()
137-
138-
def read_text(self, *args, **kwargs):
139-
return self.open(mode='r', *args, **kwargs).read()
140-
141-
def is_dir(self):
142-
return any(path.is_dir() for path in self._paths)
143-
144-
def is_file(self):
145-
return any(path.is_file() for path in self._paths)
146-
147-
def joinpath(self, child):
148-
children = (
149-
path.joinpath(child)
150-
for path in self._paths
151-
)
152-
existing = (
153-
child
154-
for child in children
155-
if child.is_dir() or child.is_file()
156-
)
157-
return Multiplexed(*existing)
158-
159-
__truediv__ = joinpath
160-
161-
def open(self, *args, **kwargs):
162-
for path in self._paths[:-1]:
163-
with suppress(Exception):
164-
return path.open(*args, **kwargs)
165-
return self._paths[-1].open(*args, **kwargs)
166-
167-
168119
class TraversableResources(ResourceReader):
169120
@abc.abstractmethod
170121
def files(self):

importlib_resources/namespace.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import itertools
2+
from ._compat import suppress
3+
from .abc import Traversable
4+
5+
6+
class Multiplexed(Traversable):
7+
"""
8+
Given a series of Traversable objects, implement a merged
9+
version of the interface across all objects. Useful for
10+
namespace packages which may be multihomed at a single
11+
name.
12+
"""
13+
14+
def __init__(self, *paths):
15+
self._paths = paths
16+
17+
def iterdir(self):
18+
return itertools.chain.from_iterable(
19+
path.iterdir() for path in self._paths)
20+
21+
def read_bytes(self):
22+
return self.open(mode='rb').read()
23+
24+
def read_text(self, *args, **kwargs):
25+
return self.open(mode='r', *args, **kwargs).read()
26+
27+
def is_dir(self):
28+
return any(path.is_dir() for path in self._paths)
29+
30+
def is_file(self):
31+
return any(path.is_file() for path in self._paths)
32+
33+
def joinpath(self, child):
34+
children = (
35+
path.joinpath(child)
36+
for path in self._paths
37+
)
38+
existing = (
39+
child
40+
for child in children
41+
if child.is_dir() or child.is_file()
42+
)
43+
return Multiplexed(*existing)
44+
45+
__truediv__ = joinpath
46+
47+
def open(self, *args, **kwargs):
48+
for path in self._paths[:-1]:
49+
with suppress(Exception):
50+
return path.open(*args, **kwargs)
51+
return self._paths[-1].open(*args, **kwargs)

0 commit comments

Comments
 (0)