Package: octarine-plumbum
Plumbum: Shell CombinatorsEver wished the compactness of shell scripts be put into arealprogramming language?
Say hello toPlumbum Shell Combinators. Plumbum (Latin forlead, which was used to create
pipes back in the day) is a small yet feature-rich library for shell script-like programs in Python.
The motto of the library is“Never write shell scripts again”, and thus it attempts to mimic
theshell syntax(“shell combinators”) where it makes sense, while keeping it allPythonic
and cross-platform.Apart from shell-like syntax and handy shortcuts, the library provides local and remote command
execution (over SSH), local and remote file-system paths, easy working-directory and environment
manipulation, and a programmatic Command-Line Interface (CLI) application toolkit.
Now let’s see some code!This is only a teaser; the full documentation can be found atRead the DocsCheat SheetBasics>>>fromplumbumimportlocal>>>ls=local["ls"]>>>lsLocalCommand(<LocalPath/bin/ls>)>>>ls()u'build.py\ndist\ndocs\nLICENSE\nplumbum\nREADME.rst\nsetup.py\ntests\ntodo.txt\n'>>>notepad=local["c:\\windows\\notepad.exe"]>>>notepad()# Notepad window pops upu''# Notepad window is closed by user, command returnsInstead of writingxxx =local["xxx"]for every program you wish to use, you can
alsoimportcommands>>>fromplumbum.cmdimportgrep,wc,cat,head>>>grepLocalCommand(<LocalPath/bin/grep>)Piping>>>chain=ls["-a"]|grep["-v","\\.py"]|wc["-l"]>>>printchain/bin/ls-a|/bin/grep-v'\.py'|/usr/bin/wc-l>>>chain()u'13\n'Redirection>>>((cat<"setup.py")|head["-n",4])()u'#!/usr/bin/env python\nimport os\n\ntry:\n'>>>(ls["-a"]>"file.list")()u''>>>(cat["file.list"]|wc["-l"])()u'17\n'Working-directory manipulation>>>local.cwd<Workdir/home/tomer/workspace/plumbum>>>>withlocal.cwd(local.cwd/"docs"):...chain()...u'15\n'Foreground and background execution>>>fromplumbumimportFG,BG>>>(ls["-a"]|grep["\\.py"])&FG# The output is printed to stdout directlybuild.py.pydevprojectsetup.py>>>(ls["-a"]|grep["\\.py"])&BG# The process runs "in the background"<Future['/bin/grep','\\.py'](running)>Command nesting>>>fromplumbum.cmdimportsudo>>>printsudo[ifconfig["-a"]]/usr/bin/sudo/sbin/ifconfig-a>>>(sudo[ifconfig["-a"]]|grep["-i","loop"])&FGloLinkencap:LocalLoopbackUPLOOPBACKRUNNINGMTU:16436Metric:1Remote commands (over SSH)SupportsopenSSH-compatible clients,PuTTY(on Windows)
andParamiko(a pure-Python implementation of SSH2)>>>fromplumbumimportSshMachine>>>remote=SshMachine("somehost",user="john",keyfile="/path/to/idrsa")>>>r_ls=remote["ls"]>>>withremote.cwd("/lib"):...(r_ls|grep["0.so.0"])()...u'libusb-1.0.so.0\nlibusb-1.0.so.0.0.0\n'CLI applicationsimportloggingfromplumbumimportcliclassMyCompiler(cli.Application):verbose=cli.Flag(["-v","--verbose"],help="Enable verbose mode")include_dirs=cli.SwitchAttr("-I",list=True,help="Specify include directories")@cli.switch("--loglevel",int)defset_log_level(self,level):"""Sets the log-level of the logger"""logging.root.setLevel(level)defmain(self,*srcfiles):print"Verbose:",self.verboseprint"Include dirs:",self.include_dirsprint"Compiling:",srcfilesif__name__=="__main__":MyCompiler.run()Sample output$ python simple_cli.py -v -I foo/bar -Ispam/eggs x.cpp y.cpp z.cpp
Verbose: True
Include dirs: ['foo/bar', 'spam/eggs']
Compiling: ('x.cpp', 'y.cpp', 'z.cpp')Colors and Stylesfromplumbumimportcolorswithcolors.red:print("This library provides safe, flexible color access.")print(colors.bold|"(and styles in general)","are easy!")print("The simple 16 colors or",colors.orchid&colors.underline|'256 named colors,',colors.rgb(18,146,64)|"or full rgb colors",'can be used.')print("Unsafe "+colors.bg.dark_khaki+"color access"+colors.bg.reset+" is available too.")