def write_bash_completion_script(plugins, path):
"""
Parameters
----------
plugins : dict
Decoded JSON object representing CLI state on a per-plugin basis (e.g.
as returned by `DeploymentCache.plugins`). See note within this
function for why this parameter is necessary.
path : str
Path to write completion script to.
"""
import os
import os.path
import stat
import textwrap
from q2cli.__main__ import qiime as root
# `write_bash_completion_script` is called by `q2cli.cache.DeploymentCache`
# when it is refreshing its cache. `q2cli.commands.RootCommand` could have
# already asked for the cache, for example, if the user ran a command and
# the cache must be refreshed. The bash completion script is generated by
# traversing the `RootCommand` tree, so there is a cycle when `RootCommand`
# attempts to access the cache in order to build itself. We work around
# this by bootstrapping the `RootCommand`'s `._plugins` attribute with the
# plugin state that has already been loaded by `DeploymentCache`.
root._plugins = plugins
cmd_reply = _generate_command_reply(root)
cmd_reply = textwrap.indent(cmd_reply, ' ')
completion_script = COMPLETION_SCRIPT_TEMPLATE.format(cmd_reply=cmd_reply)
with open(path, 'w') as fh:
fh.write(completion_script)
# Make bash completion script executable:
# http://stackoverflow.com/a/12792002/3776794
st = os.stat(path)
os.chmod(path, st.st_mode | stat.S_IEXEC)
评论列表
文章目录