Add indexing for multiple resources from one submission
This commit is contained in:
parent
eca5da7f46
commit
d78c4ca78e
2 changed files with 53 additions and 8 deletions
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
import re
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import praw.models
|
import praw.models
|
||||||
|
|
||||||
|
@ -38,13 +39,21 @@ class FileNameFormatter:
|
||||||
result = result.replace('/', '')
|
result = result.replace('/', '')
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def format_path(self, resource: Resource, destination_directory: Path) -> Path:
|
def _format_path(self, resource: Resource, destination_directory: Path, index: Optional[int] = None) -> Path:
|
||||||
subfolder = destination_directory / self._format_name(resource.source_submission, self.directory_format_string)
|
subfolder = destination_directory / self._format_name(resource.source_submission, self.directory_format_string)
|
||||||
|
index = f'_{str(index)}' if index else ''
|
||||||
file_path = subfolder / (str(self._format_name(resource.source_submission,
|
file_path = subfolder / (str(self._format_name(resource.source_submission,
|
||||||
self.file_format_string)) + resource.extension)
|
self.file_format_string)) + index + resource.extension)
|
||||||
return file_path
|
return file_path
|
||||||
|
|
||||||
@staticmethod
|
def format_resource_paths(self, resources: list[Resource],
|
||||||
|
destination_directory: Path) -> list[tuple[Path, Resource]]:
|
||||||
|
out = []
|
||||||
|
for i, res in enumerate(resources, start=1):
|
||||||
|
out.append((self._format_path(res, destination_directory, i), res))
|
||||||
|
return out
|
||||||
|
|
||||||
|
@ staticmethod
|
||||||
def validate_string(test_string: str) -> bool:
|
def validate_string(test_string: str) -> bool:
|
||||||
if not test_string:
|
if not test_string:
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest.mock import Mock
|
from typing import Optional
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
import praw.models
|
import praw.models
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -12,8 +13,8 @@ from bulkredditdownloader.resource import Resource
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def submission() -> Mock:
|
def submission() -> MagicMock:
|
||||||
test = Mock()
|
test = MagicMock()
|
||||||
test.title = 'name'
|
test.title = 'name'
|
||||||
test.subreddit.display_name = 'randomreddit'
|
test.subreddit.display_name = 'randomreddit'
|
||||||
test.author.name = 'person'
|
test.author.name = 'person'
|
||||||
|
@ -37,7 +38,7 @@ def reddit_submission(reddit_instance) -> praw.models.Submission:
|
||||||
('{DATE}', '123456789'),
|
('{DATE}', '123456789'),
|
||||||
('{REDDITOR}_{TITLE}_{POSTID}', 'person_name_12345')
|
('{REDDITOR}_{TITLE}_{POSTID}', 'person_name_12345')
|
||||||
))
|
))
|
||||||
def test_format_name_mock(format_string: str, expected: str, submission: Mock):
|
def test_format_name_mock(format_string: str, expected: str, submission: MagicMock):
|
||||||
result = FileNameFormatter._format_name(submission, format_string)
|
result = FileNameFormatter._format_name(submission, format_string)
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
|
@ -87,5 +88,40 @@ def test_format_full(
|
||||||
reddit_submission: praw.models.Submission):
|
reddit_submission: praw.models.Submission):
|
||||||
test_resource = Resource(reddit_submission, 'i.reddit.com/blabla.png')
|
test_resource = Resource(reddit_submission, 'i.reddit.com/blabla.png')
|
||||||
test_formatter = FileNameFormatter(format_string_file, format_string_directory)
|
test_formatter = FileNameFormatter(format_string_file, format_string_directory)
|
||||||
result = test_formatter.format_path(test_resource, Path('test'))
|
result = test_formatter._format_path(test_resource, Path('test'))
|
||||||
assert str(result) == expected
|
assert str(result) == expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.online
|
||||||
|
@pytest.mark.reddit
|
||||||
|
@pytest.mark.parametrize(('format_string_directory', 'format_string_file', 'index', 'expected'),
|
||||||
|
(('{SUBREDDIT}', '{POSTID}', None, 'test/Mindustry/lgilgt.png'),
|
||||||
|
('{SUBREDDIT}', '{POSTID}', 1, 'test/Mindustry/lgilgt_1.png'),
|
||||||
|
('{SUBREDDIT}', '{POSTID}', 2, 'test/Mindustry/lgilgt_2.png'),
|
||||||
|
('{SUBREDDIT}', '{TITLE}_{POSTID}', 2,
|
||||||
|
'test/Mindustry/Toxopid that is NOT humane >:(_lgilgt_2.png'),
|
||||||
|
))
|
||||||
|
def test_format_full_with_index_suffix(
|
||||||
|
format_string_directory: str,
|
||||||
|
format_string_file: str,
|
||||||
|
index: Optional[int],
|
||||||
|
expected: str,
|
||||||
|
reddit_submission: praw.models.Submission):
|
||||||
|
test_resource = Resource(reddit_submission, 'i.reddit.com/blabla.png')
|
||||||
|
test_formatter = FileNameFormatter(format_string_file, format_string_directory)
|
||||||
|
result = test_formatter._format_path(test_resource, Path('test'), index)
|
||||||
|
assert str(result) == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_format_multiple_resources():
|
||||||
|
mocks = []
|
||||||
|
for i in range(1, 5):
|
||||||
|
new_mock = MagicMock()
|
||||||
|
new_mock.url = 'https://example.com/test.png'
|
||||||
|
new_mock.extension = '.png'
|
||||||
|
new_mock.source_submission.title = 'test'
|
||||||
|
mocks.append(new_mock)
|
||||||
|
test_formatter = FileNameFormatter('{TITLE}', '')
|
||||||
|
results = test_formatter.format_resource_paths(mocks, Path('.'))
|
||||||
|
results = set([str(res[0]) for res in results])
|
||||||
|
assert results == {'test_1.png', 'test_2.png', 'test_3.png', 'test_4.png'}
|
||||||
|
|
Loading…
Reference in a new issue