From 33e99721e159df7baaad279fe86680c8d32a8ba9 Mon Sep 17 00:00:00 2001 From: Matthew Gamble Date: Mon, 24 Jan 2022 22:23:19 +1100 Subject: [PATCH] Support pathlib for feed filenames in parse() function The mopidy core is using pathlib internally, and its use is growing everywhere all the time. --- mopidy_podcast/feeds.py | 3 +++ tests/conftest.py | 14 ++++++++++---- tests/test_feeds.py | 7 ++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/mopidy_podcast/feeds.py b/mopidy_podcast/feeds.py index 2b60be4..241312e 100644 --- a/mopidy_podcast/feeds.py +++ b/mopidy_podcast/feeds.py @@ -1,6 +1,7 @@ import datetime import email.utils import re +from pathlib import Path import uritools from mopidy import models @@ -14,6 +15,8 @@ def parse(source): + if isinstance(source, Path): + source = str(source) if isinstance(source, str): url = uritools.uricompose("file", "", source) else: diff --git a/tests/conftest.py b/tests/conftest.py index 7a7aac8..a6a592d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ import functools import os +from pathlib import Path from unittest import mock import mopidy_podcast as ext @@ -7,8 +8,13 @@ @pytest.fixture -def abspath(): - return functools.partial(os.path.join, os.path.dirname(__file__)) +def testpath() -> Path: + return Path(__file__).parent + + +@pytest.fixture +def abspath(testpath: Path): + return functools.partial(os.path.join, str(testpath)) @pytest.fixture @@ -17,7 +23,7 @@ def audio(): @pytest.fixture -def config(): +def config(testpath: Path): return { "podcast": { "browse_root": "Podcasts.opml", @@ -27,7 +33,7 @@ def config(): "cache_ttl": 86400, "timeout": 10, }, - "core": {"config_dir": os.path.dirname(__file__)}, + "core": {"config_dir": testpath}, "proxy": {}, } diff --git a/tests/test_feeds.py b/tests/test_feeds.py index 0dc538c..0cf4091 100644 --- a/tests/test_feeds.py +++ b/tests/test_feeds.py @@ -1,4 +1,5 @@ import uritools +from pathlib import Path import pytest from mopidy_podcast import feeds @@ -8,8 +9,8 @@ "filename,expected", [("directory.xml", feeds.OpmlFeed), ("rssfeed.xml", feeds.RssFeed)], ) -def test_parse(abspath, filename, expected): - path = abspath(filename) +def test_parse(testpath: Path, filename: str, expected): + path = testpath / filename feed = feeds.parse(path) assert isinstance(feed, expected) - assert feed.uri == uritools.uricompose("podcast+file", "", path) + assert feed.uri == uritools.uricompose("podcast+file", "", str(path))