<s> try : <EOL> from xml . etree import ElementTree <EOL> except ImportError : <EOL> from elementtree import ElementTree <EOL> from client import * <EOL> from base_asset import BaseAsset <EOL> from cache_decorator import memoized <EOL> from special_class_methods import special_classes <EOL> from none_deref import NoneDeref <EOL> from string_utils import split_attribute <EOL> class V1Meta ( object ) : <EOL> def __init__ ( self , * args , ** kw ) : <EOL> self . server = V1Server ( * args , ** kw ) <EOL> self . global_cache = { } <EOL> self . dirtylist = [ ] <EOL> def __getattr__ ( self , attr ) : <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> return self . asset_class ( attr ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , * args , ** kw ) : <EOL> self . commit ( ) <EOL> @ memoized <EOL> def asset_class ( self , asset_type_name ) : <EOL> xmldata = self . server . get_meta_xml ( asset_type_name ) <EOL> class_members = { <EOL> '<STR_LIT>' : self , <EOL> '<STR_LIT>' : asset_type_name , <EOL> } <EOL> for operation in xmldata . findall ( '<STR_LIT>' ) : <EOL> opname = operation . get ( '<STR_LIT:name>' ) <EOL> def operation_func ( myself , opname2 = opname ) : <EOL> myself . _v1_execute_operation ( opname2 ) <EOL> class_members [ opname ] = operation_func <EOL> for attribute in xmldata . findall ( '<STR_LIT>' ) : <EOL> attr = attribute . get ( "<STR_LIT:name>" ) <EOL> if attribute . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> if attribute . get ( '<STR_LIT>' ) == '<STR_LIT:True>' : <EOL> def getter ( self , attr = attr ) : <EOL> return self . _v1_getattr ( attr ) <EOL> def setter ( self , value , attr = attr ) : <EOL> return self . _v1_setattr ( attr , list ( value ) ) <EOL> def deleter ( self , attr = attr ) : <EOL> raise NotImplementedError <EOL> else : <EOL> def getter ( self , attr = attr ) : <EOL> v = self . _v1_getattr ( attr ) <EOL> if v : <EOL> return self . _v1_getattr ( attr ) [ <NUM_LIT:0> ] <EOL> else : <EOL> return NoneDeref ( ) <EOL> def setter ( self , value , attr = attr ) : <EOL> return self . _v1_setattr ( attr , value ) <EOL> def deleter ( self , attr = attr ) : <EOL> raise NotImplementedError <EOL> else : <EOL> def getter ( self , attr = attr ) : <EOL> return self . _v1_getattr ( attr ) <EOL> def setter ( self , value , attr = attr ) : <EOL> return self . _v1_setattr ( attr , value ) <EOL> def deleter ( self , attr = attr ) : <EOL> raise NotImplementedError <EOL> class_members [ attr ] = property ( getter , setter , deleter ) <EOL> bases = [ BaseAsset , ] <EOL> if asset_type_name in special_classes : <EOL> mixin = special_classes [ asset_type_name ] <EOL> bases . append ( mixin ) <EOL> new_asset_class = type ( asset_type_name , tuple ( bases ) , class_members ) <EOL> return new_asset_class <EOL> def add_to_dirty_list ( self , asset_instance ) : <EOL> self . dirtylist . append ( asset_instance ) <EOL> def commit ( self ) : <EOL> errors = [ ] <EOL> for asset in self . dirtylist : <EOL> try : <EOL> asset . _v1_commit ( ) <EOL> except V1Error , e : <EOL> errors . append ( e ) <EOL> self . dirtylist = [ ] <EOL> return errors <EOL> def generate_update_doc ( self , newdata ) : <EOL> update_doc = Element ( '<STR_LIT>' ) <EOL> for attrname , newvalue in newdata . items ( ) : <EOL> if newvalue is None : <EOL> node = Element ( '<STR_LIT>' ) <EOL> node . set ( '<STR_LIT:name>' , attrname ) <EOL> node . set ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> elif isinstance ( newvalue , BaseAsset ) : <EOL> node = Element ( '<STR_LIT>' ) <EOL> node . set ( '<STR_LIT:name>' , attrname ) <EOL> node . set ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ra = Element ( '<STR_LIT>' ) <EOL> ra . set ( '<STR_LIT>' , newvalue . idref ) <EOL> node . append ( ra ) <EOL> elif isinstance ( newvalue , list ) : <EOL> node = Element ( '<STR_LIT>' ) <EOL> node . set ( '<STR_LIT:name>' , attrname ) <EOL> for item in newvalue : <EOL> child = Element ( '<STR_LIT>' ) <EOL> child . set ( '<STR_LIT>' , item . idref ) <EOL> child . set ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> node . append ( child ) <EOL> else : <EOL> node = Element ( '<STR_LIT>' ) <EOL> node . set ( '<STR_LIT:name>' , attrname ) <EOL> node . set ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> node . text = str ( newvalue ) <EOL> update_doc . append ( node ) <EOL> return update_doc <EOL> def create_asset ( self , asset_type_name , newdata ) : <EOL> update_doc = self . generate_update_doc ( newdata ) <EOL> new_asset_xml = self . server . create_asset ( asset_type_name , update_doc ) <EOL> asset_type , asset_oid , asset_moment = new_asset_xml . get ( '<STR_LIT:id>' ) . split ( '<STR_LIT::>' ) <EOL> return self . asset_class ( asset_type ) ( asset_oid ) <EOL> def update_asset ( self , asset_type_name , asset_oid , newdata ) : <EOL> update_doc = self . generate_update_doc ( newdata ) <EOL> return self . server . update_asset ( asset_type_name , asset_oid , update_doc ) <EOL> def execute_operation ( self , asset_type_name , oid , opname ) : <EOL> return self . server . execute_operation ( asset_type_name , oid , opname ) <EOL> def get_attr ( self , asset_type_name , oid , attrname , moment = None ) : <EOL> xml = self . server . get_attr ( asset_type_name , oid , attrname , moment ) <EOL> dummy_asset = ElementTree . Element ( '<STR_LIT>' ) <EOL> dummy_asset . append ( xml ) <EOL> return self . unpack_asset ( dummy_asset ) [ attrname ] <EOL> def query ( self , asset_type_name , wherestring , selstring ) : <EOL> return self . server . get_query_xml ( asset_type_name , wherestring , selstring ) <EOL> def read_asset ( self , asset_type_name , asset_oid , moment = None ) : <EOL> xml = self . server . get_asset_xml ( asset_type_name , asset_oid , moment ) <EOL> return self . unpack_asset ( xml ) <EOL> def unpack_asset ( self , xml ) : <EOL> output = { } <EOL> self . unpack_asset_relations ( output , xml ) <EOL> self . unpack_asset_attributes ( output , xml ) <EOL> return output <EOL> def unpack_asset_attributes ( self , output , xml ) : <EOL> for attribute in xml . findall ( '<STR_LIT>' ) : <EOL> key = attribute . get ( '<STR_LIT:name>' ) <EOL> values = [ v . text for v in attribute . findall ( '<STR_LIT>' ) ] <EOL> if len ( values ) == <NUM_LIT:0> : <EOL> values = [ attribute . text ] <EOL> self . add_attribute_to_output ( output , key , values ) <EOL> def unpack_asset_relations ( self , output , xml ) : <EOL> for relation in sorted ( xml . findall ( '<STR_LIT>' ) , key = lambda x : x . get ( '<STR_LIT:name>' ) ) : <EOL> key = relation . get ( '<STR_LIT:name>' ) <EOL> related_asset_elements = relation . findall ( '<STR_LIT>' ) <EOL> rellist = [ ] <EOL> for value_element in related_asset_elements : <EOL> relation_idref = value_element . get ( '<STR_LIT>' ) <EOL> value = self . asset_from_oid ( relation_idref ) <EOL> rellist . append ( value ) <EOL> self . add_relation_to_output ( output , key , rellist ) <EOL> def add_relation_to_output ( self , output , relation , assets ) : <EOL> if self . is_attribute_qualified ( relation ) : <EOL> ( container , leaf ) = self . split_relation_to_container_and_leaf ( relation ) <EOL> asset = self . get_related_asset ( output , container ) <EOL> if asset : <EOL> asset . with_data ( { leaf : assets } ) <EOL> else : <EOL> output [ relation ] = assets <EOL> def add_attribute_to_output ( self , output , relation , values ) : <EOL> if self . is_attribute_qualified ( relation ) : <EOL> ( container , leaf ) = self . split_relation_to_container_and_leaf ( relation ) <EOL> for ( asset , value ) in zip ( self . get_related_assets ( output , container ) , values ) : <EOL> if hasattr ( asset , '<STR_LIT>' ) : <EOL> asset . with_data ( { leaf : value } ) <EOL> else : <EOL> output [ relation ] = value <EOL> else : <EOL> output [ relation ] = values [ <NUM_LIT:0> ] <EOL> def is_attribute_qualified ( self , relation ) : <EOL> parts = split_attribute ( relation ) <EOL> return len ( parts ) > <NUM_LIT:1> <EOL> def split_relation_to_container_and_leaf ( self , relation ) : <EOL> parts = split_attribute ( relation ) <EOL> return ( '<STR_LIT:.>' . join ( parts [ : - <NUM_LIT:1> ] ) , parts [ - <NUM_LIT:1> ] ) <EOL> def get_related_assets ( self , output , relation ) : <EOL> if self . is_attribute_qualified ( relation ) : <EOL> parts = split_attribute ( relation ) <EOL> assets = output [ parts [ <NUM_LIT:0> ] ] <EOL> for part in parts [ <NUM_LIT:1> : ] : <EOL> try : <EOL> asset = assets [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return [ ] <EOL> assets = asset . _v1_getattr ( part ) <EOL> return assets <EOL> else : <EOL> return output [ relation ] <EOL> def get_related_asset ( self , output , relation ) : <EOL> assets = self . get_related_assets ( output , relation ) <EOL> try : <EOL> return assets [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return None <EOL> def asset_from_oid ( self , oidtoken ) : <EOL> oid_parts = oidtoken . split ( "<STR_LIT::>" ) <EOL> ( asset_type , asset_id , moment ) = oid_parts if len ( oid_parts ) > <NUM_LIT:2> else ( oid_parts [ <NUM_LIT:0> ] , oid_parts [ <NUM_LIT:1> ] , None ) <EOL> AssetClass = self . asset_class ( asset_type ) <EOL> instance = AssetClass ( asset_id , moment ) <EOL> return instance <EOL> def set_attachment_blob ( self , attachment , data = None ) : <EOL> intid = attachment . intid if isinstance ( attachment , BaseAsset ) else attachment <EOL> return self . server . set_attachment_blob ( intid , data ) <EOL> get_attachment_blob = set_attachment_blob </s>
