def add_timeseries(self, path, *args, **kwargs): """Add a timeseries to the smap server at the given path. This will generate a UUID for the timeseries. direct form :param path a Timeseries instance simple form :param args[0] is a uuid instance, or a key to generate a uuid with by combining it with the root uuid. :param args[1] and kwargs are arguments passed to the Timeseries constructor. Therefore you have to include at least the UnitofMeasure :param boolean replace: (kwarg) replace an existing timeseries at that path instead of throwing an exception :param boolean recurse: recursively create parent collections instead of thrwoing an exception. Default is True. :raises: :py:class:`SmapException` if the parent isn't a collection or the path already exists. """ replace = kwargs.pop('replace', False) recurse = kwargs.pop('recurse', True) klass = kwargs.pop('klass', Timeseries) if len(args) == 0 or \ not ITimeseries.providedBy(args[0]) and not IActuator.providedBy(args[0]): if len(args) == 2: if not isinstance(args[0], uuid.UUID): id = self.uuid(args[0], namespace=kwargs.get('namespace', None)) else: id = args[0] args = args[1:] elif len(args) == 1: id = self.uuid(util.norm_path(path), kwargs.get('namespace', None)) else: id = self.uuid(util.norm_path(path)) # raise SmapException("SmapInstance.add_timeseries may only be called " # "with two or three arguments") kwargs.pop('namespace', None) timeseries = klass(id, *args, **kwargs) if id != args[0]: setattr(timeseries, "key", args[0]) else: timeseries = args[0] path = util.split_path(path) if recurse: self._add_parents(path) parent = self.get_collection(util.join_path(path[:-1])) if not replace and util.join_path(path) in self.OBJS_PATH: raise SmapException("add_timeseries: path " + str(path) + " exists!") if not parent: raise SmapException("add_timeseries: parent is not a collection!") parent.add_child(path[-1]) # place the new timeseries into the uuid and path tables self.OBJS_UUID[timeseries['uuid']] = timeseries self.OBJS_PATH[util.join_path(path)] = timeseries timeseries.inst = self setattr(timeseries, 'path', util.join_path(path)) if not self.loading: self.reports.update_subscriptions() return timeseries
def _lookup_r(self, id, pred=None): """Lookup recursively in the resource hierarchy, starting with the resource identifed by "id". Returns a list of elements for which "pred" returns True""" rv = {} q = [id] root_path = getattr(self.lookup(id), 'path') while len(q) > 0: cur = self.lookup(q.pop(0)) if ICollection.providedBy(cur): for child in cur['Contents']: q.append(getattr(cur, 'path') + '/' + child) if cur and (not pred or pred(cur)): rvpath = util.norm_path(getattr(cur, 'path')[len(root_path):]) rv[rvpath] = cur return rv
def __init__(self, path, inst=None, description=None, *args): """ :param string path: the path where the collection will be added :param SmapInstance inst: the containing :py:class:`SmapInstance` object :param string description: the contents of the sMAP description field :raise SmapSchemaException: if the resulting object does not validate """ self.inst = inst setattr(self, 'path', util.norm_path(path)) if len(args) == 1 and isinstance(args[0], dict): dict.__init__(self, args[0]) else: self.__setitem__("Contents", []) if not schema.validate("Collection", self): raise SmapSchemaException("Error instantiating Collection: " "invalid parameter")