Skip to content

Commit 092e5c9

Browse files
authored
Merge pull request carpentries/styles#613
util.py: load_yaml: Don't fail when it's not necessary
2 parents 7634a8c + 2735d4e commit 092e5c9

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
@@ -189,7 +189,8 @@ def parse_args():
189189

190190
args, extras = parser.parse_known_args()
191191
require(args.parser is not None,
192-
'Path to Markdown parser not provided')
192+
'Path to Markdown parser not provided',
193+
True)
193194
require(not extras,
194195
'Unexpected trailing command-line arguments "{0}"'.format(extras))
195196

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)