From 8e6346ee218d1eefa6fb6f6dbecc077598ad2c46 Mon Sep 17 00:00:00 2001 From: Alex Tavarez Date: Thu, 22 Jan 2026 17:08:06 -0500 Subject: [PATCH] fix: made code DRYer, changed RegEx match test to succeed when appropriate --- parse.py | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/parse.py b/parse.py index 1880ea7..7808af0 100644 --- a/parse.py +++ b/parse.py @@ -3,21 +3,23 @@ import yaml as yams from configparser import ConfigParser as cfg from custtypes import ExecutedPath from re import compile as rgx, IGNORECASE +from whereami import PROJ_ROOT +from typing import Literal class Parser: def __init__(self): - self.__is_yaml = rgx(r"\.ya?ml$", flags=IGNORECASE).match - self.__is_ini = rgx(r"\.ini$", flags=IGNORECASE).match - self.__is_config = rgx(r"\.cfg$", flags=IGNORECASE).match - self.__is_json = rgx(r"\.[jb]son$", flags=IGNORECASE).match + self.__is_yaml = rgx(r".*\.ya?ml$", flags=IGNORECASE).match + self.__is_ini = rgx(r".*\.ini$", flags=IGNORECASE).match + self.__is_config = rgx(r".*\.cfg$", flags=IGNORECASE).match + self.__is_json = rgx(r".*\.[jb]son$", flags=IGNORECASE).match self.__data = None self.__content: str | None = None self.__is_path: bool = False # @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 - def load(self, filepath: ExecutedPath | str, **kwargs): + def load(self, filepath: ExecutedPath | str, method: Literal["yaml", "config", "generic"] = "generic", **kwargs): if isinstance(filepath, ExecutedPath): self.__is_path = True self.__file = filepath @@ -29,32 +31,36 @@ class Parser: else: self.__content = filepath - if self.__is_yaml(filepath): + if self.__is_yaml(filepath) or method == "yaml": self.__method = "yaml" + + if self.__is_path: + filepath = open(str(filepath), "r") + if len(kwargs) > 0: self.__data = yams.load(filepath, Loader=yams.Loader, **kwargs) else: 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.__data = cfg() + if self.__is_path: - if len(kwargs) > 0: - self.__data.read(filepath, **kwargs) - else: - self.__data.read(filepath) + read = self.__data.read else: - if len(kwargs) > 0: - self.__data.read_string(filepath, **kwargs) - else: - self.__data.read_string(filepath, **kwargs) + read = self.__data.read_string + + if len(kwargs) > 0: + self.__data.read(filepath, **kwargs) + else: + self.__data.read(filepath) else: raise TypeError return self.__data - def dump(self, obj = None, **kwargs): - if self.__method == "yaml": + def dump(self, obj = None, method: Literal["yaml", "config", "generic"] | None = "generic", **kwargs): + if self.__method == "yaml" or method == "yaml": if obj is None: obj = self.__data @@ -62,15 +68,15 @@ class Parser: self.__content = yams.dump(obj, Dumper=yams.Dumper, **kwargs) else: self.__content = yams.dump(obj, Dumper=yams.Dumper) - elif self.__method == "config": + elif self.__method == "config" or method == "config": if obj is None: if self.__is_path: - return self.__file + return self.__file.read_text() else: if self.__file is None: return self.__content else: - return self.__file + return self.__file.read_text() else: if isinstance(obj, ConfigParser): return obj @@ -79,4 +85,4 @@ class Parser: else: raise TypeError - return self.__content \ No newline at end of file + return self.__content