From 2dfcf26571d305f834b91cc4b7e7ca7360dc30b4 Mon Sep 17 00:00:00 2001 From: Shawn Davis Date: Wed, 22 Jul 2020 16:04:48 -0400 Subject: [PATCH] Fixed syntax error when initializing variables from a file. --- VERSION.txt | 2 +- scripttease/cli/__init__.py | 16 ++++---- scripttease/cli/initialize.py | 70 ++++++++++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 9 deletions(-) diff --git a/VERSION.txt b/VERSION.txt index f706c76..564bbb7 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -6.0.0-d \ No newline at end of file +6.0.1-d \ No newline at end of file diff --git a/scripttease/cli/__init__.py b/scripttease/cli/__init__.py index dd16d59..fc6ff13 100644 --- a/scripttease/cli/__init__.py +++ b/scripttease/cli/__init__.py @@ -18,13 +18,13 @@ def main_command(): """Process script configurations.""" __author__ = "Shawn Davis " - __date__ = "2020-07-21" + __date__ = "2020-07-22" __help__ = """NOTES This command is used to parse configuration files and output the commands. """ - __version__ = "0.10.0-d" + __version__ = "6.0.1-d" # Main argument parser from which sub-commands are created. parser = ArgumentParser(description=__doc__, epilog=__help__, formatter_class=RawDescriptionHelpFormatter) @@ -160,6 +160,12 @@ This command is used to parse configuration files and output the commands. if args.variables: context = initialize.context_from_cli(args.variables) + # Load additional context from file. + if args.variables_file: + variables = initialize.variable_from_file(args.variables_file) + if variables: + context.update(variables) + # Handle filters. filters = None if args.filters: @@ -170,11 +176,7 @@ This command is used to parse configuration files and output the commands. if args.options: options = initialize.options_from_cli(args.options) - if args.variables_file: - variables = initialize.variable_from_file(args.variables_file) - if variables: - context.update(variables) - + # Process the request. if args.docs_enabled: exit_code = subcommands.output_docs( args.path, diff --git a/scripttease/cli/initialize.py b/scripttease/cli/initialize.py index 4ec2f87..1ec6e7b 100644 --- a/scripttease/cli/initialize.py +++ b/scripttease/cli/initialize.py @@ -8,10 +8,29 @@ from ..constants import LOGGER_NAME log = logging.getLogger(LOGGER_NAME) +# Exports + +__all__ = ( + "context_from_cli", + "filters_from_cli", + "options_from_cli", + "variable_from_file", +) + # Functions def context_from_cli(variables): + """Takes a list of variables given in the form of ``name:value`` and converts them to a dictionary. + + :param variables: A list of strings of ``name:value`` pairs. + :type variables: list[str] + + :rtype: dict + + The ``value`` of the pair passes through "smart casting" to convert it to the appropriate Python data type. + + """ context = dict() for i in variables: key, value = i.split(":") @@ -21,6 +40,14 @@ def context_from_cli(variables): def filters_from_cli(filters): + """Takes a list of filters given in the form of ``name:value`` and converts them to a dictionary. + + :param filters: A list of strings of ``attribute:value`` pairs. + :type filters: list[str] + + :rtype: dict + + """ _filters = dict() for i in filters: key, value = i.split(":") @@ -33,6 +60,16 @@ def filters_from_cli(filters): def options_from_cli(options): + """Takes a list of variables given in the form of ``name:value`` and converts them to a dictionary. + + :param options: A list of strings of ``name:value`` pairs. + :type options: list[str] + + :rtype: dict + + The ``value`` of the pair passes through "smart casting" to convert it to the appropriate Python data type. + + """ _options = dict() for i in options: key, value = i.split(":") @@ -42,6 +79,37 @@ def options_from_cli(options): def variable_from_file(path): + """Loads variables from a given INI file. + + :param path: The path to the INI file. + :type path: str + + :rtype: dict | None + + The resulting dictionary flattens the sections and values. For example: + + .. code-block:: ini + + [copyright] + name = ACME, Inc. + year = 2020 + + [domain] + name = example.com + tld = example_com + + The dictionary would contain: + + .. code-block:: python + + { + 'copyright_name': "ACME, Inc.", + 'copyright_year': 2020, + 'domain_name': "example.com", + 'domain_tld': "example_com", + } + + """ if not os.path.exists(path): log.warning("Variables file does not exist: %s" % path) return None @@ -53,6 +121,6 @@ def variable_from_file(path): for section in ini.sections(): for key, value in ini.items(section): key = "%s_%s" % (section, key) - variables[key] = smart_cast(vaue) + variables[key] = smart_cast(value) return variables