[utils] Improve month_by_name and add tests

This commit is contained in:
Sergey M․ 2016-09-14 23:13:55 +07:00
parent a942d6cb48
commit f6717dec8a
No known key found for this signature in database
GPG Key ID: 2C393E0F18A9236D
2 changed files with 19 additions and 8 deletions

View File

@ -40,6 +40,7 @@
js_to_json, js_to_json,
limit_length, limit_length,
mimetype2ext, mimetype2ext,
month_by_name,
ohdave_rsa_encrypt, ohdave_rsa_encrypt,
OnDemandPagedList, OnDemandPagedList,
orderedSet, orderedSet,
@ -634,6 +635,16 @@ def test_mimetype2ext(self):
self.assertEqual(mimetype2ext('text/vtt;charset=utf-8'), 'vtt') self.assertEqual(mimetype2ext('text/vtt;charset=utf-8'), 'vtt')
self.assertEqual(mimetype2ext('text/html; charset=utf-8'), 'html') self.assertEqual(mimetype2ext('text/html; charset=utf-8'), 'html')
def test_month_by_name(self):
self.assertEqual(month_by_name(None), None)
self.assertEqual(month_by_name('December', 'en'), 12)
self.assertEqual(month_by_name('decembre', 'fr'), 12)
self.assertEqual(month_by_name('December'), 12)
self.assertEqual(month_by_name('decembre'), None)
self.assertEqual(month_by_name('Unknown', 'unknown'), None)
def test_m
def test_parse_codecs(self): def test_parse_codecs(self):
self.assertEqual(parse_codecs(''), {}) self.assertEqual(parse_codecs(''), {})
self.assertEqual(parse_codecs('avc1.77.30, mp4a.40.2'), { self.assertEqual(parse_codecs('avc1.77.30, mp4a.40.2'), {

View File

@ -91,9 +91,12 @@ def register_socks_protocols():
'January', 'February', 'March', 'April', 'May', 'June', 'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'] 'July', 'August', 'September', 'October', 'November', 'December']
FRENCH_MONTH_NAMES = [ MONTH_NAMES = {
'en': ENGLISH_MONTH_NAMES,
'fr': [
'janvier', 'fevrier', 'mars', 'avril', 'mai', 'juin', 'janvier', 'fevrier', 'mars', 'avril', 'mai', 'juin',
'juillet', 'aout', 'septembre', 'octobre', 'novembre', 'decembre'] 'juillet', 'aout', 'septembre', 'octobre', 'novembre', 'decembre'],
}
KNOWN_EXTENSIONS = ( KNOWN_EXTENSIONS = (
'mp4', 'm4a', 'm4p', 'm4b', 'm4r', 'm4v', 'aac', 'mp4', 'm4a', 'm4p', 'm4b', 'm4r', 'm4v', 'aac',
@ -1594,13 +1597,10 @@ def parse_count(s):
def month_by_name(name, lang='en'): def month_by_name(name, lang='en'):
""" Return the number of a month by (locale-independently) English name """ """ Return the number of a month by (locale-independently) English name """
name_list = ENGLISH_MONTH_NAMES month_names = MONTH_NAMES.get(lang, MONTH_NAMES['en'])
if lang == 'fr':
name_list = FRENCH_MONTH_NAMES
try: try:
return name_list.index(name) + 1 return month_names.index(name) + 1
except ValueError: except ValueError:
return None return None