def _lookUpPath(self, path, user, test=False): """ Look up a resource in the data hierarchy by path. :param path: path of the resource :param user: user with correct privileges to access path :param test: defaults to false, when set to true will return None instead of throwing exception when path doesn't exist """ path = path.lstrip('/') pathArray = path_util.split(path) model = pathArray[0] parent = None if model == 'user': username = pathArray[1] parent = self.model('user').findOne({'login': username}) if parent is None: if test: return None else: raise RestException('User not found: %s' % username) elif model == 'collection': collectionName = pathArray[1] parent = self.model('collection').findOne({'name': collectionName}) if parent is None: if test: return None else: raise RestException( 'Collection not found: %s' % collectionName) else: raise RestException('Invalid path format') try: document = parent self.model(model).requireAccess(document, user) for token in pathArray[2:]: document, model = self._lookUpToken(token, model, document) self.model(model).requireAccess(document, user) except RestException: if test: return None else: raise RestException('Path not found: %s' % path) result = self.model(model).filter(document, user) return result
def populate(ids, paths, data): if not (ids or paths): click.echo('Error: No destination specified') return data = json.load(open(data, 'r')) totalTargets = len(ids) + len(paths) success = 0 for collectionId in ids: collection = Collection().findOne({'_id': ObjectId(collectionId)}) if (collection): Collection().setMetadata(collection=collection, metadata=data) success += 1 else: click.echo('Warning: No collection found with ID: ' + collectionId) for path in paths: # Truncates anything past the collection level path = '/'.join(split(path.lstrip('/'))[0:2]) try: doc = lookUpPath(path, force=True) if doc['model'] != 'collection': click.echo('Warning: Ignoring non-collection path: ' + path) continue doc = doc['document'] except (ResourcePathNotFound): name = split(path)[1] doc = Collection().createCollection(name, reuseExisting=True) Collection().setMetadata(collection=doc, metadata=data) success += 1 click.echo('Successfully set metadata on ' + str(success) + '/' + str(totalTargets) + ' targets')
def _get_vobject(self, document, path, i): pathArray = split(path) root = document n = 3 + i fspath = os.path.join(document["fsPath"], "/".join(pathArray[n:])) fspath = pathlib.Path(fspath) if not fspath.exists(): raise ValidationException("Path not found: %s" % path) if fspath.is_dir(): document = self.vFolder(fspath, root) model = "folder" elif fspath.is_file(): document = self.vItem(fspath, root) model = "item" # TODO: add vLink here... return document, model
def _lookUpPath(self, path, user=None, test=False, filter=True, force=False): """ Look up a resource in the data hierarchy by path. :param path: path of the resource :param user: user with correct privileges to access path :param test: defaults to false, when set to true will return None instead of throwing exception when path doesn't exist :type test: bool :param filter: Whether the returned model should be filtered. :type filter: bool :param force: if True, don't validate the access. :type force: bool """ path = path.lstrip("/") pathArray = split(path) try: document, model = self._get_base(pathArray, test=test) except EmptyDocument: return {"model": None, "document": None} try: if not force: ModelImporter.model(model).requireAccess(document, user) for i, token in enumerate(pathArray[2:]): # noqa document, model = lookUpToken(token, model, document) if not force: ModelImporter.model(model).requireAccess(document, user) if "fsPath" in document: break if token != pathArray[-1]: document, model = self._get_vobject(document, path, i) except (ValidationException, AccessException): if test: return {"model": None, "document": None} raise ResourcePathNotFound("Path not found: %s" % path) if filter: document = ModelImporter.model(model).filter(document, user) return {"model": model, "document": document}
def testSplitPath(self): for pth, tokens in paths: self.assertEqual(path.split(pth), tokens)
def testSplitAndJoin(pth, tokens): assert path.split(pth) == tokens assert path.join(tokens) == pth