用 Python 编写插件
2010-09-22 11:14:14 来源:WEB开发网运行这个示例会产生以下输出:
# python pathtool_cli.py
Usage: pathtool [starting directory][action]
A command line tool for walking a filesystem. Takes callback 'Action'
functions as plugins. example: pathtool_cli /tmp print_path_ext
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-p PATTERN, --pattern=PATTERN
Pattern Match Examples: *.txt, *.iso, music[0-5].mp3
plain number defaults to * or match all.
Uses UNIX standard wildcard syntax.
-l, --list lists available action plugins
在这个命令的输出中可以看到,这个工具需要一个完整路径,然后是一个 “动作”。动作是开发人员创建的一个插件。我增加了一个命令行列表选项,让这个命令行工具的用户可以看到可用的插件。看一下它的输出:
# python pathtool_cli.py -l
Action Plugins Available:
move_to_tmp
print_file_path_ext
即使不太了解这个工具的工作原理,也能够通过动作的名称猜出它会执行哪些操作。我编写的 print_file_path_ext 动作仅仅输出路径、文件名和扩展名,运行它,看看它的输出:
# python pathtool_cli.py /tmp print_file_path_ext
/tmp/foo0.txt | foo0.txt | .txt
/tmp/foo1.txt | foo1.txt | .txt
/tmp/foo10.txt | foo10.txt | .txt
/tmp/foo2.txt | foo2.txt | .txt
/tmp/foo3.txt | foo3.txt | .txt
/tmp/foo4.txt | foo4.txt | .txt
/tmp/foo5.txt | foo5.txt | .txt
/tmp/foo6.txt | foo6.txt | .txt
/tmp/foo7.txt | foo7.txt | .txt
/tmp/foo8.txt | foo8.txt | .txt
/tmp/foo9.txt | foo9.txt | .txt
更多精彩
赞助商链接