Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
tests_require=extra_dependencies['tests'],
extras_require=extra_dependencies,
entry_points={
'console_scripts': ['quilla = quilla:run'],
'console_scripts': ['quilla = quilla:main'],
'pytest11': [
'quilla = pytest_quilla'
]
Expand Down
2 changes: 1 addition & 1 deletion src/pytest_quilla/pytest_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def runtest(self):
data retrieved from the JSON file.
'''
ctx = setup_context(
[*self.config.getoption('--quilla-opts').split(), ''],
[*self.config.getoption('--quilla-opts').split()],
str(self.config.rootpath),
recreate_context=True
)
Expand Down
59 changes: 39 additions & 20 deletions src/quilla/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def make_parser() -> argparse.ArgumentParser: # pragma: no cover
'''
parser = argparse.ArgumentParser(
prog='quilla',
usage='%(prog)s [options] [-f] JSON',
# usage='%(prog)s [options] [-f] JSON',
description='''
Program to provide a report of UI validations given a json representation
of the validations or given the filename containing a json document describing
Expand All @@ -44,16 +44,22 @@ def make_parser() -> argparse.ArgumentParser: # pragma: no cover
help='Prints the version of the software and quits'
)

parser.add_argument(
data_group = parser.add_mutually_exclusive_group()

data_group.add_argument(
'-f',
'--file',
dest='is_file',
action='store_true',
help='Whether to treat the argument as raw json or as a file',
dest='file_name',
action='store',
help='A file containing a Quilla test',
default=None,
)
parser.add_argument(
'json',
help='The json file name or raw json string',
data_group.add_argument(
'-r',
'--raw',
action='store',
help='A Quilla test passed in as a raw string',
default=None,
)

config_group = parser.add_argument_group(title='Configuration options')
Expand Down Expand Up @@ -129,6 +135,9 @@ def make_parser() -> argparse.ArgumentParser: # pragma: no cover
default=0
)

# Sets the application handler, i.e. the function that runs when ctx.run() is called
parser.set_defaults(handler=run)

return parser


Expand Down Expand Up @@ -277,10 +286,10 @@ def setup_context(
if not parsed_args.definitions:
parsed_args.definitions = []

if not parsed_args.is_file:
json_data = parsed_args.json
if parsed_args.file_name is None:
json_data = parsed_args.raw
else:
with open(parsed_args.json) as f:
with open(parsed_args.file_name) as f:
json_data = f.read()

logger.debug('Initializing context object')
Expand All @@ -291,13 +300,14 @@ def setup_context(
parsed_args.drivers_path,
parsed_args.pretty,
json_data,
parsed_args.is_file,
parsed_args.file_name is not None,
parsed_args.no_sandbox,
parsed_args.definitions,
logger=logger,
run_id=parsed_args.run_id,
indent=parsed_args.indent,
update_baseline=parsed_args.update_baseline,
args=parsed_args,
recreate_context=recreate_context,
)

Expand All @@ -307,15 +317,14 @@ def setup_context(
return ctx


def run():
'''
Creates the parser object, parses the command-line arguments, and runs them, finishing with the
appropriate exit code.
def run(ctx: Context):
'''
ctx = setup_context(sys.argv[1:])

ctx.logger.debug('Context setup complete, running the "execute" function')
Runs all reports and prints to stdout while providing the proper
exit code

Args:
ctx: The application context
'''
reports = execute(ctx)

ctx.logger.debug('Finished generating reports')
Expand All @@ -339,5 +348,15 @@ def run():
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__':
run()
main()
2 changes: 1 addition & 1 deletion src/quilla/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


if __name__ == '__main__': # pragma: no cover
quilla.run()
quilla.main()
20 changes: 19 additions & 1 deletion src/quilla/ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
)
import json
import uuid
from argparse import Namespace

from pluggy import PluginManager
import pydeepmerge as pdm
Expand All @@ -41,6 +42,7 @@ class Context(DriverHolder):
no_sandbox: Whether to pass the '--no-sandbox' arg to Chrome and Edge
logger: An optional configured logger instance.
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


Expand All @@ -59,6 +61,7 @@ class Context(DriverHolder):
one with the default logger.
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
args: The Namespace object that parsed related arguments, if applicable
update_baseline: Whether the VisualParity baselines should be updated or not
'''
default_context: Optional['Context'] = None
Expand All @@ -85,6 +88,7 @@ def __init__(
logger: Optional[Logger] = None,
run_id: Optional[str] = None,
indent: int = 4,
args: Optional[Namespace] = None,
update_baseline: bool = False,
):
super().__init__()
Expand All @@ -96,6 +100,7 @@ def __init__(
self.is_file = is_file
self.no_sandbox = no_sandbox
self.pretty_print_indent = indent
self.args = args
path = Path(drivers_path)

if logger is None:
Expand All @@ -115,6 +120,17 @@ def __init__(
self._context_data: Dict[str, dict] = {'Validation': {}, 'Outputs': {}, '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
def outputs(self) -> dict:
'''
Expand Down Expand Up @@ -355,6 +371,7 @@ def get_default_context(
logger: Optional[Logger] = None,
run_id: Optional[str] = None,
indent: int = 4,
args: Optional[Namespace] = None,
update_baseline: bool = False,
) -> Context:
'''
Expand Down Expand Up @@ -398,6 +415,7 @@ def get_default_context(
logger,
run_id,
indent,
update_baseline
args,
update_baseline,
)
return Context.default_context