Exemplo n.º 1
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.º 2
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.º 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 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.º 6
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.º 7
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.º 8
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.º 9
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)