From: philipp Date: Tue, 2 Feb 2016 21:09:30 +0000 (+0000) Subject: Implemented wikipage converter. X-Git-Url: https://git.toastfreeware.priv.at/philipp/winterrodeln/wrpylib.git/commitdiff_plain/3770d01f731a03249505026796dc4f1fac3d99b3 Implemented wikipage converter. git-svn-id: http://www.winterrodeln.org/svn/wrpylib/trunk@2438 7aebc617-e5e2-0310-91dc-80fb5f6d2477 --- diff --git a/tests/test_wrvalidators.py b/tests/test_wrvalidators.py index aab8407..3b6a299 100644 --- a/tests/test_wrvalidators.py +++ b/tests/test_wrvalidators.py @@ -67,14 +67,6 @@ class TestEnumConverter(unittest.TestCase): self.assertEqual(enum_to_str([]), '') -class TestDateTime(unittest.TestCase): - def test_from_str(self): - self.assertEqual(DateTime.from_str('2015-12-31 23:07:42'), datetime.datetime(2015, 12, 31, 23, 7, 42)) - - def test_to_str(self): - self.assertEqual(DateTime.to_str(datetime.datetime(2015, 12, 31, 23, 7, 42)), '2015-12-31 23:07:42') - - class TestDifficultyGerman(unittest.TestCase): def test_from_str(self): self.assertEqual(difficulty_german_from_str('leicht'), 1) @@ -244,17 +236,17 @@ class TestNightLightDays(unittest.TestCase): self.assertEqual(nightlightdays_to_str((0, None)), '0') -class TestSledRental(unittest.TestCase): +class TestOptStrOptCommentEnum(unittest.TestCase): def test_from_str(self): - self.assertEqual(sledrental_from_str(''), None) - self.assertEqual(sledrental_from_str('Nein'), []) - self.assertEqual(sledrental_from_str('Talstation'), [('Talstation', None)]) - self.assertEqual(sledrental_from_str('Talstation (unten)'), [('Talstation', 'unten')]) - self.assertEqual(sledrental_from_str('Talstation (unten); Mittelstation'), [('Talstation', 'unten'), ('Mittelstation', None)]) + self.assertEqual(opt_str_opt_comment_enum_from_str(''), None) + self.assertEqual(opt_str_opt_comment_enum_from_str('Nein'), []) + self.assertEqual(opt_str_opt_comment_enum_from_str('Talstation'), [('Talstation', None)]) + self.assertEqual(opt_str_opt_comment_enum_from_str('Talstation (unten)'), [('Talstation', 'unten')]) + self.assertEqual(opt_str_opt_comment_enum_from_str('Talstation (unten); Mittelstation'), [('Talstation', 'unten'), ('Mittelstation', None)]) with self.assertRaises(ValueError): - sledrental_from_str('(unten)') + opt_str_opt_comment_enum_from_str('(unten)') with self.assertRaises(ValueError): - sledrental_from_str('Talstation (unten); ; Mittelstation') + opt_str_opt_comment_enum_from_str('Talstation (unten); ; Mittelstation') def test_to_str(self): self.assertEqual(sledrental_to_str(None), '') @@ -278,9 +270,6 @@ class TestNoOrStr(unittest.TestCase): self.assertEqual('', opt_no_or_str_to_str((None, None))) -def uebernachtung_to_str(value): - return no_german_to_str(value, opt_str_to_str) - class TestSingleCachet(unittest.TestCase): def test_from_str(self): @@ -437,6 +426,34 @@ class TestEmails(unittest.TestCase): self.assertEqual('info@example.com; home@example.com (Privat)', emails_to_str([(('info@example.com', False), None), (('home@example.com', False), 'Privat')])) +class TestWikipage(unittest.TestCase): + def test_from_str(self): + self.assertEqual('[[Birgitzer Alm]]', wikipage_from_str('[[Birgitzer Alm]]')) + with self.assertRaises(ValueError): + wikipage_from_str('[[') + with self.assertRaises(ValueError): + wikipage_from_str('') + with self.assertRaises(ValueError): + wikipage_from_str('Birgitzer Alm') + + def test_to_str(self): + self.assertEqual('[[Birgitzer Alm]]', wikipage_to_str('[[Birgitzer Alm]]')) + + +class TestOptWikipageEnum(unittest.TestCase): + def test_from_str(self): + self.assertEqual(['[[Birgitzer Alm]]', '[[Kemater Alm]]'], opt_wikipage_enum_from_str('[[Birgitzer Alm]]; [[Kemater Alm]]')) + self.assertEqual(['[[Birgitzer Alm]]'], opt_wikipage_enum_from_str('[[Birgitzer Alm]]')) + self.assertEqual([], opt_wikipage_enum_from_str('Nein')) + self.assertEqual(None, opt_wikipage_enum_from_str('')) + + def test_to_str(self): + self.assertEqual('[[Birgitzer Alm]]; [[Kemater Alm]]', opt_wikipage_enum_to_str(['[[Birgitzer Alm]]', '[[Kemater Alm]]'])) + self.assertEqual('[[Birgitzer Alm]]', opt_wikipage_enum_to_str(['[[Birgitzer Alm]]'])) + self.assertEqual('Nein', opt_wikipage_enum_to_str([])) + self.assertEqual('', opt_wikipage_enum_to_str(None)) + + class TestBox(unittest.TestCase): def test_from_str(self): value = '{{MyTemplate|apple=2|banana=5}}' @@ -574,50 +591,6 @@ class TestWrValidators(unittest.TestCase): assert v.from_python('+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == '+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456' - def test_MaskedEmail(self): - v = wrpylib.wrvalidators.MaskedEmail() - assert v.to_python('') == (None, None) - assert v.to_python('abc.def@example.com') == ('abc.def@example.com', False) - assert v.to_python('abc.def(at)example.com') == ('abc.def@example.com', True) - assert v.from_python((None, None)) == '' - assert v.from_python(('abc.def@example.com', False)) == 'abc.def@example.com' - assert v.from_python(('abc.def@example.com', True)) == 'abc.def(at)example.com' - - - def test_EmailCommentListNeinLoopNone(self): - v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone() - assert v.to_python('') == None - assert v.to_python('Nein') == 'Nein' - assert v.to_python('first@example.com') == 'first@example.com' - assert v.to_python('first@example.com (Nur Winter); second@example.com') == 'first@example.com (Nur Winter); second@example.com' - assert v.from_python(None) == '' - assert v.from_python('Nein') == 'Nein' - assert v.from_python('first@example.com') == 'first@example.com' - assert v.from_python('first@example.com (Nur Winter); second@example.com') == 'first@example.com (Nur Winter); second@example.com' - testvalue = 'abc.def(at)example.com (comment)' - try: - v.to_python(testvalue) - assert False - except formencode.Invalid: - pass - try: - v.from_python(testvalue) - assert False - except formencode.Invalid: - pass - v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True) - assert v.to_python(testvalue) == testvalue - assert v.from_python(testvalue) == testvalue - - - def test_WikiPageListLoopNone(self): - v = wrpylib.wrvalidators.WikiPageListLoopNone() - assert v.to_python('') == None - assert v.to_python('[[Birgitzer Alm]]; [[Kemater Alm]]') == '[[Birgitzer Alm]]; [[Kemater Alm]]' - assert v.from_python(None) == '' - assert v.from_python('[[Birgitzer Alm]]; [[Kemater Alm]]') == '[[Birgitzer Alm]]; [[Kemater Alm]]' - - def test_GasthausboxDictValidator(self): v = wrpylib.wrvalidators.GasthausboxDictValidator() other = collections.OrderedDict([ diff --git a/wrpylib/wrvalidators.py b/wrpylib/wrvalidators.py index 26d9570..8d158e5 100644 --- a/wrpylib/wrvalidators.py +++ b/wrpylib/wrvalidators.py @@ -613,93 +613,36 @@ def emails_to_str(value): emails_converter = FromToConverter(emails_from_str, email_to_str) -class MaskedEmail(formencode.FancyValidator): - """A masked email address as defined here is an email address that has the `@` character replacted by the text `(at)`. - So instead of `abd.def@example.com` it would be `abc.def(at)example.com`. - This validator takes either a normal or a masked email address in it's to_python method and returns the normal email address as well - as a bool indicating whether the email address was masked. - u'' <=> (None, None) - u'abc.def@example.com' <=> (u'abc.def@example.com', False) - u'abc.def(at)example.com' <=> (u'abc.def@example.com', True) - - """ - def __init__(self, *args, **kw): - if 'strip' not in kw: kw['strip'] = True - if 'not_empty' not in kw: kw['not_empty'] = False - if 'if_empty' not in kw: kw['if_empty'] = (None, None) - self.at = '(at)' - formencode.FancyValidator.__init__(self, *args, **kw) - - def _to_python(self, value, state=None): - email = value.replace(self.at, '@') - masked = value != email - val_email = formencode.validators.Email() - return val_email.to_python(email, state), masked - - def _from_python(self, value, state=None): - email, masked = value - if email is None: return '' - val_email = formencode.validators.Email() - email = val_email.from_python(email, state) - if masked: email = email.replace('@', self.at) - return email - - -''' -class EmailCommentListNeinLoopNone(NoneValidator): - """Converts a semicolon-separated list of email addresses with optional comments to itself. - The special value of u'Nein' indicates that there are no email addresses. - The empty string translates to None: - u'' <=> None - u'Nein' <=> u'Nein' - u'first@example.com' <=> u'first@example.com' - u'first@example.com (Nur Winter); second@example.com' <=> u'first@example.com (Nur Winter); second@example.com' - - If the parameter allow_masked_email is true, the following gives no error: - u'abc.def(at)example.com (comment)' <=> u'abc.def(at)example.com (comment)' - """ - def __init__(self, allow_masked_email=False): - NoneValidator.__init__(self, NeinValidator(Loop(ValueCommentList(MaskedEmail() if allow_masked_email else formencode.validators.Email())))) -''' - -class WikiPage(formencode.FancyValidator): - """Validates wiki page name like u'[[Birgitzer Alm]]'. +def wikipage_from_str(value): + """Validates wiki page name like '[[Birgitzer Alm]]'. The page is not checked for existance. An empty string is an error. - u'[[Birgitzer Alm]]' <=> u'[[Birgitzer Alm]]' + '[[Birgitzer Alm]]' => '[[Birgitzer Alm]]' """ - def to_python(self, value, state=None): - self.assert_string(value, state) - if not value.startswith('[[') or not value.endswith(']]'): - raise formencode.Invalid('No valid wiki page name', value, state) - return value - - def from_python(self, value, state=None): - return value + if not value.startswith('[[') or not value.endswith(']]'): + raise ValueError('No valid wiki page name "{}"'.format(value)) + return value -''' -class WikiPageList(SemicolonList): - """Validates a list of wiki pages like u'[[Birgitzer Alm]]; [[Kemater Alm]]'. - u'[[Birgitzer Alm]]; [[Kemater Alm]]' <=> [u'[[Birgitzer Alm]]', u'[[Kemater Alm]]'] - u'[[Birgitzer Alm]]' <=> [u'[[Birgitzer Alm]]'] - u'' <=> [] - """ - def __init__(self): - SemicolonList.__init__(self, WikiPage()) -''' +def wikipage_to_str(value): + return value -''' -class WikiPageListLoopNone(NoneValidator): - """Validates a list of wiki pages like u'[[Birgitzer Alm]]; [[Kemater Alm]]' as string. - u'[[Birgitzer Alm]]; [[Kemater Alm]]' <=> u'[[Birgitzer Alm]]; [[Kemater Alm]]' - u'[[Birgitzer Alm]]' <=> u'[[Birgitzer Alm]]' - u'' <=> None +def opt_wikipage_enum_from_str(value): + """Validates a list of wiki pages like '[[Birgitzer Alm]]; [[Kemater Alm]]'. + '[[Birgitzer Alm]]; [[Kemater Alm]]' => ['[[Birgitzer Alm]]', '[[Kemater Alm]]'] + '[[Birgitzer Alm]]' => ['[[Birgitzer Alm]]'] + 'Nein' => [] + '' => None """ - def __init__(self): - NoneValidator.__init__(self, Loop(WikiPageList())) -''' + return opt_no_german_from_str(value, lambda val: enum_from_str(val, wikipage_from_str), False, [], None) + + +def opt_wikipage_enum_to_str(value): + return opt_no_german_to_str(value, lambda val: enum_to_str(val, wikipage_to_str), False, [], None) + + +opt_wikipage_enum_converter = FromToConverter(opt_wikipage_enum_from_str, opt_wikipage_enum_to_str) LIFT_GERMAN = ['Sessellift', 'Gondel', 'Linienbus', 'Taxi', 'Sonstige'] @@ -734,7 +677,7 @@ def lift_german_to_str(value): lift_german_converter = FromToConverter(lift_german_from_str, lift_german_to_str) -def sledrental_from_str(value): +def opt_str_opt_comment_enum_from_str(value): """The value can be an empty string, 'Nein' or a semicolon-separated list of strings with optional comments. '' => None 'Nein' => [] @@ -746,16 +689,16 @@ def sledrental_to_str(value): return opt_no_german_to_str(value, lambda val: enum_to_str(val, lambda v: value_comment_to_str(v, str_to_str, opt_str_to_str, True)), False, [], None) -sledrental_converter = FromToConverter(sledrental_from_str, sledrental_to_str) +opt_str_opt_comment_enum_converter = FromToConverter(opt_str_opt_comment_enum_from_str, sledrental_to_str) def opt_no_or_str_from_str(value): """Übernachtung. 'Nein' => (False, None); 'Nur Wochenende' => (True, 'Nur Wochenende'); 'Ja' => (True, 'Ja'); '' => (None, None)""" - return no_german_from_str(value, opt_str_from_str) + return opt_no_german_from_str(value) def opt_no_or_str_to_str(value): - return no_german_to_str(value, opt_str_to_str) + return opt_no_german_to_str(value) opt_no_or_str_converter = FromToConverter(opt_no_or_str_from_str, opt_no_or_str_to_str) @@ -833,7 +776,7 @@ RODELBAHNBOX_DICT = OrderedDict([ ('Aufstiegshilfe', lift_german_converter), # 'Gondel (unterer Teil)' ('Beleuchtungsanlage', opt_tristate_german_comment_converter), ('Beleuchtungstage', nightlightdays_converter), # '3 (Montag, Mittwoch, Freitag)' - ('Rodelverleih', sledrental_converter), # 'Talstation Serlesbahnan' + ('Rodelverleih', opt_str_opt_comment_enum_converter), # 'Talstation Serlesbahnan' ('Gütesiegel', cachet_german_converter), # 'Tiroler Naturrodelbahn-Gütesiegel 2009 mittel' ('Webauskunft', webauskunft_converter), # 'http://www.nösslachhütte.at/page9.php' ('Telefonauskunft', telefonauskunft_converter), # '+43-664-5487520 (Mitterer Alm)' @@ -864,7 +807,6 @@ def rodelbahnbox_to_str(value): GASTHAUSBOX_TEMPLATE_NAME = 'Gasthausbox' -''' GASTHAUSBOX_DICT = OrderedDict([ ('Position', opt_lonlat_converter), # '47.583333 N 15.75 E' ('Höhe', opt_uint_converter), @@ -873,13 +815,12 @@ GASTHAUSBOX_DICT = OrderedDict([ ('Übernachtung', opt_no_or_str_converter), ('Rauchfrei', opt_tristate_german_converter), ('Rodelverleih', opt_no_or_str_converter), - ('Handyempfang', ValueCommentListNeinLoopNone()), + ('Handyempfang', opt_str_opt_comment_enum_converter), ('Homepage', webauskunft_converter), ('E-Mail', emails_converter), - ('Telefon', PhoneCommentListNeinLoopNone(comments_are_optional=True)), + ('Telefon', None), # PhoneCommentListNeinLoopNone(comments_are_optional=True)), ('Bild', opt_str_converter), - ('Rodelbahnen', WikiPageListLoopNone())]) -''' + ('Rodelbahnen', opt_wikipage_enum_converter)]) def sledrun_page_title_to_pretty_url(page_title):