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

util.py: load_yaml: Don't fail when it's not necessary #613

Merged
merged 1 commit into from
Jul 12, 2021
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
3 changes: 2 additions & 1 deletion bin/lesson_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ def parse_args():

args, extras = parser.parse_known_args()
require(args.parser is not None,
'Path to Markdown parser not provided')
'Path to Markdown parser not provided',
True)
require(not extras,
'Unexpected trailing command-line arguments "{0}"'.format(extras))

Expand Down
22 changes: 13 additions & 9 deletions bin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ def split_metadata(path, text):
try:
metadata_yaml = yaml.load(metadata_raw, Loader=yaml.SafeLoader)
except yaml.YAMLError as e:
print('Unable to parse YAML header in {0}:\n{1}'.format(
path, e), file=sys.stderr)
sys.exit(1)
message = 'Unable to parse YAML header in {0}:\n{1}'
print(message.format(path, e), file=sys.stderr)

return metadata_raw, metadata_yaml, text

Expand All @@ -81,11 +80,14 @@ def load_yaml(filename):
try:
with open(filename, 'r', encoding='utf-8') as reader:
return yaml.load(reader, Loader=yaml.SafeLoader)
except (yaml.YAMLError, IOError) as e:
print('Unable to load YAML file {0}:\n{1}'.format(
filename, e), file=sys.stderr)
sys.exit(1)
except yaml.YAMLError as e:
message = 'ERROR: Unable to load YAML file {0}:\n{1}'
print(message.format(filename, e), file=sys.stderr)
except (FileNotFoundError, IOError):
message = 'ERROR: File {} not found'
print(message.format(filename), file=sys.stderr)

return {}

def check_unwanted_files(dir_path, reporter):
"""
Expand All @@ -99,9 +101,11 @@ def check_unwanted_files(dir_path, reporter):
"Unwanted file found")


def require(condition, message):
def require(condition, message, fatal=False):
"""Fail if condition not met."""

if not condition:
print(message, file=sys.stderr)
sys.exit(1)

if fatal:
sys.exit(1)