Giter Club home page Giter Club logo

oyster's Introduction

Oyster: Get 'em pearls

Oyster is a Python module for parsing shell commands. Unlike awesome modules such as docopt, optparse & argpare the primary intention of Oyster is not to parse the arguments passed to a single application - although it is capable of that too. The main objective is rather to parse any given shell command whether it be a chain of multiple commands or not.

Demo

import oyster

chain = oyster.parse('pip search -vvv --timeout=5 flask | grep session | less')
print len(chain)  # ==> 3

pip, grep, less = chain

print pip.has_option('--timeout')  # ==> True
print pip.get_option_values('--timeout')  # ==> ['5']
print pip.get_option_count('-v')  # ==> 3
print pip.arguments  # ==> ('search', '--timeout=5', 'flask')

print grep  # ==> 'grep session'
print less  # ==> 'less'

chain.remove('less')
print chain  # ==> 'pip search -vvv --timeout=5 flask | grep session'

chain += 'date -u'
print chain  # ==> 'pip search -vvv --timeout=5 flask | grep session; date -u'
utc_date = chain[chain.index('date -u')]
print utc_date  # ==> 'date -u'
print utc_date.get_option_values('-u')  # ==> [True]

Installation

Installation instructions coming soon once the setup.py has been written.

Development

Want to help out? Awesome! Here is what you need to do:

Fork the repository

Clone the repository

git clone [email protected]:<your_github_username>/oyster.git ~/your/dev/env/oyster

Create a virtual environment for Oystr

mkvirtualenv oyster
cd ~/your/dev/env/oyster
pip install -r development-requirements.txt

Run the tests

python tests.py

Submit a pull-request: Once you are done - all you need to do is follow the standard procedure for creating a pull-request. Thanks in advance!

Features

####Chains:

import oyster

chain = oyster.parse('cat /var/log/system.log | grep kernel >> system_kernel.log 2>> errors.log')
cat, grep = chain
print cat  # ==> 'cat /var/log/system.log'
print grep  # ==> 'grep kernel >> system_kernel.log 2>> errors.log'

####Commands:

import oyster

command = oyster.Command(['cat', '-n', 'foo.txt'])
print command  # ==> 'cat -n foo.txt'
print command.program  # ==> 'cat'
print command.arguments  # ==> ('-n', 'foo.txt')
print command.has_option('-n')  # ==> True
print command.get_option_values('-n')  # ==> ['foo.txt']  (See "Caveats" about this)
print command.get_option_count('-n')  # ==> 1
print command.get_options()  # ==> {'-n': ['foo.txt']}  (See "Caveats" about this)

####Redirects:

import oyster

chain = oyster.parse('cat /var/log/system.log | grep kernel >> system_kernel.log 2>> errors.log')
print chain[0].redirects  # ==> ()
print chain[1].redirects[0]  # ==> >> system_kernel.log
print chain[1].redirects[0].source == oyster.STDOUT  # ==> True
print chain[1].redirects[0].destination == 'system_kernel.log'  # ==> True
print chain[1].redirects[0].mode  # ==> 'a'
print chain[1].redirects[1]  # ==> 2>> errors.log
print chain[1].redirects[1].is_source_stderr()  # ==> True
print chain[1].redirects[1].is_destination_stdfd()  # ==> False

Caveats

  • The string passed to oyster.parse can differ from str(command) since the latter is generated using subprocess.list2cmdline which can cause different quotations.
  • Command(['cat', '-n', 'foo.txt']).get_option_values('-n') will always return a list since options can be repeated.
  • Command(['curl', '-v', 'http://localhost']).get_option_values('-v') will return ['http://localhost']. Whether this is true or not is up to the program executed, i.e curl in this instance.

Documentation

The Oyster documentation is available here. Currently, this is a work in progress and a complete documentation will be available within a few days.

License

Oyster is licensed under a three clause BSD License which can read in the included LICENSE file.

oyster's People

Contributors

birkjernstrom avatar

Stargazers

 avatar

Forkers

emnaksontini

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.