<s> from __future__ import unicode_literals <EOL> import copy <EOL> import datetime <EOL> from calendar import timegm <EOL> from decimal import Decimal as _D <EOL> import re <EOL> import time <EOL> import warnings <EOL> from uuid import uuid1 , uuid4 <EOL> from uuid import UUID as _UUID <EOL> from goblin . _compat import ( <EOL> text_type , string_types , float_types , integer_types , long_ , PY3 ) <EOL> from goblin . properties . base import GraphProperty <EOL> from goblin . properties . validators import * <EOL> class String ( GraphProperty ) : <EOL> """<STR_LIT>""" <EOL> validator = string_validator <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> required = kwargs . get ( '<STR_LIT>' , False ) <EOL> self . min_length = kwargs . pop ( '<STR_LIT>' , <NUM_LIT:1> if required else None ) <EOL> self . max_length = kwargs . pop ( '<STR_LIT:max_length>' , None ) <EOL> self . encoding = kwargs . pop ( '<STR_LIT>' , '<STR_LIT:utf-8>' ) <EOL> if '<STR_LIT:default>' in kwargs and isinstance ( kwargs [ '<STR_LIT:default>' ] , string_types ) : <EOL> if not PY3 : <EOL> kwargs [ '<STR_LIT:default>' ] = kwargs [ '<STR_LIT:default>' ] . encode ( self . encoding ) <EOL> super ( String , self ) . __init__ ( * args , ** kwargs ) <EOL> def to_python ( self , value ) : <EOL> if value is not None : <EOL> if isinstance ( value , ( bytes , bytearray ) ) and not isinstance ( value , <EOL> str ) : <EOL> return value . decode ( self . encoding ) <EOL> else : <EOL> return value <EOL> def validate ( self , value ) : <EOL> if isinstance ( value , text_type ) and not PY3 : <EOL> value = value . encode ( self . encoding ) <EOL> if self . max_length : <EOL> if len ( value ) > self . max_length : <EOL> raise ValidationError ( '<STR_LIT>' . format ( <EOL> self . property_name , self . max_length ) ) <EOL> if self . min_length : <EOL> if len ( value ) < self . min_length : <EOL> raise ValidationError ( <EOL> '<STR_LIT>' . format ( <EOL> self . property_name , self . min_length ) ) <EOL> value = super ( String , self ) . validate ( value ) <EOL> return value <EOL> class Short ( GraphProperty ) : <EOL> """<STR_LIT>""" <EOL> deserializer = int <EOL> serializer = int <EOL> validator = integer_validator <EOL> class Integer ( GraphProperty ) : <EOL> """<STR_LIT>""" <EOL> serializer = long_ <EOL> deserializer = long_ <EOL> validator = long_validator <EOL> class PositiveInteger ( Integer ) : <EOL> """<STR_LIT>""" <EOL> serializer = long_ <EOL> deserializer = long_ <EOL> validator = positive_integer_validator <EOL> class Long ( GraphProperty ) : <EOL> """<STR_LIT>""" <EOL> serializer = long_ <EOL> deserializer = long_ <EOL> validator = long_validator <EOL> class PositiveLong ( Long ) : <EOL> """<STR_LIT>""" <EOL> serializer = long_ <EOL> deserializer = long_ <EOL> validator = positive_integer_validator <EOL> class DateTimeNaive ( GraphProperty ) : <EOL> """<STR_LIT>""" <EOL> data_type = "<STR_LIT>" <EOL> validator = datetime_validator <EOL> def __init__ ( self , strict = True , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . strict = strict <EOL> super ( DateTimeNaive , self ) . __init__ ( ** kwargs ) <EOL> def to_python ( self , value ) : <EOL> if isinstance ( value , datetime . datetime ) : <EOL> return value <EOL> return datetime . datetime . fromtimestamp ( float ( value ) ) <EOL> def to_database ( self , value ) : <EOL> value = super ( DateTimeNaive , self ) . to_database ( value ) <EOL> if value is None : <EOL> return <EOL> if not isinstance ( value , datetime . datetime ) : <EOL> if not self . strict and isinstance ( value , string_types + <EOL> integer_types + float_types ) : <EOL> value = datetime . datetime . fromtimestamp ( float ( value ) ) <EOL> else : <EOL> raise ValidationError ( <EOL> "<STR_LIT>" . format ( value ) ) <EOL> tmp = time . mktime ( value . timetuple ( ) ) <EOL> tmp += float ( value . microsecond ) / <NUM_LIT> <EOL> return tmp <EOL> class DateTime ( GraphProperty ) : <EOL> """<STR_LIT>""" <EOL> data_type = "<STR_LIT>" <EOL> validator = datetime_utc_validator <EOL> def __init__ ( self , strict = True , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . strict = strict <EOL> super ( DateTime , self ) . __init__ ( ** kwargs ) <EOL> def to_python ( self , value ) : <EOL> try : <EOL> if isinstance ( value , datetime . datetime ) : <EOL> if value . tzinfo == utc : <EOL> return self . validator ( value ) <EOL> else : <EOL> return self . validator ( value ) . astimezone ( tz = utc ) <EOL> except : <EOL> pass <EOL> return datetime . datetime . utcfromtimestamp ( <EOL> float ( value ) ) . replace ( tzinfo = utc ) <EOL> def to_database ( self , value ) : <EOL> value = super ( DateTime , self ) . to_database ( value ) <EOL> if value is None : <EOL> return <EOL> if not isinstance ( value , datetime . datetime ) : <EOL> if isinstance ( value , string_types + integer_types + float_types ) : <EOL> value = datetime . datetime . utcfromtimestamp ( <EOL> float ( value ) ) . replace ( tzinfo = utc ) <EOL> else : <EOL> raise ValidationError ( <EOL> "<STR_LIT>" . format ( value ) ) <EOL> tmp = timegm ( value . utctimetuple ( ) ) <EOL> tmp += float ( value . microsecond ) / <NUM_LIT> <EOL> return tmp <EOL> class UUID ( GraphProperty ) : <EOL> """<STR_LIT>""" <EOL> serializer = str <EOL> validator = validate_uuid <EOL> def to_python ( self , value ) : <EOL> val = super ( UUID , self ) . to_python ( value ) <EOL> if value is not None : <EOL> if isinstance ( value , ( bytes , bytearray ) ) and not isinstance ( value , <EOL> str ) : <EOL> return value . decode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> return value <EOL> class Boolean ( GraphProperty ) : <EOL> """<STR_LIT>""" <EOL> deserializer = bool <EOL> validator = bool_validator <EOL> class Double ( GraphProperty ) : <EOL> """<STR_LIT>""" <EOL> deserializer = float <EOL> validator = float_validator <EOL> Float = Double <EOL> class Decimal ( GraphProperty ) : <EOL> """<STR_LIT>""" <EOL> serializer = float <EOL> validator = decimal_validator <EOL> def to_python ( self , value ) : <EOL> val = super ( Decimal , self ) . to_python ( value ) <EOL> if val is not None : <EOL> pos = <NUM_LIT:0> <EOL> if val < <NUM_LIT:0> : <EOL> pos = <NUM_LIT:1> <EOL> digs = [ int ( i ) for i in str ( val ) if i . isdigit ( ) ] <EOL> return _D ( ( pos , digs , - <NUM_LIT:3> ) ) <EOL> class URL ( String ) : <EOL> """<STR_LIT>""" <EOL> validator = validate_url <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( URL , self ) . __init__ ( * args , ** kwargs ) <EOL> def validate ( self , value ) : <EOL> return super ( URL , self ) . validate ( value ) <EOL> class Email ( GraphProperty ) : <EOL> """<STR_LIT>""" <EOL> validator = validate_email <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . encoding = kwargs . pop ( '<STR_LIT>' , '<STR_LIT:utf-8>' ) <EOL> if '<STR_LIT:default>' in kwargs and isinstance ( kwargs [ '<STR_LIT:default>' ] , string_types ) : <EOL> if not PY3 : <EOL> kwargs [ '<STR_LIT:default>' ] = kwargs [ '<STR_LIT:default>' ] . encode ( self . encoding ) <EOL> super ( Email , self ) . __init__ ( * args , ** kwargs ) <EOL> def validate ( self , value ) : <EOL> if isinstance ( value , text_type ) and not PY3 : <EOL> value = value . encode ( self . encoding ) <EOL> value = super ( Email , self ) . validate ( value ) <EOL> return value <EOL> class IPV4 ( GraphProperty ) : <EOL> """<STR_LIT>""" <EOL> serializer = str <EOL> deserializer = ipaddress . IPv4Address <EOL> validator = validate_ipv4_address <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . encoding = kwargs . pop ( '<STR_LIT>' , '<STR_LIT:utf-8>' ) <EOL> if '<STR_LIT:default>' in kwargs and isinstance ( kwargs [ '<STR_LIT:default>' ] , string_types ) : <EOL> if not PY3 : <EOL> kwargs [ '<STR_LIT:default>' ] = kwargs [ '<STR_LIT:default>' ] . encode ( self . encoding ) <EOL> super ( IPV4 , self ) . __init__ ( * args , ** kwargs ) <EOL> def to_python ( self , value ) : <EOL> if value : <EOL> value = ipaddress . ip_address ( value ) <EOL> return value <EOL> def validate ( self , value ) : <EOL> if isinstance ( value , text_type ) and not PY3 : <EOL> value = value . encode ( self . encoding ) <EOL> value = super ( IPV4 , self ) . validate ( value ) <EOL> return value <EOL> class IPV6 ( IPV4 ) : <EOL> """<STR_LIT>""" <EOL> deserializer = ipaddress . IPv4Address <EOL> validator = validate_ipv6_address <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( IPV6 , self ) . __init__ ( * args , ** kwargs ) <EOL> def validate ( self , value ) : <EOL> return super ( IPV6 , self ) . validate ( value ) <EOL> class IPV6WithV4 ( IPV6 ) : <EOL> """<STR_LIT>""" <EOL> deserializer = ipaddress . IPv4Address <EOL> validator = validate_ipv6v4_address <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( IPV6WithV4 , self ) . __init__ ( * args , ** kwargs ) <EOL> def validate ( self , value ) : <EOL> return super ( IPV6WithV4 , self ) . validate ( value ) <EOL> class Slug ( GraphProperty ) : <EOL> """<STR_LIT>""" <EOL> data_type = "<STR_LIT>" <EOL> validator = validate_slug <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . encoding = kwargs . pop ( '<STR_LIT>' , '<STR_LIT:utf-8>' ) <EOL> if '<STR_LIT:default>' in kwargs and isinstance ( kwargs [ '<STR_LIT:default>' ] , string_types ) : <EOL> if not PY3 : <EOL> kwargs [ '<STR_LIT:default>' ] = kwargs [ '<STR_LIT:default>' ] . encode ( self . encoding ) <EOL> super ( Slug , self ) . __init__ ( * args , ** kwargs ) <EOL> def validate ( self , value ) : <EOL> if isinstance ( value , text_type ) and not PY3 : <EOL> value = value . encode ( self . encoding ) <EOL> value = super ( Slug , self ) . validate ( value ) <EOL> return value </s>
