<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import copy <EOL> import threading <EOL> import collections <EOL> from contextlib import contextmanager <EOL> import salt . ext . six as six <EOL> @ contextmanager <EOL> def func_globals_inject ( func , ** overrides ) : <EOL> '''<STR_LIT>''' <EOL> if hasattr ( func , '<STR_LIT>' ) : <EOL> func = func . __func__ <EOL> func_globals = func . __globals__ <EOL> injected_func_globals = [ ] <EOL> overridden_func_globals = { } <EOL> for override in overrides : <EOL> if override in func_globals : <EOL> overridden_func_globals [ override ] = func_globals [ override ] <EOL> else : <EOL> injected_func_globals . append ( override ) <EOL> func_globals . update ( overrides ) <EOL> yield <EOL> func_globals . update ( overridden_func_globals ) <EOL> for injected in injected_func_globals : <EOL> del func_globals [ injected ] <EOL> class ContextDict ( collections . MutableMapping ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , ** data ) : <EOL> self . _state = threading . local ( ) <EOL> self . _state . data = None <EOL> self . global_data = { } <EOL> @ property <EOL> def active ( self ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return self . _state . data is not None <EOL> except AttributeError : <EOL> return False <EOL> def clone ( self , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> child = ChildContextDict ( parent = self , overrides = kwargs ) <EOL> return child <EOL> def __setitem__ ( self , key , val ) : <EOL> if self . active : <EOL> self . _state . data [ key ] = val <EOL> else : <EOL> self . global_data [ key ] = val <EOL> def __delitem__ ( self , key ) : <EOL> if self . active : <EOL> del self . _state . data [ key ] <EOL> else : <EOL> del self . global_data [ key ] <EOL> def __getitem__ ( self , key ) : <EOL> if self . active : <EOL> return self . _state . data [ key ] <EOL> else : <EOL> return self . global_data [ key ] <EOL> def __len__ ( self ) : <EOL> if self . active : <EOL> return len ( self . _state . data ) <EOL> else : <EOL> return len ( self . global_data ) <EOL> def __iter__ ( self ) : <EOL> if self . active : <EOL> return iter ( self . _state . data ) <EOL> else : <EOL> return iter ( self . global_data ) <EOL> class ChildContextDict ( collections . MutableMapping ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , parent , overrides = None ) : <EOL> self . parent = parent <EOL> self . _data = { } if overrides is None else overrides <EOL> for k , v in six . iteritems ( self . parent . global_data ) : <EOL> if k not in self . _data : <EOL> self . _data [ k ] = copy . deepcopy ( v ) <EOL> def __setitem__ ( self , key , val ) : <EOL> self . _data [ key ] = val <EOL> def __delitem__ ( self , key ) : <EOL> del self . _data [ key ] <EOL> def __getitem__ ( self , key ) : <EOL> return self . _data [ key ] <EOL> def __len__ ( self ) : <EOL> return len ( self . _data ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _data ) <EOL> def __enter__ ( self ) : <EOL> self . parent . _state . data = self . _data <EOL> def __exit__ ( self , * exc ) : <EOL> self . parent . _state . data = None <EOL> class NamespacedDictWrapper ( collections . MutableMapping , dict ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , d , pre_keys ) : <EOL> self . __dict = d <EOL> if isinstance ( pre_keys , six . string_types ) : <EOL> self . pre_keys = ( pre_keys , ) <EOL> else : <EOL> self . pre_keys = pre_keys <EOL> def _dict ( self ) : <EOL> r = self . __dict <EOL> for k in self . pre_keys : <EOL> r = r [ k ] <EOL> return r <EOL> def __setitem__ ( self , key , val ) : <EOL> self . _dict ( ) [ key ] = val <EOL> def __delitem__ ( self , key ) : <EOL> del self . _dict ( ) [ key ] <EOL> def __getitem__ ( self , key ) : <EOL> return self . _dict ( ) [ key ] <EOL> def __len__ ( self ) : <EOL> return len ( self . _dict ( ) ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _dict ( ) ) <EOL> def __copy__ ( self ) : <EOL> return type ( self ) ( copy . copy ( self . __dict ) , <EOL> copy . copy ( self . pre_keys ) ) <EOL> def __deepcopy__ ( self , memo ) : <EOL> return type ( self ) ( copy . deepcopy ( self . __dict , memo ) , <EOL> copy . deepcopy ( self . pre_keys , memo ) ) </s>
