ucltip introduction@hackingcamp 2011
DESCRIPTION
TRANSCRIPT
![Page 1: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/1.jpg)
本簡報圖檔下載於網際網路者,特別聲明為自由軟體推廣演講中進行「合理使用」,請讀者不要任意移置他用。
本簡報採用 創用 CC「姓名標示-非商業性」3.0 台灣條款
UCLTIP – 另一種 Python 執行 CLI tool 的方法
陳信屹 , Hsin-Yi Chen (hychen), <[email protected]>
2011/0716 大溪國小
![Page 3: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/3.jpg)
Some modules
■ Subprocess
■ No shell
■ subprocess.call
■ subprocess.Popen
■ os.system
■ Pass command string to shell
■ return status code, not value
![Page 4: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/4.jpg)
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
![Page 5: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/5.jpg)
●Command Args and Options ↔ Python function args / kwargs
#/usr/bin/env pythonfrom GitPython Import this_idea
# ls --quoting-style=cls(quoting_style='c')
![Page 6: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/6.jpg)
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)
![Page 7: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/7.jpg)
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
![Page 8: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/8.jpg)
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)
![Page 9: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/9.jpg)
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')
![Page 10: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/10.jpg)
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')
![Page 11: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/11.jpg)
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)
![Page 12: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/12.jpg)
command with sub command
■ Without prefix
■ git
■ pbuilder
■ apt-get
■ ...
■ With prefix
■ Zenity
■ ...
apt-get install vim zenity --info=text
![Page 13: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/13.jpg)
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')
![Page 14: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/14.jpg)
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']
![Page 15: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/15.jpg)
Use UCLTIP to create a CLI tool Python Binding
「這世界滿是假象, 我行的也是邪道。 」
~ 道士下山 , 徐皓峰
![Page 16: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/16.jpg)
Python Binding with Native Code (Cython or Ctype)
![Page 17: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/17.jpg)
Python Binding (UCLTIP))
![Page 18: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/18.jpg)
Helper function
■ regcmds('ls')
■ regcmds('apt-get', cls=ucltip.CmdDispatcher)
![Page 19: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/19.jpg)
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
![Page 20: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/20.jpg)
Reference
■ UCLTIP Homepage :http://pypi.python.org/pypi/ucltip/
■ UCLTIP Source Code: http://github.com/hychen/ucltip
■ GitPython: http://gitorious.org/projects/git-python/
![Page 21: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/21.jpg)
Demo
■ Zenity
■ Package Manager
![Page 22: UCLTIP Introduction@HackingCamp 2011](https://reader034.vdocuments.mx/reader034/viewer/2022051512/54034b378d7f72e04c8b45d9/html5/thumbnails/22.jpg)
本簡報授權聲明
■ 此簡報內容採用 Creative Commons 「姓名標示 – 非商業性 台灣 3.0 版」授權條款
■ 陳信屹 , Hsin-Yi Chen (hychen)
■ Email: [email protected]
■ Http://about.me/hychen