<s> import time <EOL> import re <EOL> from httplib import NOT_FOUND , OK , CREATED , NO_CONTENT , CONFLICT , FORBIDDEN , UNAUTHORIZED <EOL> from twisted . python import log <EOL> import vcdm <EOL> from vcdm . server . cdmi . generic import get_parent <EOL> from vcdm . utils import check_path <EOL> from vcdm . authz import authorize <EOL> def read ( avatar , requested_path , on_behalf = None ) : <EOL> """<STR_LIT>""" <EOL> expected_fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> unique_request = re . match ( '<STR_LIT>' , requested_path ) <EOL> if unique_request is not None : <EOL> requested_uid = unique_request . group ( <NUM_LIT:0> ) . split ( '<STR_LIT:/>' ) [ <NUM_LIT:2> ] <EOL> uid , vals = vcdm . env [ '<STR_LIT>' ] . find_by_uid ( requested_uid , object_type = '<STR_LIT>' , <EOL> fields = expected_fields + [ '<STR_LIT>' ] ) <EOL> fullpath = vals [ '<STR_LIT>' ] <EOL> else : <EOL> fullpath = requested_path <EOL> uid , vals = vcdm . env [ '<STR_LIT>' ] . find_by_path ( fullpath , object_type = '<STR_LIT>' , <EOL> fields = expected_fields ) <EOL> if uid is None : <EOL> return ( NOT_FOUND , None ) <EOL> else : <EOL> acls = vals [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> if not authorize ( avatar , fullpath , '<STR_LIT>' , acls ) : <EOL> return ( UNAUTHORIZED , None ) <EOL> vals [ '<STR_LIT>' ] = uid <EOL> log . msg ( type = '<STR_LIT>' , avatar = avatar if not on_behalf else on_behalf , <EOL> amount = <NUM_LIT:1> , acc_type = '<STR_LIT>' ) <EOL> return ( OK , vals ) <EOL> def create_or_update ( avatar , name , container_path , fullpath , metadata = None , <EOL> on_behalf = None ) : <EOL> """<STR_LIT>""" <EOL> log . msg ( "<STR_LIT>" % fullpath ) <EOL> parent_container = get_parent ( fullpath ) <EOL> uid , vals = vcdm . env [ '<STR_LIT>' ] . find_by_path ( fullpath , object_type = '<STR_LIT>' , <EOL> fields = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> vals [ '<STR_LIT>' ] = uid <EOL> if uid is not None and parent_container != vals [ '<STR_LIT>' ] : <EOL> log . err ( "<STR_LIT>" % ( fullpath , vals [ '<STR_LIT>' ] ) ) <EOL> return ( FORBIDDEN , vals ) <EOL> if not check_path ( container_path ) : <EOL> log . err ( "<STR_LIT>" % <EOL> '<STR_LIT:/>' . join ( container_path ) ) <EOL> return ( FORBIDDEN , vals ) <EOL> _ , cvals = vcdm . env [ '<STR_LIT>' ] . find_by_path ( parent_container , <EOL> object_type = '<STR_LIT>' , <EOL> fields = [ '<STR_LIT>' ] ) <EOL> acl = cvals [ '<STR_LIT>' ] . get ( '<STR_LIT>' , { } ) <EOL> if not authorize ( avatar , parent_container , "<STR_LIT>" , acl ) : <EOL> return ( UNAUTHORIZED , vals ) <EOL> if avatar is None : <EOL> avatar = '<STR_LIT>' <EOL> container_acl = metadata . get ( '<STR_LIT>' ) <EOL> if container_acl is None : <EOL> metadata [ '<STR_LIT>' ] = acl <EOL> metadata [ '<STR_LIT>' ] . update ( { avatar : '<STR_LIT>' } ) <EOL> if uid is None : <EOL> uid = vcdm . env [ '<STR_LIT>' ] . write ( { <EOL> '<STR_LIT:object>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : metadata , <EOL> '<STR_LIT>' : avatar , <EOL> '<STR_LIT>' : fullpath , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : parent_container , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : str ( time . time ( ) ) , <EOL> '<STR_LIT>' : str ( time . time ( ) ) } , <EOL> uid ) <EOL> vals [ '<STR_LIT>' ] = uid <EOL> if fullpath != '<STR_LIT:/>' : <EOL> _append_child ( parent_container , uid , name + "<STR_LIT:/>" ) <EOL> log . msg ( type = '<STR_LIT>' , avatar = avatar if not on_behalf else on_behalf , <EOL> amount = <NUM_LIT:1> , acc_type = '<STR_LIT>' ) <EOL> return ( CREATED , vals ) <EOL> else : <EOL> if vals . get ( '<STR_LIT>' ) is not None and vals . get ( '<STR_LIT>' ) != avatar : <EOL> return ( UNAUTHORIZED , vals ) <EOL> uid = vcdm . env [ '<STR_LIT>' ] . write ( { <EOL> '<STR_LIT>' : metadata , <EOL> '<STR_LIT>' : str ( time . time ( ) ) } , <EOL> uid ) <EOL> log . msg ( type = '<STR_LIT>' , avatar = avatar if not on_behalf else on_behalf , <EOL> amount = <NUM_LIT:1> , acc_type = '<STR_LIT>' ) <EOL> return ( NO_CONTENT , vals ) <EOL> def delete ( avatar , fullpath , on_behalf = None ) : <EOL> """<STR_LIT>""" <EOL> log . msg ( "<STR_LIT>" % fullpath ) <EOL> uid , vals = vcdm . env [ '<STR_LIT>' ] . find_by_path ( fullpath , object_type = '<STR_LIT>' , <EOL> fields = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> if uid is None : <EOL> return NOT_FOUND <EOL> else : <EOL> acls = vals [ '<STR_LIT>' ] . get ( '<STR_LIT>' , None ) <EOL> if not authorize ( avatar , fullpath , "<STR_LIT>" , acls ) : <EOL> log . err ( "<STR_LIT>" ) <EOL> return UNAUTHORIZED <EOL> if len ( vals [ '<STR_LIT>' ] ) != <NUM_LIT:0> : <EOL> log . err ( "<STR_LIT>" <EOL> % ( fullpath , vals [ '<STR_LIT>' ] ) ) <EOL> return CONFLICT <EOL> vcdm . env [ '<STR_LIT>' ] . delete ( uid ) <EOL> if fullpath != '<STR_LIT:/>' : <EOL> _remove_child ( vals [ '<STR_LIT>' ] , uid ) <EOL> log . msg ( type = '<STR_LIT>' , avatar = avatar if not on_behalf else on_behalf , <EOL> amount = <NUM_LIT:1> , acc_type = '<STR_LIT>' ) <EOL> return NO_CONTENT <EOL> def _append_child ( container_path , child_uid , child_name ) : <EOL> log . msg ( "<STR_LIT>" % ( child_uid , child_name , <EOL> container_path ) ) <EOL> cuid , cvals = vcdm . env [ '<STR_LIT>' ] . find_by_path ( container_path , <EOL> object_type = '<STR_LIT>' , <EOL> fields = [ '<STR_LIT>' ] ) <EOL> cvals [ '<STR_LIT>' ] [ unicode ( child_uid ) ] = unicode ( child_name ) <EOL> vcdm . env [ '<STR_LIT>' ] . write ( { <EOL> '<STR_LIT>' : cvals [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : str ( time . time ( ) ) } , <EOL> cuid ) <EOL> def _remove_child ( container_path , child_uid ) : <EOL> log . msg ( "<STR_LIT>" % <EOL> ( child_uid , container_path ) ) <EOL> cuid , cvals = vcdm . env [ '<STR_LIT>' ] . find_by_path ( container_path , <EOL> object_type = '<STR_LIT>' , <EOL> fields = [ '<STR_LIT>' ] ) <EOL> del cvals [ '<STR_LIT>' ] [ child_uid ] <EOL> vcdm . env [ '<STR_LIT>' ] . write ( { <EOL> '<STR_LIT>' : cvals [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : str ( time . time ( ) ) } , <EOL> cuid ) </s>
