fix, refactor: changed file buffer mode and changed conditional sequences for shorter algorithm
This commit is contained in:
29
parse.py
29
parse.py
@@ -25,22 +25,26 @@ class Parser:
|
||||
self.__file = filepath
|
||||
filepath = str(filepath)
|
||||
else:
|
||||
self.__is_path = False
|
||||
if Path(filepath).exists():
|
||||
if isinstance(filepath, str) and Path(filepath).exists():
|
||||
self.__file = Path(filepath)
|
||||
self.__is_path = True
|
||||
else:
|
||||
self.__is_path = False
|
||||
|
||||
if isinstance(filepath, str):
|
||||
self.__content = filepath
|
||||
|
||||
if self.__is_yaml(filepath) or method == "yaml":
|
||||
self.__method = "yaml"
|
||||
|
||||
if self.__is_path:
|
||||
filepath = open(str(filepath), "r")
|
||||
filepath = open(str(filepath), "r+")
|
||||
|
||||
if len(kwargs) > 0:
|
||||
self.__data = yams.load(filepath, Loader=yams.Loader, **kwargs)
|
||||
self.__data = yams.load_all(filepath, Loader=yams.Loader, **kwargs)
|
||||
else:
|
||||
self.__data = yams.load(filepath, Loader=yams.Loader)
|
||||
self.__data = yams.load_all(filepath, Loader=yams.Loader)
|
||||
filepath.close()
|
||||
elif self.__is_config(filepath) or method == "config":
|
||||
self.__method = "config"
|
||||
self.__data = cfg()
|
||||
@@ -60,15 +64,15 @@ class Parser:
|
||||
return self.__data
|
||||
|
||||
def dump(self, obj = None, method: Literal["yaml", "config", "generic"] | None = "generic", **kwargs):
|
||||
if self.__method == "yaml" or method == "yaml":
|
||||
if isinstance(obj, yams.YAMLObject) or self.__method == "yaml" or method == "yaml":
|
||||
if obj is None:
|
||||
obj = self.__data
|
||||
|
||||
if len(kwargs) > 0:
|
||||
self.__content = yams.dump(obj, Dumper=yams.Dumper, **kwargs)
|
||||
self.__content = "---\n" + yams.dump(obj, Dumper=yams.Dumper, **kwargs)
|
||||
else:
|
||||
self.__content = yams.dump(obj, Dumper=yams.Dumper)
|
||||
elif self.__method == "config" or method == "config":
|
||||
self.__content = "---\n" + yams.dump(obj, Dumper=yams.Dumper)
|
||||
elif isinstance(obj, ConfigParser) or self.__method == "config" or method == "config":
|
||||
if obj is None:
|
||||
if self.__is_path:
|
||||
return self.__file.read_text()
|
||||
@@ -77,11 +81,8 @@ class Parser:
|
||||
return self.__content
|
||||
else:
|
||||
return self.__file.read_text()
|
||||
else:
|
||||
if isinstance(obj, ConfigParser):
|
||||
return obj
|
||||
else:
|
||||
raise TypeError
|
||||
|
||||
raise NotImplementedError
|
||||
else:
|
||||
raise TypeError
|
||||
|
||||
|
||||
Reference in New Issue
Block a user