def get(cls, dao, id, provider_maker, providers_config): now = time.time() item_doc = dao.get(id) item = cls.item_class(dao, id=id) if item_doc is None: raise LookupError item.last_requested = now # first, just copy everything from the item_doc the DB gave us for k in item_doc: if k not in ["_id", "_rev"]: setattr(item, k, item_doc[k]) # the aliases property needs to be an Aliases obj, not a dict. item.aliases = Aliases(seed=item_doc['aliases']) # make the Metric objects. We have to make keys for each metric in the config # so that Providers will know which metrics to update later on. # Then we fill these Metric objects's dictionaries with the metricSnaps # from the db. item.metrics = {} metric_names = cls.get_metric_names(providers_config) for full_metric_name in metric_names: try: my_metric = item_doc["metrics"][full_metric_name] except KeyError: #this metric ain't in the item_doc from the db my_metric = {'values': {} } (provider_name, metric_name) = full_metric_name.split(":") # make the provenance url # FIXME problem if alias needed for provenance url not obtained yet? aliases_list = item.aliases.get_aliases_list() provider = provider_maker(provider_name) provenance_url = provider.provenance_url(metric_name, aliases_list) my_metric["provenance_url"] = provenance_url # populate the static_meta only if it has a provenance url if provenance_url: metric_static_meta = providers_config[provider_name]["metrics"][metric_name]["static_meta"] my_metric["static_meta"] = metric_static_meta else: my_metric["static_meta"] = {} item.metrics[full_metric_name] = my_metric return item
def make(cls, dao, id=None, collection_dict=None): if id is not None and collection_dict is not None: raise TypeError("you can load from the db or from a dict, but not both") now = time.time() collection = Collection(dao=dao) if id is None and collection_dict is None: collection.id = cls.make_id() collection.created = now collection.last_modified = now else: # load an extant item if collection_dict is None: collection_dict = dao.get(id) if collection_dict is None: raise LookupError for k in collection_dict: setattr(collection, k, collection_dict[k]) return collection