ucltip @Pyctw 2011

1,132 views
1,093 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,132
On SlideShare
0
From Embeds
0
Number of Embeds
453
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

ucltip @Pyctw 2011

  1. 1. UCLTIP - Use command line tool in Python 陳信屹 , Hsin-Yi Chen (hychen), <ossug.hychen@gmail.com> 2011/06/25, Pyc.tw 2011, ( 蘋果優仕敦南店 ) 本簡報採用 創用 CC 「姓名標示 - 非商業性」 3.0 台灣條款 本簡報圖檔下載於網際網路者,特別聲明為自由軟體推廣演講中進行「合理使用」,請讀者不要任意移置他用。
  2. 2. Who am I? ■ 陳信屹 , hychen ■ AboutMe: http://about.me/hychen ■ Linux Developer, 偶而玩一下大蟒蛇
  3. 3. 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
  4. 4. Command Args and Options ↔ Python function args / kwargs● #/usr/bin/env python from GitPython Import this_idea # ls --quoting-style=c ls(quoting-style=c)
  5. 5. 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)
  6. 6. 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
  7. 7. 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)
  8. 8. 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)
  9. 9. 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)
  10. 10. 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)
  11. 11. command with sub command ■ Without prefix ■ With prefix ■ git ■ Zenity ■ pbuilder ■ ... ■ apt-get ■ ... apt-get install vim zenity --info=text
  12. 12. 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)
  13. 13. 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]
  14. 14. Use UCLTIP to create a CLI tool Python Binding 「這世界滿是假象, 我行的也是邪道。 」 ~ 道士下山 , 徐皓峰
  15. 15. Python Binding with Native Code(Cython or Ctype)
  16. 16. Python Binding (UCLTIP))
  17. 17. Helper function ■ regcmds(ls) ■ regcmds(apt-get, cls=ucltip.CmdDispatcher)
  18. 18. 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
  19. 19. Reference ■ UCLTIP Homepage :http://pypi.python.org/pypi/ucltip/ ■ UCLTIP Source Code: http://github.com/hychen/ucltip ■ GitPython: http://gitorious.org/projects/git-python/
  20. 20. 本簡報授權聲明 ■ 此簡報內容採用 Creative Commons 「姓名標示 – 非商業性 台灣 3.0 版」授權條款 ■ 陳信屹 , Hsin-Yi Chen (hychen) ■ Email: ossug.hychen@gmail.com ■ Http://about.me/hychen

×