Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utility folder for store commonly used helper functions #5

Closed
ywangd opened this issue Dec 15, 2014 · 6 comments
Closed

Utility folder for store commonly used helper functions #5

ywangd opened this issue Dec 15, 2014 · 6 comments
Milestone

Comments

@ywangd
Copy link
Owner

ywangd commented Dec 15, 2014

For an example, un-expanduser is a common need by many other programs like pwd, ls, etc.

@dgelessus
Copy link
Contributor

Maybe make stash a package. stash.py would contain the main shell functionality, and utils.py some commonly used helper functions. __init__.py could also import objects from the submodules that are intended for external use, that way a script can import stash and immediately have access to all public functions, classes, attributes, etc.

The downside to this is that it adds an extra dependency to commands that could otherwise run standalone as well. Perhaps stash could automatically collapse all "long" paths when writing to the output (configurable of course in case it should cause problems) so the commands don't need to do tilde collapsing on their own every time they write a path.

@dgelessus
Copy link
Contributor

PS - there's a collapseuser function in whateversh. It uses os.path.relpath to collapse, which should make it reliable in almost all cases, and it correctly handles the split data/application home folder in iOS 8 and Pythonista < 1.6.

def collapseuser(path):
    """Reverse of os.path.expanduser: return path relative to ~, if
    such representation is meaningful. If path is not ~ or a
    subdirectory, the absolute path will be returned.
    """
    path = os.path.abspath(unicode(path))
    home = os.path.expanduser("~")
    if os.path.exists(os.path.expanduser("~/Pythonista.app")):
        althome = os.path.dirname(os.path.realpath(os.path.expanduser("~/Pythonista.app")))
    else:
        althome = home

    if path.startswith(home):
        collapsed = os.path.relpath(path, home)
    elif path.startswith(althome):
        collapsed = os.path.relpath(path, althome)
    else:
        collapsed = path

    ##print(path, home, althome, collapsed)

    return "~" if collapsed == "." else os.path.join("~", collapsed)

@ywangd
Copy link
Owner Author

ywangd commented Dec 19, 2014

@dgelessus Thanks for the advices. I am also still thinking about the best approach to do this.

I am kinda reluctant to make it an inherent part of StaSh. My personal feel is that the shell really is not in the position to change outputs generated by external scripts. The shell manages screen display issues like scroll and refresh, but the actual contents should only be managed by external scripts. So I thought about creating a sub-folder (similar concept as the bin sub-folder) to store these utility functions that could be used by external scripts. I do agree, like you said, it creates dependancies between bin scripts and the shell.

Your collapseuser function looks great and is what I am after. I'd like to use it as part of utils. Thanks!

@ywangd ywangd added this to the v0.3.0 milestone Jan 1, 2015
This was referenced Jan 5, 2015
@ywangd
Copy link
Owner Author

ywangd commented Jan 6, 2015

Added a lib folder under the main StaSh directory. @dgelessus collapseuser is added as the first helper function (used by version.py). Thanks!

@ywangd
Copy link
Owner Author

ywangd commented Jan 8, 2015

I plan to make some changes on how lib folder works:

Python files under lib folder that have names start with lib will be imported by StaSh at startup time. The imported module will be added to the _stash object as an attribute. For example, a libcore.py containing the collapseuser function is located at the lib folder. It will be imported and assigned as _stash.libcore. So an command script can use the function as _stash.libcore.collapseuser('PATH').

The reason of the changes are:

  • I'd like the command script to have a single dependency point that is _stash
  • lib no longer has to be in sys.path. This avoid potential module name clashes and remove the need to use leading underscore in file names.

@ywangd
Copy link
Owner Author

ywangd commented Jan 9, 2015

The changes now are committed 0322f3b

Both version.py and pwd.py now use the new interface.

@ywangd ywangd closed this as completed Jan 9, 2015
ywangd pushed a commit that referenced this issue Jul 10, 2016
Updated fork (again)
bennr01 pushed a commit that referenced this issue Aug 17, 2020
Merge pull request #408 from onyxware/dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants