跳转到主要内容

与shell和管道交互的小工具

项目描述

Chut!

Chut是一个帮助您与shell管道和命令交互的小工具。

基本上,它将帮助您在Python中编写一些shell脚本。

这更像是一个玩具而不是真正的工具,但……有时可能有用。

它已经与py3.5+ 测试

https://secure.travis-ci.org/gawel/chut.png

完整文档可以在 这里 找到

快速入门

获取 chutify 脚本

$ wget https://raw.github.com/gawel/chut/master/docs/_static/binaries/chutify
$ chmod +x chutify

编写控制台脚本

$ cat << EOF > myscript.py
from chut import *

__version__ = '0.1'

@console_script
def mycmd(args):
    """Usage: %prog [options] <directory>

    Print all chut scripts found in <directory>

    Options:

    %options
    """
    for filename in find('-name *.py') | grep('@console_script'):
        print(filename)
EOF

以开发模式运行 chutify

$ ./chutify --devel
chmod +x bin/mycmd

并使用/调试新创建的脚本

$ ./bin/mycmd -h

当您的脚本准备就绪后,则生成独立版本

$ ./chutify
chmod +x dist/scripts/mycmd

还可以查看 示例

安装

使用pip

$ pip install chut

这将安装docopt,并允许您使用 @console_script 装饰器。

另一个选项是获取 chutify 的独立版本

$ wget https://raw.github.com/gawel/chut/master/docs/_static/binaries/chutify
$ chmod +x chutify

快速入门

导入shell

>>> import chut as sh

获取包含“Chut”的文件内容

>>> grep_chut = sh.cat('README.rst') | sh.grep('Chut')
>>> if grep_chut:
...     print(grep_chut | sh.head("-n1"))
Chut!

将输出重定向到文件

>>> ret = (grep_chut | sh.head("-n1")) > '/tmp/chut.txt'
>>> ret.succeeded
True
>>> print(sh.cat('/tmp/chut.txt'))
Chut!

或将到stdout

>>> sh.cat('/tmp/chut.txt') > 1  # doctest: +SKIP
Chut!

将stdout重定向到stderr

>>> sh.cat('/tmp/chut.txt') > 2  # doctest: +SKIP
Chut!

使用进程池运行多个命令

>>> [ret.succeeded for ret in sh.ls.map(['.', ['-l', '/tmp']])]
[True, True]

使用docopt编写控制台脚本。此脚本将iface作为参数,如果找不到地址则返回代码1

>>> @sh.console_script
... def got_inet_addr(args):
...     """Usage: got_inet_addr <iface>"""
...     if sh.ifconfig(args['<iface>']) | sh.grep('inet addr:'):
...         return 1

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

chut-0.18.tar.gz (514.7 kB 查看散列值)

上传时间

构建分发

chut-0.18-py3-none-any.whl (21.1 kB 查看散列值)

上传时间 Python 3

支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面