def create_new_data_product(self, dataProductInput): reqcont = {} reqcont['dataProductInput'] = dataProductInput (cont, hdrs, msg) = yield self.rpc_send('create_new_data_product', reqcont) if cont.get('status') == 'OK': defer.returnValue(DataObject.decode(cont['value'])) else: defer.returnValue(None)
def create_new_instrument(self, userInput): reqcont = {} reqcont['userInput'] = userInput (cont, hdrs, msg) = yield self.rpc_send('create_new_instrument', reqcont) if cont.get('status') == 'OK': defer.returnValue(DataObject.decode(cont['value'])) else: defer.returnValue(None)
def op_respond(self, content, headers, msg): logging.info('op_respond: '+str(content)) print 'Type is: ', type(content) assert type(content) is dict dobj = DataObject.from_encoding(content) # The following line shows how to reply to a message yield self.reply(msg, 'reply', dobj.encode(), {})
def test_send_data_object(self): services = [ {'name':'responder','module':'ion.data.test.test_dataobject','class':'ResponseService'}, ] sup = yield self._spawn_processes(services) rs = ResponseServiceClient(sup) # Simple Send and Check value: do = yield rs.send_data_object(DataObject.from_encoding({'test':'value'})) self.assertEqual(do.get_attr('test'),'value') t=(unicode('t'),unicode('u'),unicode('p'),unicode('l'),unicode('e'),3) l=[4,'a',15,'opq'] d={'a':5,'b':3.14159} s=set() s.add(5) s.add('Cat') dct={ # 'test1':t, 'test2':d, 'test3':l} # 'test4':s} dsend = DataObject.from_encoding(dct) dreceive = yield rs.send_data_object(dsend) # Can't allow tuples because it breaks DataObject comparison ## Allow passing tuples, but they come out as lists! #self.assertIsInstance(dreceive.get_attr('test1'),list) #self.assertEqual(dreceive.get_attr('test1'),list(t)) self.assertEqual(dreceive,dsend) self.assertEqual(dreceive.get_attr('test2'),dsend.get_attr('test2')) self.assertEqual(dreceive.get_attr('test3'),l) # self.assertEqual(dreceive.get_attr('test4'),s)
def get_blob(self, key): ''' @param key, A key for a stored blob ''' yield self._check_init() # assert ? kd={'key':key} (content, headers, msg) = yield self.rpc_send('get_blob', kd) logging.info('Blob Servie Client: get_blob: '+str(content)) blob=DataObject.from_encoding(content) defer.returnValue(blob)
def op_put(self, content, headers, msg): """ Service operation: Puts a structured object into the data store. Equivalent to a git-push, with an already locally commited object. Replies with a result with the identity of the commit value """ logging.info("op_put: " + str(content)) key = content["key"] val = DataObject.from_encoding(content["value"]) parents = content["parents"] if "parents" in content else None commitref = yield self.os.put(key, val, parents, committer=headers["sender"]) yield self.reply(msg, "result", commitref.identity)
def setUp(self): yield self._start_container() services = [{'name':'association1','module':'ion.play.rdf_store.association_service','class':'AssociationService'},] sup = yield self._spawn_processes(services) self.asc = AssociationServiceClient(proc=sup) d = dict() d['S']='key1' d['O']='key2' d['P']='key3' self.dobj=DataObject.from_encoding(d) assoc = ValueObject(self.dobj.encode()) self.dobj_key=assoc.identity
def setUp(self): yield self._start_container() services = [{'name':'blob1','module':'ion.play.rdf_store.blob_service','class':'BlobService'},] sup = yield self._spawn_processes(services) self.bsc = BlobServiceClient(proc=sup) d = dict() d['a']=1 d['b']='b' d['c']=3.14159 self.dobj=DataObject.from_encoding(d) blob = ValueObject(self.dobj.encode()) self.dobj_key=blob.identity
def __init__(self, **kwargs): DataObject.__init__(self) if len(kwargs) != 0: self.setResourceTypeDesc(**kwargs)
def _build_value(self, entries, deep=False): """ Constructs a DataObject from list of tree entries @param entries list of TreeEntry instances with values @param deep boolean flag, indicates whether subtree value should be loaded or kept as ValueRef @retval a DataObject """ if not entries: return dobj = DataObject() for entry in entries: valname = entry.entry['name'] value = entry.value if isinstance(value, TreeValue): if deep: tentries = yield self.vs.get_tree_entriesvalues(value) # @todo Should this maybe a container DataObject? vdo = self._build_value(tentries, deep) if vdo: dobj.set_attr(valname, vdo) else: # Put only a the tree value object in # @todo Check if this is OK in a DataObject dobj.set_attr(valname, value) elif isinstance(value, ValueObject): if type(value.value) is dict: # case where a DataObject was encoded vdo = DataObject.from_encoding(value.value) vdo.identity = value.identity else: # case where value is a simple value vdo = DataObject() vdo.identity = value.identity vdo.set_attr('value', value.value) if len(entries) == 1: defer.returnValue(vdo) return dobj.set_attr(valname, vdo) else: raise RuntimeError('Unknown value type in store: '+repr(value)) defer.returnValue(dobj)
def test_dataobject(self): dobj = DataObject() dobj.set_attr('thing1','thing2') self.assertIsInstance(dobj.__dict__, dict) self.assertIsInstance(dobj.encode(),dict) self.assertEqual(dobj.get_attr('thing1'),'thing2') # test a simple dictionary d = dict() d['a']=1 d['b']='b' d['c']=3.14159 dobj.decode(d) self.assertEqual(dobj.get_attr('a'),1) self.assertEqual(dobj.get_attr('b'),'b') self.assertEqual(dobj.get_attr('c'),3.14159) dobj2=DataObject.from_encoding(d) self.assertEqual(dobj2.get_attr('a'),1) self.assertEqual(dobj2.get_attr('b'),'b') self.assertEqual(dobj2.get_attr('c'),3.14159) self.assertIdentical(dobj,dobj) # The Uniquie ID makes these two not equal self.assertNotEqual(dobj,dobj2) self.assertEqual(dobj2,DataObject.from_encoding(dobj2.encode())) self.assertRaises(AssertionError,dobj.set_attr, 6, 6) # Put a list, tuple, dict, DataObject and set in a dictionary # List d = dict() l=[1,'f',3.4] d['a']=l dobj3=DataObject.from_encoding(d) self.assertIsInstance(dobj3.get_attr('a'),list) self.assertEqual(dobj3.get_attr('a'),l) # For now, Assuming Lists are protected Blobs! ## Can't put complex things in a list #lx = list(l) #lx.append({6:5}) #dlx={'a':lx} #self.assertRaises(AssertionError,DataObject.from_encoding,dlx) # Tuple t=('t','u','p','l','e') dt={'b':t} self.assertRaises(AssertionError,DataObject.from_encoding,dt) #dobj3=DataObject.from_encoding(dt) ## Made Tuple an error! #self.assertIsInstance(dobj3.get_attr('b'),tuple) #self.assertEqual(dobj3.get_attr('b'),t) # Can't use tuple as Dict Key in DataObject dt={'b':t,(9,8):5} self.assertRaises(AssertionError,DataObject.from_encoding,dt) # Set s=set() for i in t: s.add(i) ds={'s':s} self.assertRaises(AssertionError,DataObject.from_encoding,ds) #self.assertIsInstance(dobj3.get_attr('c'),set) #self.assertEqual(dobj3.get_attr('c'),s) # Dictionary in a dictionary e=dict() f=dict() e['d']=d f['e']=e dobj3=DataObject.from_encoding(f) self.assertIsInstance(dobj3.get_attr('e'),DataObject) de=dobj3.get_attr('e') self.assertIsInstance(de.get_attr('d'),DataObject) dd=de.get_attr('d') self.assertEqual(dd.get_attr('a'),l) # Data Object a=DataObject.from_encoding(d) b=DataObject.from_encoding(d) c=DataObject.from_encoding(d) do=DataObject() do.set_attr('a',a) do.set_attr('b',b) do.set_attr('c',c) self.assertIsInstance(do.get_attr('a'),DataObject) self.assertEqual(do.get_attr('a'),a) print 'obja',a print 'objref',do.get_attr('a') blob = do.encode() d0 = DataObject.from_encoding(blob) #print '===a',d0.get_attr('a').__dict__ #print '===b',d0.get_attr('b').__dict__ #print '===c',d0.get_attr('c').__dict__ self.assertEqual(d0.get_attr('a'),a) # Recover the tuple # dt=d0.get_attr('a') # ti=dt.get_attr('b') # self.assertEqual(ti,t) # Recover the set # ds=d0.get_attr('a') # si=dt.get_attr('c') # self.assertEqual(si,s) self.assertEqual(do,d0) # Test Memory leak issue: # http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/ for i in range(10**2): # try 10**9 a = DataObject()
def send_data_object(self, dobj): yield self._check_init() (content, headers, msg) = yield self.rpc_send('respond', dobj.encode(), {}) logging.info('Responder replied: '+str(content)) do = DataObject.from_encoding(content) defer.returnValue(do)