From aa44fe7a60a4f3326cc47877b3e246b56d46d6ac Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 13:41:35 -0500 Subject: Add python 3.5 support, ConfigFileError, and more type hints Also, get_all, set_all, and delete_all in JsonParser have been renamed to just all. --- config_file/parsers/ini_parser.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'config_file/parsers/ini_parser.py') diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 81f2d1b..31b0dcf 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -1,9 +1,10 @@ import configparser from io import StringIO -from config_file.parsers.base_parser import BaseParser, ParsingError +from config_file.parsers.base_parser import BaseParser from config_file.parsers.parse_value import parse_value from config_file.utils import split_on_dot +from config_file.exceptions import ParsingError class IniParser(BaseParser): @@ -95,7 +96,7 @@ class IniParser(BaseParser): if "." not in section_key: if not self.parsed_content.has_section(section_key): raise ValueError( - f"Cannot delete {section_key} because it is not in the config file." + "Cannot delete {} because it is not in the config file.".format(section_key) ) self.parsed_content.remove_section(section_key) @@ -104,7 +105,7 @@ class IniParser(BaseParser): section, key = split_on_dot(section_key, only_last_dot=True) if key not in self.parsed_content[section]: raise ValueError( - f"Cannot delete {section}.{key} because {key} is not in {section}." + "Cannot delete {section}.{key} because {key} is not in {section}.".format(section = section, key = key) ) self.parsed_content.remove_option(section, key) -- cgit v1.2.3 From 58808235ea6880c3a0ecfa976a700c81431dc902 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 15:30:11 -0500 Subject: Revert Python 3.5 support --- config_file/parsers/ini_parser.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'config_file/parsers/ini_parser.py') diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 31b0dcf..2db1ae8 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -1,10 +1,10 @@ import configparser from io import StringIO +from config_file.exceptions import ParsingError from config_file.parsers.base_parser import BaseParser from config_file.parsers.parse_value import parse_value from config_file.utils import split_on_dot -from config_file.exceptions import ParsingError class IniParser(BaseParser): @@ -96,7 +96,7 @@ class IniParser(BaseParser): if "." not in section_key: if not self.parsed_content.has_section(section_key): raise ValueError( - "Cannot delete {} because it is not in the config file.".format(section_key) + f"Cannot delete {section_key} because it is not in the config file." ) self.parsed_content.remove_section(section_key) @@ -105,7 +105,7 @@ class IniParser(BaseParser): section, key = split_on_dot(section_key, only_last_dot=True) if key not in self.parsed_content[section]: raise ValueError( - "Cannot delete {section}.{key} because {key} is not in {section}.".format(section = section, key = key) + f"Cannot delete {section}.{key} because {key} is not in {section}." ) self.parsed_content.remove_option(section, key) -- cgit v1.2.3 From 461cb78f0a56b9293b5ade10fe90ec4aa77d23ad Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 19 Jul 2020 15:02:13 -0500 Subject: Change name of BaseParser to AbstractParser --- config_file/parsers/ini_parser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'config_file/parsers/ini_parser.py') diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 2db1ae8..164ff6d 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -2,12 +2,12 @@ import configparser from io import StringIO from config_file.exceptions import ParsingError -from config_file.parsers.base_parser import BaseParser +from config_file.parsers.abstract_parser import AbstractParser from config_file.parsers.parse_value import parse_value from config_file.utils import split_on_dot -class IniParser(BaseParser): +class IniParser(AbstractParser): def __init__(self, file_contents: str): """Reads in the file contents into the configparser.""" super().__init__(file_contents) -- cgit v1.2.3 From 65d4715b0b2b9974d95ac36271caa8e177bc98fa Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 22:51:51 -0500 Subject: Add BaseParser for all other parsers This allows one implementation for all parsers, with the parsers overriding it for a custom one if needed for that parser. --- config_file/parsers/ini_parser.py | 145 ++++++++++---------------------------- 1 file changed, 37 insertions(+), 108 deletions(-) (limited to 'config_file/parsers/ini_parser.py') diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 164ff6d..2a29ba7 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -1,124 +1,53 @@ import configparser from io import StringIO -from config_file.exceptions import ParsingError -from config_file.parsers.abstract_parser import AbstractParser -from config_file.parsers.parse_value import parse_value -from config_file.utils import split_on_dot +from .base_parser import BaseParser -class IniParser(AbstractParser): - def __init__(self, file_contents: str): - """Reads in the file contents into the configparser.""" - super().__init__(file_contents) - - def parse(self, file_contents: str): - try: - parser = configparser.ConfigParser() - parser.read_string(file_contents) - return parser - except configparser.Error as error: - raise ParsingError(error) - - def get(self, section_key: str, parse_types: bool = False): - """ - 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_types: Coerces the return value to its native type. - :return: The value of the key, parsed to its native type if parse_types 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. - """ - if "." not in section_key: - return self.__retrieve_section(section_key, parse_types) - - try: - section, key = split_on_dot(section_key, only_last_dot=True) - value = self.parsed_content.get(section, key) - return parse_value(value) if parse_types else value - except configparser.Error as error: - raise ParsingError(error.message) - - def __retrieve_section(self, section, parse_types): - items = dict(self.parsed_content.items(section)) - - if not parse_types: - return items - - for item in items: - items[item] = parse_value(items[item]) - - return items - - def set(self, section_key: str, value) -> bool: - """ - 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. +class IniParserCompatibility: + """ + Provides compatibility with the json, yaml, + and toml API so we can use the BaseParser + easily, while still being able to leverage + the built-in configparser to parse the file. - :param section_key: The key to set from the config file. e.g. 'section1.key' + It is used as the `module` parameter + for BaseParser, which requires us to have a + `dumps` and `loads` method. + """ - :param value: The value to set the key to. It can be any type that can - be converted to a string. + @staticmethod + def loads(file_contents: str) -> dict: + parser = configparser.ConfigParser() + parser.read_string(file_contents) - :return: True if the setting was successful. + return IniParserCompatibility.__create_configparser_dict(parser) - :raises ValueError: If there is no dot (.) in section_key - """ - section, key = split_on_dot(section_key, only_last_dot=True) - - if value is not None and not self.parsed_content.has_section(section): - self.parsed_content.add_section(section) - - if not isinstance(value, str): - value = str(value) - - self.parsed_content.set(section, key, value) - return True - - def delete(self, section_key: str) -> bool: - """ - 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. + @staticmethod + def dumps(file_contents: dict) -> str: + parser = configparser.ConfigParser() + buffer = StringIO() - :raise ValueError: If the section or key does not exist in the config file. - """ - if "." not in section_key: - if not self.parsed_content.has_section(section_key): - raise ValueError( - f"Cannot delete {section_key} because it is not in the config file." - ) + parser.read_dict(file_contents) + parser.write(buffer) - self.parsed_content.remove_section(section_key) - return True + return buffer.getvalue() - section, key = split_on_dot(section_key, only_last_dot=True) - if key not in self.parsed_content[section]: - raise ValueError( - f"Cannot delete {section}.{key} because {key} is not in {section}." - ) + @staticmethod + def __create_configparser_dict(parser: configparser.ConfigParser) -> dict: + result = {} + items = dict(parser.items()) - self.parsed_content.remove_option(section, key) - return True + for section in parser.sections(): + result.update({str(section): dict(items[section])}) - def stringify(self) -> str: - buffer = StringIO() - self.parsed_content.write(buffer) - return buffer.getvalue() + return result - def has(self, section_key: str) -> bool: - if "." not in section_key: - return self.parsed_content.has_section(section_key) - section, key = split_on_dot(section_key, only_last_dot=True) - return self.parsed_content.has_option(section, key) +class IniParser(BaseParser): + def __init__(self, file_contents: str): + super().__init__( + file_contents=file_contents, + module=IniParserCompatibility, + decode_error=configparser.Error, + ) -- cgit v1.2.3 From d8ee572672c6d5429257664c5339b23e0c84c55a Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 20:54:42 -0500 Subject: Move IniParserCompatibility to new file --- config_file/parsers/ini_parser.py | 44 ++------------------------------------- 1 file changed, 2 insertions(+), 42 deletions(-) (limited to 'config_file/parsers/ini_parser.py') diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 2a29ba7..8dc7f47 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -1,47 +1,7 @@ import configparser -from io import StringIO from .base_parser import BaseParser - - -class IniParserCompatibility: - """ - Provides compatibility with the json, yaml, - and toml API so we can use the BaseParser - easily, while still being able to leverage - the built-in configparser to parse the file. - - It is used as the `module` parameter - for BaseParser, which requires us to have a - `dumps` and `loads` method. - """ - - @staticmethod - def loads(file_contents: str) -> dict: - parser = configparser.ConfigParser() - parser.read_string(file_contents) - - return IniParserCompatibility.__create_configparser_dict(parser) - - @staticmethod - def dumps(file_contents: dict) -> str: - parser = configparser.ConfigParser() - buffer = StringIO() - - parser.read_dict(file_contents) - parser.write(buffer) - - return buffer.getvalue() - - @staticmethod - def __create_configparser_dict(parser: configparser.ConfigParser) -> dict: - result = {} - items = dict(parser.items()) - - for section in parser.sections(): - result.update({str(section): dict(items[section])}) - - return result +from .ini_parser_compat import IniParserCompatibility class IniParser(BaseParser): @@ -50,4 +10,4 @@ class IniParser(BaseParser): file_contents=file_contents, module=IniParserCompatibility, decode_error=configparser.Error, - ) + ) \ No newline at end of file -- cgit v1.2.3 From b1c23487c33fc2ad5503b6340b17980a326918c8 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 1 Aug 2020 11:53:56 -0500 Subject: Lint files --- config_file/parsers/ini_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'config_file/parsers/ini_parser.py') diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 8dc7f47..72974f1 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -10,4 +10,4 @@ class IniParser(BaseParser): file_contents=file_contents, module=IniParserCompatibility, decode_error=configparser.Error, - ) \ No newline at end of file + ) -- cgit v1.2.3 From 59eeb1321fbad41dce7c971778e37ac1943fda3b Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 20:39:28 -0500 Subject: Redo package to have a flat package structure By having a flat package structure like this, it'll avoid exposing packages and things we don't want to be exposed. --- config_file/parsers/ini_parser.py | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 config_file/parsers/ini_parser.py (limited to 'config_file/parsers/ini_parser.py') diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py deleted file mode 100644 index 72974f1..0000000 --- a/config_file/parsers/ini_parser.py +++ /dev/null @@ -1,13 +0,0 @@ -import configparser - -from .base_parser import BaseParser -from .ini_parser_compat import IniParserCompatibility - - -class IniParser(BaseParser): - def __init__(self, file_contents: str): - super().__init__( - file_contents=file_contents, - module=IniParserCompatibility, - decode_error=configparser.Error, - ) -- cgit v1.2.3