Skip to content

Commit 051634c

Browse files
authored
Merge pull request #41 from DakaraProject/refactor/37_as_file
Use `as_file` to get resources
2 parents 74e85b0 + eda1df8 commit 051634c

File tree

7 files changed

+45
-37
lines changed

7 files changed

+45
-37
lines changed

tests/integration/test_feeder_songs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from importlib.resources import path
1+
from importlib.resources import as_file, files
22
from shutil import copy
33
from tempfile import TemporaryDirectory
44
from unittest import TestCase, skipUnless
@@ -21,10 +21,10 @@ def test_feed(self, mocked_http_client_dakara_class):
2121
# create the object
2222
with TemporaryDirectory() as temp:
2323
# copy required files
24-
with path("tests.resources.media", "dummy.ass") as file:
24+
with as_file(files("tests.resources.media").joinpath("dummy.ass")) as file:
2525
copy(file, temp)
2626

27-
with path("tests.resources.media", "dummy.mkv") as file:
27+
with as_file(files("tests.resources.media").joinpath("dummy.mkv")) as file:
2828
copy(file, temp)
2929

3030
config = {"server": {}, "kara_folder": str(temp)}

tests/integration/test_feeder_works.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from importlib.resources import path
1+
from importlib.resources import as_file, files
22
from unittest import TestCase
33
from unittest.mock import call, patch
44

@@ -31,8 +31,10 @@ def test_correct_work_file(self, mocked_http_client_dakara_class):
3131
]
3232

