def reproduce_fs_tree(target_dir: pathlib2.Path, root: FileNode) -> None: """ Given a tree, recreate the tree structure in the file system :param target_dir: directory the structure is going to be reproduced in :param root: tree root node :return: None """ target_dir.mkdir(parents=True, exist_ok=True) if root and root.get_filename(): target_dir /= root.get_filename() def traverse(node: FileNode): if node.get_root() is None or node.get_relative_path() is None: raise ValueError("To reproduce given tree in file system, every node much have relative path," "since relative path is calculated with root path, FileNode has to be initialized" "with root path. both root and relative path must be available") path = target_dir / node.get_relative_path() if node.is_dir(): path.mkdir(parents=True, exist_ok=True) elif node.is_file(): path.touch(exist_ok=True) if node.get_children(): for child_node in node.get_children(): traverse(child_node) traverse(root)
def comparator(node1: FileNode, node2: FileNode): filename1, filename2 = node1.get_filename(), node2.get_filename() nums1 = list(map(int, filename1.split("."))) nums2 = list(map(int, filename2.split("."))) i = -1 while i < max_num_count and i < len(nums1) and i < len(nums2): i += 1 if nums1[i] == nums2[i]: continue elif nums1[i] < nums2[i]: return -1 else: return 1 return 0
def iterate(node_: FileNode) -> None: prefix_tabs = (node_.get_depth() - int(self.ignore_root_dir)) * '\t' path = node_.get_path() link = './' + str(path.relative_to(self.root.get_path())) if not (node_.get_depth() == 0 and self.ignore_root_dir): # Ignore Root Directory, show only top level files (start with children of root directory) if path.is_dir(): if self.full_dir_link: # link for intermediate directory print( f"{prefix_tabs}- [{node_.get_filename()}]({link})", file=self.stringio) else: # if this is a directory and contains a README.md, then add a link for this directory # no link for current directory otherwise. This behavior is based on self.dir_link if self.dir_link and (path / "README.md").exists(): if self.link_dir_readme: link = './' + \ str(path.relative_to( self.root.get_path()) / 'README.md') print( f"{prefix_tabs}- [{node_.get_filename()}]({link})", file=self.stringio) else: print( f"{prefix_tabs}- [{node_.get_filename()}]({link})", file=self.stringio) else: print(f"{prefix_tabs}- {node_.get_filename()}", file=self.stringio) elif path.is_file(): # current node is a file (should be a markdown), if self.remove_md_ext, .md will be removed from # the display name display_name = node_.get_filename().replace(".md", "") \ if node_.get_filename()[-3:] == ".md" and self.remove_md_ext else node_.get_filename() if not (self.no_readme_link and path.name == "README.md"): # README.md files will not get a link when self.no_readme_link is True print(f"{prefix_tabs}- [{display_name}]({link})", file=self.stringio) else: raise ValueError("Unhandled Error") children = node_.get_children() for child_node in children: iterate(child_node)
def iterate(node_: FileNode) -> None: print("\t" * node_.get_depth() + node_.get_filename(), file=self.stringio) if node_.get_children(): for node in node_.get_children(): iterate(node)