def pathsToDict(paths, root="", separator=None): """ Return the given paths as a nested dict. Example: paths = ["/fruit/apple", "/fruit/orange"] print pathsToDict(paths) # Result: {"fruit" : {"apple":{}}, {"orange":{}}} :type paths: list[str] :type root: str :type separator: str or None :rtype: dict """ separator = separator or DEFAULT_SEPARATOR results = collections.OrderedDict() paths = studiolibrary.normPaths(paths) for path in paths: p = results # This is to add support for grouping by the given root path. if root and root in path: path = path.replace(root, "") p = p.setdefault(root, collections.OrderedDict()) keys = path.split(separator)[0:] for key in keys: if key: p = p.setdefault(key, collections.OrderedDict()) return results
def selectedPaths(self): """ Return the paths that are selected. :rtype: list[str] """ paths = [] items = self.selectedItems() for item in items: path = item.path() paths.append(path) return studiolibrary.normPaths(paths)
def selectPaths(self, paths): """ Select the items with the given paths. :type paths: list[str] :rtype: None """ paths = studiolibrary.normPaths(paths) items = self.items() for item in items: if studiolibrary.normPath(item.path()) in paths: item.setSelected(True) else: item.setSelected(False)
def removePaths(self, paths): """ Remove the given paths from the database. :type paths: list[str] :rtype: None """ data = self.read() paths = studiolibrary.normPaths(paths) for path in paths: if path in data: del data[path] self.save(data)
def updatePaths(self, paths, data): """ Update the given paths with the given data in the database. :type paths: list[str] :type data: dict :rtype: None """ data_ = self.read() paths = studiolibrary.normPaths(paths) for path in paths: if path in data_: data_[path].update(data) else: data_[path] = data self.save(data_)