<s> import re <EOL> import uuid <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> from nymms . schemas . types import ( TimestampType , StateType , StateTypeType , <EOL> JSONType , StateNameType , StateTypeNameType ) <EOL> from schematics . models import Model <EOL> from schematics . transforms import blacklist <EOL> from schematics . types import ( <EOL> StringType , IPv4Type , UUIDType , IntType ) <EOL> import arrow <EOL> class OriginModel ( Model ) : <EOL> def __init__ ( self , raw_data = None , deserialize_mapping = None , strict = True , <EOL> origin = None ) : <EOL> super ( OriginModel , self ) . __init__ ( <EOL> raw_data = raw_data , <EOL> deserialize_mapping = deserialize_mapping , <EOL> strict = strict ) <EOL> self . _origin = origin <EOL> class Suppression ( OriginModel ) : <EOL> CURRENT_VERSION = <NUM_LIT:2> <EOL> rowkey = UUIDType ( default = uuid . uuid4 ) <EOL> regex = StringType ( required = True ) <EOL> created = TimestampType ( default = arrow . get ) <EOL> disabled = TimestampType ( serialize_when_none = False ) <EOL> expires = TimestampType ( required = True ) <EOL> ipaddr = IPv4Type ( required = True ) <EOL> userid = StringType ( required = True ) <EOL> comment = StringType ( required = True ) <EOL> version = IntType ( default = CURRENT_VERSION ) <EOL> default_sort_order = '<STR_LIT>' <EOL> pk = '<STR_LIT>' <EOL> @ property <EOL> def active ( self ) : <EOL> if self . disabled or self . expires < arrow . get ( ) : <EOL> return False <EOL> else : <EOL> return True <EOL> @ property <EOL> def state ( self ) : <EOL> if self . disabled : <EOL> return "<STR_LIT>" % ( self . disabled , <EOL> self . disabled . humanize ( ) ) <EOL> elif self . expires < arrow . get ( ) : <EOL> return "<STR_LIT>" % ( self . expires , <EOL> self . expires . humanize ( ) ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> @ property <EOL> def re ( self ) : <EOL> return re . compile ( self . regex ) <EOL> @ classmethod <EOL> def migrate ( cls , item ) : <EOL> """<STR_LIT>""" <EOL> new_suppression = None <EOL> try : <EOL> new_suppression = cls ( { <EOL> '<STR_LIT>' : uuid . UUID ( item [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : item [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : arrow . get ( int ( item [ '<STR_LIT>' ] ) ) , <EOL> '<STR_LIT>' : arrow . get ( int ( item [ '<STR_LIT>' ] ) ) , <EOL> '<STR_LIT>' : item [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : item [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : item [ '<STR_LIT>' ] } ) <EOL> if not item [ '<STR_LIT>' ] == '<STR_LIT:True>' : <EOL> new_suppression . disabled = arrow . get ( int ( item [ '<STR_LIT>' ] ) ) <EOL> except Exception : <EOL> logger . exception ( "<STR_LIT>" , item ) <EOL> return new_suppression <EOL> class APISuppression ( Suppression ) : <EOL> """<STR_LIT>""" <EOL> disabled = TimestampType ( serialize_when_none = True ) <EOL> class StateModel ( Model ) : <EOL> CURRENT_VERSION = <NUM_LIT:2> <EOL> state = StateType ( required = True ) <EOL> state_type = StateTypeType ( required = True ) <EOL> version = IntType ( default = CURRENT_VERSION ) <EOL> @ property <EOL> def state_name ( self ) : <EOL> return self . state . name <EOL> @ property <EOL> def state_type_name ( self ) : <EOL> return self . state_type . name <EOL> class Task ( OriginModel ) : <EOL> id = StringType ( required = True ) <EOL> created = TimestampType ( default = arrow . get ) <EOL> attempt = IntType ( default = <NUM_LIT:0> ) <EOL> context = JSONType ( ) <EOL> def increment_attempt ( self ) : <EOL> self . attempt += <NUM_LIT:1> <EOL> class Result ( StateModel , OriginModel ) : <EOL> id = StringType ( required = True ) <EOL> timestamp = TimestampType ( default = arrow . get ) <EOL> output = StringType ( ) <EOL> task_context = JSONType ( ) <EOL> class Options : <EOL> roles = { '<STR_LIT>' : blacklist ( '<STR_LIT>' ) } <EOL> class APIResult ( Result ) : <EOL> """<STR_LIT>""" <EOL> state = StateNameType ( required = True ) <EOL> state_type = StateTypeNameType ( required = True ) <EOL> class StateRecord ( StateModel , OriginModel ) : <EOL> id = StringType ( required = True ) <EOL> last_update = TimestampType ( default = arrow . get ) <EOL> last_state_change = TimestampType ( default = arrow . get ) <EOL> pk = '<STR_LIT:id>' <EOL> default_sort_order = '<STR_LIT>' <EOL> @ classmethod <EOL> def migrate ( cls , item ) : <EOL> new_state = None <EOL> try : <EOL> new_state = cls ( { <EOL> '<STR_LIT:id>' : item [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : arrow . get ( int ( item [ '<STR_LIT>' ] ) ) , <EOL> '<STR_LIT>' : arrow . get ( int ( item [ '<STR_LIT>' ] ) ) , <EOL> '<STR_LIT:state>' : item [ '<STR_LIT:state>' ] , <EOL> '<STR_LIT>' : item [ '<STR_LIT>' ] } ) <EOL> except Exception : <EOL> logger . exception ( "<STR_LIT>" , item ) <EOL> return new_state <EOL> class APIStateRecord ( StateRecord ) : <EOL> """<STR_LIT>""" <EOL> state = StateNameType ( required = True ) <EOL> state_type = StateTypeNameType ( required = True ) </s>
