From eb4c90fe6cc4af036cd3003378929092ee59c4ce Mon Sep 17 00:00:00 2001 From: Eric McGinnis Date: Fri, 28 Mar 2025 09:18:07 -0700 Subject: [PATCH 1/3] Give the name of the YML file which causes an exception when there is an error parsing a yml file. Right now we do not, which makes debugging and fixing the error needlessly complicated --- contentctl/input/yml_reader.py | 57 +++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/contentctl/input/yml_reader.py b/contentctl/input/yml_reader.py index 65e74069..55314ead 100644 --- a/contentctl/input/yml_reader.py +++ b/contentctl/input/yml_reader.py @@ -11,44 +11,51 @@ def load_file( add_fields: bool = True, STRICT_YML_CHECKING: bool = False, ) -> Dict[str, Any]: - try: + try: file_handler = open(file_path, "r", encoding="utf-8") + except OSError as exc: + print(f"\nThere was an unrecoverable error when opening the file '{file_path}' - we will exit immediately:\n{str(exc)}") + sys.exit(1) + + # The following code can help diagnose issues with duplicate keys or # poorly-formatted but still "compliant" YML. This code should be # enabled manually for debugging purposes. As such, strictyaml # library is intentionally excluded from the contentctl requirements + + try: if STRICT_YML_CHECKING: + # This is an extra level of verbose parsing that can be + # enabled for debugging purpose. It is intentionally done in + # addition to the regular yml parsing import strictyaml - - try: - strictyaml.dirty_load(file_handler.read(), allow_flow_style=True) - file_handler.seek(0) - except Exception as e: - print(f"Error loading YML file {file_path}: {str(e)}") - sys.exit(1) - try: - # Ideally we should use - # from contentctl.actions.new_content import NewContent - # and use NewContent.UPDATE_PREFIX, - # but there is a circular dependency right now which makes that difficult. - # We have instead hardcoded UPDATE_PREFIX - UPDATE_PREFIX = "__UPDATE__" - data = file_handler.read() - if UPDATE_PREFIX in data: - raise Exception( - f"The file {file_path} contains the value '{UPDATE_PREFIX}'. Please fill out any unpopulated fields as required." - ) - yml_obj = yaml.load(data, Loader=yaml.CSafeLoader) - except yaml.YAMLError as exc: - print(exc) + strictyaml.dirty_load(file_handler.read(), allow_flow_style=True) + file_handler.seek(0) + + print(f"Error loading YML file {file_path}: {str(e)}") sys.exit(1) - except OSError as exc: - print(exc) + + # Ideally we should use + # from contentctl.actions.new_content import NewContent + # and use NewContent.UPDATE_PREFIX, + # but there is a circular dependency right now which makes that difficult. + # We have instead hardcoded UPDATE_PREFIX + UPDATE_PREFIX = "__UPDATE__" + data = file_handler.read() + if UPDATE_PREFIX in data: + raise Exception( + f"\nThe file {file_path} contains the value '{UPDATE_PREFIX}'. Please fill out any unpopulated fields as required." + ) + yml_obj = yaml.load(data, Loader=yaml.CSafeLoader) + + except yaml.YAMLError as exc: + print(f"\nThere was an unrecoverable YML Parsing error when reading or parsing the file '{file_path}' - we will exit immediately:\n{str(exc)}") sys.exit(1) + if add_fields is False: return yml_obj From 6d7c0f32826065393b6ca0a2b57c9e84cf4dbc7b Mon Sep 17 00:00:00 2001 From: Eric McGinnis Date: Fri, 28 Mar 2025 09:29:06 -0700 Subject: [PATCH 2/3] Fix undefined variable usage caught by ruff. --- contentctl/input/yml_reader.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/contentctl/input/yml_reader.py b/contentctl/input/yml_reader.py index 55314ead..faef4dea 100644 --- a/contentctl/input/yml_reader.py +++ b/contentctl/input/yml_reader.py @@ -33,9 +33,6 @@ def load_file( import strictyaml strictyaml.dirty_load(file_handler.read(), allow_flow_style=True) file_handler.seek(0) - - print(f"Error loading YML file {file_path}: {str(e)}") - sys.exit(1) # Ideally we should use From 42fd4e7e644345612ddd5d50d15fa193c03e5c92 Mon Sep 17 00:00:00 2001 From: Eric McGinnis Date: Fri, 28 Mar 2025 11:56:07 -0700 Subject: [PATCH 3/3] fix formatting with ruff --- contentctl/input/yml_reader.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/contentctl/input/yml_reader.py b/contentctl/input/yml_reader.py index faef4dea..101c9972 100644 --- a/contentctl/input/yml_reader.py +++ b/contentctl/input/yml_reader.py @@ -1,7 +1,8 @@ -from typing import Dict, Any -import yaml -import sys import pathlib +import sys +from typing import Any, Dict + +import yaml class YmlReader: @@ -11,30 +12,29 @@ def load_file( add_fields: bool = True, STRICT_YML_CHECKING: bool = False, ) -> Dict[str, Any]: - try: + try: file_handler = open(file_path, "r", encoding="utf-8") except OSError as exc: - print(f"\nThere was an unrecoverable error when opening the file '{file_path}' - we will exit immediately:\n{str(exc)}") + print( + f"\nThere was an unrecoverable error when opening the file '{file_path}' - we will exit immediately:\n{str(exc)}" + ) sys.exit(1) - - # The following code can help diagnose issues with duplicate keys or # poorly-formatted but still "compliant" YML. This code should be # enabled manually for debugging purposes. As such, strictyaml # library is intentionally excluded from the contentctl requirements - try: if STRICT_YML_CHECKING: # This is an extra level of verbose parsing that can be # enabled for debugging purpose. It is intentionally done in # addition to the regular yml parsing import strictyaml + strictyaml.dirty_load(file_handler.read(), allow_flow_style=True) file_handler.seek(0) - # Ideally we should use # from contentctl.actions.new_content import NewContent # and use NewContent.UPDATE_PREFIX, @@ -49,10 +49,11 @@ def load_file( yml_obj = yaml.load(data, Loader=yaml.CSafeLoader) except yaml.YAMLError as exc: - print(f"\nThere was an unrecoverable YML Parsing error when reading or parsing the file '{file_path}' - we will exit immediately:\n{str(exc)}") + print( + f"\nThere was an unrecoverable YML Parsing error when reading or parsing the file '{file_path}' - we will exit immediately:\n{str(exc)}" + ) sys.exit(1) - if add_fields is False: return yml_obj