Skip to content

Commit defc095

Browse files
sambacc: support passing an opener to configuration reader
Add support to config.py so that an opener can be used in lieu of directly opening local files by path. Signed-off-by: John Mulligan <[email protected]>
1 parent 1a4b610 commit defc095

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

sambacc/config.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import sys
2626
import typing
2727

28+
from .opener import Opener
29+
2830
_VALID_VERSIONS = ["v0"]
2931

3032
# alias open to _open to support test assertions when running
@@ -153,8 +155,16 @@ def _check_config_valid(
153155
raise
154156

155157

158+
class _FileOpener:
159+
def open(self, path: str) -> typing.IO:
160+
return _open(path, "rb")
161+
162+
156163
def read_config_files(
157-
fnames: list[str], *, require_validation: typing.Optional[bool] = None
164+
fnames: list[str],
165+
*,
166+
require_validation: typing.Optional[bool] = None,
167+
opener: typing.Optional[Opener] = None,
158168
) -> GlobalConfig:
159169
"""Read the global container config from the given filenames.
160170
At least one of the files from the fnames list must exist and contain
@@ -165,12 +175,13 @@ def read_config_files(
165175
# users will be split from the main config (for security reasons) but
166176
# it would be nicer to have a good merge algorithm handle everything
167177
# smarter at some point.
178+
opener = opener or _FileOpener()
168179
gconfig = GlobalConfig()
169180
readfiles = set()
170181
for fname in fnames:
171182
config_format = _detect_format(str(fname))
172183
try:
173-
with _open(fname, "rb") as fh:
184+
with opener.open(fname) as fh:
174185
gconfig.load(
175186
fh,
176187
require_validation=require_validation,

0 commit comments

Comments
 (0)