Upload
hsinyi-chen
View
1.105
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
本簡報圖檔下載於網際網路者,特別聲明為自由軟體推廣演講中進行「合理使用」,請讀者不要任意移置他用。
本簡報採用 創用 CC「姓名標示-非商業性」3.0 台灣條款
UCLTIP – 另一種 Python 執行 CLI tool 的方法
陳信屹 , Hsin-Yi Chen (hychen), <[email protected]>
2011/0716 大溪國小
Some modules
■ Subprocess
■ No shell
■ subprocess.call
■ subprocess.Popen
■ os.system
■ Pass command string to shell
■ return status code, not value
Introduce UCLTIP
■ 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
●Command Args and Options ↔ Python function args / kwargs
#/usr/bin/env pythonfrom 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)
■ Example
■ user@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
■ Return
■ Success → return result string
■ Failed → raises CommandExecutedFalur
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 duplicate
optargs = ucltip.make_optargs('o' ,('Dir=/var','Dir::Cache=/tmp'))
Cmd('foo')(*optargs, a=True, b=True)
command with sub command
■ Without prefix
■ git
■ pbuilder
■ apt-get
■ ...
■ With prefix
■ Zenity
■ ...
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']
Use UCLTIP to create a CLI tool Python Binding
「這世界滿是假象, 我行的也是邪道。 」
~ 道士下山 , 徐皓峰
Python Binding with Native Code (Cython or Ctype)
Python Binding (UCLTIP))
Helper function
■ regcmds('ls')
■ regcmds('apt-get', cls=ucltip.CmdDispatcher)
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/
Demo
■ Zenity
■ Package Manager
本簡報授權聲明
■ 此簡報內容採用 Creative Commons 「姓名標示 – 非商業性 台灣 3.0 版」授權條款
■ 陳信屹 , Hsin-Yi Chen (hychen)
■ Email: [email protected]
■ Http://about.me/hychen