1
- from typing import Dict , Any
2
- import yaml
3
- import sys
4
1
import pathlib
2
+ import sys
3
+ from typing import Any , Dict
4
+
5
+ import yaml
5
6
6
7
7
8
class YmlReader :
@@ -11,30 +12,29 @@ def load_file(
11
12
add_fields : bool = True ,
12
13
STRICT_YML_CHECKING : bool = False ,
13
14
) -> Dict [str , Any ]:
14
- try :
15
+ try :
15
16
file_handler = open (file_path , "r" , encoding = "utf-8" )
16
17
except OSError as exc :
17
- print (f"\n There was an unrecoverable error when opening the file '{ file_path } ' - we will exit immediately:\n { str (exc )} " )
18
+ print (
19
+ f"\n There was an unrecoverable error when opening the file '{ file_path } ' - we will exit immediately:\n { str (exc )} "
20
+ )
18
21
sys .exit (1 )
19
22
20
-
21
-
22
23
# The following code can help diagnose issues with duplicate keys or
23
24
# poorly-formatted but still "compliant" YML. This code should be
24
25
# enabled manually for debugging purposes. As such, strictyaml
25
26
# library is intentionally excluded from the contentctl requirements
26
27
27
-
28
28
try :
29
29
if STRICT_YML_CHECKING :
30
30
# This is an extra level of verbose parsing that can be
31
31
# enabled for debugging purpose. It is intentionally done in
32
32
# addition to the regular yml parsing
33
33
import strictyaml
34
+
34
35
strictyaml .dirty_load (file_handler .read (), allow_flow_style = True )
35
36
file_handler .seek (0 )
36
37
37
-
38
38
# Ideally we should use
39
39
# from contentctl.actions.new_content import NewContent
40
40
# and use NewContent.UPDATE_PREFIX,
@@ -49,10 +49,11 @@ def load_file(
49
49
yml_obj = yaml .load (data , Loader = yaml .CSafeLoader )
50
50
51
51
except yaml .YAMLError as exc :
52
- print (f"\n There was an unrecoverable YML Parsing error when reading or parsing the file '{ file_path } ' - we will exit immediately:\n { str (exc )} " )
52
+ print (
53
+ f"\n There was an unrecoverable YML Parsing error when reading or parsing the file '{ file_path } ' - we will exit immediately:\n { str (exc )} "
54
+ )
53
55
sys .exit (1 )
54
56
55
-
56
57
if add_fields is False :
57
58
return yml_obj
58
59
0 commit comments