ucltip introduction@hackingcamp 2011

22
本簡報圖檔下載於網際網路者,特別聲明為自由軟體推廣演講中進行「合理使用」,請讀者不要任意移置他用。 本簡報採用 創用 CC 「姓名標示 - 非商業性」 3.0 台灣條款 UCLTIP – 另一種 Python 執行 CLI tool 的方法 陳信屹 , Hsin-Yi Chen (hychen), <[email protected]> 2011/0716 大溪國小

Upload: hsinyi-chen

Post on 31-Aug-2014

1.105 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: UCLTIP Introduction@HackingCamp  2011

本簡報圖檔下載於網際網路者,特別聲明為自由軟體推廣演講中進行「合理使用」,請讀者不要任意移置他用。

本簡報採用 創用 CC「姓名標示-非商業性」3.0 台灣條款

UCLTIP – 另一種 Python 執行 CLI tool 的方法

陳信屹 , Hsin-Yi Chen (hychen), <[email protected]>

2011/0716 大溪國小

Page 2: UCLTIP Introduction@HackingCamp  2011

Who am I?

■ 陳信屹 , hychen

■ AboutMe: http://about.me/hychen

Page 3: UCLTIP Introduction@HackingCamp  2011

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

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

●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

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

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

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

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

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

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

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

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

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

Use UCLTIP to create a CLI tool Python Binding

「這世界滿是假象, 我行的也是邪道。 」

~ 道士下山 , 徐皓峰

Page 16: UCLTIP Introduction@HackingCamp  2011

Python Binding with Native Code (Cython or Ctype)

Page 17: UCLTIP Introduction@HackingCamp  2011

Python Binding (UCLTIP))

Page 18: UCLTIP Introduction@HackingCamp  2011

Helper function

■ regcmds('ls')

■ regcmds('apt-get', cls=ucltip.CmdDispatcher)

Page 19: UCLTIP Introduction@HackingCamp  2011

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

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

Demo

■ Zenity

■ Package Manager

Page 22: UCLTIP Introduction@HackingCamp  2011

本簡報授權聲明

■ 此簡報內容採用 Creative Commons 「姓名標示 – 非商業性 台灣 3.0 版」授權條款

■ 陳信屹 , Hsin-Yi Chen (hychen)

■ Email: [email protected]

■ Http://about.me/hychen