UCLTIP Introduction @PyCon Japan 2011

1,416 views

Published on

introduce ucltip in Pycon.JP 2011

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,416
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 主任,老師,各位同仁~午安 我是【自由軟體鑄造場】 的freddi.
  • UCLTIP Introduction @PyCon Japan 2011

    1. 1. UCLTIP – 另一種執行CLI tool的方法 陳信屹, Hsin-Yi Chen (hychen), < [email_address] > 2011/08/27 PyCon JP 2011, 産業技術大学院大学, 東京 本簡報圖檔下載於網際網路者,特別聲明為自由軟體推廣演講中進行「合理使用」,請讀者不要任意移置他用。 本簡報採用 創用 CC 「姓名標示 - 非商業性」 3.0 台灣條款
    2. 2. Who am I? <ul><li>陳信屹 (hychen)
    3. 3. From Taiwan
    4. 4. Debian Maintainer
    5. 5. Work for Canonical OEM Service Team
    6. 6. More about me: http://about.me/hychen </li></ul>
    7. 7. How we execute command line tool in Python? <ul><li>Subprocess is better choice </li><ul><li>Don't use shell
    8. 8. subprocess.call
    9. 9. subprocess.Popen </li></ul><li>os.system </li><ul><li>Pass command string to shell
    10. 10. return status code, not value </li></ul></ul>
    11. 11. Question: Why UCLTIP <ul><li>Provide a hyper interface for executing command line tool with subprocess </li></ul>
    12. 12. Introduce UCLTIP <ul><li>Helper interface of pipeline as C libpipeline
    13. 13. CLI Tool arguments to function/method arguments
    14. 14. CLI Tool Boolean option style to function/method keyword arguments
    15. 15. CLI Tool Key-Value option style to function/method keyword arguments
    16. 16. CLI Tool Key-Value option style to function/method keyword arguments
    17. 17. CLI Command as Python Class and use it in your script </li></ul>
    18. 18. Pipe <ul><li>>>>pipe = ucltip.Pipe()
    19. 19. >>>pipe.add('expr', 1, '+' 3)
    20. 20. >>>pipe.add('sed', 's/4/5/', '--posix')
    21. 21. >>>pipe.wait()
    22. 22. >>>pipe.stdout.read() </li></ul>
    23. 23. Demo: pipe
    24. 24. <ul><li>Command Args and Options ↔ Python function args / kwargs </li></ul>#/usr/bin/env python from GitPython Import this_idea # ls --quoting-style=c ls(quoting_style= 'c' )
    25. 25. Transform CLI Tool arguments <ul><li>The command string combine the option string if the value is True(boolean)
    26. 26. Example </li><ul><li>[email_address] : expr 1 + 3 -> expr(1, '+', 3) </li></ul></ul>
    27. 27. UCLTIP - Cmd <ul><li>For command without sub command
    28. 28. Create a callable instance by implemented __call__
    29. 29. Raises ComandNotFound if command not exists
    30. 30. Return </li><ul><li>Success -> return result string
    31. 31. Failed -> raises CommandExecutedFalur </li></ul></ul>
    32. 32. Demo: ex_get and ex_err To get a result
    33. 33. Transform CLI Tool Boolean option style <ul><li>The command string combine the option string if the value is True(boolean)
    34. 34. Example </li><ul><li>-d -> func(d=True)
    35. 35. --dry-run -> func(dry_run=True) </li></ul></ul>
    36. 36. Transform CLI Tool Key-Value option style <ul><li>The command string will execute combines If the option value is string or number
    37. 37. Example: </li><ul><li>-t maverick -> func(t='maverick')
    38. 38. --text hello -> func(text='hello') </li></ul></ul>
    39. 39. Transform CLI Tool Key-Value option style II <ul><li>The command string will execute combines If the option value is string or number
    40. 40. Example: </li><ul><li>-t = maverick -> func(t='maverick')
    41. 41. --text = hello -> func(text='hello') </li></ul></ul>
    42. 42. Same option name, but more than 1 <ul># `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) </ul>
    43. 43. command with sub command <ul><li>Without prefix </li><ul><li>git
    44. 44. pbuilder
    45. 45. apt-get
    46. 46. ... </li></ul></ul><ul><li>With prefix </li><ul><li>Zenity
    47. 47. ... </li></ul></ul>apt-get install vim zenity --info=text
    48. 48. UCLTIP - CmdDispatcher <ul><li>For CLI tool has sub command
    49. 49. Command name -> instance
    50. 50. Subcommand name -> method name
    51. 51. args, options -> method args, kwargs
    52. 52. Example: </li><ul><li>apt-get install vim git -t maverick
    53. 53. apt_get . install ( 'vim' , 'git' , t='maverick' ) </li></ul></ul>
    54. 54. Set Default Option <ul>>>>apt_get.opts(t='maverick') >>>apt_get.install('vim') ['apt-get', 'install', 'vim', '-t maverick'] <ul>>>>apt_get.opts(t=False) </ul>['apt-get', 'install', 'vim'] </ul>
    55. 55. Demo: ext_zenity1 and ext_zenity2 <ul>j </ul>
    56. 56. Use UCLTIP to create a CLI tool Python Binding 「這世界滿是假象, 我行的也是邪道。 」 ~ 道士下山 , 徐皓峰
    57. 57. Python Binding with Native Code (Cython or Ctype)
    58. 58. Python Binding (UCLTIP))
    59. 59. Demo: ext_cls <ul><li>Create a custom Binding class
    60. 60. Another example: </li><ul><li>https://github.com/hychen/vsgui </li></ul></ul>
    61. 61. Helper function / PIPE <ul><li>regcmds('ls')
    62. 62. regcmds('apt-get', cls=ucltip.CmdDispatcher)
    63. 63. ucltip.PIPE </li></ul>
    64. 64. UCLTIP Installation <ul><li>user@host# add-apt-repository ppa:ossug-hychen/python-ucltip
    65. 65. [email_address] # apt-get install python-ucltip
    66. 66. Source Code can be found in http://github.com/hychen/ucltip </li></ul>
    67. 67. Reference <ul><li>UCLTIP Homepage :http://pypi.python.org/pypi/ucltip/
    68. 68. UCLTIP Source Code: http://github.com/hychen/ucltip
    69. 69. GitPython: http://gitorious.org/projects/git-python/ </li></ul>
    70. 70. Questions? <ul>謝謝聆聽 </ul>
    71. 71. Open Source Conferences in Taiwan <ul><li>OSDC - Open Source Developers' Conference </li><ul><li>http://osdc.tw/
    72. 72. Usually in March or April
    73. 73. A lot Ruby or Perl speaker from Japan gave talks
    74. 74. Sadly, No many Python talks! </li></ul><li>COSCUP - Conference for Open Source Coder, Users and Promoters </li><ul><li>http://coscup.org
    75. 75. Usually in August
    76. 76. 1000+ people this year, biggest open source conference in Taiwan </li></ul></ul>
    77. 77. 本簡報授權聲明 <ul><li>此簡報內容採用 Creative Commons 「姓名標示 – 非商業性 台灣 3.0 版」 授權條款 </li><ul><li>陳信屹 , Hsin-Yi Chen (hychen)
    78. 78. Email: [email_address]
    79. 79. Http://about.me/hychen </li></ul></ul>

    ×