chg: Set up the main running function to use handler functions (#45)

This commit is contained in:
Natalia Maximo 2021-07-23 12:45:29 -04:00 коммит произвёл GitHub
Родитель 127a46123d
Коммит 14e0ad4e93
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 61 добавлений и 24 удалений

Просмотреть файл

@ -75,7 +75,7 @@ setup(
tests_require=extra_dependencies['tests'], tests_require=extra_dependencies['tests'],
extras_require=extra_dependencies, extras_require=extra_dependencies,
entry_points={ entry_points={
'console_scripts': ['quilla = quilla:run'], 'console_scripts': ['quilla = quilla:main'],
'pytest11': [ 'pytest11': [
'quilla = pytest_quilla' 'quilla = pytest_quilla'
] ]

Просмотреть файл

@ -67,7 +67,7 @@ class QuillaItem(pytest.Item):
data retrieved from the JSON file. data retrieved from the JSON file.
''' '''
ctx = setup_context( ctx = setup_context(
[*self.config.getoption('--quilla-opts').split(), ''], [*self.config.getoption('--quilla-opts').split()],
str(self.config.rootpath), str(self.config.rootpath),
recreate_context=True recreate_context=True
) )

Просмотреть файл

@ -30,7 +30,7 @@ def make_parser() -> argparse.ArgumentParser: # pragma: no cover
''' '''
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog='quilla', prog='quilla',
usage='%(prog)s [options] [-f] JSON', # usage='%(prog)s [options] [-f] JSON',
description=''' description='''
Program to provide a report of UI validations given a json representation Program to provide a report of UI validations given a json representation
of the validations or given the filename containing a json document describing of the validations or given the filename containing a json document describing
@ -44,16 +44,22 @@ def make_parser() -> argparse.ArgumentParser: # pragma: no cover
help='Prints the version of the software and quits' help='Prints the version of the software and quits'
) )
parser.add_argument( data_group = parser.add_mutually_exclusive_group()
data_group.add_argument(
'-f', '-f',
'--file', '--file',
dest='is_file', dest='file_name',
action='store_true', action='store',
help='Whether to treat the argument as raw json or as a file', help='A file containing a Quilla test',
default=None,
) )
parser.add_argument( data_group.add_argument(
'json', '-r',
help='The json file name or raw json string', '--raw',
action='store',
help='A Quilla test passed in as a raw string',
default=None,
) )
config_group = parser.add_argument_group(title='Configuration options') config_group = parser.add_argument_group(title='Configuration options')
@ -129,6 +135,9 @@ def make_parser() -> argparse.ArgumentParser: # pragma: no cover
default=0 default=0
) )
# Sets the application handler, i.e. the function that runs when ctx.run() is called
parser.set_defaults(handler=run)
return parser return parser
@ -277,10 +286,10 @@ def setup_context(
if not parsed_args.definitions: if not parsed_args.definitions:
parsed_args.definitions = [] parsed_args.definitions = []
if not parsed_args.is_file: if parsed_args.file_name is None:
json_data = parsed_args.json json_data = parsed_args.raw
else: else:
with open(parsed_args.json) as f: with open(parsed_args.file_name) as f:
json_data = f.read() json_data = f.read()
logger.debug('Initializing context object') logger.debug('Initializing context object')
@ -291,13 +300,14 @@ def setup_context(
parsed_args.drivers_path, parsed_args.drivers_path,
parsed_args.pretty, parsed_args.pretty,
json_data, json_data,
parsed_args.is_file, parsed_args.file_name is not None,
parsed_args.no_sandbox, parsed_args.no_sandbox,
parsed_args.definitions, parsed_args.definitions,
logger=logger, logger=logger,
run_id=parsed_args.run_id, run_id=parsed_args.run_id,
indent=parsed_args.indent, indent=parsed_args.indent,
update_baseline=parsed_args.update_baseline, update_baseline=parsed_args.update_baseline,
args=parsed_args,
recreate_context=recreate_context, recreate_context=recreate_context,
) )
@ -307,15 +317,14 @@ def setup_context(
return ctx return ctx
def run(): def run(ctx: Context):
''' '''
Creates the parser object, parses the command-line arguments, and runs them, finishing with the Runs all reports and prints to stdout while providing the proper
appropriate exit code. exit code
Args:
ctx: The application context
''' '''
ctx = setup_context(sys.argv[1:])
ctx.logger.debug('Context setup complete, running the "execute" function')
reports = execute(ctx) reports = execute(ctx)
ctx.logger.debug('Finished generating reports') ctx.logger.debug('Finished generating reports')
@ -339,5 +348,15 @@ def run():
sys.exit(exit_code) sys.exit(exit_code)
def main():
'''
Creates the context and parses all arguments, then runs the default handler function
'''
ctx = setup_context(sys.argv[1:])
ctx.logger.debug('Context setup complete, running the Quilla handler')
ctx.run()
if __name__ == '__main__': if __name__ == '__main__':
run() main()

Просмотреть файл

@ -2,4 +2,4 @@ import quilla # pragma: no cover
if __name__ == '__main__': # pragma: no cover if __name__ == '__main__': # pragma: no cover
quilla.run() quilla.main()

Просмотреть файл

@ -15,6 +15,7 @@ from logging import (
) )
import json import json
import uuid import uuid
from argparse import Namespace
from pluggy import PluginManager from pluggy import PluginManager
import pydeepmerge as pdm import pydeepmerge as pdm
@ -41,6 +42,7 @@ class Context(DriverHolder):
no_sandbox: Whether to pass the '--no-sandbox' arg to Chrome and Edge no_sandbox: Whether to pass the '--no-sandbox' arg to Chrome and Edge
logger: An optional configured logger instance. logger: An optional configured logger instance.
run_id: A string that uniquely identifies the run of Quilla. run_id: A string that uniquely identifies the run of Quilla.
args: The Namespace object that parsed related arguments, if applicable
update_baseline: Whether the VisualParity baselines should be updated or not update_baseline: Whether the VisualParity baselines should be updated or not
@ -59,6 +61,7 @@ class Context(DriverHolder):
one with the default logger. one with the default logger.
run_id: A string that uniquely identifies the run of Quilla. run_id: A string that uniquely identifies the run of Quilla.
pretty_print_indent: How many spaces to use for indentation when pretty-printing the output pretty_print_indent: How many spaces to use for indentation when pretty-printing the output
args: The Namespace object that parsed related arguments, if applicable
update_baseline: Whether the VisualParity baselines should be updated or not update_baseline: Whether the VisualParity baselines should be updated or not
''' '''
default_context: Optional['Context'] = None default_context: Optional['Context'] = None
@ -85,6 +88,7 @@ class Context(DriverHolder):
logger: Optional[Logger] = None, logger: Optional[Logger] = None,
run_id: Optional[str] = None, run_id: Optional[str] = None,
indent: int = 4, indent: int = 4,
args: Optional[Namespace] = None,
update_baseline: bool = False, update_baseline: bool = False,
): ):
super().__init__() super().__init__()
@ -96,6 +100,7 @@ class Context(DriverHolder):
self.is_file = is_file self.is_file = is_file
self.no_sandbox = no_sandbox self.no_sandbox = no_sandbox
self.pretty_print_indent = indent self.pretty_print_indent = indent
self.args = args
path = Path(drivers_path) path = Path(drivers_path)
if logger is None: if logger is None:
@ -115,6 +120,17 @@ class Context(DriverHolder):
self._context_data: Dict[str, dict] = {'Validation': {}, 'Outputs': {}, 'Definitions': {}} self._context_data: Dict[str, dict] = {'Validation': {}, 'Outputs': {}, 'Definitions': {}}
self._load_definition_files(definitions) self._load_definition_files(definitions)
def run(self):
'''
Runs Quilla, assuming a proper Namespace object with a handler has been passed in.
This function is not guaranteed to do anything, and is just a passthrough to allow
the proper parser handlers to execute, as described in the documentation for ArgParse
https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_subparsers
'''
if self.args is not None:
self.args.handler(self)
@property @property
def outputs(self) -> dict: def outputs(self) -> dict:
''' '''
@ -355,6 +371,7 @@ def get_default_context(
logger: Optional[Logger] = None, logger: Optional[Logger] = None,
run_id: Optional[str] = None, run_id: Optional[str] = None,
indent: int = 4, indent: int = 4,
args: Optional[Namespace] = None,
update_baseline: bool = False, update_baseline: bool = False,
) -> Context: ) -> Context:
''' '''
@ -398,6 +415,7 @@ def get_default_context(
logger, logger,
run_id, run_id,
indent, indent,
update_baseline args,
update_baseline,
) )
return Context.default_context return Context.default_context