Skip to content

Commit

Permalink
#469: clean up and comment code
Browse files Browse the repository at this point in the history
  • Loading branch information
cwschilly committed Oct 18, 2023
1 parent 0ceda3f commit 5f98dfa
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions config/update-config-script.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __parse_args(self):


def __read_configuration_file(self, config_path):
"""Convert a yaml into a dict"""
try:
with open(config_path, "r") as file:
config_dict = yaml.load(file)
Expand All @@ -57,17 +58,18 @@ def __read_configuration_file(self, config_path):
return None # Return None in case of errors

def __update_conf(self, config: dict):

"""Make new changes to LBAF_Viz parameters"""
# Look for key parameters
if "output_dir" in config:
output_dir = config["output_dir"]
else:
output_dir = "../output"

if "output_file_stem" in config:
file_stem = config["output_file_stem"]
else:
file_stem = "output_file"

# Make necessary changes
if "LBAF_Viz" in config:
config["visualization"] = config.pop("LBAF_Viz")
config["visualization"]["output_visualization_dir"] = output_dir
Expand All @@ -76,24 +78,25 @@ def __update_conf(self, config: dict):
return config

def __write_config(self, config: dict, output_dir: str, filename: str):
"""Convert a dict into a yaml"""
# Establish output directory
if not os.path.exists(output_dir):
os.makedirs(output_dir)

output_file_path = os.path.join(output_dir, filename)

# Write out yaml
with open(output_file_path, 'w') as file:
yaml.dump(config, file)

self.__logger.info(f"New configuration file written to {output_file_path}")

def initialize(self):

"""Obtain configuration files to be updated""""
# Parse command line arguments
self.__parse_args()

directory = str(self.__args.directory)
filepath = str(self.__args.filepath)

# Update every file in given directory
if directory:
# Check if the directory path is valid
if os.path.exists(directory) and os.path.isdir(directory):
Expand All @@ -105,28 +108,32 @@ def initialize(self):
else:
self.__logger.error(f"The directory path {directory} is invalid.")

# Update specific file
if filepath:
self.run(filepath)

return

def run(self, filepath: str):
"""Update configuration files"""
# Specify output dir and filename
output_dir = str(self.__args.output)
filename = os.path.basename(filepath)

if not os.path.exists(filepath):
self.__logger.error(f"File not found: {filepath}")
return

# Convert file into dict
config = self.__read_configuration_file(filepath)

if config is None:
self.__logger.error(f"Could not find configuration file at {filepath}")
return

# Update configuration dict
self.__logger.info(f"Updating {filename}")

new_config = self.__update_conf(config)

# Validate dict and write out yaml
self.__validate_configuration(new_config)
self.__write_config(new_config, output_dir, filename)

Expand Down

0 comments on commit 5f98dfa

Please sign in to comment.