Fixed syntax error when initializing variables from a file.

development
Shawn Davis 4 years ago
parent 244ee5da6d
commit 2dfcf26571
  1. 2
      VERSION.txt
  2. 16
      scripttease/cli/__init__.py
  3. 70
      scripttease/cli/initialize.py

@ -1 +1 @@
6.0.0-d 6.0.1-d

@ -18,13 +18,13 @@ def main_command():
"""Process script configurations.""" """Process script configurations."""
__author__ = "Shawn Davis <shawn@develmaycare.com>" __author__ = "Shawn Davis <shawn@develmaycare.com>"
__date__ = "2020-07-21" __date__ = "2020-07-22"
__help__ = """NOTES __help__ = """NOTES
This command is used to parse configuration files and output the commands. 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. # Main argument parser from which sub-commands are created.
parser = ArgumentParser(description=__doc__, epilog=__help__, formatter_class=RawDescriptionHelpFormatter) 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: if args.variables:
context = initialize.context_from_cli(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. # Handle filters.
filters = None filters = None
if args.filters: if args.filters:
@ -170,11 +176,7 @@ This command is used to parse configuration files and output the commands.
if args.options: if args.options:
options = initialize.options_from_cli(args.options) options = initialize.options_from_cli(args.options)
if args.variables_file: # Process the request.
variables = initialize.variable_from_file(args.variables_file)
if variables:
context.update(variables)
if args.docs_enabled: if args.docs_enabled:
exit_code = subcommands.output_docs( exit_code = subcommands.output_docs(
args.path, args.path,

@ -8,10 +8,29 @@ from ..constants import LOGGER_NAME
log = logging.getLogger(LOGGER_NAME) log = logging.getLogger(LOGGER_NAME)
# Exports
__all__ = (
"context_from_cli",
"filters_from_cli",
"options_from_cli",
"variable_from_file",
)
# Functions # Functions
def context_from_cli(variables): 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() context = dict()
for i in variables: for i in variables:
key, value = i.split(":") key, value = i.split(":")
@ -21,6 +40,14 @@ def context_from_cli(variables):
def filters_from_cli(filters): 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() _filters = dict()
for i in filters: for i in filters:
key, value = i.split(":") key, value = i.split(":")
@ -33,6 +60,16 @@ def filters_from_cli(filters):
def options_from_cli(options): 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() _options = dict()
for i in options: for i in options:
key, value = i.split(":") key, value = i.split(":")
@ -42,6 +79,37 @@ def options_from_cli(options):
def variable_from_file(path): 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): if not os.path.exists(path):
log.warning("Variables file does not exist: %s" % path) log.warning("Variables file does not exist: %s" % path)
return None return None
@ -53,6 +121,6 @@ def variable_from_file(path):
for section in ini.sections(): for section in ini.sections():
for key, value in ini.items(section): for key, value in ini.items(section):
key = "%s_%s" % (section, key) key = "%s_%s" % (section, key)
variables[key] = smart_cast(vaue) variables[key] = smart_cast(value)
return variables return variables

Loading…
Cancel
Save