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

add yaml.constructor.ConstructorError capture #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
27 changes: 16 additions & 11 deletions omg/must_gather/generate_rdefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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))
47 changes: 25 additions & 22 deletions omg/utils/load_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down