earnestt1234 / seedir

A Python package for creating, editing, and reading folder tree diagrams
MIT License
122 stars 9 forks source link

Paramiko SFTP, improving documentation, feedback #3

Closed busla closed 3 years ago

busla commented 3 years ago

Hey, great job!

Love the simplicity, clear documentation and a nice API πŸ‘πŸΌ

Not sure how detailed examples you want to add to the README and the API docs but since I had to dig a bit in the code to figure out how to build a tree from path strings so I thought I'd share a snippet

When dealing with filesystem paths from strings, e.g. Paramiko SFTP tree, this might be useful. Mentioning Paramiko here for Google on purpose πŸ˜„

The snippet is part of a cli written with Typer and recursively iterates through a remote sftp tree and builds a FakeDir tree.

def list_files(sftp: SFTPClient, root_dir: str, tree: FakeDir) -> FakeDir:
    """
    Recursively list all remote files from root dir
    """
    files = sftp.listdir(root_dir)
    if not files:
        msg_error(f"No files found in {root_dir}")
        raise typer.Exit()
    else:
        for f in files:
            file_path = os.path.join(root_dir, f)
            file_attr = sftp.lstat(file_path)
            if stat.S_ISDIR(file_attr.st_mode):
                current = FakeDir(f, tree)
                list_files(sftp, file_path, current)
            elif stat.S_ISREG(file_attr.st_mode):
                tree.create_file(f)
    return tree
earnestt1234 commented 3 years ago

@busla thanks for the feedback! I appreciate you sharing an example for other's sake πŸ˜„. I am definitely interested in improving the documentation, if there is something you can think of that would have helped you? I have some information about expanding FakeDir objects in these examples, but I could certainly add on.

I am not really familiar with SFTP, but I guess the issue is there is no equivalent to seedir.fakedir() (for making a FakeDir from a local directory). So it is a nice example.

busla commented 3 years ago

Ahh, these examples are great! They demonstrate exactly what I was doing.

x.create_file(['__init__.py', 'main.py', 'styles.txt'])
x.create_folder('docs')

y = sd.FakeDir('resources', parent=x)

z = sd.FakeDir('images')
z.parent = y

The docs were really good in terms of rendering a tree from local files. Maybe just add the examples you referenced to the docs?

Regarding (s)ftp paths, they fit perfectly with seedir since they're just slash separated strings, .e.g. foo/bar/some/file.text, so nothing out of the ordinary.

earnestt1234 commented 3 years ago

@busla gotcha! I had initially intended have this example Notebook for demonstrative purposes, and the docs for cataloging the tools available, but it would be an improvement to add examples into the docs (like here); will do so

earnestt1234 commented 3 years ago

Added more example code to the docstrings for 0.1.3.