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

Field table yaml updates #52

Merged
merged 14 commits into from
Jul 30, 2024
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
174 changes: 83 additions & 91 deletions fms_yaml_tools/field_table/combine_field_table_yamls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,63 +21,86 @@

from os import path, strerror
import errno
import argparse
import click
import yaml
from .. import __version__

""" Combines a series of field_table.yaml files into one file
Author: Uriel Ramirez 11/20/2023
"""


def is_duplicate(field_table, new_entry):
@click.command()
@click.argument('in-files', nargs=-1)
@click.option('--debug/--no-debug', type=click.BOOL, show_default=True, default=False,
help="Print steps in the conversion")
@click.option('--output-yaml', type=click.STRING, show_default=True, default="field_table.yaml",
help="Path to the output field yable yaml")
@click.option('--force-write/--no-force-write', type=click.BOOL, show_default=True, default=False,
help="Overwrite the output yaml file if it already exists")
@click.version_option(__version__, "--version")
def combine_field_table_yaml(in_files, debug, output_yaml, force_write):
""" Combines a series of field_table.yaml files into one file \n
in-files - Space seperated list with the names of the field_table.yaml files to combine \n
"""
Check if a field_table entry was already defined in a different file
verboseprint = print if debug else lambda *a, **k: None
try:
field_table = combine_yaml(in_files, verboseprint)
out_file_op = "x" # Exclusive write
if force_write:
out_file_op = "w"
verboseprint("Writing the output yaml: " + output_yaml)
with open(output_yaml, out_file_op) as myfile:
yaml.dump(field_table, myfile, default_flow_style=False)

Args:
field_table: List of dictionaries containing all of the field_table
entries that have been combined
new_entry: Dictionary of the field_table entry to check
"""
is_duplicate = False
return is_duplicate
except Exception as err:
raise SystemExit(err)


def field_type_exists(field_type, curr_entries):
for entry in curr_entries:
if field_type == entry['field_type']:
return True
return False

def add_new_field(new_entry, curr_entries):
new_field_type = new_entry['field_type']
for entry in curr_entries:
if new_field_type == entry['field_type']:
if entry == new_entry:
# If the field_type already exists but it is exactly the same, move on
continue
new_modlist = new_entry['modlist']
for mod in new_modlist:
if model_type_exists(mod['model_type'], entry):
add_new_mod(mod, entry)
else:
#If the model type does not exist, just append it
entry['modlist'].append(mod)

def add_new_mod(new_mod, curr_entries):
model_type = new_mod['model_type']
for entry in curr_entries['modlist']:
if model_type == entry['model_type']:
if new_mod == entry:
# If the model_type already exists but it is exactly the same, move on
continue
new_varlist = new_mod['varlist']
curr_varlist = entry['varlist']
for new_var in new_varlist:
for curr_var in curr_varlist:
if new_var == curr_var:
continue
curr_varlist.append(new_var)
for entry in curr_entries:
if field_type == entry['field_type']:
return True
return False


def add_new_field(new_entry, curr_entries, verboseprint):
new_field_type = new_entry['field_type']
for entry in curr_entries:
if new_field_type == entry['field_type']:
if entry == new_entry:
# If the field_type already exists but it is exactly the same, move on
verboseprint("---> The field_type:" + entry['field_type'] + " already exists. Moving on")
return
verboseprint("---> Checking for a new entry for the field_type:" + entry['field_type'])
new_modlist = new_entry['modlist']
for mod in new_modlist:
if model_type_exists(mod['model_type'], entry):
add_new_mod(mod, entry, verboseprint)
else:
# If the model type does not exist, just append it
verboseprint("----> Adding the model_type: " + mod['model_type'] + " to field_type:"
+ new_entry['field_type'])
entry['modlist'].append(mod)


def add_new_mod(new_mod, curr_entries, verboseprint):
model_type = new_mod['model_type']
for entry in curr_entries['modlist']:
if model_type == entry['model_type']:
if new_mod == entry:
# If the model_type already exists but it is exactly the same, move on
verboseprint("----> The model_type:" + entry['model_type'] + " already exists. Moving on")
return
verboseprint("----> Checking for a new entry for the model_type:" + entry['model_type'])
new_varlist = new_mod['varlist']
curr_varlist = entry['varlist']
for new_var in new_varlist:
found = False
for curr_var in curr_varlist:
if new_var == curr_var:
found = True
verboseprint("-----> variable:" + new_var['variable'] + " already exists. Moving on")
break
if not found:
verboseprint("-----> new variable:" + new_var['variable'] + " found. Adding it.")
curr_varlist.append(new_var)


def model_type_exists(model_type, curr_entries):
Expand All @@ -86,7 +109,8 @@ def model_type_exists(model_type, curr_entries):
return True
return False

def combine_yaml(files):

def combine_yaml(files, verboseprint):
"""
Combines a list of yaml files into one

Expand All @@ -96,61 +120,29 @@ def combine_yaml(files):
field_table = {}
field_table['field_table'] = []
for f in files:
verboseprint("Opening on the field_table yaml:" + f)
# Check if the file exists
if not path.exists(f):
raise FileNotFoundError(errno.ENOENT,
strerror(errno.ENOENT),
f)
with open(f) as fl:
my_table = yaml.safe_load(fl)
verboseprint("Parsing the data_table yaml:" + f)
try:
my_table = yaml.safe_load(fl)
except yaml.YAMLError as err:
print("---> Error when parsing the file " + f)
raise err
entries = my_table['field_table']
for entry in entries:
if not field_type_exists(entry['field_type'], field_table['field_table']):
verboseprint("---> Adding the field_type: " + entry['field_type'])
# If the field table does not exist, just add it to the current field table
field_table['field_table'].append(entry)
else:
add_new_field(entry, field_table['field_table'])
add_new_field(entry, field_table['field_table'], verboseprint)
return field_table


def main():
#: parse user input
parser = argparse.ArgumentParser(
prog='combine_field_table_yaml',
description="Combines a list of field_table.yaml files into one file" +
"Requires pyyaml (https://pyyaml.org/)")
parser.add_argument('-f', '--in-files',
dest='in_files',
type=str,
nargs='+',
default=["field_table"],
help='Space seperated list with the '
'Names of the field_table.yaml files to combine')
parser.add_argument('-o', '--output',
dest='out_file',
type=str,
default='field_table.yaml',
help="Ouput file name of the converted YAML \
(Default: 'field_table.yaml')")
parser.add_argument('-F', '--force',
action='store_true',
help="Overwrite the output field table yaml file.")
parser.add_argument('-V', '--version',
action="version",
version=f"%(prog)s {__version__}")
args = parser.parse_args()

try:
field_table = combine_yaml(args.in_files)
out_file_op = "x" # Exclusive write
if args.force:
out_file_op = "w"
with open(args.out_file, out_file_op) as myfile:
yaml.dump(field_table, myfile, default_flow_style=False)

except Exception as err:
raise SystemExit(err)


if __name__ == "__main__":
main()
combine_field_table_yaml(prog_name="combine_field_table_yaml")
Loading
Loading