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 :
@@ -13,40 +14,44 @@ def load_file(
13
14
) -> Dict [str , Any ]:
14
15
try :
15
16
file_handler = open (file_path , "r" , encoding = "utf-8" )
17
+ except OSError as 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
+ )
21
+ sys .exit (1 )
16
22
17
23
# The following code can help diagnose issues with duplicate keys or
18
24
# poorly-formatted but still "compliant" YML. This code should be
19
25
# enabled manually for debugging purposes. As such, strictyaml
20
26
# library is intentionally excluded from the contentctl requirements
21
27
28
+ try :
22
29
if STRICT_YML_CHECKING :
30
+ # This is an extra level of verbose parsing that can be
31
+ # enabled for debugging purpose. It is intentionally done in
32
+ # addition to the regular yml parsing
23
33
import strictyaml
24
34
25
- try :
26
- strictyaml .dirty_load (file_handler .read (), allow_flow_style = True )
27
- file_handler .seek (0 )
28
- except Exception as e :
29
- print (f"Error loading YML file { file_path } : { str (e )} " )
30
- sys .exit (1 )
31
- try :
32
- # Ideally we should use
33
- # from contentctl.actions.new_content import NewContent
34
- # and use NewContent.UPDATE_PREFIX,
35
- # but there is a circular dependency right now which makes that difficult.
36
- # We have instead hardcoded UPDATE_PREFIX
37
- UPDATE_PREFIX = "__UPDATE__"
38
- data = file_handler .read ()
39
- if UPDATE_PREFIX in data :
40
- raise Exception (
41
- f"The file { file_path } contains the value '{ UPDATE_PREFIX } '. Please fill out any unpopulated fields as required."
42
- )
43
- yml_obj = yaml .load (data , Loader = yaml .CSafeLoader )
44
- except yaml .YAMLError as exc :
45
- print (exc )
46
- sys .exit (1 )
47
-
48
- except OSError as exc :
49
- print (exc )
35
+ strictyaml .dirty_load (file_handler .read (), allow_flow_style = True )
36
+ file_handler .seek (0 )
37
+
38
+ # Ideally we should use
39
+ # from contentctl.actions.new_content import NewContent
40
+ # and use NewContent.UPDATE_PREFIX,
41
+ # but there is a circular dependency right now which makes that difficult.
42
+ # We have instead hardcoded UPDATE_PREFIX
43
+ UPDATE_PREFIX = "__UPDATE__"
44
+ data = file_handler .read ()
45
+ if UPDATE_PREFIX in data :
46
+ raise Exception (
47
+ f"\n The file { file_path } contains the value '{ UPDATE_PREFIX } '. Please fill out any unpopulated fields as required."
48
+ )
49
+ yml_obj = yaml .load (data , Loader = yaml .CSafeLoader )
50
+
51
+ except yaml .YAMLError as 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
+ )
50
55
sys .exit (1 )
51
56
52
57
if add_fields is False :
0 commit comments