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

Save parameters to json file #551

Open
wants to merge 2 commits into
base: 519-policy-api-updates
Choose a base branch
from
Open
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
49 changes: 49 additions & 0 deletions scubagoggles/orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import shutil
import webbrowser

from collections.abc import Callable
from datetime import datetime, timezone
from pathlib import Path
from tqdm import tqdm
Expand All @@ -31,6 +32,44 @@ class UserRuntimeError(RuntimeError):
to do with the code).
"""


class ArgumentsEncoder(json.JSONEncoder):

"""Custom JSON encoder for the ScubaGoggles command line arguments data
structure.
"""

def default(self, o):

"""Handles special encoding for certain fields in the arguments
data structure.

For example, there are some fields in the arguments for internal
use, such as the dispatch function. This is not serializable with
the default encoder.

The Path values are converted to strings.

Any data types not handled by this method are processed by the base
class method.

:param o: data (arbitrary type) to be serialized.

:return: a serializable version of the given object.
"""

# The "dispatch" value is a function and does not need to be
# serialized.

if isinstance(o, Callable):
return None

if isinstance(o, Path):
return str(o)

return super().default(o)


class Orchestrator:

"""The Orchestrator class runs the provider to get the GWS configuration
Expand Down Expand Up @@ -312,6 +351,12 @@ def _run_reporter(self):
total_output.update({'Summary': summary})
total_output.update({'Results': results})

args_dict = vars(args)
param_out_path = out_folder / 'args_param.json'

with param_out_path.open(mode='w', encoding='UTF-8') as parm_file:
json.dump(args_dict, parm_file, indent=4, cls=ArgumentsEncoder)

# Create the ScubaResults files
scuba_results_file = out_folder / f'{args.outputproviderfilename}.json'
with scuba_results_file.open(encoding='UTF-8') as file:
Expand Down Expand Up @@ -389,6 +434,10 @@ def _run_cached(self):
with open(f'{args.outputpath}/{args.outputproviderfilename}.json',
'w', encoding='UTF-8') as provider_file:
json.dump(provider_output, provider_file)
args_dict = vars(args)
param_out_path = os.path.join(args.outputpath, 'args_param.json')
with open(param_out_path, mode='w', encoding='UTF-8') as parm_file:
json.dump(args_dict, parm_file, indent=4, cls=ArgumentsEncoder)

self._rego_eval()
self._run_reporter()
Expand Down
Loading