Skip to content

Commit 2735d4e

Browse files
committed
util.py: load_yaml: Don't fail when it's not necessary
Also, make 'require()' function not fail by default. The only case where we really need to fail is when 'kramdown' parser is not specified. This is a highly unlikely scenario, tbh (because arguments to `lesson_check.py` are set in the Makefile), but we can think about reworking/optimizing this part later.
1 parent 6b49768 commit 2735d4e

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

bin/lesson_check.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ def parse_args():
180180

181181
args, extras = parser.parse_known_args()
182182
require(args.parser is not None,
183-
'Path to Markdown parser not provided')
183+
'Path to Markdown parser not provided',
184+
True)
184185
require(not extras,
185186
'Unexpected trailing command-line arguments "{0}"'.format(extras))
186187

bin/util.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ def split_metadata(path, text):
6565
try:
6666
metadata_yaml = yaml.load(metadata_raw, Loader=yaml.SafeLoader)
6767
except yaml.YAMLError as e:
68-
print('Unable to parse YAML header in {0}:\n{1}'.format(
69-
path, e), file=sys.stderr)
70-
sys.exit(1)
68+
message = 'Unable to parse YAML header in {0}:\n{1}'
69+
print(message.format(path, e), file=sys.stderr)
7170

7271
return metadata_raw, metadata_yaml, text
7372

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

90+
return {}
8991

9092
def check_unwanted_files(dir_path, reporter):
9193
"""
@@ -99,9 +101,11 @@ def check_unwanted_files(dir_path, reporter):
99101
"Unwanted file found")
100102

101103

102-
def require(condition, message):
104+
def require(condition, message, fatal=False):
103105
"""Fail if condition not met."""
104106

105107
if not condition:
106108
print(message, file=sys.stderr)
107-
sys.exit(1)
109+
110+
if fatal:
111+
sys.exit(1)

0 commit comments

Comments
 (0)