Beispiel #1
0
 def testRemoves(self):
     store = vesper.app.createStore({
     "id": "hello", 
     "tags": [
       { "id": "tag1" }, 
       { "id": "tag2"}
     ]})
     #print 'store', store.query('{*}')        
     #utils.debugp( 'testremove', store.model.by_s)                
     #print 'hello', store.model.by_s.get('hello')
     
     #a non-null value will only remove that particular value
     store.remove({"id":"hello","tags":"@tag2"})
     #utils.debugp('hello', store.model.by_s.get('hello'))
     from vesper import pjson
     self.assertEquals(pjson.tojson(store.model.getStatements())['data'],
         [{"id": "@hello", 
            "tags": ["@tag1"]
         }])            
     self.assertEquals(store.query('{*}'), [{'id': '@hello', 'tags': ['@tag1']}])
     
     #null value will remove the property and any associated values
     store.remove({"id":"hello","tags":None})
     self.assertEquals(pjson.tojson(store.model.getStatements())['data'],
         [{"id": "@hello" }])
     self.assertEquals(store.query('{*}'), [{'id': '@hello'}])
     
     #strings are treated as resource ids and the entire object is removed
     store.remove(["@hello"])
     self.assertEquals(pjson.tojson(store.model.getStatements())['data'], [])
     self.assertEquals(store.query('{*}'), [])        
Beispiel #2
0
    def _runActions(self, trigger, morekw=None):
       actions = self.server.actions.get(trigger)
       if actions:
            state = self.state
            kw = state.kw.copy()            
            if morekw is None:
                morekw = {'_dbchanges' :
                  [ utils.attrdict({
              '_addedStatements' : dbstate.additions,
              '_removedStatements' : dbstate.removals,
              '_added' : pjson.tojson(dbstate.additions,
                **db.model_options.get('serializeOptions',{}).get('pjson',{})),
              '_removed' : pjson.tojson(dbstate.removals,
                **db.model_options.get('serializeOptions',{}).get('pjson',{})),
              '_newResources' : dbstate.newResources,
              '_db' : db
              })  for db, dbstate in self.state.dbstates.items() ]
                }

            kw.update(morekw)
            kw['_txninfo'] = self.state.info
            errorSequence= self.server.actions.get(trigger+'-error')
            self.server.callActions(actions, kw, state.retVal,
                    globalVars=morekw.keys(),
                    errorSequence=errorSequence)
Beispiel #3
0
 def handleRequest(id=None, method=0, params=0, jsonrpc=None):
     requestid = id; action = method; data = params
     response = dict(id=requestid, jsonrpc='2.0')
     if requestid is None or jsonrpc != '2.0':
         response['error'] = dict(code=-32600, message='Invalid Request')
         return response
     
     #don't catch exceptions for write operations because we want 
     #the whole request transaction to be aborted
     #sendJsonRpcError below it will turn the error into json-rpc error response
     if action == 'update':
         addStmts, removeStmts = dataStore.update(data)
         #XXX better return values
         result = dict(added=pjson.tojson(addStmts), removed=pjson.tojson(removeStmts))
     elif action == 'replace':
         addStmts, removeStmts = dataStore.replace(data)
         #XXX better return values
         result = dict(added=pjson.tojson(addStmts), removed=pjson.tojson(removeStmts))
     elif action == 'add':
         addJson = dataStore.add(data)
         result = dict(added=addJson)
     elif action == 'create':
         addJson, newresources = dataStore.create(data)
         if newresources:
             newresourcesCreated['new'+str(id)] = newresources[0]
         result = dict(added=addJson, new=newresources)
     elif action == 'query':
         #returns { errors, results }
         if isinstance(data, (str, unicode)):
             result = dataStore.query(data, captureErrors=True) 
         else:
             data['captureErrors'] = True
             result = dataStore.query(**data)
         if result.errors:
             response['error'] = dict(code=0, message='query failed', 
                                                 data = result.errors)
             return response
     elif action == 'remove':
         removeJson = dataStore.remove(data)
         result = dict(removed=removeJson)
     elif action == 'transaction_info':
         comment = params.get('comment')
         result = {}
         if comment:
             comment = Template(comment).safe_substitute(newresourcesCreated)
             result['comment'] = comment
             kw['__transaction_comment'] = comment
     else:
         response['error'] = dict(code=-32601, message='Method not found')
         return response
     
     response['result'] = result
     return response
