def get_resource_tree(self, root_records): """For a given plugin and resource class, find all instances of that class and return a tree of resource instances (with additional 'children' attribute)""" storage_plugin_log.debug(">> get_resource_tree") tree = [] for record in root_records: tree.append(self._load_record_and_children(record)) storage_plugin_log.debug("<< get_resource_tree") return tree
def _load_record_and_children(self, record): storage_plugin_log.debug("load_record_and_children: %s" % record) resource = self._record_to_resource_parents(record) if resource: children_records = StorageResourceRecord.objects.filter( parents=record) children_resources = [] for c in children_records: child_resource = self._load_record_and_children(c) children_resources.append(child_resource) resource._children = children_resources return resource
def _record_to_resource_parents(self, record): if isinstance(record, StorageResourceRecord): pk = record.pk else: pk = record if pk in self._pk_to_resource: storage_plugin_log.debug("Got record %s from cache" % record) return self._pk_to_resource[pk] else: resource = self._record_to_resource(record) if resource: resource._parents = [self._record_to_resource_parents(p) for p in record.parents.all()] return resource
def _load_plugin(self, module, module_name, plugin_klass): storage_plugin_log.debug("_load_plugin %s %s" % (module_name, plugin_klass)) self.loaded_plugins[module_name] = LoadedPlugin(self, module, module_name, plugin_klass)