@@ -60,6 +60,28 @@ def load_content(self) -> None:
6060 def validate_content (self ) -> None :
6161 pass
6262
63+ @classmethod
64+ def init (cls , location : Path , multiple_documents : bool , content : dict | None ) -> Self :
65+ if not content :
66+ return cls ._file_is_empty (path = location , has_multiple_document = multiple_documents )
67+
68+ return cls (location = location , multiple_documents = multiple_documents , content = content )
69+
70+ @classmethod
71+ def _file_is_empty (cls , path : Path , has_multiple_document : bool ) -> Self :
72+ return cls (
73+ location = path , multiple_documents = has_multiple_document , error_message = "Invalid YAML/JSON file" , valid = False
74+ )
75+
76+ @classmethod
77+ def _file_is_invalid (cls , path : Path , has_multiple_document : bool ) -> Self :
78+ return cls (
79+ location = path ,
80+ multiple_documents = has_multiple_document ,
81+ error_message = "Invalid YAML/JSON file" ,
82+ valid = False ,
83+ )
84+
6385 @classmethod
6486 def load_file_from_disk (cls , path : Path ) -> list [Self ]:
6587 yaml_files : list [Self ] = []
@@ -71,24 +93,23 @@ def load_file_from_disk(cls, path: Path) -> list[Self]:
7193
7294 if has_multiple_document :
7395 for content in yaml .safe_load_all (file_content ):
74- yaml_files .append (cls (location = path , multiple_documents = has_multiple_document , content = content ))
96+ yaml_files .append (
97+ cls .init (location = path , multiple_documents = has_multiple_document , content = content )
98+ )
99+
75100 else :
76101 yaml_files .append (
77- cls (location = path , multiple_documents = has_multiple_document , content = yaml .safe_load (file_content ))
102+ cls .init (
103+ location = path , multiple_documents = has_multiple_document , content = yaml .safe_load (file_content )
104+ )
78105 )
106+
79107 except FileNotValidError as exc :
80108 yaml_files .append (
81109 cls (location = path , multiple_documents = has_multiple_document , error_message = exc .message , valid = False )
82110 )
83111 except (yaml .YAMLError , ParserError ):
84- yaml_files .append (
85- cls (
86- location = path ,
87- multiple_documents = has_multiple_document ,
88- error_message = "Invalid YAML/JSON file" ,
89- valid = False ,
90- )
91- )
112+ yaml_files .append (cls ._file_is_invalid (path , has_multiple_document ))
92113
93114 if has_multiple_document :
94115 for idx , file in enumerate (yaml_files ):
0 commit comments