<s> import json <EOL> import re <EOL> import sys <EOL> import types <EOL> from . import validators <EOL> __version__ = "<STR_LIT>" <EOL> Delete = '<STR_LIT>' <EOL> Retain = '<STR_LIT>' <EOL> Snapshot = '<STR_LIT>' <EOL> AWS_ACCOUNT_ID = '<STR_LIT>' <EOL> AWS_NOTIFICATION_ARNS = '<STR_LIT>' <EOL> AWS_NO_VALUE = '<STR_LIT>' <EOL> AWS_REGION = '<STR_LIT>' <EOL> AWS_STACK_ID = '<STR_LIT>' <EOL> AWS_STACK_NAME = '<STR_LIT>' <EOL> valid_names = re . compile ( r'<STR_LIT>' ) <EOL> class BaseAWSObject ( object ) : <EOL> def __init__ ( self , title , template = None , ** kwargs ) : <EOL> self . title = title <EOL> self . template = template <EOL> self . propnames = self . props . keys ( ) <EOL> self . attributes = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> if self . title : <EOL> self . validate_title ( ) <EOL> self . properties = { } <EOL> dictname = getattr ( self , '<STR_LIT>' , None ) <EOL> if dictname : <EOL> self . resource = { <EOL> dictname : self . properties , <EOL> } <EOL> else : <EOL> self . resource = self . properties <EOL> if hasattr ( self , '<STR_LIT>' ) and self . resource_type is not None : <EOL> self . resource [ '<STR_LIT>' ] = self . resource_type <EOL> self . __initialized = True <EOL> for k , ( _ , required ) in self . props . items ( ) : <EOL> v = getattr ( type ( self ) , k , None ) <EOL> if v is not None and k not in kwargs : <EOL> self . __setattr__ ( k , v ) <EOL> for k , v in kwargs . items ( ) : <EOL> self . __setattr__ ( k , v ) <EOL> if self . template is not None : <EOL> self . template . add_resource ( self ) <EOL> def __getattr__ ( self , name ) : <EOL> try : <EOL> return self . properties . __getitem__ ( name ) <EOL> except KeyError : <EOL> if name == '<STR_LIT:name>' : <EOL> return self . __getattribute__ ( '<STR_LIT:title>' ) <EOL> raise AttributeError ( name ) <EOL> def __setattr__ ( self , name , value ) : <EOL> if name in self . __dict__ . keys ( ) or '<STR_LIT>' not in self . __dict__ : <EOL> return dict . __setattr__ ( self , name , value ) <EOL> elif name in self . attributes : <EOL> self . resource [ name ] = value <EOL> return None <EOL> elif name in self . propnames : <EOL> expected_type = self . props [ name ] [ <NUM_LIT:0> ] <EOL> if isinstance ( value , AWSHelperFn ) : <EOL> return self . properties . __setitem__ ( name , value ) <EOL> elif isinstance ( expected_type , types . FunctionType ) : <EOL> try : <EOL> value = expected_type ( value ) <EOL> except : <EOL> sys . stderr . write ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( self . __class__ , <EOL> self . title , <EOL> name , <EOL> expected_type . __name__ ) ) <EOL> raise <EOL> return self . properties . __setitem__ ( name , value ) <EOL> elif isinstance ( expected_type , list ) : <EOL> if not isinstance ( value , list ) : <EOL> self . _raise_type ( name , value , expected_type ) <EOL> for v in value : <EOL> if not isinstance ( v , tuple ( expected_type ) ) and not isinstance ( v , AWSHelperFn ) : <EOL> self . _raise_type ( name , v , expected_type ) <EOL> return self . properties . __setitem__ ( name , value ) <EOL> elif isinstance ( value , expected_type ) : <EOL> return self . properties . __setitem__ ( name , value ) <EOL> else : <EOL> self . _raise_type ( name , value , expected_type ) <EOL> type_name = getattr ( self , '<STR_LIT>' , self . __class__ . __name__ ) <EOL> if type_name == '<STR_LIT>' or type_name . startswith ( '<STR_LIT>' ) : <EOL> return self . properties . __setitem__ ( name , value ) <EOL> raise AttributeError ( "<STR_LIT>" % <EOL> ( type_name , name ) ) <EOL> def _raise_type ( self , name , value , expected_type ) : <EOL> raise TypeError ( '<STR_LIT>' % ( self . __class__ , <EOL> self . title , <EOL> name , <EOL> type ( value ) , <EOL> expected_type ) ) <EOL> def validate_title ( self ) : <EOL> if not valid_names . match ( self . title ) : <EOL> raise ValueError ( '<STR_LIT>' % self . title ) <EOL> def validate ( self ) : <EOL> pass <EOL> @ classmethod <EOL> def from_dict ( cls , title , dict ) : <EOL> obj = cls ( title ) <EOL> obj . properties . update ( dict ) <EOL> return obj <EOL> def JSONrepr ( self ) : <EOL> for k , ( _ , required ) in self . props . items ( ) : <EOL> if required and k not in self . properties : <EOL> rtype = getattr ( self , '<STR_LIT>' , "<STR_LIT>" ) <EOL> raise ValueError ( <EOL> "<STR_LIT>" % ( k , rtype ) ) <EOL> self . validate ( ) <EOL> if self . properties : <EOL> return self . resource <EOL> elif hasattr ( self , '<STR_LIT>' ) : <EOL> d = { } <EOL> for k , v in self . resource . items ( ) : <EOL> if k != '<STR_LIT>' : <EOL> d [ k ] = v <EOL> return d <EOL> else : <EOL> return { } <EOL> class AWSObject ( BaseAWSObject ) : <EOL> dictname = '<STR_LIT>' <EOL> class AWSDeclaration ( BaseAWSObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , title , ** kwargs ) : <EOL> super ( AWSDeclaration , self ) . __init__ ( title , ** kwargs ) <EOL> class AWSProperty ( BaseAWSObject ) : <EOL> """<STR_LIT>""" <EOL> dictname = None <EOL> def __init__ ( self , title = None , ** kwargs ) : <EOL> super ( AWSProperty , self ) . __init__ ( title , ** kwargs ) <EOL> class AWSAttribute ( BaseAWSObject ) : <EOL> dictname = None <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , title = None , ** kwargs ) : <EOL> super ( AWSAttribute , self ) . __init__ ( title , ** kwargs ) <EOL> def validate_pausetime ( pausetime ) : <EOL> if not pausetime . startswith ( '<STR_LIT>' ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return pausetime <EOL> class UpdatePolicy ( BaseAWSObject ) : <EOL> def __init__ ( self , title , ** kwargs ) : <EOL> raise DeprecationWarning ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> class AWSHelperFn ( object ) : <EOL> def getdata ( self , data ) : <EOL> if isinstance ( data , BaseAWSObject ) : <EOL> return data . title <EOL> else : <EOL> return data <EOL> class GenericHelperFn ( AWSHelperFn ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data ) : <EOL> self . data = self . getdata ( data ) <EOL> def JSONrepr ( self ) : <EOL> return self . data <EOL> class Base64 ( AWSHelperFn ) : <EOL> def __init__ ( self , data ) : <EOL> self . data = { '<STR_LIT>' : data } <EOL> def JSONrepr ( self ) : <EOL> return self . data <EOL> class FindInMap ( AWSHelperFn ) : <EOL> def __init__ ( self , mapname , key , value ) : <EOL> self . data = { '<STR_LIT>' : [ self . getdata ( mapname ) , key , value ] } <EOL> def JSONrepr ( self ) : <EOL> return self . data <EOL> class GetAtt ( AWSHelperFn ) : <EOL> def __init__ ( self , logicalName , attrName ) : <EOL> self . data = { '<STR_LIT>' : [ self . getdata ( logicalName ) , attrName ] } <EOL> def JSONrepr ( self ) : <EOL> return self . data <EOL> class GetAZs ( AWSHelperFn ) : <EOL> def __init__ ( self , region = "<STR_LIT>" ) : <EOL> self . data = { '<STR_LIT>' : region } <EOL> def JSONrepr ( self ) : <EOL> return self . data <EOL> class If ( AWSHelperFn ) : <EOL> def __init__ ( self , cond , true , false ) : <EOL> self . data = { '<STR_LIT>' : [ self . getdata ( cond ) , true , false ] } <EOL> def JSONrepr ( self ) : <EOL> return self . data <EOL> class Equals ( AWSHelperFn ) : <EOL> def __init__ ( self , value_one , value_two ) : <EOL> self . data = { '<STR_LIT>' : [ value_one , value_two ] } <EOL> def JSONrepr ( self ) : <EOL> return self . data <EOL> class And ( AWSHelperFn ) : <EOL> def __init__ ( self , cond_one , cond_two , * conds ) : <EOL> self . data = { '<STR_LIT>' : [ cond_one , cond_two ] + list ( conds ) } <EOL> def JSONrepr ( self ) : <EOL> return self . data <EOL> class Or ( AWSHelperFn ) : <EOL> def __init__ ( self , cond_one , cond_two , * conds ) : <EOL> self . data = { '<STR_LIT>' : [ cond_one , cond_two ] + list ( conds ) } <EOL> def JSONrepr ( self ) : <EOL> return self . data <EOL> class Not ( AWSHelperFn ) : <EOL> def __init__ ( self , cond ) : <EOL> self . data = { '<STR_LIT>' : [ self . getdata ( cond ) ] } <EOL> def JSONrepr ( self ) : <EOL> return self . data <EOL> class Join ( AWSHelperFn ) : <EOL> def __init__ ( self , delimiter , values ) : <EOL> self . data = { '<STR_LIT>' : [ delimiter , values ] } <EOL> def JSONrepr ( self ) : <EOL> return self . data <EOL> class Name ( AWSHelperFn ) : <EOL> def __init__ ( self , data ) : <EOL> self . data = self . getdata ( data ) <EOL> def JSONrepr ( self ) : <EOL> return self . data <EOL> class Select ( AWSHelperFn ) : <EOL> def __init__ ( self , indx , objects ) : <EOL> self . data = { '<STR_LIT>' : [ indx , objects ] } <EOL> def JSONrepr ( self ) : <EOL> return self . data <EOL> class Ref ( AWSHelperFn ) : <EOL> def __init__ ( self , data ) : <EOL> self . data = { '<STR_LIT>' : self . getdata ( data ) } <EOL> def JSONrepr ( self ) : <EOL> return self . data <EOL> class Condition ( AWSHelperFn ) : <EOL> def __init__ ( self , data ) : <EOL> self . data = { '<STR_LIT>' : self . getdata ( data ) } <EOL> def JSONrepr ( self ) : <EOL> return self . data <EOL> class awsencode ( json . JSONEncoder ) : <EOL> def default ( self , obj ) : <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> return obj . JSONrepr ( ) <EOL> return json . JSONEncoder . default ( self , obj ) <EOL> class Tags ( AWSHelperFn ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . tags = [ ] <EOL> for k , v in sorted ( kwargs . iteritems ( ) ) : <EOL> self . tags . append ( { <EOL> '<STR_LIT>' : k , <EOL> '<STR_LIT>' : v , <EOL> } ) <EOL> def __add__ ( self , newtags ) : <EOL> newtags . tags = self . tags + newtags . tags <EOL> return newtags <EOL> def JSONrepr ( self ) : <EOL> return self . tags <EOL> class Template ( object ) : <EOL> props = { <EOL> '<STR_LIT>' : ( basestring , False ) , <EOL> '<STR_LIT>' : ( basestring , False ) , <EOL> '<STR_LIT>' : ( dict , False ) , <EOL> '<STR_LIT>' : ( dict , False ) , <EOL> '<STR_LIT>' : ( dict , False ) , <EOL> '<STR_LIT>' : ( dict , False ) , <EOL> } <EOL> def __init__ ( self ) : <EOL> self . description = None <EOL> self . metadata = { } <EOL> self . conditions = { } <EOL> self . mappings = { } <EOL> self . outputs = { } <EOL> self . parameters = { } <EOL> self . resources = { } <EOL> self . version = None <EOL> def add_description ( self , description ) : <EOL> self . description = description <EOL> def add_metadata ( self , metadata ) : <EOL> self . metadata = metadata <EOL> def add_condition ( self , name , condition ) : <EOL> self . conditions [ name ] = condition <EOL> def handle_duplicate_key ( self , key ) : <EOL> raise ValueError ( '<STR_LIT>' % key ) <EOL> def _update ( self , d , values ) : <EOL> if isinstance ( values , list ) : <EOL> for v in values : <EOL> if v . title in d : <EOL> self . handle_duplicate_key ( v . title ) <EOL> d [ v . title ] = v <EOL> else : <EOL> if values . title in d : <EOL> self . handle_duplicate_key ( values . title ) <EOL> d [ values . title ] = values <EOL> return values <EOL> def add_output ( self , output ) : <EOL> return self . _update ( self . outputs , output ) <EOL> def add_mapping ( self , name , mapping ) : <EOL> self . mappings [ name ] = mapping <EOL> def add_parameter ( self , parameter ) : <EOL> return self . _update ( self . parameters , parameter ) <EOL> def add_resource ( self , resource ) : <EOL> return self . _update ( self . resources , resource ) <EOL> def add_version ( self , version = None ) : <EOL> if version : <EOL> self . version = version <EOL> else : <EOL> self . version = "<STR_LIT>" <EOL> def to_json ( self , indent = <NUM_LIT:4> , sort_keys = True , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) ) : <EOL> t = { } <EOL> if self . description : <EOL> t [ '<STR_LIT>' ] = self . description <EOL> if self . metadata : <EOL> t [ '<STR_LIT>' ] = self . metadata <EOL> if self . conditions : <EOL> t [ '<STR_LIT>' ] = self . conditions <EOL> if self . mappings : <EOL> t [ '<STR_LIT>' ] = self . mappings <EOL> if self . outputs : <EOL> t [ '<STR_LIT>' ] = self . outputs <EOL> if self . parameters : <EOL> t [ '<STR_LIT>' ] = self . parameters <EOL> if self . version : <EOL> t [ '<STR_LIT>' ] = self . version <EOL> t [ '<STR_LIT>' ] = self . resources <EOL> return json . dumps ( t , cls = awsencode , indent = indent , <EOL> sort_keys = sort_keys , separators = separators ) <EOL> def JSONrepr ( self ) : <EOL> return [ self . parameters , self . mappings , self . resources ] <EOL> class Output ( AWSDeclaration ) : <EOL> props = { <EOL> '<STR_LIT>' : ( basestring , False ) , <EOL> '<STR_LIT>' : ( basestring , True ) , <EOL> } <EOL> class Parameter ( AWSDeclaration ) : <EOL> STRING_PROPERTIES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> NUMBER_PROPERTIES = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> props = { <EOL> '<STR_LIT>' : ( basestring , True ) , <EOL> '<STR_LIT>' : ( basestring , False ) , <EOL> '<STR_LIT>' : ( bool , False ) , <EOL> '<STR_LIT>' : ( list , False ) , <EOL> '<STR_LIT>' : ( basestring , False ) , <EOL> '<STR_LIT>' : ( validators . positive_integer , False ) , <EOL> '<STR_LIT>' : ( validators . positive_integer , False ) , <EOL> '<STR_LIT>' : ( validators . integer , False ) , <EOL> '<STR_LIT>' : ( validators . integer , False ) , <EOL> '<STR_LIT>' : ( basestring , False ) , <EOL> '<STR_LIT>' : ( basestring , False ) , <EOL> } <EOL> def validate ( self ) : <EOL> if self . properties [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> for p in self . STRING_PROPERTIES : <EOL> if p in self . properties : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % p ) <EOL> if self . properties [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> for p in self . NUMBER_PROPERTIES : <EOL> if p in self . properties : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % p ) </s>
