[metadatafromtitle] Some improvements and cleanup

* Remove the 'songtitle' field, 'title' can be used instead.
* Remove newlines in the help text, for consistency with other options.
* Add 'from __future__ import unicode_literals'.
* Call '__init__' from the parent class.
* Add test for the format_to_regex method
This commit is contained in:
Jaime Marquínez Ferrándiz 2015-03-14 19:55:42 +01:00
parent e7db87f700
commit 88cf6fb368
4 changed files with 25 additions and 11 deletions

View file

@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import re
@ -12,20 +12,19 @@ class MetadataFromTitlePPError(PostProcessingError):
class MetadataFromTitlePP(PostProcessor):
def __init__(self, downloader, titleformat):
super(MetadataFromTitlePP, self).__init__(downloader)
self._titleformat = titleformat
self._titleregex = self.fmtToRegex(titleformat)
self._titleregex = self.format_to_regex(titleformat)
def fmtToRegex(self, fmt):
def format_to_regex(self, fmt):
"""
Converts a string like
'%(title)s - %(artist)s'
to a regex like
'(?P<title>.+)\ \-\ (?P<artist>.+)'
and a list of the named groups [title, artist]
"""
lastpos = 0
regex = ""
groups = []
# replace %(..)s with regex group and escape other string parts
for match in re.finditer(r'%\((\w+)\)s', fmt):
regex += re.escape(fmt[lastpos:match.start()])