<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import inspect <EOL> import logging <EOL> from salt . utils . odict import OrderedDict <EOL> import salt . ext . six as six <EOL> REQUISITES = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> log = logging . getLogger ( __name__ ) <EOL> class StateException ( Exception ) : <EOL> pass <EOL> class DuplicateState ( StateException ) : <EOL> pass <EOL> class InvalidFunction ( StateException ) : <EOL> pass <EOL> class Registry ( object ) : <EOL> '''<STR_LIT>''' <EOL> states = OrderedDict ( ) <EOL> requisites = [ ] <EOL> includes = [ ] <EOL> extends = OrderedDict ( ) <EOL> enabled = True <EOL> @ classmethod <EOL> def empty ( cls ) : <EOL> cls . states = OrderedDict ( ) <EOL> cls . requisites = [ ] <EOL> cls . includes = [ ] <EOL> cls . extends = OrderedDict ( ) <EOL> @ classmethod <EOL> def include ( cls , * args ) : <EOL> if not cls . enabled : <EOL> return <EOL> cls . includes += args <EOL> @ classmethod <EOL> def salt_data ( cls ) : <EOL> states = OrderedDict ( [ <EOL> ( id_ , states_ ) <EOL> for id_ , states_ in six . iteritems ( cls . states ) <EOL> ] ) <EOL> if cls . includes : <EOL> states [ '<STR_LIT>' ] = cls . includes <EOL> if cls . extends : <EOL> states [ '<STR_LIT>' ] = OrderedDict ( [ <EOL> ( id_ , states_ ) <EOL> for id_ , states_ in six . iteritems ( cls . extends ) <EOL> ] ) <EOL> cls . empty ( ) <EOL> return states <EOL> @ classmethod <EOL> def add ( cls , id_ , state , extend = False ) : <EOL> if not cls . enabled : <EOL> return <EOL> if extend : <EOL> attr = cls . extends <EOL> else : <EOL> attr = cls . states <EOL> if id_ in attr : <EOL> if state . full_func in attr [ id_ ] : <EOL> raise DuplicateState ( <EOL> "<STR_LIT>" . format ( <EOL> id_ , <EOL> state . full_func <EOL> ) <EOL> ) <EOL> else : <EOL> attr [ id_ ] = OrderedDict ( ) <EOL> if len ( cls . requisites ) > <NUM_LIT:0> : <EOL> for req in cls . requisites : <EOL> if req . requisite not in state . kwargs : <EOL> state . kwargs [ req . requisite ] = [ ] <EOL> state . kwargs [ req . requisite ] . append ( req ( ) ) <EOL> attr [ id_ ] . update ( state ( ) ) <EOL> @ classmethod <EOL> def extend ( cls , id_ , state ) : <EOL> cls . add ( id_ , state , extend = True ) <EOL> @ classmethod <EOL> def make_extend ( cls , name ) : <EOL> return StateExtend ( name ) <EOL> @ classmethod <EOL> def push_requisite ( cls , requisite ) : <EOL> if not cls . enabled : <EOL> return <EOL> cls . requisites . append ( requisite ) <EOL> @ classmethod <EOL> def pop_requisite ( cls ) : <EOL> if not cls . enabled : <EOL> return <EOL> del cls . requisites [ - <NUM_LIT:1> ] <EOL> class StateExtend ( object ) : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> class StateRequisite ( object ) : <EOL> def __init__ ( self , requisite , module , id_ ) : <EOL> self . requisite = requisite <EOL> self . module = module <EOL> self . id_ = id_ <EOL> def __call__ ( self ) : <EOL> return { self . module : self . id_ } <EOL> def __enter__ ( self ) : <EOL> Registry . push_requisite ( self ) <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> Registry . pop_requisite ( ) <EOL> class StateFactory ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , module , valid_funcs = None ) : <EOL> self . module = module <EOL> if valid_funcs is None : <EOL> valid_funcs = [ ] <EOL> self . valid_funcs = valid_funcs <EOL> def __getattr__ ( self , func ) : <EOL> if len ( self . valid_funcs ) > <NUM_LIT:0> and func not in self . valid_funcs : <EOL> raise InvalidFunction ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> func , <EOL> self . module <EOL> ) ) <EOL> def make_state ( id_ , ** kwargs ) : <EOL> return State ( <EOL> id_ , <EOL> self . module , <EOL> func , <EOL> ** kwargs <EOL> ) <EOL> return make_state <EOL> def __call__ ( self , id_ , requisite = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> return StateRequisite ( requisite , self . module , id_ ) <EOL> class State ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , id_ , module , func , ** kwargs ) : <EOL> self . id_ = id_ <EOL> self . module = module <EOL> self . func = func <EOL> for attr in REQUISITES : <EOL> if attr in kwargs : <EOL> try : <EOL> iter ( kwargs [ attr ] ) <EOL> except TypeError : <EOL> kwargs [ attr ] = [ kwargs [ attr ] ] <EOL> self . kwargs = kwargs <EOL> if isinstance ( self . id_ , StateExtend ) : <EOL> Registry . extend ( self . id_ . name , self ) <EOL> self . id_ = self . id_ . name <EOL> else : <EOL> Registry . add ( self . id_ , self ) <EOL> self . requisite = StateRequisite ( '<STR_LIT>' , self . module , self . id_ ) <EOL> @ property <EOL> def attrs ( self ) : <EOL> kwargs = self . kwargs <EOL> for attr in REQUISITES : <EOL> if attr in kwargs : <EOL> kwargs [ attr ] = [ <EOL> req ( ) if isinstance ( req , StateRequisite ) else req <EOL> for req in kwargs [ attr ] <EOL> ] <EOL> return [ <EOL> { k : kwargs [ k ] } <EOL> for k in sorted ( six . iterkeys ( kwargs ) ) <EOL> ] <EOL> @ property <EOL> def full_func ( self ) : <EOL> return "<STR_LIT>" . format ( self . module , self . func ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . id_ , self . full_func , self . attrs ) <EOL> def __call__ ( self ) : <EOL> return { <EOL> self . full_func : self . attrs <EOL> } <EOL> def __enter__ ( self ) : <EOL> Registry . push_requisite ( self . requisite ) <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> Registry . pop_requisite ( ) <EOL> class SaltObject ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , salt ) : <EOL> self . _salt = salt <EOL> def __getattr__ ( self , mod ) : <EOL> class __wrapper__ ( object ) : <EOL> def __getattr__ ( wself , func ) : <EOL> try : <EOL> return self . _salt [ '<STR_LIT>' . format ( mod , func ) ] <EOL> except KeyError : <EOL> raise AttributeError <EOL> return __wrapper__ ( ) <EOL> class MapMeta ( type ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( cls , name , bases , nmspc ) : <EOL> cls . __set_attributes__ ( ) <EOL> super ( MapMeta , cls ) . __init__ ( name , bases , nmspc ) <EOL> def __set_attributes__ ( cls ) : <EOL> match_groups = OrderedDict ( [ ] ) <EOL> for item in cls . __dict__ : <EOL> if item [ <NUM_LIT:0> ] == '<STR_LIT:_>' : <EOL> continue <EOL> filt = cls . __dict__ [ item ] <EOL> if not inspect . isclass ( filt ) : <EOL> continue <EOL> grain = getattr ( filt , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if grain not in match_groups : <EOL> match_groups [ grain ] = OrderedDict ( [ ] ) <EOL> if hasattr ( filt , '<STR_LIT>' ) : <EOL> match = filt . __match__ <EOL> else : <EOL> match = item <EOL> match_groups [ grain ] [ match ] = OrderedDict ( [ ] ) <EOL> for name in filt . __dict__ : <EOL> if name [ <NUM_LIT:0> ] == '<STR_LIT:_>' : <EOL> continue <EOL> match_groups [ grain ] [ match ] [ name ] = filt . __dict__ [ name ] <EOL> attrs = { } <EOL> for grain in match_groups : <EOL> filtered = Map . __salt__ [ '<STR_LIT>' ] ( match_groups [ grain ] , <EOL> grain = grain ) <EOL> if filtered : <EOL> attrs . update ( filtered ) <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> pillar = Map . __salt__ [ '<STR_LIT>' ] ( cls . merge ) <EOL> if pillar : <EOL> attrs . update ( pillar ) <EOL> for name in attrs : <EOL> setattr ( cls , name , attrs [ name ] ) <EOL> def need_salt ( * a , ** k ) : <EOL> log . error ( "<STR_LIT>" ) <EOL> return { } <EOL> class Map ( six . with_metaclass ( MapMeta , object ) ) : <EOL> __salt__ = { <EOL> '<STR_LIT>' : need_salt , <EOL> '<STR_LIT>' : need_salt <EOL> } </s>