Beispiel #4
0
def serializeRDF_Stream(statements,stream,type,uri2prefixMap=None,options=None):
    '''    
    type can be one of the following:
        "rdfxml", "ntriples", "ntjson", "yaml", "mjson", or "pjson"
    '''
    from vesper.data import base
    if type.startswith('http://rx4rdf.sf.net/ns/wiki#rdfformat-'):
        type = type.split('-', 1)[1]
    
    options = options or {}
    if type == 'rdfxml':        
        if rdfxmlSerializer:
            return rdfxmlSerializer(stream, statements, uri2prefixMap, **options)
        try:
            _serializeRDFXMLWithRdflib(stream, statements, uri2prefixMap, **options)
        except ImportError:
            try:
                _serializeRDFXMLWithRedland(stream, statements, uri2prefixMap)
            except ImportError:
                raise ParseException("no RDF/XML serializer installed")
    elif type == 'ntriples':
        writeTriples(statements, stream)
    elif type == 'ntjson':
        writeTriples(statements, stream, writejson=True)
    elif type == 'pjson' or type == 'mjson':
        isMjson = type == 'mjson'
        from vesper import pjson, multipartjson        
        if 'pjson' in options:
            options = options.copy()
            pjsonOptions = options.pop('pjson')
        else:
            pjsonOptions = {}        
        objs = pjson.tojson(statements, preserveTypeInfo=True, asList=isMjson, **pjsonOptions)
        if isMjson:
            return multipartjson.dump(objs, stream, **options)
        else:
            return json.dump(objs, stream, **options)
    elif type == 'yaml':
        import yaml
        from vesper import pjson
        #for yaml options see http://pyyaml.org/wiki/PyYAMLDocumentation#Theyamlpackage
        #also note default_style=" ' | >  for escaped " unescaped ' literal | folded >        
        #use default_flow_style: True always flow (json-style output), False always block    
        # default_flow_style=None block if nested collections other flow
        defaultoptions = dict(default_style="'")
        if options: 
            defaultoptions.update(options)
        if 'pjson' in defaultoptions:
            pjsonOptions = defaultoptions.pop('pjson')
        else:
            pjsonOptions = {}        
        return yaml.safe_dump( pjson.tojson(statements, preserveTypeInfo=True, 
                                     **pjsonOptions), stream, **defaultoptions)
Beispiel #5
0
 def testUpdate(self):
     store = vesper.app.createStore({
     "id": "hello", 
     "tags": [
       { "id": "tag1" }, 
       { "id": "tag2"}
     ]})
     #print 'store', store.query('{*}')        
     #utils.debugp( 'testremove', store.model.by_s)                
     #print 'hello', store.model.by_s.get('hello')
     store.update({"id":"hello","tags":"@tag1"})
     #utils.debugp('hello', store.model.by_s.get('hello'))
     from vesper import pjson
     self.assertEquals(pjson.tojson(store.model.getStatements())['data'],
         [{"id": "@hello", 
            "tags": ["@tag1"]
         }])            
     self.assertEquals(store.query('{*}'), [{'id': '@hello', 'tags': ['@tag1']}])
Beispiel #6
0
 def XXXtestUpdate2(self):
     #XXX bug: update needs to replace the embedded object,
     #not just update the given properties in the embedded object
     store = vesper.app.createStore({
       "id": "1",
       "custom":
         {
           "item": "@2",
           "custom": {
             "item": "@3",
           }
         }
       ,
     })
     from vesper import pjson
     print 'update', store.update(
     {"id":"1", "custom":{"item":{"id":"@2"}}}
     )
     print 'model', pjson.tojson(store.model.getStatements())['data']
Beispiel #7
0
 def removeHook(self, db, stmts, jsonrep=None):
     '''
     This is intended to be set as the DataStore's removeTrigger
     '''
     state = self.state
     if self.isActive() and state.safeToJoin:
         dbchanges = state.dbstates.setdefault(db, DbChanges())
         additions, removals = dbchanges.additions, dbchanges.removals
         removals.extend(stmts)
         for stmt in stmts:
             if stmt in additions:
                 additions.pop( additions.index(stmt) )
         if 'before-remove' in self.server.actions:
             if jsonrep is None:
                 jsonrep = pjson.tojson(stmts)
             kw = {
                 '_removedStatements' : removals, 
                 '_removed' : jsonrep, 
                 '_newResources' : dbchanges.newResources,
                 '_db' : db
             }
             self._runActions('before-remove', kw)
Beispiel #8
0
 def addHook(self, db, stmts, jsonrep=None):
     '''
     This is intended to be set as the DataStore's addTrigger
     '''
     state = self.state
     if self.isActive() and state.safeToJoin:
         dbchanges = state.dbstates.setdefault(db, DbChanges())
         additions, removals = dbchanges.additions, dbchanges.removals
         additions.extend(stmts)
         for stmt in stmts:
             if stmt in removals:
                 removals.pop( removals.index(stmt) )
         if 'before-add' in self.server.actions:
             if jsonrep is None:
                 jsonrep = pjson.tojson(stmts,
             **db.model_options.get('serializeOptions',{}).get('pjson',{}))
             kw = {
                 '_addedStatements' : additions,
                 '_added' : jsonrep, 
                 '_newResources' : dbchanges.newResources,
                 '_db' : db
              }
             self._runActions('before-add', kw)