[MetadataFromField] Improve regex and add tests

This commit is contained in:
pukkandan 2021-04-21 11:12:04 +05:30
parent 8c54a3051d
commit a3faeb7de4
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698
2 changed files with 14 additions and 4 deletions

View File

@ -16,6 +16,15 @@ def test_format_to_regex(self):
pp = MetadataFromFieldPP(None, ['title:%(title)s - %(artist)s']) pp = MetadataFromFieldPP(None, ['title:%(title)s - %(artist)s'])
self.assertEqual(pp._data[0]['regex'], r'(?P<title>.+)\ \-\ (?P<artist>.+)') self.assertEqual(pp._data[0]['regex'], r'(?P<title>.+)\ \-\ (?P<artist>.+)')
def test_field_to_outtmpl(self):
pp = MetadataFromFieldPP(None, ['title:%(title)s : %(artist)s'])
self.assertEqual(pp._data[0]['tmpl'], '%(title)s')
def test_in_out_seperation(self):
pp = MetadataFromFieldPP(None, ['%(title)s \\: %(artist)s:%(title)s : %(artist)s'])
self.assertEqual(pp._data[0]['in'], '%(title)s : %(artist)s')
self.assertEqual(pp._data[0]['out'], '%(title)s : %(artist)s')
class TestMetadataFromTitle(unittest.TestCase): class TestMetadataFromTitle(unittest.TestCase):
def test_format_to_regex(self): def test_format_to_regex(self):

View File

@ -7,7 +7,7 @@
class MetadataFromFieldPP(PostProcessor): class MetadataFromFieldPP(PostProcessor):
regex = r'(?P<in>.+):(?P<out>.+)$' regex = r'(?P<in>.*?)(?<!\\):(?P<out>.+)$'
def __init__(self, downloader, formats): def __init__(self, downloader, formats):
PostProcessor.__init__(self, downloader) PostProcessor.__init__(self, downloader)
@ -17,10 +17,11 @@ def __init__(self, downloader, formats):
assert isinstance(f, compat_str) assert isinstance(f, compat_str)
match = re.match(self.regex, f) match = re.match(self.regex, f)
assert match is not None assert match is not None
inp = match.group('in').replace('\\:', ':')
self._data.append({ self._data.append({
'in': match.group('in'), 'in': inp,
'out': match.group('out'), 'out': match.group('out'),
'tmpl': self.field_to_template(match.group('in')), 'tmpl': self.field_to_template(inp),
'regex': self.format_to_regex(match.group('out')), 'regex': self.format_to_regex(match.group('out')),
}) })
@ -68,6 +69,6 @@ def run(self, info):
class MetadataFromTitlePP(MetadataFromFieldPP): # for backward compatibility class MetadataFromTitlePP(MetadataFromFieldPP): # for backward compatibility
def __init__(self, downloader, titleformat): def __init__(self, downloader, titleformat):
super(MetadataFromTitlePP, self).__init__(downloader, ['title:%s' % titleformat]) super(MetadataFromTitlePP, self).__init__(downloader, ['%%(title)s:%s' % titleformat])
self._titleformat = titleformat self._titleformat = titleformat
self._titleregex = self._data[0]['regex'] self._titleregex = self._data[0]['regex']