From 0d59cd78efd53f9f03918e27edbf4b83cb97c66b Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 17 Jan 2020 15:56:30 -0600 Subject: Add ini parser and config file object --- CHANGELOG.md | 9 +++- config_file/__init__.py | 11 ++-- config_file/config_file.py | 71 ++++++++++++++++++++++++++ config_file/parsers/ini_parser.py | 105 ++++++++++++++++++++++++++++++++++++++ tests/test_ini_parser.py | 103 +++++++++++++++++++++++++++++++++++++ 5 files changed, 295 insertions(+), 4 deletions(-) create mode 100644 config_file/config_file.py create mode 100644 config_file/parsers/ini_parser.py create mode 100644 tests/test_ini_parser.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 499b757..ba35025 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Added -- Base parser for concrete file format parser implementations +- Abstract base parser as a contract for concrete file format parser implementations. +- Exposed `ConfigFile`, `BaseParser`, and `ParsingError` to the Public API. The base + parser is exposed to allow future custom extensions of the config file and what it + can parse by its users. +- `has_section` and `has_key` is now changed to a single `has` method which determines + whether you're checking a section or key by the presence of a dot. +- `IniParser` to support the ini format. It uses configparser internally, but it is + only exposed through the `ConfigFile` object. ## 0.2.0 - 2020-01-04 diff --git a/config_file/__init__.py b/config_file/__init__.py index 6e82ea7..09ec563 100644 --- a/config_file/__init__.py +++ b/config_file/__init__.py @@ -1,5 +1,10 @@ -""" -config_file: Manage your configuration files. -""" +from config_file.config_file import ConfigFile +from config_file.parsers.base_parser import BaseParser, ParsingError __version__ = "0.2.0" + +__all__ = [ + "ConfigFile", + "ParsingError", + "BaseParser", +] 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) + + + diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py new file mode 100644 index 0000000..9a69164 --- /dev/null +++ b/config_file/parsers/ini_parser.py @@ -0,0 +1,105 @@ +import configparser +from io import StringIO + +from config_file.parsers.base_parser import BaseParser, ParsingError +from config_file.parsers.parse_value import parse_value + + +class IniParser(BaseParser): + + def __init__(self, file_contents: str): + """Reads in the file contents into the parser.""" + try: + self.parser = configparser.ConfigParser() + super().__init__(file_contents) + except configparser.Error as error: + raise ParsingError(error) + + def parse(self, file_contents: str): + self.parser.read_string(file_contents) + + def get(self, section_key, parse_type=True): + """ + Read the value of `section.key` of the config file. + + :param section_key: The section and key to read from in the config file. + e.g. 'section1.key2' + :param parse_type: Coerces the return value to its native type. + :return: The value of the key, parsed to its native type if parse_type is True. + + :raises ValueError: if the section_key given is not in a dot format. e.g. 'section1.key' + :raises ParsingError: if the specified `section.key` is not found, in an + invalid format, or if we are unable to coerce the return value to value_type. + """ + section, key = self._split_on_dot(section_key) + + try: + value = self.parser.get(section, key) + return parse_value(value) if parse_type else value + except configparser.Error as error: + raise ParsingError(error.message) + + def set(self, section_key, value): + """ + Sets the value of 'section.key' of the config file. If the specified section + is not in the configuration file, it will be created before adding the key + to it. + + :param section_key: The key to set from the config file. e.g. 'section1.key' + :param value: The value to set the key to. + + :return: True if the setting was successful. + :rtype: boolean + + :raises ValueError: If there is no dot (.) in section_key + """ + section, key = self._split_on_dot(section_key) + + if value is not None and not self.parser.has_section(section): + self.parser.add_section(section) + + self.parser.set(section, key, value) + return True + + def delete(self, section_key): + """ + Deletes a key or an entire section. + + :param section_key: The key to delete from the config file. + e.g. 'ocr.engine'. If no dot (.) is present, it will assume you are trying + to delete the entire section. + + :return: True if the deletion succeeded. + :rtype: boolean + + :raise ValueError: If the section or key does not exist in the config file. + """ + if "." not in section_key: + if not self.parser.has_section(section_key): + raise ValueError( + f"Cannot delete {section_key} because it is not in the config file." + ) + + self.parser.remove_section(section_key) + return True + + section, key = self._split_on_dot(section_key) + if key not in self.parser[section]: + raise ValueError( + f"Cannot delete {section}.{key} because {key} is not in {section}." + ) + + self.parser.remove_option(section, key) + return True + + def stringify(self) -> str: + buffer = StringIO() + self.parser.write(buffer) + return buffer.getvalue() + + def has(self, section_key: str) -> bool: + if "." not in section_key: + return self.parser.has_section(section_key) + + section, key = self._split_on_dot(section_key) + return self.parser.has_option(section, key) diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py new file mode 100644 index 0000000..6e5b0a8 --- /dev/null +++ b/tests/test_ini_parser.py @@ -0,0 +1,103 @@ +import pytest + +from config_file.parsers.ini_parser import IniParser, ParsingError + + +@pytest.mark.parametrize( + "ini_file", + [ + "blahblahblah", + "{ 'key1': 5 }", + ] +) +def test_incorrect_ini_formats(ini_file): + with pytest.raises(ParsingError): + IniParser(ini_file) + + +@pytest.mark.parametrize( + "section_key,expected_value,parse_type", + [ + ("test_section.intkey", 5, True), + ("test_section.strkey", "blah", True), + ("test_section.boolkey", False, True), + ("test_section.floatkey", 5.3, True), + + ("test_section.intkey", "5", False), + ("test_section.strkey", "blah", False), + ("test_section.boolkey", "false", False), + ("test_section.floatkey", "5.3", False), + ], +) +def test_that_ini_parser_can_get_values(section_key, expected_value, parse_type): + ini_file = """[test_section]\nintkey = 5\nstrkey = blah\nboolkey = false\nfloatkey = 5.3\n\n""" + parser = IniParser(ini_file) + assert parser.get(section_key, parse_type=parse_type) == expected_value + + +@pytest.mark.parametrize( + "section_key,value,expected_value", + [ + ("test.key1", "different value", "[test]\nkey1 = different value\n\n"), + ("test.key2", 5, "[test]\nkey1 = different value\nkey2 = 5\n\n"), + ("test2.key", False, "[test]\nkey1 = value1\n[test2]\nkey = False\n\n"), + ] +) +def test_that_ini_parser_can_set_values(section_key, value, expected_value): + ini_file = """[test]\nkey1 = value1\n\n""" + parser = IniParser(ini_file) + assert parser.stringify() == ini_file + + +@pytest.mark.parametrize( + "ini_file", + [ + "[test]\nkey1 = value1\n\n", + "[test]\nblah = 5\n\n[section2]\nkey = 2\n\n", + "", + ] +) +def test_that_ini_parser_can_be_stringified(ini_file): + parser = IniParser(ini_file) + assert parser.stringify() == ini_file + + +@pytest.mark.parametrize( + "section_key,expected_result", + [ + ("test_section", ""), + ("test_section.key1", "[test_section]\n\n"), + ], +) +def test_that_ini_parser_can_delete(section_key, expected_result): + ini_file = """[test_section]\nkey1 = value\n\n""" + parser = IniParser(ini_file) + parser.delete(section_key) + assert parser.stringify() == expected_result + + +@pytest.mark.parametrize( + "section_key", + [ + "section_is_not_present", + "test_section.but_key_is_not_present", + ], +) +def test_that_ini_parser_delete_raises_value_errors(section_key): + ini_file = """[test_section]\nkey1 = value\n\n""" + parser = IniParser(ini_file) + with pytest.raises(ValueError): + parser.delete(section_key) + + +@pytest.mark.parametrize( + "section_key", + [ + "test_section", + "test_section.key1", + ], +) +def test_that_ini_parser_can_check_its_section_and_keys(section_key): + ini_file = """[test_section]\nkey1 = value\n\n""" + parser = IniParser(ini_file) + assert parser.has(section_key) -- cgit v1.2.3