Download - UCLTIP Introduction @PyCon Japan 2011
CC-3.0
UCLTIP CLI tool
, Hsin-Yi Chen (hychen),
2011/08/27 PyCon JP 2011, ,
,,~ freddi.
Who am I?
(hychen)
From Taiwan
Debian Maintainer
Work for Canonical OEM Service Team
More about me: http://about.me/hychen
How we execute command line tool in Python?
Subprocess is better choiceDon't use shell
subprocess.call
subprocess.Popen
os.systemPass command string to shell
return status code, not value
Question: Why UCLTIP
Provide a hyper interface for executing command line tool with subprocess
Introduce UCLTIP
Helper interface of pipeline as C libpipeline
CLI Tool arguments to function/method arguments
CLI Tool Boolean option style to function/method keyword arguments
CLI Tool Key-Value option style to function/method keyword arguments
CLI Tool Key-Value option style to function/method keyword arguments
CLI Command as Python Class and use it in your script
Pipe
>>>pipe = ucltip.Pipe()
>>>pipe.add('expr', 1, '+' 3)
>>>pipe.add('sed', 's/4/5/', '--posix')
>>>pipe.wait()
>>>pipe.stdout.read()
Demo: pipe
Command Args and Options Python function args / kwargs
#/usr/bin/env python
from GitPython Import this_idea
# ls --quoting-style=cls(quoting_style='c')
Transform CLI Tool arguments
The command string combine the option string if the value is True(boolean)
Exampleuser@host: expr 1 + 3 expr(1, '+', 3)
UCLTIP - Cmd
For command without sub command
Create a callable instance by implemented __call__
Raises ComandNotFound if command not exists
ReturnSuccess return result string
Failed raises CommandExecutedFalur
Demo: ex_get and ex_err
To get a result
Transform CLI Tool Boolean option style
The command string combine the option string if the value is True(boolean)
Example-d func(d=True)
--dry-run func(dry_run=True)
Transform CLI Tool Key-Value option style
The command string will execute combines If the option value is string or number
Example:-t maverick func(t='maverick')
--text hello func(text='hello')
Transform CLI Tool Key-Value option style II
The command string will execute combines If the option value is string or number
Example:-t=maverick func(t='maverick')
--text=hello func(text='hello')
Same option name, but more than 1
# `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
duplicateoptargs = ucltip.make_optargs('o'
,('Dir=/var','Dir::Cache=/tmp'))Cmd('foo')(*optargs, a=True,
b=True)
command with sub command
Without prefixgit
pbuilder
apt-get
...
With prefixZenity
...
apt-get install vim
zenity --info=text
UCLTIP - CmdDispatcher
For CLI tool has sub command
Command name instance
Subcommand name method name
args, options method args, kwargs
Example:apt-get install vim git -t maverick
apt_get.install('vim', 'git', t='maverick')
Set Default Option
>>>apt_get.opts(t='maverick') >>>apt_get.install('vim') ['apt-get', 'install', 'vim', '-t maverick'] >>>apt_get.opts(t=False)
['apt-get', 'install', 'vim']
Demo: ext_zenity1 and ext_zenity2
j
Use UCLTIP to create a CLI tool Python Binding
~ ,
Python Binding with Native Code (Cython or Ctype)
Python Binding (UCLTIP))
Demo: ext_cls
Create a custom Binding class
Another example:https://github.com/hychen/vsgui
Helper function / PIPE
regcmds('ls')
regcmds('apt-get', cls=ucltip.CmdDispatcher)
ucltip.PIPE
UCLTIP Installation
user@host# add-apt-repository ppa:ossug-hychen/python-ucltip
user@host# apt-get install python-ucltip
Source Code can be found in http://github.com/hychen/ucltip
Reference
UCLTIP Homepage :http://pypi.python.org/pypi/ucltip/
UCLTIP Source Code: http://github.com/hychen/ucltip
GitPython: http://gitorious.org/projects/git-python/
Questions?
Open Source Conferences in Taiwan
OSDC - Open Source Developers' Conferencehttp://osdc.tw/
Usually in March or April
A lot Ruby or Perl speaker from Japan gave talks
Sadly, No many Python talks!
COSCUP - Conference for Open Source Coder, Users and Promotershttp://coscup.org
Usually in August
1000+ people this year, biggest open source conference in Taiwan
Creative Commons 3.0 , Hsin-Yi Chen (hychen)
Email: [email protected]
Http://about.me/hychen
--