<s> import re <EOL> import datetime <EOL> from pyramid . i18n import TranslationString as _ <EOL> from colander import ( <EOL> deferred , <EOL> SchemaNode , <EOL> String , <EOL> OneOf , <EOL> Range , <EOL> Sequence , <EOL> Length , <EOL> ContainsOnly , <EOL> Int , <EOL> Decimal , <EOL> Boolean , <EOL> Regex , <EOL> Email , <EOL> Date , <EOL> DateTime , <EOL> Mapping , <EOL> drop , <EOL> ) <EOL> from . import registry , TypeField , TypeFieldNode <EOL> from . json import JSONList <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> @ registry . add ( '<STR_LIT:int>' ) <EOL> class IntField ( TypeField ) : <EOL> node = Int <EOL> hint = _ ( '<STR_LIT>' ) <EOL> @ registry . add ( '<STR_LIT:string>' ) <EOL> class StringField ( TypeField ) : <EOL> node = String <EOL> hint = _ ( '<STR_LIT>' ) <EOL> @ registry . add ( '<STR_LIT:text>' ) <EOL> class TextField ( TypeField ) : <EOL> node = String <EOL> hint = _ ( '<STR_LIT>' ) <EOL> @ registry . add ( '<STR_LIT>' ) <EOL> class AnnotationField ( TypeField ) : <EOL> required = False <EOL> @ classmethod <EOL> def definition ( cls , ** kwargs ) : <EOL> schema = SchemaNode ( Mapping ( unknown = "<STR_LIT>" ) ) <EOL> schema . add ( SchemaNode ( String ( ) , name = '<STR_LIT:label>' , missing = u'<STR_LIT>' ) ) <EOL> schema . add ( SchemaNode ( String ( ) , name = '<STR_LIT:type>' , <EOL> validator = OneOf ( [ "<STR_LIT>" ] ) ) ) <EOL> return schema <EOL> @ registry . add ( '<STR_LIT>' ) <EOL> class DecimalField ( TypeField ) : <EOL> node = Decimal <EOL> hint = _ ( '<STR_LIT>' ) <EOL> @ registry . add ( '<STR_LIT>' ) <EOL> class BooleanField ( TypeField ) : <EOL> node = Boolean <EOL> hint = _ ( '<STR_LIT>' ) <EOL> @ registry . add ( '<STR_LIT>' ) <EOL> class EnumField ( TypeField ) : <EOL> node = String <EOL> hint = _ ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def definition ( cls , ** kwargs ) : <EOL> schema = super ( EnumField , cls ) . definition ( ) <EOL> schema . add ( SchemaNode ( Sequence ( ) , SchemaNode ( String ( ) ) , <EOL> name = '<STR_LIT>' , validator = Length ( min = <NUM_LIT:1> ) ) ) <EOL> return schema <EOL> @ classmethod <EOL> def validation ( cls , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = OneOf ( kwargs [ '<STR_LIT>' ] ) <EOL> return super ( EnumField , cls ) . validation ( ** kwargs ) <EOL> @ registry . add ( '<STR_LIT>' ) <EOL> class ChoicesField ( TypeField ) : <EOL> node = JSONList <EOL> hint = _ ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def definition ( cls , ** kwargs ) : <EOL> schema = super ( ChoicesField , cls ) . definition ( ) <EOL> schema . add ( SchemaNode ( Sequence ( ) , SchemaNode ( String ( ) ) , <EOL> name = '<STR_LIT>' , validator = Length ( min = <NUM_LIT:1> ) ) ) <EOL> return schema <EOL> @ classmethod <EOL> def validation ( cls , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = ContainsOnly ( kwargs [ '<STR_LIT>' ] ) <EOL> return super ( ChoicesField , cls ) . validation ( ** kwargs ) <EOL> @ registry . add ( '<STR_LIT>' ) <EOL> class RangeField ( TypeField ) : <EOL> node = Int <EOL> hint = _ ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def definition ( cls , ** kwargs ) : <EOL> schema = super ( RangeField , cls ) . definition ( ) <EOL> schema . add ( SchemaNode ( Int ( ) , name = '<STR_LIT>' ) ) <EOL> schema . add ( SchemaNode ( Int ( ) , name = '<STR_LIT>' ) ) <EOL> return schema <EOL> @ classmethod <EOL> def validation ( cls , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = Range ( min = kwargs . get ( '<STR_LIT>' ) , <EOL> max = kwargs . get ( '<STR_LIT>' ) ) <EOL> return super ( RangeField , cls ) . validation ( ** kwargs ) <EOL> @ registry . add ( '<STR_LIT>' ) <EOL> class RegexField ( TypeField ) : <EOL> """<STR_LIT>""" <EOL> node = String <EOL> hint = _ ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def definition ( cls , ** kwargs ) : <EOL> schema = super ( RegexField , cls ) . definition ( ) <EOL> schema . add ( SchemaNode ( String ( ) , name = '<STR_LIT>' , validator = Length ( min = <NUM_LIT:1> ) ) ) <EOL> return schema <EOL> @ classmethod <EOL> def validation ( cls , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = Regex ( kwargs [ '<STR_LIT>' ] ) <EOL> return super ( RegexField , cls ) . validation ( ** kwargs ) <EOL> @ registry . add ( '<STR_LIT:email>' ) <EOL> class EmailField ( TypeField ) : <EOL> """<STR_LIT>""" <EOL> node = String <EOL> hint = _ ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def validation ( cls , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = Email ( ) <EOL> return super ( EmailField , cls ) . validation ( ** kwargs ) <EOL> @ registry . add ( '<STR_LIT:url>' ) <EOL> class URLField ( TypeField ) : <EOL> """<STR_LIT>""" <EOL> node = String <EOL> hint = _ ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def validation ( cls , ** kwargs ) : <EOL> urlpattern = re . compile ( <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' , re . IGNORECASE ) <EOL> kwargs [ '<STR_LIT>' ] = Regex ( urlpattern , msg = "<STR_LIT>" ) <EOL> return super ( URLField , cls ) . validation ( ** kwargs ) <EOL> class AutoNowMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> autonow = False <EOL> @ classmethod <EOL> def definition ( cls , ** kwargs ) : <EOL> schema = super ( AutoNowMixin , cls ) . definition ( ) <EOL> schema . add ( SchemaNode ( Boolean ( ) , name = '<STR_LIT>' , <EOL> missing = cls . autonow ) ) <EOL> return schema <EOL> @ classmethod <EOL> def validation ( cls , ** kwargs ) : <EOL> autonow = kwargs . get ( '<STR_LIT>' , cls . autonow ) <EOL> if autonow : <EOL> kwargs [ '<STR_LIT>' ] = cls . auto_value <EOL> return super ( AutoNowMixin , cls ) . validation ( ** kwargs ) . bind ( ) <EOL> @ registry . add ( '<STR_LIT:date>' ) <EOL> class DateField ( AutoNowMixin , TypeField ) : <EOL> """<STR_LIT>""" <EOL> node = Date <EOL> hint = _ ( '<STR_LIT>' ) <EOL> @ deferred <EOL> def auto_value ( node , kw ) : <EOL> return datetime . date . today ( ) <EOL> @ registry . add ( '<STR_LIT>' ) <EOL> class DateTimeField ( AutoNowMixin , TypeField ) : <EOL> """<STR_LIT>""" <EOL> node = DateTime <EOL> hint = _ ( '<STR_LIT>' ) <EOL> @ deferred <EOL> def auto_value ( node , kw ) : <EOL> return datetime . datetime . now ( ) <EOL> @ registry . add ( '<STR_LIT>' ) <EOL> class GroupField ( TypeField ) : <EOL> @ classmethod <EOL> def definition ( cls , ** kwargs ) : <EOL> schema = super ( GroupField , cls ) . definition ( ** kwargs ) <EOL> schema . children = [ c for c in schema . children <EOL> if c . name not in ( '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' ) ] <EOL> schema . add ( SchemaNode ( String ( ) , name = '<STR_LIT:description>' , missing = drop ) ) <EOL> schema . add ( SchemaNode ( Sequence ( ) , SchemaNode ( TypeFieldNode ( ) ) , <EOL> name = '<STR_LIT>' , validator = Length ( min = <NUM_LIT:1> ) ) ) <EOL> return schema <EOL> @ classmethod <EOL> def validation ( cls , ** kwargs ) : <EOL> rootnode = kwargs . pop ( '<STR_LIT:root>' ) <EOL> for field in kwargs [ '<STR_LIT>' ] : <EOL> field [ '<STR_LIT:root>' ] = rootnode <EOL> fieldtype = field . pop ( '<STR_LIT:type>' ) <EOL> rootnode . add ( registry . validation ( fieldtype , ** field ) ) <EOL> return SchemaNode ( String ( ) , missing = drop ) </s>
