Skip to content

Instantly share code, notes, and snippets.

@voidfiles
Last active January 1, 2016 00:08
Show Gist options
  • Select an option

  • Save voidfiles/8064212 to your computer and use it in GitHub Desktop.

Select an option

Save voidfiles/8064212 to your computer and use it in GitHub Desktop.
Batman is a way to quickly write small helpfull commands
#!/usr/bin/env python
# To Use:
# pip install clint docopt
# place file in your ~/bin/ files
"""
____ _
| _ \ | |
| |_) | __ _| |_ _ __ ___ __ _ _ __
| _ < / _` | __| '_ ` _ \ / _` | '_ `
| |_) | (_| | |_| | | | | | (_| | | | |
|____/ \__,_|\__|_| |_| |_|\__,_|_| |_|
=======================================
The command line utility belt
To find help for an indvidual command please type: batman <command> --help
Usage:
batman <command> [<command_args>...]
batman (-h | --help)
Options:
-h --help Show this screen
"""
import sys
from clint.textui import puts, colored, indent
DISPATCH = {}
def add_to_dispatch(function):
DISPATCH[function.__name__] = function
def inner(*args, **kwargs):
return function(*args, **kwargs)
return inner
@add_to_dispatch
def urlencode(arguments):
"""urlencode - quickly url encode a bunch of strings.
Usage:
batman urlencode <name>...
batman urlencode (-h | --help)
Options:
-h --help Show this screen.
"""
from urllib import quote_plus
encoded = map(quote_plus, arguments['<name>'])
for _ in encoded:
puts(_)
@add_to_dispatch
def soup(arguments):
"""soup - get url contents and add to a BeautifulSoup
Usage:
batman soup <url>
batman soup (-h | --help)
Options:
-h --help Show this screen.
"""
from bs4 import BeautifulSoup
import IPython
import requests
resp = requests.get(arguments['<url>'])
resp.raise_for_status()
soup = BeautifulSoup(resp.content)
soup # pyflakes
IPython.embed()
if __name__ == '__main__':
from docopt import docopt
arguments = {}
if sys.argv[1:]:
arguments = docopt(__doc__, help=False)
command = arguments.get('<command>')
func = DISPATCH.get(command)
if not func and arguments:
puts(colored.red('%s isn\'t a batman command' % command))
if not (command and func):
puts(__doc__)
puts('Registered Commands:')
with indent(4):
for func in DISPATCH.values():
puts(func.__doc__.split('\n')[0])
exit(1)
arguments = docopt(func.__doc__)
func(arguments)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment