A collection of classes and commands for automated command line scripting using Python.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.2 KiB

# Imports
from commonkit import copy_tree, highlight_code, indent, write_file
from commonkit.shell import EXIT
from markdown import markdown
import os
from ..lib.factories import command_factory
from ..constants import PROFILE
from ..variables import PATH_TO_SCRIPT_TEASE
4 years ago
# Exports
__all__ = (
"copy_inventory",
"generate_docs",
"generate_script",
4 years ago
)
# Functions
def copy_inventory(name, to_path=None):
"""Copy an inventory item to a path.
4 years ago
:param name: The name of the inventory item. ``?`` will list available items.
:type name: str
4 years ago
:param to_path: The path to where the item should be copied. Defaults to the current working directory.
:type to_path: str
4 years ago
:rtype: int
:returns: An exit code.
"""
path = os.path.join(PATH_TO_SCRIPT_TEASE, "data", "inventory")
if name == "?":
for d in os.listdir(path):
print(d)
return EXIT.OK
from_path = os.path.join(path, name)
if to_path is None:
to_path = os.path.join(os.getcwd(), name)
os.makedirs(to_path)
if copy_tree(from_path, to_path):
return EXIT.OK
return EXIT.ERROR
4 years ago
def generate_docs(loader, output_file=None, output_format="md", profile=PROFILE.UBUNTU):
"""Generate documentation from a commands file.
4 years ago
:param loader: The loader instance.
:type loader: BaseType[scripttease.lib.loaders.BaseLoader]
4 years ago
:param output_file: The path to the output file.
:type output_file: str
4 years ago
:param output_format: The output format; ``html``, ``md`` (Markdown, the default), ``plain`` (text), ``rst``
(ReStructuredText).
:type output_format: str
4 years ago
:param profile: The operating system profile to use.
:type profile: str
4 years ago
:rtype: int
:returns: An exit code.
"""
commands = command_factory(loader, profile=profile)
if commands is None:
return EXIT.ERROR
output = list()
for command in commands:
if command.name in ("explain", "screenshot"):
output.append(command.get_output(output_format))
elif command.name == "template":
if output_format == "plain":
output.append("+++")
output.append(command.get_content())
output.append("+++")
output.append("")
elif output_format == "rst":
output.append(".. code-block:: %s" % command.get_target_language())
output.append("")
output.append(indent(command.get_content()))
output.append("")
else:
output.append("```%s" % command.get_target_language())
output.append(command.get_content())
output.append("```")
output.append("")
else:
statement = command.get_statement(include_comment=False, include_register=False, include_stop=False)
if statement is not None:
line = command.comment.replace("#", "")
output.append("%s:" % line.capitalize())
output.append("")
if output_format == "plain":
output.append("---")
output.append(statement)
output.append("---")
output.append("")
elif output_format == "rst":
output.append(".. code-block:: bash")
output.append("")
output.append(indent(statement))
output.append("")
else:
output.append("```bash")
output.append(statement)
output.append("```")
output.append("")
if output_format == "html":
_output = markdown("\n".join(output), extensions=['fenced_code'])
else:
_output = "\n".join(output)
print(_output)
if output_file:
write_file(output_file, _output)
return EXIT.OK
def generate_script(loader, color_enabled=False, include_shebang=False, output_file=None, profile=PROFILE.UBUNTU):
"""Generate statements from a commands file.
4 years ago
:param loader: The loader instance.
:type loader: BaseType[scripttease.lib.loaders.BaseLoader]
4 years ago
:param color_enabled: Colorize the output.
4 years ago
:type color_enabled: bool
:param include_shebang: Add the shebang to the beginning of the output.
:type include_shebang: bool
4 years ago
:param output_file: The path to the output file.
:type output_file: str
4 years ago
:param profile: The operating system profile to use.
:type profile: str
4 years ago
:rtype: int
:returns: An exit code.
"""
commands = command_factory(loader, profile=profile)
if commands is None:
return EXIT.ERROR
output = list()
if include_shebang:
output.append("#! /usr/bin/env bash")
for command in commands:
if command.name in ("explain", "screenshot"):
continue
statement = command.get_statement(include_comment=True)
if statement is not None:
output.append(statement)
output.append("")
if color_enabled:
print(highlight_code("\n".join(output), language="bash"))
else:
print("\n".join(output))
if output_file:
write_file(output_file, "\n".join(output))
return EXIT.OK