Skip to content

Commit

Permalink
ini return text for robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Jun 10, 2020
1 parent 90441ac commit 13f486e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include pylivestream/pylivestream.ini pylivestream/data/logo.png
include src/pylivestream/pylivestream.ini src/pylivestream/data/logo.png
6 changes: 4 additions & 2 deletions src/pylivestream/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ def osparam(self, key: str):
C = ConfigParser(inline_comment_prefixes=("#", ";"))
if self.inifn is None:
logging.info("using package default pylivestream.ini")
self.inifn = utils.get_inifile("pylivestream.ini")
cfg = utils.get_inifile("pylivestream.ini")
else:
cfg = Path(self.inifn).expanduser().read_text()

C.read_string(Path(self.inifn).expanduser().read_text(), source=str(self.inifn))
C.read_string(cfg)

self.exe = get_exe(C.get(sys.platform, "exe", fallback="ffmpeg"))
self.probeexe = get_exe(C.get(sys.platform, "ffprobe_exe", fallback="ffprobe"))
Expand Down
11 changes: 6 additions & 5 deletions src/pylivestream/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,21 @@ def _check_disp(fn: Path) -> int:
return ret == 0


def get_inifile(fn: str) -> Path:
def get_inifile(fn: str) -> str:

for file in (fn, "~/pylivestream.ini"):
if not file:
continue
inifn = Path(file).expanduser()
if inifn.is_file():
return inifn
return inifn.read_text(errors="ignore")

try:
with importlib.resources.path("pylivestream", "pylivestream.ini") as inifn:
return inifn
return importlib.resources.read_text("pylivestream", "pylivestream.ini", errors="ignore")
except NameError:
return Path(pkg_resources.resource_filename(__name__, "logo.png"))
return pkg_resources.resource_string(__name__, "pylivestream.ini").decode(
"utf8", errors="ignore"
)


def meta_caption(meta) -> str:
Expand Down
5 changes: 4 additions & 1 deletion tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

@pytest.mark.parametrize("fn", ["pylivestream.ini"])
def test_get_ini_file(fn):
assert pls.utils.get_inifile(fn).is_file()

cfg = pls.utils.get_inifile(fn)

assert isinstance(cfg, str)


@pytest.mark.parametrize("keyin,keyout", [("abc123", "abc123"), (R / "periscope.key", "abcd1234")])
Expand Down

0 comments on commit 13f486e

Please sign in to comment.