Coder Social home page Coder Social logo

Comments (6)

dgelessus avatar dgelessus commented on August 24, 2024

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.

from stash.

dgelessus avatar dgelessus commented on August 24, 2024

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)

from stash.

ywangd avatar ywangd commented on August 24, 2024

@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!

from stash.

ywangd avatar ywangd commented on August 24, 2024

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

from stash.

ywangd avatar ywangd commented on August 24, 2024

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.

from stash.

ywangd avatar ywangd commented on August 24, 2024

The changes now are committed 0322f3b

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

from stash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.