Skip to main content

Use command line tool in Python

Project description

This library makes you use command line tool in Python more easier. The original idea and most of basic codes are from GitPython project http://pypi.python.org/pypi/GitPython/

Basic Usage

# without options
uname = SingleCmd('uname')
# result is Linux
print uname()

# with args
expr  = SingleCmd('expr')
# result is 10
print expr(7, '+', 3)

# with options, the style is '-a - l'
# example: ls -a -l
ls = SingleCmd('ls')
# enable debug mode to see what command string will be executed.
# show the debug message like this: DBG: execute cmd 'ls -a -l'
ls.__DEBUG__ = True
print ls(l=True, a=True)

# with boolean options, the style is 'ls --all --almost-all'
# variable has '-' should replaced by '_', otherwise syntax error happens
print ls(all=True, almost_all=True)

# with key-value optons, has 2 different style,
# `--key value` or `--key=value`, you can use opt_style variable to control them
wget = SingleCmd('wget')

# replacement of wget -o log http://url
wget('http://url', o='log')

# replacement of wget -o log=http://url
wget = SingleCmd('wget', opt_style=1)
wget('http://url', o='log')

# you can also overwrite the bound command
ls.cmdname = 'echo'
# the result is
# DBG: execute cmd 'echo hi'
#'hi\n'
print ls("hi")

The command be executed by subprocess.call, it bypass the shell.

# the result is $HOME, and it will not show output directly
print SingleCmd('echo')("$HOME")

if you want execute command via shell and use shell enviroment variable, please do as follow, if args of function includes via_shell=True, the command be executed by os.system

# the result is "/home/somebody", and show output directly
SingleCmd('echo')("$HOME", via_shell=True)

And here is the replacement if you want to do pipe for mutiple commands

ls = SingleCmd('ls')
grep = SingleCmd('grep')
# the result is setup.py
print grep('setup.py', stdin=ls(a=True, interact=True).stdout))

#p.s ls(a=True, interact=True) return a Popen instance, so you can have more control
#    of that process

Handling Error of command execution

if the command you want to use is not exists, the exception ucltip.CommandNotFound raises

>> a=ucltip.SingleCmd('oo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ucltip/__init__.py", line 103, in __init__
    raise CommandNotFound()
ucltip.CommandNotFound

if the command be executed falied, the exception ucltip.CommandExecutedFalur raises

>>> a=ucltip.SingleCmd('ls')
>>> a
SingleCmd object bound 'ls'
>>> a(ccc=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ucltip/__init__.py", line 109, in __call__
    return self._callProcess(*args, **kwargs)
  File "ucltip/__init__.py", line 126, in _callProcess
    return self.execute(call, **_kwargs)
  File "ucltip/__init__.py", line 169, in execute
    raise CommandExecutedFalur(status, stderr_value)
ucltip.CommandExecutedFalur: ls: unrecognized option '--ccc'
Try `ls --help' for more information.

here is a example to hanlde error:

try:
        print ucltip.SingleCmd('ls')
except ucltip.CommandExecutedFalur as e:
        print e

Command Dispatcher

Some command tools has sub command, like git, zenity, pbuilder, apt-get, etc. and some commands like zenity, they have prefix string in their sub command.

# the sub command name is the method name
git = CmdDispatcher('git')
git.log()
# and you can also give args and options like what SingleCmd can use
git.log(raw=True, since='2010')

# you can get Popen instance also
proc = git.log(interact=True)

# zenity has '--' prefix in its sub command, so you need to specify prefix string
# and option style
zenity = CmdDispatcher('zneity', opt_style=1, subcmd_prefix='--')

# zneity --info --text=hi
zneity.info(text="hi")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ucltip-0.3.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ucltip-0.3.1.linux-i686.tar.gz (6.2 kB view details)

Uploaded Source

File details

Details for the file ucltip-0.3.1.tar.gz.

File metadata

  • Download URL: ucltip-0.3.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ucltip-0.3.1.tar.gz
Algorithm Hash digest
SHA256 146a9d2251061e7a6d95f1255a9e38b3efa4c5b1cf2ab17a0bdae245a5c6b99f
MD5 ad827e2180d08783b039ea8d22a33914
BLAKE2b-256 8ff347c1d0086f912500b38a037ab5f0278962116ba49e34184c7f363af2f0db

See more details on using hashes here.

File details

Details for the file ucltip-0.3.1.linux-i686.tar.gz.

File metadata

File hashes

Hashes for ucltip-0.3.1.linux-i686.tar.gz
Algorithm Hash digest
SHA256 ea6b001bcf5e782ff845534609bdb79dd2544a9c94e97093c9f550c67989ab54
MD5 c49bc6d3f47ee432e092a86cb7db3607
BLAKE2b-256 3902da6a5e454150ceb213c76bbfaab5d882cf63398fbac0e242c6483fb3d44d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page