aboutsummaryrefslogtreecommitdiffstats
path: root/config_file/config_file.py
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-01-17 15:56:30 -0600
committerEugene Triguba <eugenetriguba@gmail.com>2020-01-17 15:56:30 -0600
commit0d59cd78efd53f9f03918e27edbf4b83cb97c66b (patch)
treed2585e282a13797b935ada839333185297b4ce0e /config_file/config_file.py
parentExpose private attrs and methods in base parser (diff)
downloadconfig-file-0d59cd78efd53f9f03918e27edbf4b83cb97c66b.zip
Add ini parser and config file object
Diffstat (limited to 'config_file/config_file.py')
-rw-r--r--config_file/config_file.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/config_file/config_file.py b/config_file/config_file.py
new file mode 100644
index 0000000..a95aa2d
--- /dev/null
+++ b/config_file/config_file.py
@@ -0,0 +1,71 @@
+from pathlib import Path
+
+from config_file.parsers.ini_parser import IniParser
+from config_file.parsers.base_parser import BaseParser
+
+
+class ConfigFile:
+
+ def __init__(self, file_path: str, parser=None):
+ self.path = self.__create_config_path(file_path)
+
+ with open(self.path, "r") as file:
+ self.contents = file.read()
+
+ self.parser = self.__determine_parser(file_path, parser)
+
+ @staticmethod
+ def __create_config_path(file_path: str):
+ if len(file_path) > 1 and file_path[0] == "~":
+ return Path(file_path).expanduser().resolve()
+
+ return Path(file_path)
+
+ def __determine_parser(self, file_path: str, parser):
+ if isinstance(BaseParser, parser):
+ return parser(self.contents)
+
+ file_type = self._split_on_dot(file_path, all_dots=True)[-1]
+ if file_type == "ini":
+ return IniParser(self.contents)
+ else:
+ raise ValueError(
+ f"File path contains an unsupported or "
+ f"unrecognized file type: {file_path}"
+ )
+
+ def get(self, key, parse_type=True):
+ """
+ Retrieve the value of a key in its native type.
+ This means the string 'true' will be parsed back as the
+ boolean True.
+
+ If parse_type is set to False, all values will be returned
+ back as strings.
+ """
+ return self.parser.get(key, parse_type=parse_type)
+
+ def set(self, key, value):
+ """Sets the value of a key."""
+ return self.parser.set(key, value)
+
+ def delete(self, section_key):
+ """Deletes a key/value pair or entire sections."""
+ return self.parser.delete(section_key)
+
+ def stringify(self) -> str:
+ """Convert the parsed internal representation of the file back into a string."""
+ return self.parser.stringify()
+
+ def has(self, section_key: str) -> bool:
+ """
+ Check if a section, sub-section, or key exists in the file
+ using a section.key format.
+
+ Some formats, like JSON, do not have sections and therefore,
+ it would only be checking if the section
+ """
+ return self.parser.has(section_key)
+
+
+