diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-07 20:25:00 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-07 20:25:00 -0500 |
| commit | 05dba35cab34c2524b945da0164327b7b8a1159d (patch) | |
| tree | e1f263a316839c0d32a413c120a3c92426f36817 /config_file | |
| parent | Create python-package-ci.yml (diff) | |
| parent | Fix ConfigFile default (diff) | |
| download | config-file-05dba35cab34c2524b945da0164327b7b8a1159d.zip | |
Merge pull request #24 from eugenetriguba/dev
v0.11.0
Diffstat (limited to 'config_file')
| -rw-r--r-- | config_file/__init__.py | 10 | ||||
| -rw-r--r-- | config_file/abstract_parser.py | 118 | ||||
| -rw-r--r-- | config_file/base_parser.py | 139 | ||||
| -rw-r--r-- | config_file/config_file.py | 269 | ||||
| -rw-r--r-- | config_file/config_file_path.py | 116 | ||||
| -rw-r--r-- | config_file/exceptions.py | 9 | ||||
| -rw-r--r-- | config_file/ini_parser.py | 13 | ||||
| -rw-r--r-- | config_file/ini_parser_compat.py | 42 | ||||
| -rw-r--r-- | config_file/json_parser.py | 15 | ||||
| -rw-r--r-- | config_file/nested_lookup.py | 402 | ||||
| -rw-r--r-- | config_file/parse_value.py | 218 | ||||
| -rw-r--r-- | config_file/parsers/__init__.py | 0 | ||||
| -rw-r--r-- | config_file/parsers/base_parser.py | 79 | ||||
| -rw-r--r-- | config_file/parsers/ini_parser.py | 123 | ||||
| -rw-r--r-- | config_file/parsers/json_parser.py | 129 | ||||
| -rw-r--r-- | config_file/parsers/parse_value.py | 87 | ||||
| -rw-r--r-- | config_file/toml_parser.py | 18 | ||||
| -rw-r--r-- | config_file/utils.py | 43 | ||||
| -rw-r--r-- | config_file/yaml_parser.py | 21 |
19 files changed, 1295 insertions, 556 deletions
diff --git a/config_file/__init__.py b/config_file/__init__.py index 3bae28a..853da2a 100644 --- a/config_file/__init__.py +++ b/config_file/__init__.py @@ -1,10 +1,6 @@ -from config_file.config_file import ConfigFile -from config_file.parsers.base_parser import BaseParser, ParsingError +from .config_file import ConfigFile +from .exceptions import ParsingError __version__ = "0.10.0" -__all__ = [ - "ConfigFile", - "ParsingError", - "BaseParser", -] +__all__ = ["ConfigFile", "ParsingError"] diff --git a/config_file/abstract_parser.py b/config_file/abstract_parser.py new file mode 100644 index 0000000..689fece --- /dev/null +++ b/config_file/abstract_parser.py @@ -0,0 +1,118 @@ +from abc import ABC, abstractmethod +from typing import Any + + +class AbstractParser(ABC): + """Abstract Parser is the API contract for all parsers. + + Every parser in ConfigFile implements this parser, and + any user created parsers must implement this parser in + order for this package to be able to use it. + + Every key is specified in a dot (.) syntax + e.g. retrieving.some.deeply.nested.value + + If there is no dot in the key, it is assumed we are + performing an action on a top level key, which could + be an entire section. + + Args: + file_contents: The stringified version of a file. + + Attributes: + file_contents: The stringified version of a file. + """ + + @abstractmethod + def __init__(self, file_contents: str) -> None: + self.file_contents = file_contents + + @abstractmethod + def reset_internal_contents(self, file_contents: str) -> None: + """Reset the state of this parser to a new file. + + When restoring to the original file in ConfigFile, + we need a way to reset the internal contents to this + new file. + + While this may be as simple as setting the file_contents + attribute value to the passed in file_contents argument, + it likely would not be. This is because the initial + passed in file contents would likely be parsed into some + special form for the parser to work with more easily. + + Args: + file_contents: The file contents of the new file. + """ + raise NotImplementedError + + @abstractmethod + def get(self, key: str) -> Any: + """Retrieve the value of a key from the working file. + + Args: + key: The key to retrieve. + + Returns: + Whatever value the given key happens to contain. + + Raises: + KeyError: If the key to retrieve does not exist. + """ + raise NotImplementedError + + @abstractmethod + def set(self, key: str, value: Any) -> None: + """Set the value of a key in the working file. + + If the key does not exist, it should be created. + + Args: + key: The key to set. + value: The value to set the key to. + """ + raise NotImplementedError + + @abstractmethod + def delete(self, key: str) -> None: + """Deletes a key from the working file. + + Args: + key: The key we would like to remove. + + Raises: + KeyError: If the key to remove does not exist. + """ + raise NotImplementedError + + @abstractmethod + def stringify(self) -> str: + """Stringify the working file. + + Since the ConfigFile does not write out to disk + after every operation, this method is what is + used as the output for saving the file again. + This parser should never write out to the disk. + + Returns: + The file we are working with as a string. + """ + raise NotImplementedError + + @abstractmethod + def has(self, key: str, wild: bool = False) -> bool: + """Check whether the given key is in the parsed working file. + + This should not behave like a wild card, as in check + if the key exists anywhere in the working file. Instead, + if a key is given without a dot, it should be assumed we + are checking the top level. + + Args: + key: The key to search for. + wild: Optionally search for any occurrence of key in the file. + + Returns: + True if the working file has the key. False otherwise. + """ + raise NotImplementedError diff --git a/config_file/base_parser.py b/config_file/base_parser.py new file mode 100644 index 0000000..a440dcb --- /dev/null +++ b/config_file/base_parser.py @@ -0,0 +1,139 @@ +from types import ModuleType +from typing import Any, Type + +from config_file.abstract_parser import AbstractParser +from config_file.exceptions import ParsingError +from config_file.nested_lookup import get_occurrence_of_key +from config_file.utils import split_on_dot + + +class BaseParser(AbstractParser): + def __init__( + self, file_contents: str, module: ModuleType, decode_error: Type[Exception] + ): + """The BaseParser implements the AbstractParser for us by giving it any module. + + As long as the module has a `loads` and `dumps` method, the BaseParser can use + it. Every file is then represented and worked with as a dictionary. + + Args: + file_contents: The contents of the file to parse. + + module: The module to use to parse the files. + Must have a `loads` and `dumps` method. + + decode_error: The error the module raises when the + file cannot be decoded. + + Raises: + ParsingError: If the decode_error is raised. + """ + 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: ModuleType, decode_error: Exception) -> dict: + """Parse the file contents by running the `loads` method on the module. + + Args: + module: The module to use to parse the file. + decode_error: The error that is raised from the module if the file + cannot be decoded. + + Raises: + ParsingError: If the decode_error is raised while running `loads`. + + Returns: + The parsed file as a dictionary. + """ + try: + return module.loads(self.file_contents) + except decode_error as error: + raise ParsingError(error) + + def reset_internal_contents(self, file_contents: str) -> None: + """Reset the file contents and parsed contents of the parser. + + Args: + file_contents: The new file contents. + + Raises: + ParsingError: If we're unable to parse the new file contents. + """ + self.file_contents = file_contents + self.parsed_content = self.parse_file_contents( + self.__module, self.__decode_error + ) + + def get(self, search_key: str) -> Any: + key_causing_error = None + + try: + if "." not in search_key: + key_causing_error = search_key + return self.parsed_content[search_key] + else: + split_keys = split_on_dot(search_key) + + content_reference = self.parsed_content + for key in split_keys: + key_causing_error = key + content_reference = content_reference[key] + + except (KeyError, TypeError): + raise KeyError( + f"cannot `get` {search_key} because " + f"{key_causing_error} is not subscriptable." + ) + + return content_reference + + 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 KeyError(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 diff --git a/config_file/config_file.py b/config_file/config_file.py index 91202da..2a8d9ae 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -1,155 +1,188 @@ -import inspect -from pathlib import Path, PurePath +from pathlib import Path from shutil import copyfile +from typing import Any, Union -from config_file.parsers.base_parser import BaseParser, ParsingError -from config_file.parsers.ini_parser import IniParser -from config_file.parsers.json_parser import JsonParser -from config_file.utils import split_on_dot +from config_file.config_file_path import ConfigFilePath +from config_file.parse_value import parse_value +from config_file.utils import Default class ConfigFile: - def __init__(self, file_path, parser: BaseParser = None): + def __init__(self, file_path: Union[str, Path]) -> 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. - It must be a concrete implementation of BaseParser. + Args: + file_path: The path to your configuration 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. + FileNotFoundError: If the specified file path does not exist. """ - self.path = self.__create_config_path(file_path) - self.contents = self.__read_config_file() - self.parser = self.__determine_parser(file_path, parser) - - @staticmethod - def __create_config_path(file_path, original: bool = False) -> str: - if isinstance(file_path, PurePath): - file_path = str(file_path) - - if len(file_path) > 1 and file_path[0] == "~": - return str(Path(file_path).expanduser()) - - if original: - file_parts = split_on_dot(file_path, only_last_dot=True) - file_parts.insert(-1, "original") - file_path = ".".join(file_parts) - - if Path(file_path).is_dir(): - raise ValueError(f"The specified config file ({file_path}) is a directory.") - - return file_path - - def __determine_parser(self, file_path, parser: BaseParser): - if isinstance(file_path, PurePath): - file_path = str(file_path) - - if parser is not None and not inspect.isabstract(parser): - return parser(self.contents) - - file_type = split_on_dot(file_path, only_last_dot=True)[-1] - if file_type == "ini": - return IniParser(self.contents) - elif file_type == "json": - return JsonParser(self.contents) - else: - raise ValueError( - f"File path contains an unsupported or " - f"unrecognized file type: {file_path}" - ) - - def __read_config_file(self): - with open(self.path, "r") as file: - return file.read() - - def get(self, key: str, parse_types: bool = False, return_type=None, default=None): + self.__path = ConfigFilePath(file_path).validate() + self.__parser = self.__path.parser + + @property + def path(self) -> Path: + return Path(self.__path) + + @property + def original_path(self) -> Path: + return Path(self.__path.original_path) + + def __getitem__(self, key: str): + return self.__parser.parsed_content[key] + + def __setitem__(self, key: str, value: Any): + self.__parser.parsed_content[key] = value + + def __delitem__(self, key: str): + del self.__parser.parsed_content[key] + + def get( + self, + key: str, + parse_types: bool = False, + return_type: Any = None, + default: Any = Default(None), + ) -> Any: """ 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. + + Returns: + The value of the key. + + Raises: + KeyError: If a key is attempted to be retrieved that does not exist. + ValueError: If the value is not able to be coerced into return_type. """ - if default is None: - key_value = self.parser.get(key, parse_types=parse_types) - else: - try: - key_value = self.parser.get(key, parse_types=parse_types) - except ParsingError: - return default + try: + key_value = self.__parser.get(key) + except KeyError as error: + if isinstance(default, Default) and default.value is None: + raise KeyError(error) + else: + key_value = default + + if parse_types: + key_value = parse_value(key_value) return return_type(key_value) if return_type else key_value - def set(self, key: str, value): - """Sets the value of a key.""" - return self.parser.set(key, value) + def set(self, key: str, value: Any) -> None: + """Sets the value of a key. - def delete(self, section_key: str): - """Deletes a key/value pair or entire sections.""" - return self.parser.delete(section_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) - def stringify(self) -> str: - """Convert the parsed internal representation of the file back into a string.""" - return self.parser.stringify() + 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}}. - def has(self, section_key: str) -> bool: + Args: + key: The section, sub-section, or key to delete. + value: The value to set the key to. """ - Check if a section, sub-section, or key exists in the file - using a section.key format. + self.__parser.set(key, value) + + def delete(self, key: str) -> None: + """Deletes a section or key. + + Args: + key: The section, sub-section, or key to delete. - Some formats, like JSON, do not have sections and therefore, - it would only be checking if the key exists. + Raises: + KeyError: If a key is attempted to be deleted that + does not exist. """ - return self.parser.has(section_key) + self.__parser.delete(key) - def restore_original(self, original_file_path=None): + def stringify(self) -> str: + """Retrieves file contents as a string. + + Returns: + The internal representation of the file + that has been read in converted to a string. + """ + return self.__parser.stringify() + + def has(self, key: str, wild: bool = False) -> bool: """ - Restores the original the config file by deleting it and copying the original - back in its place. The internal contents of this config file object are then set - to the newly reset config file. + Check if a section, sub-section, or key exists. + + Some formats, like JSON, do not have sections and + therefore, it would only be checking if a particular + key exists. + + Args: + key: The section, sub-section, or key to find. + wild: Whether or not to search everywhere. - :param original_file_path: The path to the original config file to reset it to. - If this is not provided and, say, your configuration file is config.json, the - method would look for a config.original.json in the same folder by default. + Without `wild`, a single word `key` without a `.` + will look at the outer most hierarchy of the file for it. - :return: True if the reset succeeded - :raises OSError: If the original configuration file is not present or if an - error occurred when trying to copy the file. + With `wild`, that single word `key` will be searched + for throughout the entire file. + + Returns: + True if the key exists. False otherwise. """ - if original_file_path is None: - original_file_path = self.__create_config_path( - str(self.path), original=True - ) + return self.__parser.has(key, wild=wild) + + def restore_original(self, original_path: Union[str, Path, None] = None) -> None: + """Restores the original the configuration file. + + The current one is deleted and the original is copied back + in its place. The internal contents are then reset to the + new file. - if not Path(original_file_path).exists(): - raise OSError( - f"The {original_file_path} file to restore to does not exist." - ) + Args: + original_path: The original file to reset to. - Path(self.path).expanduser().unlink() - copyfile(original_file_path, self.path) - self.contents = self.__read_config_file() - self.parser.parsed_content = self.parser.parse(self.contents) + Defaults to the original_path property if it is not + provided. - return True + Raises: + FileNotFoundError: If the original configuration file (whether + calculated or passed in) does not exist or if the current + configuration path is passed in as the original_path (since + it is deleted before the original file is copied over). - def save(self): + OSError: If the current configuration path is not writable. """ - Save the configuration changes by writing the file out to the specified path - :return: True if the save succeeded. + original_path = ConfigFilePath( + original_path if original_path else self.original_path + ).validate() + + self.__path.unlink() + copyfile(original_path, self.__path) + self.__parser.reset_internal_contents(self.__path.contents) + + def save(self) -> None: """ - with open(self.path, "w") as config_file: - config_file.write(self.parser.stringify()) + Save your configuration changes. - return True + This writes the file back out, including any changes + 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.stringify()) diff --git a/config_file/config_file_path.py b/config_file/config_file_path.py new file mode 100644 index 0000000..11a5daa --- /dev/null +++ b/config_file/config_file_path.py @@ -0,0 +1,116 @@ +import os +from pathlib import Path as _Path_ +from pathlib import _posix_flavour, _windows_flavour +from typing import Type + +from config_file.abstract_parser import AbstractParser +from config_file.ini_parser import IniParser +from config_file.json_parser import JsonParser +from config_file.toml_parser import TomlParser +from config_file.yaml_parser import YamlParser + +from .utils import split_on_dot + + +class ConfigFilePath(_Path_): + """A ConfigFilePath object, which subclasses Path. + + It provides us easy ways to grab the original config file + path, file extension, validate the path, or determine the parser + that should be use for the given file type. + + See https://codereview.stackexchange.com/questions/162426/subclassing-pathlib-path + for more details on subclassing the pathlib.Path object. + """ + + _flavour = _windows_flavour if os.name == "nt" else _posix_flavour + + @property + def original_path(self) -> "ConfigFilePath": + """Computes the original configuration file path. + + e.g. ConfigFilePath("./config.ini").original_path -> "./config.original.ini" + + Returns: + The path to the original configuration file, which + may or may not exist. + """ + file_parts = split_on_dot(self, only_last_dot=True) + file_parts.insert(-1, "original") + return ConfigFilePath(".".join(file_parts)) + + @property + def extension(self) -> str: + """Retrieves the extension of the file + path we passed in, without the '.'. + + e.g. ConfigFilePath("./pyproject.toml").extension -> "toml" + + Returns: + The extension of the current file path. + """ + _, extension = os.path.splitext(str(self)) + return extension[1:] + + @property + def parser(self) -> Type[AbstractParser]: + """Determine what parser should be used for this file. + + Raises: + ValueError: If the extension of the file is not recognized. + + Returns: + The instantiated parser that should be used for this file. + """ + if self.extension == "": + raise ValueError( + "Tried to determine a parser to use, but the file at " + f"{self} does not have an extension." + ) + + if self.extension == "ini": + return IniParser(self.contents) + elif self.extension == "json": + return JsonParser(self.contents) + elif self.extension == "yaml" or self.extension == "yml": + return YamlParser(self.contents) + elif self.extension == "toml": + return TomlParser(self.contents) + else: + raise ValueError( + f"File path at `{self}` contains an unrecognized file type." + ) + + @property + def contents(self) -> str: + """Retrieve the contents of the file. + + Returns: + The contents of the file at this path. + """ + with open(self, "r") as file: + return file.read() + + def validate(self) -> "ConfigFilePath": + """Validates that the current path exists, is a directory, + and expands any home tidles. + + Raises: + FileNotFoundError: If the path does not exist. + ValueError: If the path leads to a directory. + + Returns: + A ConfigFilePath object with any ~'s expanded. + """ + if len(self.parts) >= 1 and self.parts[0] == "~": + self = ConfigFilePath(self.expanduser()) + + if not self.exists(): + raise FileNotFoundError( + f"The specified config file ({self}) does not exist." + ) + + if self.is_dir(): + raise ValueError(f"The specified config file ({self}) is a directory") + + return self diff --git a/config_file/exceptions.py b/config_file/exceptions.py new file mode 100644 index 0000000..f68b7e8 --- /dev/null +++ b/config_file/exceptions.py @@ -0,0 +1,9 @@ +class ParsingError(ValueError): + """ + Unable to parse the configuration file. + + Since every module that parses the different + files has it's own decode error, this standardizes + it for the package to only raise a ParsingError + if a error occurs in those instances. + """ diff --git a/config_file/ini_parser.py b/config_file/ini_parser.py new file mode 100644 index 0000000..72974f1 --- /dev/null +++ b/config_file/ini_parser.py @@ -0,0 +1,13 @@ +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, + ) diff --git a/config_file/ini_parser_compat.py b/config_file/ini_parser_compat.py new file mode 100644 index 0000000..9dfea2e --- /dev/null +++ b/config_file/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/json_parser.py b/config_file/json_parser.py new file mode 100644 index 0000000..5314cd7 --- /dev/null +++ b/config_file/json_parser.py @@ -0,0 +1,15 @@ +import json + +from .base_parser import BaseParser + + +class JsonParser(BaseParser): + def __init__(self, file_contents: str): + super().__init__( + file_contents=file_contents, + module=json, + decode_error=json.decoder.JSONDecodeError, + ) + + def stringify(self) -> str: + return json.dumps(self.parsed_content, indent=4) diff --git a/config_file/nested_lookup.py b/config_file/nested_lookup.py new file mode 100644 index 0000000..7b13d83 --- /dev/null +++ b/config_file/nested_lookup.py @@ -0,0 +1,402 @@ +import copy +import warnings +from collections import defaultdict + +__all__ = [ + "get_all_keys", + "get_occurrences_and_values", + "get_occurrence_of_value", + "get_occurrence_of_key", + "nested_update", + "nested_delete", + "nested_alter", + "nested_lookup", +] + + +values_list = [] + + +def nested_lookup(key, document, wild=False, with_keys=False): + """Lookup a key in a nested document, return a list of values""" + if with_keys: + d = defaultdict(list) + for k, v in _nested_lookup(key, document, wild=wild, with_keys=with_keys): + d[k].append(v) + return d + return list(_nested_lookup(key, document, wild=wild, with_keys=with_keys)) + + +def _is_case_insensitive_substring(a, b): + """return True if `a` is a case insensitive substring of `b`, else False""" + return str(a).lower() in str(b).lower() + + +def _nested_lookup(key, document, wild=False, with_keys=False): + """Lookup a key in a nested document, yield a value""" + if isinstance(document, list): + for d in document: + for result in _nested_lookup(key, d, wild=wild, with_keys=with_keys): + yield result + + if isinstance(document, dict): + for k, v in document.items(): + if key == k or (wild and _is_case_insensitive_substring(key, k)): + if with_keys: + yield k, v + else: + yield v + if isinstance(v, dict): + for result in _nested_lookup(key, v, wild=wild, with_keys=with_keys): + yield result + elif isinstance(v, list): + for d in v: + for result in _nested_lookup( + key, d, wild=wild, with_keys=with_keys + ): + yield result + + +def get_all_keys(dictionary): + """ + Method to get all keys from a nested dictionary as a List + Args: + dictionary: Nested dictionary + Returns: + List of keys in the dictionary + """ + result_list = [] + + def recrusion(document): + if isinstance(document, list): + for list_items in document: + recrusion(document=list_items) + elif isinstance(document, dict): + for key, value in document.items(): + result_list.append(key) + recrusion(document=value) + return + + recrusion(document=dictionary) + return result_list + + +def get_occurrence_of_key(dictionary, key): + """ + Method to get occurrence of a key in a nested dictionary + + Args: + dictionary: Nested dictionary + key: Key to search for the occurrences + Return: + Number of occurrence (Integer) + """ + return _get_occurrence(dictionary=dictionary, item="key", keyword=key) + + +def get_occurrences_and_values(items, value): + """ + Method to get occurrence of a value in a nested list of dictionary + + Args: + items: list of dictionary: Nested dictionary + value: Value to search for the occurrences + + Return: + Dict where the key is the value arg and his value is a new + dict with occurrences and values + """ + occurrences = {} + occurrence = 0 + value_list = [] + + for item in items: + occurrence_result, values = _get_occurrence_with_values( + dictionary=item, item="value", keyword=value + ) + occurrence = occurrence + occurrence_result + if occurrence_result: + value_list.extend(values) + + occurrences[value] = {"occurrences": occurrence, "values": value_list} + + return occurrences + + +def _get_occurrence_with_values(dictionary, item, keyword): + occurrence = [0] + + result_recursion = _recursion(dictionary, item, keyword, occurrence, True) + + global values_list + values_list = [] + + return occurrence[0], result_recursion + + +def get_occurrence_of_value(dictionary, value): + """ + Method to get occurrence of a value in a nested dictionary + + Args: + dictionary: Nested dictionary + value: Value to search for the occurrences + Return: + Number of occurrence (Integer) + """ + return _get_occurrence(dictionary=dictionary, item="value", keyword=value) + + +def _recursion(dictionary, item, keyword, occurrence, with_values=False): + + global values_list + + if item == "key": + if dictionary.get(keyword) is not None: + occurrence[0] += 1 + elif keyword in list(dictionary.values()): + occurrence[0] += list(dictionary.values()).count(keyword) + if with_values: + values_list.append(dictionary) + for key, value in dictionary.items(): + if isinstance(value, dict): + _recursion(value, item, keyword, occurrence, with_values) + elif isinstance(value, list): + for list_items in value: + if hasattr(list_items, "items"): + _recursion(list_items, item, keyword, occurrence, with_values) + elif list_items == keyword: + occurrence[0] += 1 if item == "value" else 0 + + if values_list: + return values_list + + +def _get_occurrence(dictionary, item, keyword): + """ + Method to get occurrence of a key or value in a nested dictionary + + Args: + dictionary: Nested dictionary + item: Mostly (key or value) + keyword: key word to find occurrence + Return: + Number of occurrence of the given keyword in the dict + """ + occurrence = [0] + _recursion(dictionary, item, keyword, occurrence) + + global values_list + values_list = [] + + return occurrence[0] + + +def nested_delete(document, key, in_place=False): + if not in_place: + document = copy.deepcopy(document) + return _nested_delete(document=document, key=key) + + +def _nested_delete(document, key): + """ + Method to delete a key->value pair from a nested document + Args: + document: Might be List of Dicts (or) Dict of Lists (or) + Dict of List of Dicts etc... + key: Key to delete + Return: + Returns a document that includes everything but the given key + """ + if isinstance(document, list): + for list_items in document: + _nested_delete(document=list_items, key=key) + elif isinstance(document, dict): + if document.get(key): + del document[key] + for dict_key, dict_value in document.items(): + _nested_delete(document=dict_value, key=key) + return document + + +def nested_update(document, key, value, in_place=False, treat_as_element=True): + """ + Method to update a key->value pair in a nested document + + Args: + document: Might be List of Dicts (or) Dict of Lists (or) + Dict of List of Dicts etc... + + key: Key to update the value + + value: Value to set + + in_place (bool): + True: modify the dict in place; + False: create a deep copy of the dict and modify it + Defaults to False + + treat_as_element (bool): + True: if a list is provided as "value", the function trys + to match the list elements to the occurences of the key. + If the key occures more often than the provided list has + elements, the first element gets recycled. + False: the provided list is treated as one scalar value and + will be set as value to every key that matches. + Defaults to True (because of backwards portability of the package). + + Return: + Returns a document that has updated key, value pair. + """ + + # check if a list or scalar value is provided and create a list + # from the scalar value + # check the length of the list and provide it to _nested_update + if not treat_as_element and not isinstance(value, list): + raise Exception("The value must be a list when treat_as_element is False.") + elif treat_as_element: + value = [value] + + if not in_place: + document = copy.deepcopy(document) + return _nested_update(document=document, key=key, value=value) + + +def _nested_update(document, key, value): + """ + Method to update a key->value pair in a nested document. + If the number of passed values is less than the number of key matches + when scanning for updates, use last value as default. + Args: + document: Might be List of Dicts (or) Dict of Lists (or) + Dict of List of Dicts etc... + key (str): Key to update the value + value (list): value(s) which should be used for replacement purpouse + Return: + Returns a document that has updated key, value pair. + """ + if isinstance(document, list): + for list_items in document: + _nested_update(document=list_items, key=key, value=value) + elif isinstance(document, dict): + for dict_key, dict_value in document.items(): + if dict_key == key: + document[key] = value[0] + if len(value) > 1: + value.pop(0) + _nested_update(document=dict_value, key=key, value=value) + return document + + +def nested_alter( + document, + key, + callback_function=None, + function_parameters=None, + conversion_function=None, + wild_alter=False, + in_place=False, +): + """ + Method to alter all values of the occurences of the key "key". + The provided callback_function is used to alter the scalar values + Args: + document: Might be List of Dicts (or) Dict of Lists (or) + Dict of List of Dicts etc... + key: Key to update the value + callback_function :A callback function which alters a scalar value + HINT: You should be aware that not every element might be of + the same type, please check this in your function! + function_parameters (list): + If the callback_function has additional input arguments except + the scalar value, please specify those in this list. + conversion_function: A conversion function like str() which should be + applied to every found value before it is passed to the + "callback_function" + wild_alter: Find matching elements via wild-match by the given keys + and alter those. + HINT: Keep in mind that the wild-match might return unexpected types! + in_place (bool): + True: modify the dict in place; + False: create a deep copy of the dict and modify it + Defaults to False + Return: + Returns a document that has updated key, value pair. + """ + # check if a list or scalar value is provided and create a list from + # the scalar value + # check the length of the list and provide it to _nested_update + if isinstance(key, list): + key_len = len(key) + else: + key = [key] + key_len = len(key) + + if not in_place: + document = copy.deepcopy(document) + return _nested_alter( + document=document, + keys=key, + callback_function=callback_function, + function_parameters=function_parameters, + conversion_function=conversion_function, + wild_alter=wild_alter, + in_place=in_place, + key_len=key_len, + ) + + +def _call_callback( + value_list, callback_function, function_parameters, conversion_function +): + """ + internal helper to call the callback function + """ + return_list = [] + # loop over all values + for value in value_list: + # apply the conversion function + if conversion_function is not None: + value = conversion_function(value) + # if functions arguments are present, expand the list to variables + # via the magic operator * + if function_parameters: + trans_val = callback_function(value, *function_parameters) + else: + trans_val = callback_function(value) + # append the transformed element to the list + return_list.append(trans_val) + return return_list + + +def _nested_alter( + document, + keys, + callback_function, + function_parameters, + conversion_function, + wild_alter, + in_place, + key_len, +): + + # return data if no callback_function is provided + if callback_function is None: + warnings.warn("Please provide a callback_function to nested_alter().") + return document + + # iterate over all given keys in the list + for key in keys: + # try to find the key: + findings = nested_lookup(key, document, with_keys=True, wild=wild_alter) + for k, v in findings.items(): + trans_val = _call_callback( + v, callback_function, function_parameters, conversion_function + ) + # use the transformed value and apply the update to the key + # (dont treat the lists as elements here) + document = nested_update( + document, k, trans_val, in_place=in_place, treat_as_element=False + ) + + return document diff --git a/config_file/parse_value.py b/config_file/parse_value.py new file mode 100644 index 0000000..12032d6 --- /dev/null +++ b/config_file/parse_value.py @@ -0,0 +1,218 @@ +"""This module allows you to parse a string into its native type.""" +import ast +import re +from distutils.util import strtobool +from typing import Any + + +def parse_value(value: Any) -> Any: + """Parse a value into native type. + + parse_value('5') -> 5 (int) + parse_value('-5') -> -5 (int) + + parse_value('5.52') -> 5.52 (float) + parse_value('-53.2') -> -53.2 (float) + + parse_value('blahfafda') -> 'blahfafda' (string) + + parse_value('true') -> True (bool) + parse_value('FALSE') -> False (bool) + + parse_value(" [ [ [ [] ]] ] ") -> [[[[]]]] (list) + + Args: + value: The value to parse. + + Returns: + The parsed value, if it could be parsed + as a dictionary, list, int, float, or bool. + Otherwise, it simply returns the original + passed in value. + """ + if isinstance(value, dict): + parsed = {} + + for item in value: + parsed[item] = parse_value(value[item]) + + return parsed + + if isinstance(value, list): + parsed = [] + + for item in value: + parsed.append(parse_value(item)) + + return parsed + + if can_be_parsed_as_int(value): + return int(value.strip()) if isinstance(value, str) else int(value) + + if can_be_parsed_as_float(value): + return float(value.strip()) if isinstance(value, str) else float(value) + + if can_be_parsed_as_bool(value): + return bool(value) if isinstance(value, bool) else bool(strtobool(value)) + + if can_be_parsed_as_dict(value): + return parse_value(ast.literal_eval(value.strip())) + + if can_be_parsed_as_list(value): + return parse_value(ast.literal_eval(value.strip())) + + return value + + +def can_be_parsed_as_int(value: Any) -> bool: + """Check whether a value can be parsed as a integer. + + can_be_parsed_as_int(5) -> True + can_be_parsed_as_int("5") -> True + can_be_parsed_as_int("-5") -> True + can_be_parsed_as_int(True) -> False + + Args: + value: The value to check if it can be parsed as an integer. + + Returns: + True if we can parse the value as a integer. False otherwise. + """ + if isinstance(value, int) and not isinstance(value, bool): + return True + + if not isinstance(value, str): + return False + + value = value.strip() + + if value.startswith("-"): + return value[1:].isdigit() + + return value.isdigit() + + +def can_be_parsed_as_float(value: Any) -> bool: + """Checks whether a value can be parsed as a float. + + can_be_parsed_as_float(45) -> False + can_be_parsed_as_float(1.1) -> True + can_be_parsed_as_float("1.1") -> True + can_be_parsed_as_float("-1.1") -> True + can_be_parsed_as_float(".1") -> True + + Args: + value: The value to be parsed. + + Returns: + True if the value can be parsed as a float. False otherwise. + """ + FLOAT_REGEX = r"^\d*\.\d+$" + + if isinstance(value, float): + return True + + if not isinstance(value, str): + return False + + value = value.strip() + + if value.startswith("-"): + value = value[1:] + + return bool(re.match(FLOAT_REGEX, value)) + + +def can_be_parsed_as_bool(value: Any) -> bool: + """Checks whether a value can be parsed as a boolean. + + can_be_parsed_as_bool(True) -> True + can_be_parsed_as_bool("true") -> True + can_be_parsed_as_bool("false") -> True + can_be_parsed_as_bool("TRUE") -> True + can_be_parsed_as_bool(0) -> False + + Args: + value: The value to be parsed. + + Returns: + True if the value can be parsed as a boolean. False otherwise. + """ + if isinstance(value, bool): + return True + + if not isinstance(value, str): + return False + + value = value.lower().strip() + + return value == "true" or value == "false" + + +def can_be_parsed_as_dict(value: Any) -> bool: + """Checks whether a value can be parsed as a dictionary. + + can_be_parsed_as_dict({}) -> True + can_be_parsed_as_dict("{}") -> True + can_be_parsed_as_dict(" { } ") -> True + can_be_parsed_as_dict("{ 'hello': 5 }") -> True + can_be_parsed_as_dict([]]) -> False + can_be_parsed_as_dict(" { invalid } ") -> False + + Args: + value: The value to be parsed. + + Returns: + True if the value can be parsed as a dictionary. False otherwise. + """ + if isinstance(value, dict): + return True + + if not isinstance(value, str): + return False + + value = value.strip() + + if value[0] == "{" and value[-1] == "}": + try: + ast.literal_eval(value) + return True + except Exception: + return False + + return False + + +def can_be_parsed_as_list(value: Any) -> bool: + """Checks whether a value can be parsed as a list. + + can_be_parsed_as_list([]) -> True + can_be_parsed_as_list([1, 2]) -> True + can_be_parsed_as_list("[]") -> True + can_be_parsed_as_list(" [] ") -> True + can_be_parsed_as_list("[1, 2]") -> True + can_be_parsed_as_list({}) -> False + can_be_parsed_as_list(" [ invalid ] ") -> False + + Args: + value: The value to be parsed. + + Returns: + True if the value can be parsed as a list. False otherwise. + """ + if isinstance(value, list): + return True + + if not isinstance(value, str): + return False + + value = value.strip() + + if value[0] == "[" and value[-1] == "]": + try: + ast.literal_eval(value) + return True + except Exception: + return False + + return False diff --git a/config_file/parsers/__init__.py b/config_file/parsers/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/config_file/parsers/__init__.py +++ /dev/null diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py deleted file mode 100644 index 42f93b1..0000000 --- a/config_file/parsers/base_parser.py +++ /dev/null @@ -1,79 +0,0 @@ -from abc import ABC, abstractmethod - - -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): - """ - 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, parse_types=False): - """ - 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, value) -> bool: - """Sets the value of a key.""" - raise NotImplementedError - - @abstractmethod - def delete(self, section_key) -> 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): - """ - 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. - """ - raise NotImplementedError - - -class ParsingError(Exception): - """Unable to parse the configuration file""" diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py deleted file mode 100644 index 81f2d1b..0000000 --- a/config_file/parsers/ini_parser.py +++ /dev/null @@ -1,123 +0,0 @@ -import configparser -from io import StringIO - -from config_file.parsers.base_parser import BaseParser, ParsingError -from config_file.parsers.parse_value import parse_value -from config_file.utils import split_on_dot - - -class IniParser(BaseParser): - 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. - - :param section_key: The key to set from the config file. e.g. 'section1.key' - - :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. - - :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. - - :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." - ) - - self.parsed_content.remove_section(section_key) - return True - - 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}." - ) - - self.parsed_content.remove_option(section, key) - return True - - def stringify(self) -> str: - buffer = StringIO() - self.parsed_content.write(buffer) - return buffer.getvalue() - - 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) diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py deleted file mode 100644 index 20eb085..0000000 --- a/config_file/parsers/json_parser.py +++ /dev/null @@ -1,129 +0,0 @@ -import json - -from nested_lookup import ( - get_occurrence_of_key, - nested_delete, - nested_lookup, - nested_update, -) - -from config_file.parsers.base_parser import BaseParser, ParsingError -from config_file.parsers.parse_value import parse_value -from config_file.utils import split_on_dot - - -class JsonParser(BaseParser): - """ - The JsonParser allows use of json files with ConfigFile. The syntax it - supports is a bit more flexible than the IniParser. - - By default, the dot syntax is used. However, if you'd like to get/set/delete all - of a particular key, you can specify so with an optional parameter. - - Example: - - json = { - "key1": { - "key2": "foo", - "key1": "bar" - }, - "foo": "bar" - } - parser = JsonParser(json.dumps(json)) - parser.get('key1') - >>> { "key2": "foo", "key1": "bar" } - parser.get('key1.key2') - >>> "foo" - parser.get('key1', get_all=True) - >>> [{ "key2": "foo", "key1": "bar" }, "bar"] - """ - - def __init__(self, file_contents: str): - """Reads in the file contents into the parser.""" - super().__init__(file_contents) - - def parse(self, file_contents: str): - try: - return json.loads(file_contents) - except json.decoder.JSONDecodeError as error: - raise ParsingError(error) - - def get(self, key: str, parse_types: bool = False, get_all: bool = False): - """ - Retrieve values using a dot syntax. - - :param key: The key to retrieve. e.g. 'foo.bar.boo' - - :param parse_types: Whether you'd like the type parsed into its native one. - - :param get_all: Specify whether you'd like to recursively receive - all values that match the given key. Returned as a list. - e.g. 'foo' would retrieve all values that have the key 'foo', anywhere - in the json. - - :return: the value of the given key - """ - if get_all: - result = nested_lookup(key, self.parsed_content) - elif "." in key: - split_keys = split_on_dot(key) - result = self.parsed_content - - # Used so we can more precisely specify what key is causing the error. - key_for_error = None - try: - for key in split_keys: - key_for_error = key - result = result[key] - except TypeError: - raise ParsingError( - f"Cannot get {key} because {key_for_error} is not subscriptable." - ) - else: - result = self.parsed_content[key] - - return parse_value(result) if parse_types else result - - def set(self, key: str, value, set_all: bool = False): - if set_all: - nested_update(self.parsed_content, key, value, in_place=True) - elif "." in key: - indexes = split_on_dot(key) - indexes_length = len(indexes) - - ref = self.parsed_content - for index, key in enumerate(indexes): - if index == indexes_length - 1: - ref[key] = value - break - - ref = ref[key] - else: - self.parsed_content[key] = value - - return True - - def delete(self, key, delete_all: bool = False): - if delete_all: - nested_delete(self.parsed_content, key, in_place=True) - elif "." in key: - 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] - else: - del self.parsed_content[key] - - return True - - def stringify(self) -> str: - return json.dumps(self.parsed_content) - - def has(self, search_key: str) -> bool: - return get_occurrence_of_key(self.parsed_content, key=search_key) > 0 diff --git a/config_file/parsers/parse_value.py b/config_file/parsers/parse_value.py deleted file mode 100644 index e58781a..0000000 --- a/config_file/parsers/parse_value.py +++ /dev/null @@ -1,87 +0,0 @@ -""" -Parse types allows you to check or parse a string into its native type. - -Example: - parse_value('5') -> 5 (int) - parse_value('-5') -> -5 (int) - - parse_value('5.52') -> 5.52 (float) - parse_value('-53.2') -> -53.2 (float) - - parse_value('blahfafda') -> 'blahfafda' (string) - - parse_value('true') -> True (bool) - parse_value('FALSE') -> False (bool) - - -"5".isdigit() -> True -"-5".isdigit() -> False -can_be_parsed_as_int("-5") -> True -""" -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 = {} - - for item in value: - parsed[item] = parse_value(value[item]) - - return parsed - - if type(value) is list: - parsed = [] - - for item in value: - parsed.append(parse_value(item)) - - return parsed - - return value - - -def can_be_parsed_as_int(value) -> bool: - if type(value) is int: - return True - - if type(value) is not str: - return False - - if value.startswith("-"): - return value[1:].isdigit() - - return value.isdigit() - - -def can_be_parsed_as_float(value) -> bool: - if type(value) is float: - return True - - if type(value) is not str: - return False - - if value.startswith("-"): - return bool(re.match(r"^\d*\.\d+$", value[1:])) - - return bool(re.match(r"^\d*\.\d+$", value)) - - -def can_be_parsed_as_bool(value) -> bool: - if type(value) is bool: - return True - - if type(value) is not str: - return False - - return value.lower() == "true" or value.lower() == "false" diff --git a/config_file/toml_parser.py b/config_file/toml_parser.py new file mode 100644 index 0000000..d3cf226 --- /dev/null +++ b/config_file/toml_parser.py @@ -0,0 +1,18 @@ +from .base_parser import BaseParser + + +class TomlParser(BaseParser): + def __init__(self, file_contents: str): + try: + import tomlkit + from tomlkit.exceptions import ParseError + + super().__init__( + file_contents=file_contents, module=tomlkit, decode_error=ParseError, + ) + except ImportError: + raise ImportError( + "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/utils.py b/config_file/utils.py index 61f0ae0..cbbf7a7 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -1,21 +1,38 @@ -def split_on_dot(line: str, only_last_dot=False): +from pathlib import Path +from typing import List, Union + + +def split_on_dot(line: Union[str, Path], only_last_dot: bool = 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) + if "." not in line: - # TODO: This is used in the parser for section.keys where a more specific - # error message would be nice, but it is only used more generically to, say, - # split a file path. We may just have to insert some try-catches to catch the - # ValueError and insert a more specific error message or create a custom error - # for this method? - raise ValueError( - "section_key must contain the section and key separated by a dot. " - + "e.g. 'section.key'" - ) + raise ValueError(f"The given string does not have a dot to split on: {line}") return line.rsplit(".", 1) if only_last_dot else line.split(".") + + +class Default: + """ + Default is used for the `default` value in ConfigFile's `get`. + + Previously, the default value for `get` was None. However, then + the user cannot have `get` return a default value of None. So this + class is used instead to get around that limitation. + """ + + def __init__(self, value): + self.value = value + + def __repr__(self): + return "Default Value: {} ({})".format(self.value, type(self.value)) diff --git a/config_file/yaml_parser.py b/config_file/yaml_parser.py new file mode 100644 index 0000000..8d88881 --- /dev/null +++ b/config_file/yaml_parser.py @@ -0,0 +1,21 @@ +from .base_parser import BaseParser + + +class YamlParser(BaseParser): + def __init__(self, file_contents: str): + try: + from ruamel.yaml import YAML, YAMLError, round_trip_dump, round_trip_load + + yaml = YAML(typ="safe") + yaml.loads = round_trip_load + yaml.dumps = round_trip_dump + + super().__init__( + file_contents=file_contents, module=yaml, decode_error=YAMLError + ) + except ImportError: + raise ImportError( + "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]`." + ) |