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 --- README.md | 16 ++------ config_file/parsers/abstract_parser.py | 75 ++++++++++++++++++++++++++++++++++ config_file/parsers/base_parser.py | 75 ---------------------------------- 3 files changed, 78 insertions(+), 88 deletions(-) create mode 100644 config_file/parsers/abstract_parser.py delete mode 100644 config_file/parsers/base_parser.py diff --git a/README.md b/README.md index bb9a617..6b3b781 100644 --- a/README.md +++ b/README.md @@ -23,30 +23,20 @@ Config File is available to download through PyPI. $ pip install config-file ``` +### Installing Extras + If you want to manipulate YAML and TOML, you'll want to download the extras as well. ```bash $ pip install config-file[yaml, toml] ``` -You can also use Poetry. +You can also use [Poetry](https://python-poetry.org). ```bash $ poetry install config-file -E yaml -E toml ``` -If you use ZSH as your shell, brackets (`[]`) are used for glob matching -so you can work around that if installing extras in one of the following -ways. - -```zsh -% pip install 'config-file[yaml, toml]' -``` - -```zsh -% noglob pip install config-file[yaml, toml] -``` - ## Usage Say you have an INI file you want to manipulate. It must have an .ini diff --git a/config_file/parsers/abstract_parser.py b/config_file/parsers/abstract_parser.py new file mode 100644 index 0000000..da6e90a --- /dev/null +++ b/config_file/parsers/abstract_parser.py @@ -0,0 +1,75 @@ +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 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