fix: made code DRYer, changed RegEx match test to succeed when appropriate

This commit is contained in:
2026-01-22 17:08:06 -05:00
parent c698bc831a
commit 8e6346ee21

View File

@@ -3,21 +3,23 @@ import yaml as yams
from configparser import ConfigParser as cfg from configparser import ConfigParser as cfg
from custtypes import ExecutedPath from custtypes import ExecutedPath
from re import compile as rgx, IGNORECASE from re import compile as rgx, IGNORECASE
from whereami import PROJ_ROOT
from typing import Literal
class Parser: class Parser:
def __init__(self): def __init__(self):
self.__is_yaml = rgx(r"\.ya?ml$", flags=IGNORECASE).match self.__is_yaml = rgx(r".*\.ya?ml$", flags=IGNORECASE).match
self.__is_ini = rgx(r"\.ini$", flags=IGNORECASE).match self.__is_ini = rgx(r".*\.ini$", flags=IGNORECASE).match
self.__is_config = rgx(r"\.cfg$", flags=IGNORECASE).match self.__is_config = rgx(r".*\.cfg$", flags=IGNORECASE).match
self.__is_json = rgx(r"\.[jb]son$", flags=IGNORECASE).match self.__is_json = rgx(r".*\.[jb]son$", flags=IGNORECASE).match
self.__data = None self.__data = None
self.__content: str | None = None self.__content: str | None = None
self.__is_path: bool = False self.__is_path: bool = False
# @TODO use Enum child class for below type hint instead # @TODO use Enum child class for below type hint instead
self.__method: Literal["yaml", "config"] | None = None self.__method: Literal["yaml", "config", "generic"] = "generic"
self.__file: ExecutedPath | None = None self.__file: ExecutedPath | None = None
def load(self, filepath: ExecutedPath | str, **kwargs): def load(self, filepath: ExecutedPath | str, method: Literal["yaml", "config", "generic"] = "generic", **kwargs):
if isinstance(filepath, ExecutedPath): if isinstance(filepath, ExecutedPath):
self.__is_path = True self.__is_path = True
self.__file = filepath self.__file = filepath
@@ -29,32 +31,36 @@ class Parser:
else: else:
self.__content = filepath self.__content = filepath
if self.__is_yaml(filepath): if self.__is_yaml(filepath) or method == "yaml":
self.__method = "yaml" self.__method = "yaml"
if self.__is_path:
filepath = open(str(filepath), "r")
if len(kwargs) > 0: if len(kwargs) > 0:
self.__data = yams.load(filepath, Loader=yams.Loader, **kwargs) self.__data = yams.load(filepath, Loader=yams.Loader, **kwargs)
else: else:
self.__data = yams.load(filepath, Loader=yams.Loader) self.__data = yams.load(filepath, Loader=yams.Loader)
elif self.__is_config(filepath): elif self.__is_config(filepath) or method == "config":
self.__method = "config" self.__method = "config"
self.__data = cfg() self.__data = cfg()
if self.__is_path: if self.__is_path:
read = self.__data.read
else:
read = self.__data.read_string
if len(kwargs) > 0: if len(kwargs) > 0:
self.__data.read(filepath, **kwargs) self.__data.read(filepath, **kwargs)
else: else:
self.__data.read(filepath) self.__data.read(filepath)
else:
if len(kwargs) > 0:
self.__data.read_string(filepath, **kwargs)
else:
self.__data.read_string(filepath, **kwargs)
else: else:
raise TypeError raise TypeError
return self.__data return self.__data
def dump(self, obj = None, **kwargs): def dump(self, obj = None, method: Literal["yaml", "config", "generic"] | None = "generic", **kwargs):
if self.__method == "yaml": if self.__method == "yaml" or method == "yaml":
if obj is None: if obj is None:
obj = self.__data obj = self.__data
@@ -62,15 +68,15 @@ class Parser:
self.__content = yams.dump(obj, Dumper=yams.Dumper, **kwargs) self.__content = yams.dump(obj, Dumper=yams.Dumper, **kwargs)
else: else:
self.__content = yams.dump(obj, Dumper=yams.Dumper) self.__content = yams.dump(obj, Dumper=yams.Dumper)
elif self.__method == "config": elif self.__method == "config" or method == "config":
if obj is None: if obj is None:
if self.__is_path: if self.__is_path:
return self.__file return self.__file.read_text()
else: else:
if self.__file is None: if self.__file is None:
return self.__content return self.__content
else: else:
return self.__file return self.__file.read_text()
else: else:
if isinstance(obj, ConfigParser): if isinstance(obj, ConfigParser):
return obj return obj