<s> import logging <EOL> import collections <EOL> import json <EOL> import time <EOL> import string <EOL> import random <EOL> logger = logging . getLogger ( __name__ ) <EOL> from schematics . types import BaseType <EOL> from schematics . exceptions import ValidationError <EOL> from nymms . utils import parse_time <EOL> import arrow <EOL> class TimestampType ( BaseType ) : <EOL> def to_native ( self , value , context = None ) : <EOL> if isinstance ( value , arrow . arrow . Arrow ) : <EOL> return value <EOL> try : <EOL> return parse_time ( value ) <EOL> except ValueError : <EOL> return arrow . get ( value ) <EOL> def to_primitive ( self , value , context = None ) : <EOL> return value . isoformat ( ) <EOL> def _mock ( self , context = None ) : <EOL> year = <NUM_LIT> * <NUM_LIT> <EOL> return arrow . get ( time . time ( ) + ( random . randrange ( - <NUM_LIT:1> * <NUM_LIT:20> * year , <EOL> <NUM_LIT:200> * year ) ) ) <EOL> class JSONType ( BaseType ) : <EOL> def to_native ( self , value , context = None ) : <EOL> if isinstance ( value , basestring ) : <EOL> return json . loads ( value ) <EOL> return value <EOL> def to_primitive ( self , value , context = None ) : <EOL> return json . dumps ( value ) <EOL> def _mock ( self , context = None ) : <EOL> return dict ( <EOL> [ ( random . choice ( string . ascii_letters ) , <EOL> random . choice ( string . printable ) ) for i in <EOL> range ( random . randrange ( <NUM_LIT:4> , <NUM_LIT:10> ) ) ] ) <EOL> StateObject = collections . namedtuple ( '<STR_LIT>' , [ '<STR_LIT:name>' , '<STR_LIT:code>' ] ) <EOL> STATE_OK = StateObject ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> STATE_WARNING = STATE_WARN = StateObject ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> STATE_CRITICAL = STATE_CRIT = StateObject ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> STATE_UNKNOWN = StateObject ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> STATES = collections . OrderedDict ( [ <EOL> ( '<STR_LIT>' , STATE_OK ) , <EOL> ( '<STR_LIT>' , STATE_WARNING ) , <EOL> ( '<STR_LIT>' , STATE_CRITICAL ) , <EOL> ( '<STR_LIT>' , STATE_UNKNOWN ) ] ) <EOL> class StateType ( BaseType ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( StateType , self ) . __init__ ( * args , choices = STATES . values ( ) , <EOL> ** kwargs ) <EOL> def to_native ( self , value , context = None ) : <EOL> if isinstance ( value , StateObject ) : <EOL> return value <EOL> try : <EOL> int_value = int ( value ) <EOL> try : <EOL> return STATES . values ( ) [ int_value ] <EOL> except IndexError : <EOL> return STATE_UNKNOWN <EOL> except ValueError : <EOL> try : <EOL> return STATES [ value . lower ( ) ] <EOL> except KeyError : <EOL> raise ValidationError ( self . messages [ '<STR_LIT>' ] . format ( <EOL> unicode ( self . choices ) ) ) <EOL> def to_primitive ( self , value , context = None ) : <EOL> return value . code <EOL> class StateNameType ( StateType ) : <EOL> def to_primitive ( self , value , context = None ) : <EOL> return value . name <EOL> StateTypeObject = collections . namedtuple ( '<STR_LIT>' , [ '<STR_LIT:name>' , '<STR_LIT:code>' ] ) <EOL> STATE_TYPE_SOFT = StateTypeObject ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> STATE_TYPE_HARD = StateTypeObject ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> STATE_TYPES = collections . OrderedDict ( [ <EOL> ( '<STR_LIT>' , STATE_TYPE_SOFT ) , <EOL> ( '<STR_LIT>' , STATE_TYPE_HARD ) ] ) <EOL> class StateTypeType ( BaseType ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( StateTypeType , self ) . __init__ ( * args , <EOL> choices = STATE_TYPES . values ( ) , <EOL> ** kwargs ) <EOL> def to_native ( self , value , context = None ) : <EOL> if isinstance ( value , StateTypeObject ) : <EOL> return value <EOL> try : <EOL> return STATE_TYPES . values ( ) [ int ( value ) ] <EOL> except ValueError : <EOL> try : <EOL> return STATE_TYPES [ value . lower ( ) ] <EOL> except KeyError : <EOL> raise ValidationError ( self . messages [ '<STR_LIT>' ] . format ( <EOL> unicode ( self . choices ) ) ) <EOL> def to_primitive ( self , value , context = None ) : <EOL> return value . code <EOL> class StateTypeNameType ( StateTypeType ) : <EOL> def to_primitive ( self , value , context = None ) : <EOL> return value . name </s>
