Skip to content
Merged
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
61 changes: 33 additions & 28 deletions contentctl/input/yml_reader.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -13,40 +14,44 @@ def load_file(
) -> Dict[str, Any]:
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)
sys.exit(1)

except OSError as exc:
print(exc)
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,
# 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:
Expand Down
Loading