Add option to determine restriction scheme
This commit is contained in:
parent
bdce6101ae
commit
f40ac35f4a
|
@ -28,12 +28,19 @@ class FileNameFormatter:
|
||||||
"upvotes",
|
"upvotes",
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, file_format_string: str, directory_format_string: str, time_format_string: str):
|
def __init__(
|
||||||
|
self,
|
||||||
|
file_format_string: str,
|
||||||
|
directory_format_string: str,
|
||||||
|
time_format_string: str,
|
||||||
|
restriction_scheme: Optional[str] = None,
|
||||||
|
):
|
||||||
if not self.validate_string(file_format_string):
|
if not self.validate_string(file_format_string):
|
||||||
raise BulkDownloaderException(f'"{file_format_string}" is not a valid format string')
|
raise BulkDownloaderException(f'"{file_format_string}" is not a valid format string')
|
||||||
self.file_format_string = file_format_string
|
self.file_format_string = file_format_string
|
||||||
self.directory_format_string: list[str] = directory_format_string.split("/")
|
self.directory_format_string: list[str] = directory_format_string.split("/")
|
||||||
self.time_format_string = time_format_string
|
self.time_format_string = time_format_string
|
||||||
|
self.restiction_scheme = restriction_scheme.lower().strip() if restriction_scheme else None
|
||||||
|
|
||||||
def _format_name(self, submission: Union[Comment, Submission], format_string: str) -> str:
|
def _format_name(self, submission: Union[Comment, Submission], format_string: str) -> str:
|
||||||
if isinstance(submission, Submission):
|
if isinstance(submission, Submission):
|
||||||
|
@ -52,9 +59,11 @@ class FileNameFormatter:
|
||||||
|
|
||||||
result = result.replace("/", "")
|
result = result.replace("/", "")
|
||||||
|
|
||||||
if platform.system() == "Windows":
|
if self.restiction_scheme is None:
|
||||||
|
if platform.system() == "Windows":
|
||||||
|
result = FileNameFormatter._format_for_windows(result)
|
||||||
|
elif self.restiction_scheme == "windows":
|
||||||
result = FileNameFormatter._format_for_windows(result)
|
result = FileNameFormatter._format_for_windows(result)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -33,6 +33,10 @@ def submission() -> MagicMock:
|
||||||
return test
|
return test
|
||||||
|
|
||||||
|
|
||||||
|
def check_valid_windows_path(test_string: str):
|
||||||
|
return test_string == FileNameFormatter._format_for_windows(test_string)
|
||||||
|
|
||||||
|
|
||||||
def do_test_string_equality(result: Union[Path, str], expected: str) -> bool:
|
def do_test_string_equality(result: Union[Path, str], expected: str) -> bool:
|
||||||
if platform.system() == "Windows":
|
if platform.system() == "Windows":
|
||||||
expected = FileNameFormatter._format_for_windows(expected)
|
expected = FileNameFormatter._format_for_windows(expected)
|
||||||
|
@ -91,6 +95,15 @@ def test_check_format_string_validity(test_string: str, expected: bool):
|
||||||
|
|
||||||
@pytest.mark.online
|
@pytest.mark.online
|
||||||
@pytest.mark.reddit
|
@pytest.mark.reddit
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"restriction_scheme",
|
||||||
|
(
|
||||||
|
"windows",
|
||||||
|
"linux",
|
||||||
|
"bla",
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
)
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("test_format_string", "expected"),
|
("test_format_string", "expected"),
|
||||||
(
|
(
|
||||||
|
@ -102,10 +115,17 @@ def test_check_format_string_validity(test_string: str, expected: bool):
|
||||||
("{REDDITOR}_{TITLE}_{POSTID}", "Kirsty-Blue_George Russel acknowledges the Twitter trend about him_w22m5l"),
|
("{REDDITOR}_{TITLE}_{POSTID}", "Kirsty-Blue_George Russel acknowledges the Twitter trend about him_w22m5l"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
def test_format_name_real(test_format_string: str, expected: str, reddit_submission: praw.models.Submission):
|
def test_format_name_real(
|
||||||
test_formatter = FileNameFormatter(test_format_string, "", "")
|
test_format_string: str,
|
||||||
|
expected: str,
|
||||||
|
reddit_submission: praw.models.Submission,
|
||||||
|
restriction_scheme: Optional[str],
|
||||||
|
):
|
||||||
|
test_formatter = FileNameFormatter(test_format_string, "", "", restriction_scheme)
|
||||||
result = test_formatter._format_name(reddit_submission, test_format_string)
|
result = test_formatter._format_name(reddit_submission, test_format_string)
|
||||||
assert do_test_string_equality(result, expected)
|
assert do_test_string_equality(result, expected)
|
||||||
|
if restriction_scheme == "windows":
|
||||||
|
assert check_valid_windows_path(result)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.online
|
@pytest.mark.online
|
||||||
|
|
Loading…
Reference in a new issue