3333
# create the object
34-
with path(
35-
"tests.integration.resources.works", "correct_work_file.json"
34+
with as_file(
35+
files("tests.integration.resources.works").joinpath(
36+
"correct_work_file.json"
37+
)
3638
) as filepath:
3739
feeder = WorksFeeder(self.config, filepath, progress=False)
3840

tests/integration/test_metadata.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import timedelta
2-
from importlib.resources import path
2+
from importlib.resources import as_file, files
33
from pathlib import Path
44
from tempfile import TemporaryDirectory
55
from unittest import TestCase, skipUnless
@@ -26,7 +26,7 @@ def test_parse_not_found_error(self):
2626

2727
def test_get_duration(self):
2828
"""Test to get duration."""
29-
with path("tests.resources.media", "dummy.mkv") as file:
29+
with as_file(files("tests.resources.media").joinpath("dummy.mkv")) as file:
3030
parser = MediainfoMetadataParser.parse(file)
3131

3232
self.assertEqual(
@@ -35,14 +35,14 @@ def test_get_duration(self):
3535

3636
def test_get_number_audio_tracks(self):
3737
"""Test to get number of audio tracks."""
38-
with path("tests.resources.media", "dummy.mkv") as file:
38+
with as_file(files("tests.resources.media").joinpath("dummy.mkv")) as file:
3939
parser = MediainfoMetadataParser.parse(file)
4040

4141
self.assertEqual(parser.get_audio_tracks_count(), 2)
4242

4343
def test_get_number_subtitle_tracks(self):
4444
"""Test to get number of subtitle tracks."""
45-
with path("tests.resources.media", "dummy.mkv") as file:
45+
with as_file(files("tests.resources.media").joinpath("dummy.mkv")) as file:
4646
parser = MediainfoMetadataParser.parse(file)
4747

4848
self.assertEqual(parser.get_subtitle_tracks_count(), 1)
@@ -74,7 +74,7 @@ def test_parse_invalid_error(self):
7474

7575
def test_get_duration(self):
7676
"""Test to get duration."""
77-
with path("tests.resources.media", "dummy.mkv") as file:
77+
with as_file(files("tests.resources.media").joinpath("dummy.mkv")) as file:
7878
parser = FFProbeMetadataParser.parse(file)
7979

8080
self.assertEqual(
@@ -83,14 +83,14 @@ def test_get_duration(self):
8383

8484
def test_get_number_audio_tracks(self):
8585
"""Test to get number of audio tracks."""
86-
with path("tests.resources.media", "dummy.mkv") as file:
86+
with as_file(files("tests.resources.media").joinpath("dummy.mkv")) as file:
8787
parser = FFProbeMetadataParser.parse(file)
8888

8989
self.assertEqual(parser.get_audio_tracks_count(), 2)
9090

9191
def test_get_number_subtitle_tracks(self):
9292
"""Test to get number of subtitle tracks."""
93-
with path("tests.resources.media", "dummy.mkv") as file:
93+
with as_file(files("tests.resources.media").joinpath("dummy.mkv")) as file:
9494
parser = FFProbeMetadataParser.parse(file)
9595

9696
self.assertEqual(parser.get_subtitle_tracks_count(), 1)

tests/integration/test_subtitle_extraction.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from importlib.resources import path
1+
from importlib.resources import as_file, files
22
from pathlib import Path
33
from unittest import TestCase, skipUnless
44

@@ -11,11 +11,11 @@ class FFmpegSubtitleExtractorTestCase(TestCase):
1111

1212
def test_extract(self):
1313
"""Test to extract subtitle from file."""
14-
with path("tests.resources.media", "dummy.mkv") as file:
14+
with as_file(files("tests.resources.media").joinpath("dummy.mkv")) as file:
1515
extractor = FFmpegSubtitleExtractor.extract(file)
1616
subtitle = extractor.get_subtitle()
1717

18-
with path("tests.resources.subtitles", "dummy.ass") as file:
18+
with as_file(files("tests.resources.subtitles").joinpath("dummy.ass")) as file:
1919
subtitle_expected = file.read_text()
2020

2121
self.assertEqual(subtitle, subtitle_expected)

tests/unit/test_customization.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import inspect
2-
from importlib import resources
2+
from importlib.resources import as_file, files
33
from pathlib import Path
44
from re import escape
55
from types import ModuleType
@@ -254,7 +254,7 @@ def test_alteration(self, mocked_sys):
254254
class ImportFromFileTestCase(TestCase):
255255
def test_import_file(self):
256256
"""Test to import a file."""
257-
with resources.path("tests.resources", "my_module.py") as file:
257+
with as_file(files("tests.resources").joinpath("my_module.py")) as file:
258258
module = customization.import_from_file(Path(file))
259259

260260
self.assertTrue(inspect.ismodule(module))

tests/unit/test_directory.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from importlib.resources import path
1+
from importlib.resources import as_file, files
22
from pathlib import Path
33
from shutil import copy
44
from tempfile import TemporaryDirectory
@@ -167,10 +167,10 @@ def test_list_directory(self):
167167
# call the function
168168
with TemporaryDirectory() as temp:
169169
# copy required files
170-
with path("tests.resources.media", "dummy.ass") as file:
170+
with as_file(files("tests.resources.media").joinpath("dummy.ass")) as file:
171171
copy(file, temp)
172172

173-
with path("tests.resources.media", "dummy.mkv") as file:
173+
with as_file(files("tests.resources.media").joinpath("dummy.mkv")) as file:
174174
copy(file, temp)
175175

176176
with self.assertLogs("dakara_feeder.directory", "DEBUG"):
@@ -188,38 +188,40 @@ class GetMainTypeTestCase(TestCase):
188188

189189
def test_video(self):
190190
"""Test the common video files."""
191-
with path("tests.resources.filetype", "file.avi") as file:
191+
with as_file(files("tests.resources.filetype").joinpath("file.avi")) as file:
192192
self.assertEqual(get_main_type(file), "video")
193193

194-
with path("tests.resources.filetype", "file.mkv") as file:
194+
with as_file(files("tests.resources.filetype").joinpath("file.mkv")) as file:
195195
self.assertEqual(get_main_type(file), "video")
196196

197-
with path("tests.resources.filetype", "file_upper.MKV") as file:
197+
with as_file(
198+
files("tests.resources.filetype").joinpath("file_upper.MKV")
199+
) as file:
198200
self.assertEqual(get_main_type(file), "video")
199201

200-
with path("tests.resources.filetype", "file.mp4") as file:
202+
with as_file(files("tests.resources.filetype").joinpath("file.mp4")) as file:
201203
self.assertEqual(get_main_type(file), "video")
202204

203205
def test_audio(self):
204206
"""Test the common audio files."""
205-
with path("tests.resources.filetype", "file.flac") as file:
207+
with as_file(files("tests.resources.filetype").joinpath("file.flac")) as file:
206208
self.assertEqual(get_main_type(file), "audio")
207209

208-
with path("tests.resources.filetype", "file.mp3") as file:
210+
with as_file(files("tests.resources.filetype").joinpath("file.mp3")) as file:
209211
self.assertEqual(get_main_type(file), "audio")
210212

211-
with path("tests.resources.filetype", "file.ogg") as file:
213+
with as_file(files("tests.resources.filetype").joinpath("file.ogg")) as file:
212214
self.assertEqual(get_main_type(file), "audio")
213215

214216
def test_subtitle(self):
215217
"""Test the common subtitles files."""
216-
with path("tests.resources.filetype", "file.ass") as file:
218+
with as_file(files("tests.resources.filetype").joinpath("file.ass")) as file:
217219
self.assertIsNone(get_main_type(file))
218220

219-
with path("tests.resources.filetype", "file.ssa") as file:
221+
with as_file(files("tests.resources.filetype").joinpath("file.ssa")) as file:
220222
self.assertIsNone(get_main_type(file))
221223

222-
with path("tests.resources.filetype", "file.srt") as file:
224+
with as_file(files("tests.resources.filetype").joinpath("file.srt")) as file:
223225
self.assertIsNone(get_main_type(file))
224226

225227

tests/unit/test_subtitle_parsing.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from importlib.resources import path
1+
from importlib.resources import as_file, files
22
from pathlib import Path
33
from unittest import TestCase
44
from unittest.mock import patch
@@ -16,7 +16,7 @@ class TXTSubtitleParserTestCase(TestCase):
1616

1717
def test_parse(self):
1818
"""Parse text file."""
19-
with path("tests.resources.subtitles", "plain.txt") as file:
19+
with as_file(files("tests.resources.subtitles").joinpath("plain.txt")) as file:
2020
parser = TXTSubtitleParser.parse(file)
2121
self.assertEqual(parser.get_lyrics(), file.read_text())
2222

@@ -38,13 +38,15 @@ def generic_test_subtitle(self, file_name):
3838
This method is called from other tests methods.
3939
"""
4040
# open and parse given file
41-
with path("tests.resources.subtitles", file_name) as file:
41+
with as_file(files("tests.resources.subtitles").joinpath(file_name)) as file:
4242
parser = Pysubs2SubtitleParser.parse(file)
4343
lyrics = parser.get_lyrics()
4444
lines = lyrics.splitlines()
4545

4646
# open expected result
47-
with path("tests.resources.subtitles", file_name + "_expected") as file:
47+
with as_file(
48+
files("tests.resources.subtitles").joinpath(file_name + "_expected")
49+
) as file:
4850
expected_lines = file.read_text().splitlines()
4951

5052
# check against expected file
@@ -57,14 +59,16 @@ def test_simple(self):
5759
def test_simple_string(self):
5860
"""Test simple ass file from string."""
5961
# open and parse given file
60-
with path("tests.resources.subtitles", "simple.ass") as file:
62+
with as_file(files("tests.resources.subtitles").joinpath("simple.ass")) as file:
6163
content = file.read_text()
6264
parser = Pysubs2SubtitleParser.parse_string(content)
6365
lyrics = parser.get_lyrics()
6466
lines = lyrics.splitlines()
6567

6668
# open expected result
67-
with path("tests.resources.subtitles", "simple.ass_expected") as file:
69+
with as_file(
70+
files("tests.resources.subtitles").joinpath("simple.ass_expected")
71+
) as file:
6872
expected_lines = file.read_text().splitlines()
6973

7074
# check against expected file

0 commit comments

Comments
 (0)