<s> import inspect <EOL> class VIProperty ( object ) : <EOL> def __init__ ( self , server , obj ) : <EOL> self . _server = server <EOL> self . _obj = obj <EOL> self . _values_set = False <EOL> self . _type = obj . typecode . type [ <NUM_LIT:1> ] <EOL> def _flush_cache ( self ) : <EOL> if not self . _values_set : <EOL> return <EOL> for name in self . _values . iterkeys ( ) : <EOL> try : <EOL> delattr ( self , name ) <EOL> except AttributeError : <EOL> pass <EOL> self . _values_set = False <EOL> def _get_all ( self ) : <EOL> if self . _type == '<STR_LIT>' : <EOL> oc = self . _server . _get_object_properties ( self . _obj , get_all = True ) <EOL> ps = oc . get_element_propSet ( ) <EOL> self . _values = dict ( [ ( i . Name , i . Val ) for i in ps ] ) <EOL> else : <EOL> methods = getmembers ( self . _obj , predicate = inspect . ismethod ) <EOL> self . _values = { } <EOL> for name , method in methods : <EOL> try : <EOL> if name . startswith ( "<STR_LIT>" ) : <EOL> self . _values [ name [ <NUM_LIT:12> : ] ] = method ( ) <EOL> except AttributeError : <EOL> continue <EOL> self . _values_set = True <EOL> def __getattr__ ( self , name ) : <EOL> if not self . _values_set : <EOL> self . _get_all ( ) <EOL> if not name in self . _values : <EOL> raise AttributeError ( "<STR_LIT>" % name ) <EOL> ret = self . _get_prop_value ( self . _values [ name ] ) <EOL> setattr ( self , name , ret ) <EOL> return ret <EOL> def _get_prop_value ( self , prop ) : <EOL> basic_types = ( bool , int , float , basestring , tuple , long ) <EOL> class_name = prop . __class__ . __name__ <EOL> if class_name == "<STR_LIT>" and hasattr ( prop , "<STR_LIT>" ) : <EOL> return VIProperty ( self . _server , prop ) <EOL> if isinstance ( prop , basic_types ) : <EOL> return prop <EOL> if isinstance ( prop , list ) : <EOL> ret = [ ] <EOL> for i in prop : <EOL> ret . append ( self . _get_prop_value ( i ) ) <EOL> return ret <EOL> if class_name == "<STR_LIT>" : <EOL> return VIProperty ( self . _server , prop ) <EOL> if class_name . startswith ( "<STR_LIT>" ) and class_name . endswith ( "<STR_LIT>" ) : <EOL> inner_prop = class_name [ <NUM_LIT:7> : - <NUM_LIT:7> ] <EOL> ret = [ ] <EOL> for i in getattr ( prop , "<STR_LIT>" + inner_prop ) ( ) : <EOL> ret . append ( self . _get_prop_value ( i ) ) <EOL> return ret <EOL> else : <EOL> return prop <EOL> def getmembers ( obj , predicate = None ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> for key in dir ( obj ) : <EOL> try : <EOL> value = getattr ( obj , key ) <EOL> except AttributeError : <EOL> continue <EOL> if not predicate or predicate ( value ) : <EOL> results . append ( ( key , value ) ) <EOL> results . sort ( ) <EOL> return results </s>
