diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-02-02 14:21:02 -0600 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-02-02 14:21:02 -0600 |
| commit | 592ea6812da05ec49a7428862309e078704bc740 (patch) | |
| tree | c200a4924bcfd3cb83997c4aa4dc83c9cf7c8f41 | |
| parent | Add ability to parse values in list to native types (diff) | |
| download | config-file-592ea6812da05ec49a7428862309e078704bc740.zip | |
Add type hints + contents is now content in BaseParser
| -rw-r--r-- | config_file/parsers/base_parser.py | 8 | ||||
| -rw-r--r-- | config_file/parsers/ini_parser.py | 11 |
2 files changed, 9 insertions, 10 deletions
diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py index 22e286a..54756cd 100644 --- a/config_file/parsers/base_parser.py +++ b/config_file/parsers/base_parser.py @@ -21,8 +21,8 @@ class BaseParser(ABC): 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.contents = file_contents - self.parsed_content = self.parse(self.contents) + self.content = file_contents + self.parsed_content = self.parse(self.content) @abstractmethod def get(self, key, parse_type=True): @@ -37,12 +37,12 @@ class BaseParser(ABC): raise NotImplementedError @abstractmethod - def set(self, key, value): + def set(self, key, value) -> bool: """Sets the value of a key.""" raise NotImplementedError @abstractmethod - def delete(self, section_key): + def delete(self, section_key) -> bool: """Deletes a key/value pair or entire sections.""" raise NotImplementedError diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 549ed37..93f239c 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -19,7 +19,7 @@ class IniParser(BaseParser): except configparser.Error as error: raise ParsingError(error) - def get(self, section_key, parse_type=True): + def get(self, section_key: str, parse_type: bool = True): """ Read the value of `section.key` of the config file. @@ -54,18 +54,17 @@ class IniParser(BaseParser): return items - def set(self, section_key, value): + 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. :param section_key: The key to set from the config file. e.g. 'section1.key' - :param value: The value to set the key to. + :param value: The value to set the key to. It can be any type that can + be converted to a string. :return: True if the setting was successful. - :rtype: boolean - :raises ValueError: If there is no dot (.) in section_key """ section, key = split_on_dot(section_key, only_last_dot=True) @@ -79,7 +78,7 @@ class IniParser(BaseParser): self.parsed_content.set(section, key, value) return True - def delete(self, section_key): + def delete(self, section_key: str) -> bool: """ Deletes a key or an entire section. |