Exemplo n.º 1
0
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)
Exemplo n.º 2
0
 def scrape(self, path: pathlib2.Path, depth: int) -> Tuple[FileNode, bool]:
     """
     Use recursion to scrape a given path and return a tree structure
     :param path: target file path to scrape
     :param depth: depth of node with respect to the root node
     :return: the scraped file node tree and whether any target files set by filters were found
     """
     children = []
     found_any = False
     paths = list(path.iterdir())
     for filter_ in self.filters:
         paths = filter_(paths)
     if depth != self.depth_limit:
         for filepath in paths:
             node = FileNode(filepath, depth=depth + 1, root=self.root)
             if (filepath.is_symlink() or filepath.is_dir()
                 ) and node.get_id() not in self.history:
                 subtree, found_any_ = self.scrape(filepath, depth + 1)
                 if found_any_:
                     found_any = True
                 if self._keep_empty_dir or found_any_:
                     children.append(subtree)
             elif filepath.is_file():
                 children.append(node)
                 found_any = True
             else:
                 pass
             self.history.add(node.get_id())
     return FileNode(path, children=children, depth=depth,
                     root=self.root), found_any
Exemplo n.º 3
0
        def iterate(node_: FileNode) -> Iterable:
            prefix_tabs = node_.get_depth() * '\t'
            if node_.get_path().is_file():
                link = './' + \
                       str(node_.get_path().relative_to(self.root.get_path()))
                yield f"{prefix_tabs}- [{node_.get_filename()}]({link})"
            else:
                yield f"{prefix_tabs}- {node_.get_filename()}"

            children = node_.get_children()
            for child_node in children:
                yield from iterate(child_node)
Exemplo n.º 4
0
 def iterate(node_: FileNode) -> Iterable:
     prefix_tabs = node_.get_depth() * '\t'
     if node_.is_file():
         yield f"{prefix_tabs}<li>{node_.get_filename()}</li>"
     else:
         yield f"{prefix_tabs}<ul>"
         yield f"{prefix_tabs}\t<lh>{node_.get_filename()}</lh>"
     children = node_.get_children()
     for child_node in children:
         yield from iterate(child_node)
     if node_.is_dir():
         yield f"{prefix_tabs}</ul>"
Exemplo n.º 5
0
 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
Exemplo n.º 6
0
 def iterate(node_: FileNode, prefix: str = '') -> Iterable:
     children = node_.get_children()
     # contents each get pointers that are ├── with a final └── :
     pointers = [TreeCommandFormatter.tee] * \
         (len(children) - 1) + [TreeCommandFormatter.last]
     for pointer, node in zip(pointers, children):
         yield prefix + pointer + node.get_filename()
         if children:  # extend the prefix and recurse:
             extension = TreeCommandFormatter.branch if pointer == TreeCommandFormatter.tee else \
                 TreeCommandFormatter.space
             # i.e. space because last, └── , above so no more |
             yield from iterate(node, prefix=prefix + extension)
Exemplo n.º 7
0
 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)
Exemplo n.º 8
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)
Exemplo n.º 9
0
 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)
Exemplo n.º 10
0
 def iterate(node_: FileNode) -> Iterable:
     prefix_tabs = node_.get_depth() * '\t'
     yield f"{prefix_tabs}- {node_.get_filename()}"
     children = node_.get_children()
     for child_node in children:
         yield from iterate(child_node)
Exemplo n.º 11
0
 def iterate(node_: FileNode) -> Iterable:
     if node_.get_path().is_file():
         yield node_.get_path()
     children = node_.get_children()
     for child_node in children:
         yield from iterate(child_node)
Exemplo n.º 12
0
 def traverse(node: FileNode):
     if node.get_children():
         node.set_children(self.sorted(node.get_children()))
     for child in node.get_children():
         traverse(child)