def test_invalid_attr(self): id = str(uuid.uuid1()) ts = core.Timeseries(id, 'kW') self.assertRaises(KeyError, ts.__setitem__, 'foo', 'bar') self.assertRaises(util.SmapSchemaException, ts.__setitem__, 'Properties', '10') self.assertRaises(util.SmapSchemaException, ts.__setitem__, 'Description', 10)
def test_set_metadata(self): id = str(uuid.uuid1()) ts = core.Timeseries(id, 'kW') self.assertRaises(KeyError, ts.__getitem__, 'Metadata') test_1 = {'Metadata': {'Extra': {'Test': 'Foo'}}} ts.set_metadata(test_1) self.assertEqual(ts['Metadata'], test_1['Metadata']) test_1 = test_1['Metadata'] test_1['Extra']['Test'] = 'Bar' ts.set_metadata(test_1) self.assertEqual(ts['Metadata'], test_1)
def process(self, data): # maybe transform to smap-xml data = etree.XML(data) if self.xslt: data = self.xslt(data) for xmlts in data.getroot().getchildren(): if not 'path' in xmlts.attrib: log.err("skipping timeseries: no path attribute") continue # maybe make/add a new timeseries if we haven't seen this one before path = xmlts.attrib['path'] ts = self.get_timeseries(path) if not ts: ts = self.make_jsonts(xmlts) ts['uuid'] = self.uuid(path) ts = core.Timeseries(ts, None) self.add_timeseries(path, ts) for xmlts in data.getroot().getchildren(): if not 'path' in xmlts.attrib: continue # add all of the readings path = xmlts.attrib['path'] for r in xmlts.find('Readings').getchildren(): try: if not self.ignore_time: rtime = self.parse_time(ts, r.find("Timestamp").text) else: rtime = time.time() rval = self.parse_val(ts, r.find("Value").text) except (ValueError, TypeError), e: log.err() continue self._add(path, rtime, rval)
# arg1 : key to generate uuid with, or Collection instance s.add_collection("/steve") # easy-add -- create a timeseries automatically. kwargs pass through # to the timeseries so you can change the data type, description, etc. # # the parent must exist and be a collection for this to work. # # arg0 : path to add at # arg1 : either a unique string (key) or a uuid instance # arg2 : units s.add_timeseries("/sensor0", "sdh", "V") # alternative -- add an existing timeseries s.add_timeseries("/sensor1", core.Timeseries(s.uuid("sdh2"), "F", buffersz=2)) # add readings to a timeseries # get_timeseries will look up based on either path or uuid s.get_timeseries("/sensor0").add(util.now(), 12) s.get_timeseries("/sensor0").add(util.now(), 13) # you can set timeseries properties by accessing it as a dict. The # changes you make must follow the smap schema and you will get a # SmapSchemaException if you try to write an invalid object. s.get_timeseries("/sensor0")['Metadata'] = \ {'Instrument' : { 'Manufacturer' : "Stephen Dawson-Haggerty" }, 'Extra' : { 'Sucks' : 'Andrew'