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")

# some options is mutiple, which means the name is name, but you can give
# many different values, for example
# `foo -a -b -o Dir=/var -o Dir::Cache=/tmp`
# so you need to use make_optargs to create args if the opt name is duplicate
optargs = ucltip.make_optargs('o', ('Dir=/var','Dir::Cache=/tmp'))
SingleCmd('foo')(optargs, a=True, b=True)

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.4.1.tar.gz (4.5 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.4.1.linux-i686.exe (71.2 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for ucltip-0.4.1.tar.gz
Algorithm Hash digest
SHA256 14ad9784ba43abe3dae815302ed352d3a1bfd51c8b8cc6d9a55a35b694b1a800
MD5 68b9a3084da50863e90709606467f340
BLAKE2b-256 daacce187172a229a0ac8eaa9fdb2a87288357d6b767cb6787f4ea77f458bfae

See more details on using hashes here.

File details

Details for the file ucltip-0.4.1.linux-i686.exe.

File metadata

File hashes

Hashes for ucltip-0.4.1.linux-i686.exe
Algorithm Hash digest
SHA256 961de763d723da6b8a05416d4757ef13fff716216ba25afaaa68bba0e05e2010
MD5 7be5684cfb3f16f07c2e913ab0ef1cff
BLAKE2b-256 93ab3bd674506f07254fbb47b99c126d9c9bd6a232f9cd105b41a3b71c67901d

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