Parse unicode escapes in file name fields (#254)

This commit is contained in:
Serene 2021-04-06 23:43:03 +10:00 committed by Ali Parlakci
parent 942ca2afea
commit 9bceafc3e9
2 changed files with 14 additions and 1 deletions

View File

@ -43,7 +43,9 @@ class FileNameFormatter:
result = format_string
for key in attributes.keys():
if re.search(fr'(?i).*{{{key}}}.*', result):
result = re.sub(fr'(?i){{{key}}}', str(attributes.get(key, 'unknown')), result)
key_value = attributes.get(key, 'unknown')
key_value = bytes(key_value, 'utf-8').decode('unicode-escape')
result = re.sub(fr'(?i){{{key}}}', key_value, result,)
logger.log(9, f'Found key string {key} in name')
result = result.replace('/', '')

View File

@ -294,3 +294,14 @@ def test_multilevel_folder_scheme(
result = result.relative_to(tmp_path)
assert str(result.parent) == expected
assert len(result.parents) == (len(expected.split('/')) + 1)
@pytest.mark.online
@pytest.mark.reddit
@pytest.mark.parametrize(('test_submission_id', 'test_file_scheme', 'expected'), (
('mecwk7', '{TITLE}', 'My cats paws are so cute'), # Unicode escape in title
))
def test_edge_case_names(test_submission_id: str, test_file_scheme: str, expected: str, reddit_instance: praw.Reddit):
test_submission = reddit_instance.submission(id=test_submission_id)
result = FileNameFormatter._format_name(test_submission, test_file_scheme)
assert result == expected