<s> from __future__ import absolute_import <EOL> import logging <EOL> import collections <EOL> import salt . exceptions <EOL> log = logging . getLogger ( __name__ ) <EOL> def verify_fun ( lazy_obj , fun ) : <EOL> '''<STR_LIT>''' <EOL> if not fun : <EOL> raise salt . exceptions . SaltInvocationError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if fun not in lazy_obj : <EOL> raise salt . exceptions . CommandExecutionError ( lazy_obj . missing_fun_string ( fun ) ) <EOL> class LazyDict ( collections . MutableMapping ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . clear ( ) <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . _dict or not self . loaded ) <EOL> def __bool__ ( self ) : <EOL> return self . __nonzero__ ( ) <EOL> def clear ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _dict = { } <EOL> self . loaded = False <EOL> def _load ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplementedError ( ) <EOL> def _load_all ( self ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplementedError ( ) <EOL> def _missing ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> return False <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> '''<STR_LIT>''' <EOL> if self . _missing ( key ) : <EOL> raise KeyError ( key ) <EOL> if key not in self . _dict and not self . loaded : <EOL> if self . _load ( key ) : <EOL> log . debug ( '<STR_LIT>' . format ( key ) ) <EOL> return self . _dict [ key ] <EOL> else : <EOL> log . debug ( '<STR_LIT>' . format ( key ) ) <EOL> raise KeyError ( key ) <EOL> else : <EOL> return self . _dict [ key ] <EOL> def __len__ ( self ) : <EOL> if not self . loaded : <EOL> self . _load_all ( ) <EOL> return len ( self . _dict ) <EOL> def __iter__ ( self ) : <EOL> if not self . loaded : <EOL> self . _load_all ( ) <EOL> return iter ( self . _dict ) </s>
