diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-01 08:57:17 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-01 08:57:17 -0500 |
| commit | ba655e72f74bc488cb8e5988766fef8f5dd45bcc (patch) | |
| tree | f967eb203b0a6e389022c5383a7eb23414059051 /config_file | |
| parent | Add name to security workflow (diff) | |
| parent | Fix wording of docstring in missing dependency error (diff) | |
| download | config-file-ba655e72f74bc488cb8e5988766fef8f5dd45bcc.zip | |
Merge branch 'dev' into dev
Diffstat (limited to 'config_file')
| -rw-r--r-- | config_file/config_file.py | 96 | ||||
| -rw-r--r-- | config_file/exceptions.py | 4 | ||||
| -rw-r--r-- | config_file/parsers/base_parser.py | 39 | ||||
| -rw-r--r-- | config_file/parsers/ini_parser.py | 44 | ||||
| -rw-r--r-- | config_file/parsers/ini_parser_compat.py | 42 | ||||
| -rw-r--r-- | config_file/parsers/parse_value.py | 63 | ||||
| -rw-r--r-- | config_file/parsers/toml_parser.py | 2 | ||||
| -rw-r--r-- | config_file/parsers/yaml_parser.py | 19 | ||||
| -rw-r--r-- | config_file/utils.py | 8 |
9 files changed, 204 insertions, 113 deletions
diff --git a/config_file/config_file.py b/config_file/config_file.py index 3ac4b91..9b14072 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -17,25 +17,26 @@ class ConfigFile: def __init__( self, file_path: Union[str, Path], - parser: Optional[Type[AbstractParser]] = None, + parser: Optional[Type[AbstractParser]] = None ) -> None: """ - Saves the config file path and expands it if needed, reads in + Stores the config file path and expands it if needed, reads in the file contents, and determines what parser should be used for the given file. - :param file_path: The path to your configuration file. - :param parser: A custom parser you'd like used for your config file. + Args: + file_path: The path to your configuration file. + parser: A custom parser you'd like used for your config file. - :raises ValueError: If the specified file path does not have an extension - that is supported or it is a directory. + Raises: + ValueError: If the specified file path does not have an extension + that is supported or it is a directory. """ if not isinstance(file_path, Path): file_path = Path(file_path) self.__path = create_config_path(file_path) - self.__contents = read_file(self.__path) - self.__parser = self.__determine_parser(file_path, parser) + self.__parser = self.__determine_parser(self.__path, parser) @property def path(self) -> Path: @@ -44,8 +45,10 @@ class ConfigFile: def __determine_parser( self, file_path: Path, parser: Optional[Type[AbstractParser]] = None ) -> Type[AbstractParser]: + file_contents = read_file(file_path) + if parser is not None and not inspect.isabstract(parser): - return parser(self.__contents) + return parser(file_contents) try: file_type = split_on_dot(file_path, only_last_dot=True)[-1] @@ -56,7 +59,7 @@ class ConfigFile: ) return self.__find_parser_by_file_type( - file_type=file_type, file_contents=self.__contents, file_path=self.__path + file_type=file_type, file_contents=file_contents, file_path=file_path ) @staticmethod @@ -86,18 +89,23 @@ class ConfigFile: """ Retrieve the value of a key. - :param key: The key to retrieve. - :param parse_types: Automatically parse ints, floats, booleans, dicts, and - lists. This recursively parses all types in whatever you're - retrieving, not just a single type. e.g. If you are - retrieving a section, all values in that section with be - parsed. - :param return_type: The type to coerce the return value to. - :param default: The default value to return if the value of the key is empty. + Args: + key: The key to retrieve. + parse_types: Automatically parse ints, floats, booleans, dicts, and + lists. This recursively parses all types in whatever you're + retrieving, not just a single type. + + e.g. If you are retrieving a section, all values in that section + will be parsed. - :return: The value of the key. + return_type: The type to coerce the return value to. + default: The default value to return if the value of the key is empty. - :raises ValueError: If the value is not able to be coerced into return_type + Returns: + The value of the key. + + Raises: + ValueError: If the value is not able to be coerced into return_type. """ try: key_value = self.__parser.get(key) @@ -115,16 +123,33 @@ class ConfigFile: def set(self, key: str, value: Any) -> None: """Sets the value of a key. + If the given key does not exist, it will be automatically + created for you. That includes if there are multiple keys + in a row that do not exist. + e.g. set('exists.does_not.does_not.also_does_not', 5) + + The behavior of how this is done, however, depends on the + file used. For example, with INI, subsections are not supported. + So it would create a key in the section `exists` called `does_not` + and set it to the value {'does_not': {'also_does_not': 5}}. + + Args: + key: The section, sub-section, or key to delete. + value: The value to set the key to. """ self.__parser.set(key, value) - def delete(self, section_key: str) -> None: + def delete(self, key: str) -> None: """Deletes a section or key. Args: - section_key: + key: The section, sub-section, or key to delete. + + Raises: + MissingKeyError: If a key is attempted to be deleted that + does not exist. """ - self.__parser.delete(section_key) + self.__parser.delete(key) def stringify(self) -> str: """Retrieves file contents as a string. @@ -135,7 +160,7 @@ class ConfigFile: """ return self.__parser.stringify() - def has(self, section_key: str) -> bool: + def has(self, key: str, wild: bool = False) -> bool: """ Check if a section, sub-section, or key exists. @@ -144,12 +169,19 @@ class ConfigFile: key exists. Args: - section_key: + key: The section, sub-section, or key to find. + wild: Whether or not to search everywhere. + + Without `wild`, a single word `key` without a `.` + will look at the outer most heirarchy of the file for it. + + With `wild`, that single word `key` will be searched + for throughout the entire file. Returns: - True if + True if the key exists. False otherwise. """ - return self.__parser.has(section_key) + return self.__parser.has(key, wild=wild) def restore_original( self, original_file_path: Union[str, Path, None] = None @@ -187,17 +219,15 @@ class ConfigFile: self.__path.expanduser().unlink() copyfile(original_file_path, self.__path) - - self.__contents = read_file(self.__path) - self.__parser.reset_internal_contents(self.__contents) + self.__parser.reset_internal_contents(read_file(self.__path)) def save(self) -> None: """ Save your configuration changes. This writes the file back out, including any changes - you've made, to the specified path when this object - was created. + you've made, to the specified path given from this + object's constructor. """ with open(str(self.__path), "w") as config_file: - config_file.write(self.__parser.stringify()) + config_file.write(self.stringify()) diff --git a/config_file/exceptions.py b/config_file/exceptions.py index d11e373..d1b466b 100644 --- a/config_file/exceptions.py +++ b/config_file/exceptions.py @@ -4,8 +4,8 @@ class ConfigFileError(Exception): class MissingDependencyError(ConfigFileError): """ - PyYaml or toml is not installed, but the parsers - for them were attempted to be used. + yaml or toml dependencies were not installed, + but the parsers for them were attempted to be used. """ 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 + 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 diff --git a/config_file/parsers/ini_parser_compat.py b/config_file/parsers/ini_parser_compat.py new file mode 100644 index 0000000..9dfea2e --- /dev/null +++ b/config_file/parsers/ini_parser_compat.py @@ -0,0 +1,42 @@ +import configparser +from io import StringIO + + +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 diff --git a/config_file/parsers/parse_value.py b/config_file/parsers/parse_value.py index 26bdf2e..aa8d1fe 100644 --- a/config_file/parsers/parse_value.py +++ b/config_file/parsers/parse_value.py @@ -19,20 +19,12 @@ Example: can_be_parsed_as_int("-5") -> True parse_value("-5") -> -5 """ +import ast import re from distutils.util import strtobool def parse_value(value): - if can_be_parsed_as_int(value): - return int(value) - - if can_be_parsed_as_float(value): - return float(value) - - if can_be_parsed_as_bool(value): - return value if type(value) is bool else bool(strtobool(value)) - if type(value) is dict: parsed = {} @@ -49,6 +41,21 @@ def parse_value(value): return parsed + if can_be_parsed_as_int(value): + return int(value) + + if can_be_parsed_as_float(value): + return float(value) + + if can_be_parsed_as_bool(value): + return value if type(value) is bool else bool(strtobool(value)) + + if can_be_parsed_as_dict(value): + return parse_value(ast.literal_eval(value)) + + if can_be_parsed_as_list(value): + return parse_value(ast.literal_eval(value)) + return value @@ -75,7 +82,7 @@ def can_be_parsed_as_float(value) -> bool: return False if value.startswith("-"): - return bool(re.match(FLOAT_REGEX, value[1:])) + value = value[1:] return bool(re.match(FLOAT_REGEX, value)) @@ -88,3 +95,39 @@ def can_be_parsed_as_bool(value) -> bool: return False return value.lower() == "true" or value.lower() == "false" + + +def can_be_parsed_as_dict(value) -> bool: + if type(value) is dict: + return True + + if type(value) is not str: + return False + + value = value.strip() + if value[0] == "{" and value[-1] == "}": + try: + ast.literal_eval(value) + return True + except SyntaxError: + return False + + return False + + +def can_be_parsed_as_list(value) -> bool: + if type(value) is dict: + return True + + if type(value) is not str: + return False + + value = value.strip() + if value[0] == "[" and value[-1] == "]": + try: + ast.literal_eval(value) + return True + except SyntaxError: + return False + + return False diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py index d369003..1c7f8a7 100644 --- a/config_file/parsers/toml_parser.py +++ b/config_file/parsers/toml_parser.py @@ -14,7 +14,7 @@ class TomlParser(BaseParser): def __init__(self, file_contents: str): if not TOML_AVAILABLE: raise MissingDependencyError( - "It doesn't appear `toml` is installed, but a toml " + "It doesn't appear `tomlkit` is installed, but a toml " "file was attempted to be used. Install the `toml` " "extra first with `pip install config-file[toml]`." ) diff --git a/config_file/parsers/yaml_parser.py b/config_file/parsers/yaml_parser.py index 77b59fe..babe873 100644 --- a/config_file/parsers/yaml_parser.py +++ b/config_file/parsers/yaml_parser.py @@ -1,12 +1,7 @@ try: - import yaml + from ruamel.yaml import YAML, YAMLError, round_trip_dump, round_trip_load YAML_AVAILABLE = True - - # Conform yaml to json and - # toml package syntax - yaml.loads = yaml.load - yaml.dumps = yaml.dump except ImportError: YAML_AVAILABLE = False @@ -19,11 +14,15 @@ class YamlParser(BaseParser): def __init__(self, file_contents: str): if not YAML_AVAILABLE: raise MissingDependencyError( - "It doesn't appear `PyYaml` is installed, but a yaml " - "file was attempted to be used. Install the `toml` " - "extra first with `pip install config-file[toml]`." + "It doesn't appear `ruamel.yaml` is installed, but a yaml " + "file was attempted to be used. Install the `yaml` " + "extra first with `pip install config-file[yaml]`." ) + yaml = YAML(typ="safe") + yaml.loads = round_trip_load + yaml.dumps = round_trip_dump + super().__init__( - file_contents=file_contents, module=yaml, decode_error=yaml.YAMLError + file_contents=file_contents, module=yaml, decode_error=YAMLError ) diff --git a/config_file/utils.py b/config_file/utils.py index de31c69..a75968c 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -6,10 +6,12 @@ def split_on_dot(line: Union[str, Path], only_last_dot=False) -> List[str]: """ Split a string on the dot character (.). - :param line: The line ot split on. - :param only_last_dot: Only split on the last occurrence of the dot. + Args: + line: The line ot split on. + only_last_dot: Only split on the last occurrence of the dot. - :raises ValueError: if the line does not have a dot. + Raises: + ValueError: if the line does not have a dot. """ if isinstance(line, Path): line = str(line) |