-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.py
33 lines (24 loc) · 825 Bytes
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import codecs
from collections.abc import Mapping, Sequence
from ruamel.yaml import YAML
def _fmt_log(message):
return 'flask-filealchemy: {}'.format(message)
class LoadError(Exception):
pass
def parse_yaml_file(file_: str):
try:
with codecs.open(file_) as fd:
data = fd.read()
values = YAML(typ='safe').load(data)
if isinstance(values, Sequence):
for value in values:
if not isinstance(value, Mapping):
raise ValueError()
elif not isinstance(values, Mapping):
raise ValueError()
except IOError:
raise LoadError(_fmt_log('could not open {}'.format(file_)))
except ValueError:
raise LoadError(_fmt_log('{} contains invalid YAML'.format(file_)))
else:
return values