Giter VIP home page Giter VIP logo

pysh's Introduction

PySh

Very tiny dynamic interface for shell scripting with python.

####Examples:

>>> from pysh import sh
>>> sh.cp("file1", "file2")
>>> sh.mv("file3", "file4", "file5", "directoryA")
>>> sh.ls()
    ... ls output ...
>>>
>>> sh.asdfghjkl()
pysh: asdfghjkl: command not found

####Aliases: Just like in Bash, you can define aliases for shell calls you make regularly.

>>> from pysh import sh
>>> sh.alias(ls='ls --color=auto')
>>> sh.ls()
    ... ls output, colored ...
>>> sh.listalias()
pysh: ls is aliased to 'ls --color=auto'
>>> sh.showalias("ls")
pysh: ls is aliased to 'ls --color=auto'
>>> sh.rmalias("ls")
pysh: ls: alias removed

#####How does it work? Magic and faerie dust.

#####Seriously though, how does it work? Overloading the __getattribute__ class method. This gets called whenever an object's attribute is referenced with the syntax object.attribute (or the getattr function, as it turns out).

It's pretty aggressive, so you can get into infinite loops pretty quickly if you don't use its parent class's __getattribute__ method with self passed in as the first parameter. The __getattribute__ method gets passed two arguments: the object instance (by convention named self) and the name of the attribute which has been referenced. I take that attribute name and return a partial subprocess.call function (i.e. an object that, when called, has the first parameter 'frozen' as the attribute name I pass in when creating the partial function).

It's not really the subprocess.call function itself though, it's a kind of wrapper function that allows some more flexibility when passing in parameters (see examples above).

The wrapper is mainly implemented with the itertools.chain function, which can flatten shallow lists. Some type-checking in a list comp means you can also pass in your list of arguments as an iterable, and it will be flattened accordingly.

####Wishlist:

  • [DONE] Multiple shell calls per function/alias, e.g.
sh.alias(up = 'sudo apt-get update;'
                'sudo apt-get upgrade -y;'
                'printf "[+] Update completed successfully"'
                )
  • Colored (red/green) status indicator for each command run, possibly getting the return value from check_call or parsing error messages.
  • Fully-working pipe syntax, or some equally functional alternative

pysh's People

Contributors

bedekelly avatar

Watchers

 avatar

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.