def _select_tree_dn(root, selector, data_root, depth, gap=0): dirs = { e.name: e.path for e in safe_scandir(root) if e.is_dir() } if dirs.pop(data_root, None): gap = 0 sel = selector[0] if sel in os.path.basename(root): selector.pop(0) if not selector: yield api.get_entry(util.unipath(root)) return if gap == depth: return for dir in dirs.values(): yield _select_tree_dn( dir, list(selector), data_root, depth, gap + 1 )
def _search_dn(root, depth=DEFAULT_SEARCH_DN_DEPTH, gap=0, levels=DEFAULT_SEARCH_DN_LEVELS, level=0, skip_root=False, at_root=True, data_root=None): dirs = { e.name: e.path for e in safe_scandir(root) if e.is_dir() } if dirs.pop(data_root, None): gap = 0 if not (skip_root and at_root): level += 1 yield api.get_entry(util.unipath(root)) if gap == depth or (levels and level == levels): return for dir in dirs.values(): yield _search_dn( dir, depth, gap + 1, levels, level, skip_root, False, data_root )
def copy(self, dest, only_data=False): '''Copy this Entry and it's children to a new location Arguments: dest (str): Destination path for new Entry only_data (bool): Copy only Entry data, includes no files outside the Entry's data directories Raises: OSError: Raised when dest already exists or copy_tree fails. Entry is left in original location, any files partially copied will be removed. ''' if os.path.exists(dest): raise OSError('Can not copy Entry to existing location...') try: if not only_data: util.copy_tree(self.path, dest, force=True, overwrite=True) else: hierarchy = [self] + list(self.children()) for entry in hierarchy: old_data_path = entry.data.path rel_data_path = os.path.relpath(old_data_path, self.path) new_data_path = util.unipath(dest, rel_data_path) util.copy_tree(old_data_path, new_data_path) except: if os.path.exists(dest): shutil.rmtree(dest) raise # Update uuids and send EntryCreated signals new_entry = api.get_entry(dest) new_entry.data._set_uuid() new_entry.created.send(new_entry) for child in new_entry.children(): child.data._set_uuid() child.created.send(child) return new_entry
def _search_up(root, levels=DEFAULT_SEARCH_UP_DEPTH, skip_root=False, data_root=None): level = -1 next_root = root while True: root = next_root level += 1 if levels and level > levels: break if skip_root and level == 0: next_root = os.path.dirname(root) continue if os.path.isdir(root + '/' + data_root): yield api.get_entry(root) next_root = os.path.dirname(root) if next_root == root: break
def _select_tree_up(root, selector, data_root, depth, level=0): level = -1 next_root = root while True: root = next_root level += 1 if level > depth: break if os.path.isdir(root + '/' + data_root): level = 0 sel = selector[-1] if sel in os.path.basename(root): selector.pop() if not selector: yield api.get_entry(root) return next_root = os.path.dirname(root) if next_root == root: break