diff --git a/omg/must_gather/generate_rdefs.py b/omg/must_gather/generate_rdefs.py index 278f412..76705e0 100644 --- a/omg/must_gather/generate_rdefs.py +++ b/omg/must_gather/generate_rdefs.py @@ -26,12 +26,14 @@ def generate_rdefs(path): try: with open(rdefs_f, "r") as r_f: rdefs.extend(yaml.safe_load(r_f)) - except Exception: - pass + except Exception as e: + lg.error("Error loading existing rdefs: {}".format(e)) + else: + lg.info("{} file does not exist. It will be created.".format(rdefs_f)) try: crds = load_res(path, "crd") - + lg.trace("load path {} successfully".format(path)) if crds: for crd in crds: @@ -47,11 +49,14 @@ def generate_rdefs(path): if rdef not in rdefs: rdefs.append(rdef) - - with open(rdefs_f, "w") as rdf: - yaml.dump(rdefs, rdf, default_flow_style=False) - lg.debug("{} rdefs written to: {}".format(len(rdefs), rdefs_f)) - - except Exception: - # lg.warning("Unable to generate rdef file {}: {}".format(rdefs_f, e)) - pass + try: + with open(rdefs_f, "w") as rdf: + yaml.safe_dump(rdefs, rdf, default_flow_style=False) + lg.debug("{} rdefs written to: {}".format(len(rdefs), rdefs_f)) + except yaml.YAMLError as exc: + lg.error("YAML error while writing {}: {}".format(rdefs_f, exc)) + except Exception as exc: + lg.error("Error writing {}: {}".format(rdefs_f, exc)) + + except Exception as e: + lg.error("Unable to generate rdef file {}: {}".format(rdefs_f, e)) diff --git a/omg/utils/load_yaml.py b/omg/utils/load_yaml.py index a67608c..40e6ed6 100644 --- a/omg/utils/load_yaml.py +++ b/omg/utils/load_yaml.py @@ -27,31 +27,34 @@ def load_yaml(yfile): ydata = None with open(yfile, "r") as y_f: - lg.debug("Opened yaml file: " + yfile) y_d = y_f.read() try: ydata = yaml.load(y_d, Loader=SafeLoader) - except (yaml.scanner.ScannerError, yaml.parser.ParserError): - # yaml load/parse failed - # try skipping lines from the bottom - # Until we are able to load the yaml file - # We will try until > 1 lines are left - lines_total = y_d.count("\n") - lines_skipped = 0 - while y_d.count("\n") > 1: - # skip last line - y_d = y_d[:y_d.rfind("\n")] - lines_skipped += 1 - try: - ydata = yaml.load(y_d, Loader=SafeLoader) - except (yaml.scanner.ScannerError, yaml.parser.ParserError): - pass - else: - lg.warning("Skipped " + - str(lines_skipped) + "/" + str(lines_total) + - " lines from the end of " + yfile + - " to the load the yaml file properly") - break + except (yaml.scanner.ScannerError, yaml.parser.ParserError, yaml.constructor.ConstructorError): + lg.trace("Fail Opened yaml file: " + yfile) + pass + # # yaml load/parse failed + # # try skipping lines from the bottom + # # Until we are able to load the yaml file + # # We will try until > 1 lines are left + # lg.error("Fail Opened yaml file: " + yfile) + # lines_total = y_d.count("\n") + # lines_skipped = 0 + # while y_d.count("\n") > 1: + # # skip last line + # y_d = y_d[:y_d.rfind("\n")] + # lines_skipped += 1 + # try: + # ydata = yaml.load(y_d, Loader=SafeLoader) + # except (yaml.scanner.ScannerError, yaml.parser.ParserError, yaml.constructor.ConstructorError): + # lg.error("lines_skipped {}, lines_total {}".format(lines_skipped, lines_total)) + # pass + # else: + # lg.error("Skipped " + + # str(lines_skipped) + "/" + str(lines_total) + + # " lines from the end of " + yfile + + # " to the load the yaml file properly") + # break lg.debug("yaml file loaded in ydata. type: " + str(type(ydata))) lg.trace("ydata: " + str(ydata))