self.validator = validator
self.python_none = python_none
- def to_python(self, value):
- self.assert_string(value, None)
+ def to_python(self, value, state=None):
+ self.assert_string(value, state)
if value == u'': return self.python_none
- return self.validator.to_python(value)
+ return self.validator.to_python(value, state)
- def from_python(self, value):
+ def from_python(self, value, state=None):
if value == self.python_none: return u''
- return self.validator.from_python(value)
+ return self.validator.from_python(value, state)
class NeinValidator(formencode.FancyValidator):
self.validator = validator
self.python_no = python_no
- def to_python(self, value):
- self.assert_string(value, None)
+ def to_python(self, value, state=None):
+ self.assert_string(value, state)
if value == u'Nein': return self.python_no
- return self.validator.to_python(value)
+ return self.validator.to_python(value, state)
- def from_python(self, value):
+ def from_python(self, value, state=None):
if value == self.python_no: return u'Nein'
- return self.validator.from_python(value)
+ return self.validator.from_python(value, state)
class Unicode(formencode.FancyValidator):
"""Converts an unicode string to an unicode string:
u'any string' <=> u'any string'"""
- def to_python(self, value):
- self.assert_string(value, None)
+ def to_python(self, value, state=None):
+ self.assert_string(value, state)
return unicode(value)
- def from_python(self, value):
+ def from_python(self, value, state=None):
return unicode(value)
def __init__(self, max=None):
self.iv = formencode.validators.Int(min=0, max=max)
- def to_python(self, value):
- self.assert_string(value, None)
- return self.iv.to_python(value)
+ def to_python(self, value, state=None):
+ self.assert_string(value, state)
+ return self.iv.to_python(value, state)
- def from_python(self, value):
+ def from_python(self, value, state=None):
return unicode(value)
def __init__(self, validator):
self.validator = validator
- def to_python(self, value):
- self.assert_string(value, None)
- return self.validator.from_python(self.validator.to_python(value))
+ def to_python(self, value, state=None):
+ self.assert_string(value, state)
+ return self.validator.from_python(self.validator.to_python(value, state))
- def from_python(self, value):
+ def from_python(self, value, state=None):
# we don't call self.validator.to_python(self.validator.from_python(value))
# here because our to_python implementation basically leaves the input untouched
# and so should from_python do.
- return self.validator.from_python(self.validator.to_python(value))
+ return self.validator.from_python(self.validator.to_python(value, state))
class DictValidator(formencode.FancyValidator):
def __init__(self, dict):
self.dict = dict
- def to_python(self, value):
- self.assert_string(value, None)
- if not self.dict.has_key(value): raise formencode.Invalid("Key not found in dict.", value, None)
+ def to_python(self, value, state=None):
+ self.assert_string(value, state)
+ if not self.dict.has_key(value): raise formencode.Invalid("Key not found in dict.", value, state)
return self.dict[value]
- def from_python(self, value):
+ def from_python(self, value, state=None):
for k, v in self.dict.iteritems():
if v == value:
return k
- raise formencode.Invalid('Invalid value', value, None)
+ raise formencode.Invalid('Invalid value', value, state)
class GermanBoolNone(DictValidator):
self.comment_validator = comment_validator
self.comment_is_optional = comment_is_optional
- def to_python(self, value):
- self.assert_string(value, None)
+ def to_python(self, value, state=None):
+ self.assert_string(value, state)
if value == u'':
v = value
c = value
else:
right = value.rfind(')')
if right+1 != len(value):
- if not self.comment_is_optional: raise formencode.Invalid(u'Mandatory comment not present', value, None)
+ if not self.comment_is_optional: raise formencode.Invalid(u'Mandatory comment not present', value, state)
v = value
c = u''
else:
left = value.rfind('(')
- if left < 0: raise formencode.Invalid(u'Invalid format', value, None)
+ if left < 0: raise formencode.Invalid(u'Invalid format', value, state)
v = value[:left].strip()
c = value[left+1:right].strip()
- return self.value_validator.to_python(v), self.comment_validator.to_python(c)
+ return self.value_validator.to_python(v, state), self.comment_validator.to_python(c, state)
- def from_python(self, value):
+ def from_python(self, value, state=None):
assert len(value) == 2
- v = self.value_validator.from_python(value[0])
- c = self.comment_validator.from_python(value[1])
+ v = self.value_validator.from_python(value[0], state)
+ c = self.comment_validator.from_python(value[1], state)
if len(c) > 0:
if len(v) > 0: return u'%s (%s)' % (v, c)
else: return u'(%s)' % c
def __init__(self, validator=Unicode()):
self.validator = validator
- def to_python(self, value):
- self.assert_string(value, None)
- return [self.validator.to_python(s.strip()) for s in value.split(';')]
+ def to_python(self, value, state=None):
+ self.assert_string(value, state)
+ return [self.validator.to_python(s.strip(), state) for s in value.split(';')]
- def from_python(self, value):
- return "; ".join([self.validator.from_python(s) for s in value])
+ def from_python(self, value, state=None):
+ return "; ".join([self.validator.from_python(s, state) for s in value])
class ValueCommentList(SemicolonList):
self.date_time_format = date_time_format
def to_python(self, value, state=None):
- self.assert_string(value, None)
+ self.assert_string(value, state)
try: return datetime.datetime.strptime(value, self.date_time_format)
- except ValueError, e: raise formencode.Invalid(str(e), value, None)
+ except ValueError, e: raise formencode.Invalid(str(e), value, state)
def from_python(self, value, state=None):
return value.strftime(self.date_time_format)
class Geo(formencode.FancyValidator):
"""Formats to coordinates '47.076207 N 11.453553 E' to the (latitude, longitude) tuplet."""
- def to_python(self, value):
- self.assert_string(value, None)
+ def to_python(self, value, state=None):
+ self.assert_string(value, state)
r = re.match(u'(\d+\.\d+) N (\d+\.\d+) E', value)
- if r is None: raise formencode.Invalid(u"Coordinates '%s' have not a format like '47.076207 N 11.453553 E'" % value, value, None)
+ if r is None: raise formencode.Invalid(u"Coordinates '%s' have not a format like '47.076207 N 11.453553 E'" % value, value, state)
return (float(r.groups()[0]), float(r.groups()[1]))
- def from_python(self, value):
+ def from_python(self, value, state=None):
latitude, longitude = value
return u'%.6f N %.6f E' % (latitude, longitude)
self.output_format = output_format
formencode.FancyValidator.__init__(self, if_empty = (None, None, None), **keywords)
- def to_python(self, value):
- self.assert_string(value, None)
+ def to_python(self, value, state=None):
+ self.assert_string(value, state)
input_format = self.input_format
if not input_format in [self.FORMAT_GUESS, self.FORMAT_GEOCACHING, self.FORMAT_WINTERRODELN, self.FORMAT_GMAPPLUGIN, self.FORMAT_GPX]:
- raise formencode.Invalid(u"input_format %d is not recognized" % input_format, value, None) # Shouldn't it be an other type of runtime error?
+ raise formencode.Invalid(u"input_format %d is not recognized" % input_format, value, state) # Shouldn't it be an other type of runtime error?
lines = [line.strip() for line in value.split("\n") if len(line.strip()) > 0]
result = []
continue
except (ExpatError, IndexError, ValueError): pass
- raise formencode.Invalid(u"Coordinates '%s' have no known format" % line, value, None)
+ raise formencode.Invalid(u"Coordinates '%s' have no known format" % line, value, state)
return result
- def from_python(self, value):
+ def from_python(self, value, state=None):
output_format = self.output_format
result = []
for latitude, longitude, height in value:
else: result.append(u'<trkpt lat="%.6f" lon="%.6f"/>' % (latitude, longitude))
else:
- raise formencode.Invalid(u"output_format %d is not recognized" % output_format, value, None) # Shouldn't it be an other type of runtime error?
+ raise formencode.Invalid(u"output_format %d is not recognized" % output_format, value, state) # Shouldn't it be an other type of runtime error?
return "\n".join(result)
default_cc = 43 # Default country code
messages = {'phoneFormat': "'%%(value)s' is an invalid format. Please enter a number in the form +43/###/####### or 0###/########."}
- def to_python(self, value):
- self.assert_string(value, None)
+ def to_python(self, value, state=None):
+ self.assert_string(value, state)
m = re.match(u'^(?:\+(\d+)/)?([\d/]+)(?:-(\d+))?$', value)
# This will separate
# u'+43/512/1234567-89' => (u'43', u'512/1234567', u'89')
# u'+43/512/1234/567-89' => (u'43', u'512/1234/567', u'89')
# u'+43/512/1234/567' => (u'43', u'512/1234/567', None)
# u'0512/1234567' => (None, u'0512/1234567', None)
- if m is None: raise formencode.Invalid(self.message('phoneFormat', None) % {'value': value}, value, None)
+ if m is None: raise formencode.Invalid(self.message('phoneFormat', None) % {'value': value}, value, state)
(country, phone, extension) = m.groups()
# Phone
- if phone.find(u'//') > -1 or phone.count('/') == 0: raise formencode.Invalid(self.message('phoneFormat', None) % {'value': value}, value, None)
+ if phone.find(u'//') > -1 or phone.count('/') == 0: raise formencode.Invalid(self.message('phoneFormat', None) % {'value': value}, value, state)
# Country
if country is None:
- if phone[0] != '0': raise formencode.Invalid(self.message('phoneFormat', None) % {'value': value}, value, None)
+ if phone[0] != '0': raise formencode.Invalid(self.message('phoneFormat', None) % {'value': value}, value, state)
phone = phone[1:]
country = unicode(self.default_cc)
u'' <=> None
u'Nein' <=> 'Nein'
u'Tiroler Naturrodelbahn-Gütesiegel 2009 mittel' <=> u'Tiroler Naturrodelbahn-Gütesiegel 2009 mittel'"""
- def to_python(self, value):
- self.assert_string(value, None)
+ def to_python(self, value, state=None):
+ self.assert_string(value, state)
if value == u'': return None
elif value == u'Nein': return value
elif value.startswith(u'Tiroler Naturrodelbahn-Gütesiegel '):
p = value.split(" ")
- Unsigned().to_python(p[2]) # check if year can be parsed
- if not p[3] in ['leicht', 'mittel', 'schwer']: raise formencode.Invalid("Unbekannter Schwierigkeitsgrad", value, None)
+ Unsigned().to_python(p[2], state) # check if year can be parsed
+ if not p[3] in ['leicht', 'mittel', 'schwer']: raise formencode.Invalid("Unbekannter Schwierigkeitsgrad", value, state)
return value
- else: raise formencode.Invalid(u"Unbekanntes Gütesiegel", value, None)
+ else: raise formencode.Invalid(u"Unbekanntes Gütesiegel", value, state)
- def from_python(self, value):
- if value == None: return u''
+ def from_python(self, value, state=None):
+ if value is None: return u''
assert value != u''
- return self.to_python(value)
+ return self.to_python(value, state)
class Url(formencode.FancyValidator):
# formencode 1.2.5 to formencode 1.3.0a1 sometimes raise ValueError instead of Invalid exceptions
# https://github.com/formencode/formencode/pull/61
urlv = formencode.validators.URL()
- def to_python(self, value):
- self.assert_string(value, None)
+
+ def to_python(self, value, state=None):
+ self.assert_string(value, state)
v = value
v = v.replace(u'ä', u'a')
v = v.replace(u'ö', u'o')
v = v.replace(u'ü', u'u')
v = v.replace(u'ß', u'ss')
- v = self.urlv.to_python(v)
+ v = self.urlv.to_python(v, state)
return value
- def from_python(self, value):
+ def from_python(self, value, state=None):
return value
def __init__(self, default_cc=43):
self.validator = formencode.national.InternationalPhoneNumber(default_cc=lambda: default_cc)
- def to_python(self, value):
- return unicode(self.validator.to_python(value))
+ def to_python(self, value, state=None):
+ return unicode(self.validator.to_python(value, state))
- def from_python(self, value):
- return self.validator.from_python(value)
+ def from_python(self, value, state=None):
+ return self.validator.from_python(value, state)
class PhoneCommentListNeinLoopNone(NoneValidator):
self.at = '(at)'
formencode.FancyValidator.__init__(self, *args, **kw)
- def _to_python(self, value, state):
+ 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), masked
+ return val_email.to_python(email, state), masked
- def _from_python(self, value, state):
+ def _from_python(self, value, state=None):
email, masked = value
if email is None: return u''
val_email = formencode.validators.Email()
- email = val_email.from_python(email)
+ email = val_email.from_python(email, state)
if masked: email = email.replace('@', self.at)
return email
An empty string is an error.
u'[[Birgitzer Alm]]' <=> u'[[Birgitzer Alm]]'
"""
- def to_python(self, value):
- self.assert_string(value, None)
+ 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, None)
+ raise formencode.Invalid('No valid wiki page name', value, state)
return value
- def from_python(self, value):
+ def from_python(self, value, state=None):
return value
self.first = first
self.validator = validator
- def to_python(self, value):
- self.assert_string(value, None)
- return self.first, self.validator.to_python(value)
+ def to_python(self, value, state=None):
+ self.assert_string(value, state)
+ return self.first, self.validator.to_python(value, state)
- def from_python(self, value):
+ def from_python(self, value, state=None):
assert value[0] == self.first
- return self.validator.from_python(value[1])
+ return self.validator.from_python(value[1], state)
class BoolUnicodeTupleValidator(NoneValidator):