实用工具¶
内部的实用小程序。;例如:遇到错误信息后终止执行,或者对多行输出进行缩进处理。
-
fabric.utils.abort(msg)¶ 终止执行,向 stderr 输入错误信息
msg并退出(错误状态 1)。This function currently makes use of SystemExit in a manner that is similar to sys.exit (but which skips the automatic printing to stderr, allowing us to more tightly control it via settings).
Therefore, it’s possible to detect and recover from inner calls to
abortby usingexcept SystemExitor similar.
-
fabric.utils.error(message, func=None, exception=None, stdout=None, stderr=None)¶ 给定错误信息
message以调用func。如果
func为None,将根据env.warn_only来调用abort还是warn。如果
exception`参数(应当是字符串类型)存在,将会在用户传入的message周围输出它。如果指定了
stdin和/或者stderr,将作为打印输出的终端。
-
fabric.utils.fastprint(text, show_prefix=False, end='', flush=True)¶ 立即打印
text不添加任何前缀或后缀。该函数只是
puts的别名,区别在参数的迷默认值不同,text将会不加任何装饰地输出出去。当你想要输出可能被 Python 输出所缓冲(比如在计算密集的
for循环中)时会很需要它。Since such use cases typically also require a lack of line endings (such as printing a series of dots to signify progress) it also omits the traditional newline by default.注解
由于
fastprint会调用puts,因此其 output level 也取决于user。0.9.2 新版功能.
参见
-
fabric.utils.indent(text, spaces=4, strip=False)¶ 根据给定空格数缩进
text。如果
text并非字符串,将被当作单行输出的列表,并使用\n连接、排列。若
strip为True,a minimum amount of whitespace is removed from the left-hand side of the given string (so that relative indents are preserved, but otherwise things are left-stripped). 这样你就能有效地“规范化”某些输出前的缩进。
-
fabric.utils.puts(text, show_prefix=None, end='\n', flush=False)¶ print函数的别名,同样受 Fabric 管理输出。换句话说,这个函数只是简单地将输出指向
sys.stdout` ,如果``user将 output level 设置为 False 则隐藏输出。如果
show_prefix=False,puts将略过默认添加的输出头[hostname]。(如果env.host_string为空的话也能起到同样的效果。)设置
end为空字符串''将不会在末尾输出换行。()你可以通过设置
flush=True来强制立即输出(例如绕过输出缓冲)。0.9.2 新版功能.
参见
-
fabric.utils.warn(msg)¶ 打印警告信息,但不退出执行。
该函数遵循 Fabric output controls 如果已开启
warnings等级(默认开启),将会向 stderr 打印msg。