Skip to content

Commit 7dee55e

Browse files
sambacc: make FileOpener a common type
Move the private implementation of the minimal FileOpener opener type to the opener.py module. This will allow other parts of sambacc to reuse that opener. Update the config test to match the changes. Signed-off-by: John Mulligan <[email protected]>
1 parent 8dbeee5 commit 7dee55e

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

sambacc/config.py

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

28-
from .opener import Opener
28+
from .opener import Opener, FileOpener
2929

3030
_VALID_VERSIONS = ["v0"]
3131

@@ -155,11 +155,6 @@ def _check_config_valid(
155155
raise
156156

157157

158-
class _FileOpener:
159-
def open(self, path: str) -> typing.IO:
160-
return _open(path, "rb")
161-
162-
163158
def read_config_files(
164159
fnames: list[str],
165160
*,
@@ -175,7 +170,7 @@ def read_config_files(
175170
# users will be split from the main config (for security reasons) but
176171
# it would be nicer to have a good merge algorithm handle everything
177172
# smarter at some point.
178-
opener = opener or _FileOpener()
173+
opener = opener or FileOpener()
179174
gconfig = GlobalConfig()
180175
readfiles = set()
181176
for fname in fnames:

sambacc/opener.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __init__(
4343
open_fn: typing.Optional[typing.Callable[..., typing.IO]] = None,
4444
) -> None:
4545
self._openers = openers
46-
self._open_fn = open_fn or open
46+
self._open_fn = open_fn or FileOpener.open
4747

4848
def open(self, path_or_uri: str) -> typing.IO:
4949
for opener in self._openers:
@@ -55,3 +55,11 @@ def open(self, path_or_uri: str) -> typing.IO:
5555

5656
def _open(self, path: str) -> typing.IO:
5757
return self._open_fn(path)
58+
59+
60+
class FileOpener:
61+
"""Minimal opener that only supports opening local files."""
62+
63+
@staticmethod
64+
def open(path: str) -> typing.IO:
65+
return open(path, "rb")

tests/test_config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import unittest
2323

2424
import sambacc.config
25+
import sambacc.opener
2526

2627
config1 = """
2728
{
@@ -451,7 +452,7 @@ def test_tesd_config_files_realerr_rootok(monkeypatch):
451452
def err_open(*args):
452453
raise OSError("test!")
453454

454-
monkeypatch.setattr(sambacc.config, "_open", err_open)
455+
monkeypatch.setattr(sambacc.opener.FileOpener, "open", err_open)
455456
fname = "/etc/foobar"
456457
with pytest.raises(OSError):
457458
sambacc.config.read_config_files([fname])

0 commit comments

Comments
 (0)