Parse json file

This commit is contained in:
zandercymatics 2023-11-07 14:58:24 -07:00
parent cb9b135178
commit eca8852d00
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
3 changed files with 37 additions and 19 deletions

View file

@ -1,3 +1,4 @@
import json
import sys
import csv
import logging
@ -41,7 +42,7 @@ class Command(BaseCommand):
for testing purposes, but USE WITH CAUTION
"""
parser.add_argument(
"migration_Json_filename",
"migration_json_filename",
help=(
"A JSON file that holds the location and filenames"
"of all the data files used for migrations"
@ -319,10 +320,32 @@ class Command(BaseCommand):
def handle( # noqa: C901
self,
migration_Json_filename,
migration_json_filename,
**options,
):
"""Parse the data files and create TransitionDomains."""
### Process JSON file ###
# If a JSON was provided, use its values instead of defaults.
# TODO: there is no way to discern user overrides from those args defaults.
with open(migration_json_filename, "r") as jsonFile:
# load JSON object as a dictionary
try:
data = json.load(jsonFile)
# Create an instance of TransitionDomainArguments
args = TransitionDomainArguments()
# Iterate over the data from the JSON file
for key, value in data.items():
# Check if the key exists in TransitionDomainArguments
if hasattr(args, key):
# If it does, update the options
options[key] = value
except Exception as err:
logger.error(f"""{TerminalColors.FAIL}There was an error loading the JSON responsible
for providing filepaths.
{TerminalColors.ENDC}
""")
raise err
sep = options.get("sep")
# If --resetTable was used, prompt user to confirm
@ -346,9 +369,9 @@ class Command(BaseCommand):
# Main script filenames
# TODO: @ZANDER to replace this with new TransitionDomainArgument object
domain_contacts_filename = directory+ options.get("domain_contacts_filename")
contacts_filename = directory+ options.get("contacts_filename")
domain_statuses_filename = directory+ options.get("domain_statuses_filename")
domain_contacts_filename = directory + "/" + options.get("domain_contacts_filename")
contacts_filename = directory + "/" + options.get("contacts_filename")
domain_statuses_filename = directory + "/" + options.get("domain_statuses_filename")
# Agency information
agency_adhoc_filename = options.get("agency_adhoc_filename")

View file

@ -99,7 +99,7 @@ class Command(BaseCommand):
# TODO: make this a mandatory argument (if/when we strip out defaults, it will be mandatory)
# TODO: use the migration directory arg or force user to type FULL filepath?
parser.add_argument(
"migration_Json_filename",
"migration_json_filename",
help=(
"A JSON file that holds the location and filenames"
"of all the data files used for migrations"
@ -281,11 +281,8 @@ class Command(BaseCommand):
# ======================================================
def run_load_transition_domain_script(
self,
migration_Json_filename: str,
migration_json_filename: str,
file_location: str,
domain_contacts_filename: str,
contacts_filename: str,
domain_statuses_filename: str,
sep: str,
reset_table: bool,
debug_on: bool,
@ -297,7 +294,7 @@ class Command(BaseCommand):
command_script = "load_transition_domain"
command_string = (
f"./manage.py {command_script} "
f"{file_location+migration_Json_filename}"
f"{file_location+migration_json_filename}"
)
if sep is not None and sep != "|":
command_string += f"--sep {sep} "
@ -321,7 +318,7 @@ class Command(BaseCommand):
if proceed:
call_command(
command_script,
f"{file_location+migration_Json_filename}",
f"{file_location+migration_json_filename}",
sep=sep,
resetTable=reset_table,
debug=debug_on,
@ -368,7 +365,7 @@ class Command(BaseCommand):
def run_migration_scripts(
self,
migration_Json_filename,
migration_json_filename,
file_location,
domain_contacts_filename,
contacts_filename,
@ -420,11 +417,8 @@ class Command(BaseCommand):
# Proceed executing the migration scripts
self.run_load_transition_domain_script(
migration_Json_filename,
migration_json_filename,
file_location,
domain_contacts_filename,
contacts_filename,
domain_statuses_filename,
sep,
reset_table,
debug_on,
@ -435,7 +429,7 @@ class Command(BaseCommand):
def handle(
self,
migration_Json_filename,
migration_json_filename,
**options,
):
"""
@ -530,7 +524,7 @@ class Command(BaseCommand):
# Run migration scripts
self.run_migration_scripts(
migration_Json_filename,
migration_json_filename,
file_location,
domain_contacts_filename,
contacts_filename,

View file

@ -46,6 +46,7 @@ class TransitionDomainArguments:
domain_additional_filename: Optional[str] = field(default=EnumFilenames.DOMAIN_ADDITIONAL.value[1], repr=True)
domain_contacts_filename: Optional[str] = field(default=None, repr=True)
domain_statuses_filename: Optional[str] = field(default=None, repr=True)
contacts_filename: Optional[str] = field(default=None, repr=True)
# Flags #
debug: Optional[bool] = field(default=False, repr=True)