def create_object(self, info, update_if_exists=False): if len(info.location) != self._num_levels: raise base.InvalidLocationQuery(info.location) entity = self._db.Location.get_by_db_location(info.location) if entity and not update_if_exists: raise base.LocationAlreadyExists(info.location) if entity: if info.metadata is not None: entity.metadata = info.metadata if entity.object: entity.object.filename = info.filename entity.object.mime = info.mime entity.object.uri = info.uri else: entity.object = self._db.Object(location=entity, filename=info.filename, uri=info.uri, mime=info.mime) return False # updated else: entity = self._db.Object.from_db_location(info.location, metadata=info.metadata or {}, filename=info.filename, mime=info.mime, uri=info.uri) assert entity.location.as_db_location() == info.location, ( entity.as_db_location(), info.location) return True # newly created location
def get_object(self, location): if len(location) != self._num_levels: raise base.InvalidLocationQuery(location) entity = self._db.Location.get_by_db_location(location) if not entity: raise base.LocationDoesNotExist(location) return entity.object.as_db_object_info()
def list_location(self, location, filter=None): # XXX Implement filter. if len(location) >= self._num_levels: raise base.InvalidLocationQuery(location) entity = self._db.Location.get_by_db_location(location) if not entity: raise base.LocationDoesNotExist(location) return (x.as_db_location_info() for x in entity.children)
def list_objects(self, location, filter=None): # XXX Implement filter. if len(location) not in (self._num_levels, self._num_levels - 1): raise base.InvalidLocationQuery(location) entity = self._db.Location.get_by_db_location(location) if not entity: raise base.LocationDoesNotExist(location) if len(location) == self._num_levels: yield entity.object.as_db_object_info() else: yield from (x.object.as_db_object_info() for x in entity.children if x.object)
def create_location(self, info, update_if_exists=False): if len(info.location) > (self._num_levels - 1): raise base.InvalidLocationQuery(info.location) entity = self._db.Location.get_by_db_location(info.location) if entity and not update_if_exists: raise base.LocationAlreadyExists(info.location) if entity: if info.metadata is not None: entity.metadata = info.metadata return False # updated else: entity = self._db.Location.from_db_location(info.location, metadata=info.metadata or {}) assert entity.as_db_location() == info.location, ( entity.as_db_location(), info.location) return True # newly created location
def delete_location(self, location, recursive): if len(location) > self._num_levels: raise base.InvalidLocationQuery(location) entity = self._db.Location.get_by_db_location(location) if not entity: raise base.LocationDoesNotExist(location) if entity.children and not recursive: raise base.LocationHasChildren(location) objects = [x.as_db_object_info() for x in entity.collect_objects()] if len(location) == 0: # The root location can not be deleted, but it's children can be. entity.children.select().delete() else: if not entity: raise base.LocationDoesNotExist(location) entity.delete() return objects