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/base_parser.py | 3 --- 1 file changed, 3 deletions(-) (limited to 'config_file/parsers/base_parser.py') diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py index 42f93b1..5fc47c2 100644 --- a/config_file/parsers/base_parser.py +++ b/config_file/parsers/base_parser.py @@ -74,6 +74,3 @@ class BaseParser(ABC): """ raise NotImplementedError - -class ParsingError(Exception): - """Unable to parse the configuration file""" -- cgit v1.2.3 From b3fb6aae1201faca1238b3c79e5e95ba57282bf1 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 15:35:05 -0500 Subject: Add more type hints to ConfigFile and lint all files --- config_file/parsers/base_parser.py | 1 - 1 file changed, 1 deletion(-) (limited to 'config_file/parsers/base_parser.py') diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py index 5fc47c2..bbdfd91 100644 --- a/config_file/parsers/base_parser.py +++ b/config_file/parsers/base_parser.py @@ -73,4 +73,3 @@ class BaseParser(ABC): if you're going to be using the super() constructor. """ raise NotImplementedError - -- cgit v1.2.3 From ee78049f1e33bcb9894f43d51e44b079edfab91f Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 16:32:23 -0500 Subject: Add types to base parser and update build badge --- config_file/parsers/base_parser.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'config_file/parsers/base_parser.py') diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py index bbdfd91..a1d38d4 100644 --- a/config_file/parsers/base_parser.py +++ b/config_file/parsers/base_parser.py @@ -1,4 +1,5 @@ from abc import ABC, abstractmethod +from typing import Any class BaseParser(ABC): @@ -25,7 +26,7 @@ class BaseParser(ABC): self.parsed_content = self.parse(self.content) @abstractmethod - def get(self, key, parse_types=False): + def get(self, key: str, parse_types: bool = False, all: bool = False): """ Retrieve the value of a key in its native type. This means the string 'true' will be parsed back as the @@ -37,12 +38,12 @@ class BaseParser(ABC): raise NotImplementedError @abstractmethod - def set(self, key, value) -> bool: + def set(self, key: str, value: Any) -> bool: """Sets the value of a key.""" raise NotImplementedError @abstractmethod - def delete(self, section_key) -> bool: + def delete(self, section_key: str) -> bool: """Deletes a key/value pair or entire sections.""" raise NotImplementedError @@ -66,10 +67,9 @@ class BaseParser(ABC): raise NotImplementedError @abstractmethod - def parse(self, file_contents: str): + def parse(self, file_contents: str) -> Any: """ Parse the file_contents into an internal representation - the given parser can work with. It should not return anything - if you're going to be using the super() constructor. + the given parser can work with. """ raise NotImplementedError -- cgit v1.2.3 From c42fd6044f15102d6688cc958a50ab134df046ba Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 12 Jul 2020 09:11:53 -0500 Subject: Add return type hints to BaseParser --- config_file/parsers/base_parser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'config_file/parsers/base_parser.py') diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py index a1d38d4..da6e90a 100644 --- a/config_file/parsers/base_parser.py +++ b/config_file/parsers/base_parser.py @@ -12,7 +12,7 @@ class BaseParser(ABC): """ @abstractmethod - def __init__(self, file_contents: str): + def __init__(self, file_contents: str) -> None: """ All parsers will take in the file as a string, parse them internally to their own representation that they @@ -26,7 +26,7 @@ class BaseParser(ABC): self.parsed_content = self.parse(self.content) @abstractmethod - def get(self, key: str, parse_types: bool = False, all: bool = False): + def get(self, key: str, parse_types: bool = False, all: bool = False) -> Any: """ Retrieve the value of a key in its native type. This means the string 'true' will be parsed back as the -- cgit v1.2.3 From c476c1ed7065910fddc15ccfe67442c81f777bbf Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 19 Jul 2020 14:59:05 -0500 Subject: Remove zsh info and add extras sub header --- config_file/parsers/base_parser.py | 75 -------------------------------------- 1 file changed, 75 deletions(-) delete mode 100644 config_file/parsers/base_parser.py (limited to 'config_file/parsers/base_parser.py') diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py deleted file mode 100644 index da6e90a..0000000 --- a/config_file/parsers/base_parser.py +++ /dev/null @@ -1,75 +0,0 @@ -from abc import ABC, abstractmethod -from typing import Any - - -class BaseParser(ABC): - """ - The abstract base parser for all other parsers to implement. - - All the keys are specified in a 'dot' syntax. So if you have - sections with subsections with a key that you're trying to get - the value of, you'd use 'section.subsection.key' as the key parameter. - """ - - @abstractmethod - def __init__(self, file_contents: str) -> None: - """ - All parsers will take in the file as a string, parse - them internally to their own representation that they - can work with, and are able to stringify them back out - when we need to save (write) them. - - It is up to the caller of the parser to read in the file to - the parser and save the file when they are done using stringify(). - """ - self.content = file_contents - self.parsed_content = self.parse(self.content) - - @abstractmethod - def get(self, key: str, parse_types: bool = False, all: bool = False) -> Any: - """ - 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_types is set to False, all values will be returned - back as strings. - """ - raise NotImplementedError - - @abstractmethod - def set(self, key: str, value: Any) -> bool: - """Sets the value of a key.""" - raise NotImplementedError - - @abstractmethod - def delete(self, section_key: str) -> bool: - """Deletes a key/value pair or entire sections.""" - raise NotImplementedError - - @abstractmethod - def stringify(self) -> str: - """ - Using self.__contents, converts the parsed internal representation - of the file back into a string. - """ - raise NotImplementedError - - @abstractmethod - 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 - """ - raise NotImplementedError - - @abstractmethod - def parse(self, file_contents: str) -> Any: - """ - Parse the file_contents into an internal representation - the given parser can work with. - """ - raise NotImplementedError -- 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/base_parser.py | 86 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 config_file/parsers/base_parser.py (limited to 'config_file/parsers/base_parser.py') diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py new file mode 100644 index 0000000..09fbbbf --- /dev/null +++ b/config_file/parsers/base_parser.py @@ -0,0 +1,86 @@ +from typing import Any + +from config_file.exceptions import MissingKeyError, ParsingError +from config_file.nested_lookup import get_occurrence_of_key +from config_file.utils import split_on_dot + +from .abstract_parser import AbstractParser + + +class BaseParser(AbstractParser): + def __init__(self, file_contents: str, module, decode_error): + super().__init__(file_contents) + self.__module = module + self.__decode_error = decode_error + self.parsed_content = self.parse_file_contents(module, decode_error) + + def parse_file_contents(self, module, decode_error) -> Any: + try: + return module.loads(self.file_contents) + except decode_error as error: + raise ParsingError(error) + + def reset_internal_contents(self, file_contents: str) -> None: + self.file_contents = file_contents + self.parsed_content = self.parse_file_contents( + self.__module, self.__decode_error + ) + + def get(self, search_key: str) -> Any: + if "." not in search_key: + result = self.parsed_content[search_key] + return result + else: + split_keys = split_on_dot(search_key) + + result = self.parsed_content + key_causing_error = None + try: + for key in split_keys: + key_causing_error = key + result = result[key] + except TypeError: + raise ParsingError( + f"Cannot get {search_key} because " + f"{key_causing_error} is not subscriptable." + ) + + return result + + def set(self, key: str, value: Any) -> None: + if "." not in key: + self.parsed_content[key] = value + return + + keys = split_on_dot(key) + content_reference = self.parsed_content + for index, key in enumerate(keys): + if index == len(keys) - 1: + content_reference[key] = value + return + + content_reference = content_reference[key] + + def delete(self, key) -> None: + try: + if "." not in key: + del self.parsed_content[key] + else: + indexes = split_on_dot(key) + indexes_length = len(indexes) + + ref = self.parsed_content + for index, key in enumerate(indexes): + if index == indexes_length - 1: + del ref[key] + break + + ref = ref[key] + except KeyError: + raise MissingKeyError(f"The specified key '{key}' was not found.") + + def stringify(self) -> str: + return self.__module.dumps(self.parsed_content) + + def has(self, search_key: str) -> bool: + return get_occurrence_of_key(self.parsed_content, key=search_key) > 0 -- cgit v1.2.3 From 03811338b3bc9d07d491454ba5646ba2dd912463 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 21:33:54 -0500 Subject: Fix `get`, `set`, `delete` and `has` in BaseParser --- config_file/parsers/base_parser.py | 39 ++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) (limited to 'config_file/parsers/base_parser.py') diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py index 09fbbbf..546450c 100644 --- a/config_file/parsers/base_parser.py +++ b/config_file/parsers/base_parser.py @@ -1,4 +1,5 @@ from typing import Any +from types import ModuleType from config_file.exceptions import MissingKeyError, ParsingError from config_file.nested_lookup import get_occurrence_of_key @@ -8,7 +9,9 @@ from .abstract_parser import AbstractParser class BaseParser(AbstractParser): - def __init__(self, file_contents: str, module, decode_error): + def __init__( + self, file_contents: str, module: ModuleType, decode_error: Exception + ): super().__init__(file_contents) self.__module = module self.__decode_error = decode_error @@ -39,9 +42,9 @@ class BaseParser(AbstractParser): for key in split_keys: key_causing_error = key result = result[key] - except TypeError: + except (KeyError, TypeError): raise ParsingError( - f"Cannot get {search_key} because " + f"Cannot `get` {search_key} because " f"{key_causing_error} is not subscriptable." ) @@ -59,9 +62,13 @@ class BaseParser(AbstractParser): content_reference[key] = value return - content_reference = content_reference[key] + try: + content_reference = content_reference[key] + except KeyError: + content_reference[key] = {} + content_reference = content_reference[key] - def delete(self, key) -> None: + def delete(self, key: str) -> None: try: if "." not in key: del self.parsed_content[key] @@ -69,18 +76,26 @@ class BaseParser(AbstractParser): indexes = split_on_dot(key) indexes_length = len(indexes) - ref = self.parsed_content + content_reference = self.parsed_content for index, key in enumerate(indexes): if index == indexes_length - 1: - del ref[key] + del content_reference[key] break - ref = ref[key] - except KeyError: - raise MissingKeyError(f"The specified key '{key}' was not found.") + content_reference = content_reference[key] + except (KeyError, TypeError): + raise MissingKeyError(f"The specified key '{key}' to delete was not found.") def stringify(self) -> str: return self.__module.dumps(self.parsed_content) - def has(self, search_key: str) -> bool: - return get_occurrence_of_key(self.parsed_content, key=search_key) > 0 + def has(self, search_key: str, wild: bool = False) -> bool: + if wild: + return get_occurrence_of_key(self.parsed_content, key=search_key) > 0 + + try: + self.get(search_key) + return True + except (KeyError, ParsingError): + return False + -- 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/base_parser.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'config_file/parsers/base_parser.py') diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py index 546450c..c2a7d71 100644 --- a/config_file/parsers/base_parser.py +++ b/config_file/parsers/base_parser.py @@ -1,5 +1,5 @@ -from typing import Any from types import ModuleType +from typing import Any from config_file.exceptions import MissingKeyError, ParsingError from config_file.nested_lookup import get_occurrence_of_key @@ -9,9 +9,7 @@ from .abstract_parser import AbstractParser class BaseParser(AbstractParser): - def __init__( - self, file_contents: str, module: ModuleType, decode_error: Exception - ): + def __init__(self, file_contents: str, module: ModuleType, decode_error: Exception): super().__init__(file_contents) self.__module = module self.__decode_error = decode_error @@ -92,10 +90,9 @@ class BaseParser(AbstractParser): def has(self, search_key: str, wild: bool = False) -> bool: if wild: return get_occurrence_of_key(self.parsed_content, key=search_key) > 0 - + try: self.get(search_key) return True except (KeyError, ParsingError): return False - -- 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/base_parser.py | 98 -------------------------------------- 1 file changed, 98 deletions(-) delete mode 100644 config_file/parsers/base_parser.py (limited to 'config_file/parsers/base_parser.py') diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py deleted file mode 100644 index c2a7d71..0000000 --- a/config_file/parsers/base_parser.py +++ /dev/null @@ -1,98 +0,0 @@ -from types import ModuleType -from typing import Any - -from config_file.exceptions import MissingKeyError, ParsingError -from config_file.nested_lookup import get_occurrence_of_key -from config_file.utils import split_on_dot - -from .abstract_parser import AbstractParser - - -class BaseParser(AbstractParser): - def __init__(self, file_contents: str, module: ModuleType, decode_error: Exception): - super().__init__(file_contents) - self.__module = module - self.__decode_error = decode_error - self.parsed_content = self.parse_file_contents(module, decode_error) - - def parse_file_contents(self, module, decode_error) -> Any: - try: - return module.loads(self.file_contents) - except decode_error as error: - raise ParsingError(error) - - def reset_internal_contents(self, file_contents: str) -> None: - self.file_contents = file_contents - self.parsed_content = self.parse_file_contents( - self.__module, self.__decode_error - ) - - def get(self, search_key: str) -> Any: - if "." not in search_key: - result = self.parsed_content[search_key] - return result - else: - split_keys = split_on_dot(search_key) - - result = self.parsed_content - key_causing_error = None - try: - for key in split_keys: - key_causing_error = key - result = result[key] - except (KeyError, TypeError): - raise ParsingError( - f"Cannot `get` {search_key} because " - f"{key_causing_error} is not subscriptable." - ) - - return result - - def set(self, key: str, value: Any) -> None: - if "." not in key: - self.parsed_content[key] = value - return - - keys = split_on_dot(key) - content_reference = self.parsed_content - for index, key in enumerate(keys): - if index == len(keys) - 1: - content_reference[key] = value - return - - try: - content_reference = content_reference[key] - except KeyError: - content_reference[key] = {} - content_reference = content_reference[key] - - def delete(self, key: str) -> None: - try: - if "." not in key: - del self.parsed_content[key] - else: - indexes = split_on_dot(key) - indexes_length = len(indexes) - - content_reference = self.parsed_content - for index, key in enumerate(indexes): - if index == indexes_length - 1: - del content_reference[key] - break - - content_reference = content_reference[key] - except (KeyError, TypeError): - raise MissingKeyError(f"The specified key '{key}' to delete was not found.") - - def stringify(self) -> str: - return self.__module.dumps(self.parsed_content) - - def has(self, search_key: str, wild: bool = False) -> bool: - if wild: - return get_occurrence_of_key(self.parsed_content, key=search_key) > 0 - - try: - self.get(search_key) - return True - except (KeyError, ParsingError): - return False -- cgit v1.2.3