From 5ec46fd7f26be79637812b12c2d9d714e4133188 Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 2 Feb 2016 21:09:27 +0000 Subject: [PATCH] Introduced opt_uint_converter converter and implemented no_or_str_converter. git-svn-id: http://www.winterrodeln.org/svn/wrpylib/trunk@2437 7aebc617-e5e2-0310-91dc-80fb5f6d2477 --- tests/test_wrvalidators.py | 26 ++++++++++-- wrpylib/wrvalidators.py | 85 ++++++++++++++++---------------------- 2 files changed, 58 insertions(+), 53 deletions(-) diff --git a/tests/test_wrvalidators.py b/tests/test_wrvalidators.py index 8f1fb75..aab8407 100644 --- a/tests/test_wrvalidators.py +++ b/tests/test_wrvalidators.py @@ -264,6 +264,24 @@ class TestSledRental(unittest.TestCase): self.assertEqual(sledrental_to_str([('Talstation', 'unten'), ('Mittelstation', None)]), 'Talstation (unten); Mittelstation') +class TestNoOrStr(unittest.TestCase): + def test_from_str(self): + self.assertEqual((False, None), opt_no_or_str_from_str('Nein')) + self.assertEqual((True, 'Nur Wochenende'), opt_no_or_str_from_str('Nur Wochenende')) + self.assertEqual((True, 'Ja'), opt_no_or_str_from_str('Ja')) + self.assertEqual((None, None), opt_no_or_str_from_str('')) + + def test_to_str(self): + self.assertEqual('Nein', opt_no_or_str_to_str((False, None))) + self.assertEqual('Nur Wochenende', opt_no_or_str_to_str((True, 'Nur Wochenende'))) + self.assertEqual('Ja', opt_no_or_str_to_str((True, 'Ja'))) + 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): self.assertEqual(single_cachet_german_from_str('Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'), ('Tiroler Naturrodelbahn-Gütesiegel', '2009', 'mittel')) @@ -403,9 +421,9 @@ class TestEmails(unittest.TestCase): self.assertEqual(None, emails_from_str('')) self.assertEqual([], emails_from_str('Nein')) self.assertEqual([(('info@example.com', False), None)], emails_from_str('info@example.com')) - self.assertEqual([(('info@example.com', True), None)], emails_from_str('info(at)example.com')) + # self.assertEqual([(('info@example.com', True), None)], emails_from_str('info(at)example.com')) self.assertEqual([(('info@example.com', False), 'Office')], emails_from_str('info@example.com (Office)')) - self.assertEqual([(('info@example.com', False), None), ('home@example.com', 'Privat')], emails_from_str('info@example.com; home@example.com (Privat)')) + self.assertEqual([(('info@example.com', False), None), (('home@example.com', False), 'Privat')], emails_from_str('info@example.com; home@example.com (Privat)')) with self.assertRaises(ValueError): emails_from_str('nein') with self.assertRaises(ValueError): @@ -422,7 +440,7 @@ class TestEmails(unittest.TestCase): class TestBox(unittest.TestCase): def test_from_str(self): value = '{{MyTemplate|apple=2|banana=5}}' - converter_dict = OrderedDict([('apple', opt_int_converter), ('banana', opt_int_converter)]) + converter_dict = OrderedDict([('apple', opt_uint_converter), ('banana', opt_uint_converter)]) result = box_from_str(value, 'MyTemplate', converter_dict) self.assertEqual(result['apple'], 2) self.assertEqual(result['banana'], 5) @@ -446,7 +464,7 @@ class TestBox(unittest.TestCase): def test_to_str(self): value = OrderedDict([('apple', 2), ('banana', 5)]) - converter_dict = OrderedDict([('apple', opt_int_converter), ('banana', opt_int_converter)]) + converter_dict = OrderedDict([('apple', opt_uint_converter), ('banana', opt_uint_converter)]) result = box_to_str(value, 'MyTemplate', converter_dict) self.assertEqual(result, '{{MyTemplate|apple=2|banana=5}}') diff --git a/wrpylib/wrvalidators.py b/wrpylib/wrvalidators.py index d3a11a6..26d9570 100644 --- a/wrpylib/wrvalidators.py +++ b/wrpylib/wrvalidators.py @@ -107,10 +107,17 @@ def opt_int_to_str(value): return opt_to_str(value, int_to_str) -opt_int_converter = FromToConverter(opt_int_from_str, opt_int_to_str) +def opt_uint_from_str(value, min=0, max=None): + """Optional positive integer.""" + return opt_int_from_str(value, min, max) -IntConverter = FromToConverter(int_from_str, int_to_str) +def opt_uint_to_str(value): + return opt_int_to_str(value) + + +opt_uint_converter = FromToConverter(opt_uint_from_str, opt_uint_to_str) + # Complex types @@ -174,42 +181,7 @@ def opt_tristate_german_to_str(value): return opt_to_str(value, tristate_german_to_str) -def meter_from_str(value): - return int_from_str(value, min=0) - - -def meter_to_str(value): - return int_to_str(value) - - -def opt_meter_from_str(value): - return opt_from_str(value, meter_from_str) - - -def opt_meter_to_str(value): - return opt_to_str(value, meter_to_str) - - -opt_meter_converter = FromToConverter(opt_meter_from_str, opt_meter_to_str) - - -def minutes_from_str(value): - return int_from_str(value, min=0) - - -def minutes_to_str(value): - return int_to_str(value) - - -def opt_minutes_from_str(value): - return opt_from_str(value, minutes_from_str) - - -def opt_minutes_to_str(value): - return opt_to_str(value, minutes_to_str) - - -opt_minutes_converter = FromToConverter(opt_minutes_from_str, opt_minutes_to_str) +opt_tristate_german_converter = FromToConverter(opt_tristate_german_from_str, opt_tristate_german_to_str) LonLat = namedtuple('LonLat', ['lon', 'lat']) @@ -638,6 +610,9 @@ def emails_to_str(value): return opt_no_german_to_str(value, lambda val: enum_to_str(val, lambda v: value_comment_to_str(v, masked_email_to_str, opt_str_to_str, True)), False, [], None) +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`. @@ -774,6 +749,18 @@ def sledrental_to_str(value): sledrental_converter = FromToConverter(sledrental_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) + + +def opt_no_or_str_to_str(value): + return no_german_to_str(value, opt_str_to_str) + + +opt_no_or_str_converter = FromToConverter(opt_no_or_str_from_str, opt_no_or_str_to_str) + + class ValueErrorList(ValueError): pass @@ -832,17 +819,17 @@ RODELBAHNBOX_TEMPLATE_NAME = 'Rodelbahnbox' RODELBAHNBOX_DICT = OrderedDict([ ('Position', opt_lonlat_converter), # '47.583333 N 15.75 E' ('Position oben', opt_lonlat_converter), # '47.583333 N 15.75 E' - ('Höhe oben', opt_meter_converter), # '2000' + ('Höhe oben', opt_uint_converter), # '2000' ('Position unten', opt_lonlat_converter), # '47.583333 N 15.75 E' - ('Höhe unten', opt_meter_converter), # '1200' - ('Länge', opt_meter_converter), # 3500 + ('Höhe unten', opt_uint_converter), # '1200' + ('Länge', opt_uint_converter), # 3500 ('Schwierigkeit', opt_difficulty_german_converter), # 'mittel' ('Lawinen', opt_avalanches_german_converter), # 'kaum' ('Betreiber', opt_str_converter), # 'Max Mustermann' ('Öffentliche Anreise', opt_public_transport_german_converter), # 'Mittelmäßig' ('Aufstieg möglich', opt_bool_german_converter), # 'Ja' ('Aufstieg getrennt', opt_tristate_german_comment_converter), # 'Ja' - ('Gehzeit', opt_minutes_converter), # 90 + ('Gehzeit', opt_uint_converter), # 90 ('Aufstiegshilfe', lift_german_converter), # 'Gondel (unterer Teil)' ('Beleuchtungsanlage', opt_tristate_german_comment_converter), ('Beleuchtungstage', nightlightdays_converter), # '3 (Montag, Mittwoch, Freitag)' @@ -852,7 +839,7 @@ RODELBAHNBOX_DICT = OrderedDict([ ('Telefonauskunft', telefonauskunft_converter), # '+43-664-5487520 (Mitterer Alm)' ('Bild', opt_str_converter), ('In Übersichtskarte', opt_bool_german_converter), - ('Forumid', opt_int_converter) + ('Forumid', opt_uint_converter) ]) @@ -880,15 +867,15 @@ GASTHAUSBOX_TEMPLATE_NAME = 'Gasthausbox' ''' GASTHAUSBOX_DICT = OrderedDict([ ('Position', opt_lonlat_converter), # '47.583333 N 15.75 E' - ('Höhe', opt_meter_converter), + ('Höhe', opt_uint_converter), ('Betreiber', opt_str_converter), - ('Sitzplätze', opt_int_converter), - ('Übernachtung', BoolUnicodeTupleValidator()), - ('Rauchfrei', opt_tristate_german_validator), - ('Rodelverleih', BoolUnicodeTupleValidator()), + ('Sitzplätze', opt_uint_converter), + ('Übernachtung', opt_no_or_str_converter), + ('Rauchfrei', opt_tristate_german_converter), + ('Rodelverleih', opt_no_or_str_converter), ('Handyempfang', ValueCommentListNeinLoopNone()), ('Homepage', webauskunft_converter), - ('E-Mail', EmailCommentListNeinLoopNone(allow_masked_email=True)), + ('E-Mail', emails_converter), ('Telefon', PhoneCommentListNeinLoopNone(comments_are_optional=True)), ('Bild', opt_str_converter), ('Rodelbahnen', WikiPageListLoopNone())]) -- 2.39.5