From 94fe938075c4edba742db66280c8e02198d79e1e 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. --- CHANGELOG.md | 195 +++++++++-------- config_file/__init__.py | 4 +- config_file/config_file.py | 18 +- config_file/exceptions.py | 6 + config_file/parsers/base_parser.py | 3 - config_file/parsers/ini_parser.py | 7 +- config_file/parsers/json_parser.py | 21 +- config_file/parsers/toml_parser.py | 0 config_file/parsers/yaml_parser.py | 0 poetry.lock | 425 +++++++++++++++++++------------------ pyproject.toml | 16 +- tests/test_config_file.py | 11 +- tests/test_ini_parser.py | 3 +- tests/test_json_parser.py | 11 +- tox.ini | 2 +- 15 files changed, 381 insertions(+), 341 deletions(-) create mode 100644 config_file/exceptions.py create mode 100644 config_file/parsers/toml_parser.py create mode 100644 config_file/parsers/yaml_parser.py diff --git a/CHANGELOG.md b/CHANGELOG.md index e0a3ce8..e6ac91f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,167 +5,182 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +Added + +- Support for Python 3.5 by removing use of f-strings from codebase. +- A generic `ConfigFileError`. +- More type hints from `typings` have been added to `ConfigFile` for better type information. + +Changed + +- `ParsingError` now inherits from `ConfigFileError` rather than `Exception`. +- Errors have been moved to `exceptions.py`. +- The `get_all`, `set_all`, and `delete_all` optional parameter options in `JsonParser` have + been renamed to just `all`. This shouldn't have any effect since `JsonParser` is not intended + to be used directly, and that option is not in `ConfigFile`. + ## 0.10.0 - 2020-05-10 Changed - - `toml` and `pyyaml` are now optional extra dependencies. This allows you to - not have to install them if you aren't using them. - - - `retrieve_all` in `JsonParser`'s `get` method is now called `get_all` for - consistency. It isn't publicly available yet. The thought is to write a custom - ini parser first that supports multiple of the same keys and subsections. - This way you can have the `get_all`, `set_all`, `delete_all`, etc. methods - for all file type parsers. I would have to weight if that added complexity - of not using the built-in configparser and roll our own for some added - features is worth it or see if there are ways to work around it with - configparser that are ideal. +- `toml` and `pyyaml` are now optional extra dependencies. This allows you to + not have to install them if you aren't using them. -Fixed +- `retrieve_all` in `JsonParser`'s `get` method is now called `get_all` for + consistency. It isn't publicly available yet. The thought is to write a custom + ini parser first that supports multiple of the same keys and subsections. + This way you can have the `get_all`, `set_all`, `delete_all`, etc. methods + for all file type parsers. I would have to weight if that added complexity + of not using the built-in configparser and roll our own for some added + features is worth it or see if there are ways to work around it with + configparser that are ideal. - - You can now specify a default and still coerce your return type. Previously, - if you specified a default, there was no logic in that branch to coerce your - return type as well. +Fixed +- You can now specify a default and still coerce your return type. Previously, + if you specified a default, there was no logic in that branch to coerce your + return type as well. ## 0.9.0 - 2020-02-09 Added - - `default` optional parameter to the `get()` method of `ConfigFile`. - The allows a default value to be fallen back to if the given key is missing. - - - `return_type` optional parameter to `get()` method of `ConfigFile`. This - allows you to coerce the return type to one of your choosing by feeding it - the return value. +- `default` optional parameter to the `get()` method of `ConfigFile`. + The allows a default value to be fallen back to if the given key is missing. + +- `return_type` optional parameter to `get()` method of `ConfigFile`. This + allows you to coerce the return type to one of your choosing by feeding it + the return value. Changed - - Automatic type parsing is now off by default. This is because of the addition of - the `return_type` optional parameter. After using the package more, I think - the explicitness of specifying the type you're after or that you'd like to - automatically parse the type to one of the basic types is more maintainable. - However, I think the option to automatically parse or parse a whole section of - values is still a useful one. - - - The `parse_type` parameter to `ConfigFile`'s `get()` method is now called - `parse_types`. +- Automatic type parsing is now off by default. This is because of the addition of + the `return_type` optional parameter. After using the package more, I think + the explicitness of specifying the type you're after or that you'd like to + automatically parse the type to one of the basic types is more maintainable. + However, I think the option to automatically parse or parse a whole section of + values is still a useful one. + +- The `parse_type` parameter to `ConfigFile`'s `get()` method is now called + `parse_types`. ## 0.8.0 - 2020-02-02 Added - - More type hints to `ConfigFile` and `IniParser`. - - `nested_lookup` dependency to help with modifying deeply nested structures - (JSON + YAML) - - `JsonParser` so you can now specify `.json` files to be parsed. - +- More type hints to `ConfigFile` and `IniParser`. +- `nested_lookup` dependency to help with modifying deeply nested structures + (JSON + YAML) +- `JsonParser` so you can now specify `.json` files to be parsed. + Changed - - The original content of the passed in file is now called `content` - instead of `contents`. This is for consistency since the parsed version is - called `parsed_content`. +- The original content of the passed in file is now called `content` + instead of `contents`. This is for consistency since the parsed version is + called `parsed_content`. ## 0.7.0 - 2020-01-27 Fixed - - Support for using custom parsers with `ConfigFile` with the `parser` optional - argument. This was technically supported before, but it was not tested and found - to not actually use the passed in parser once tested. +- Support for using custom parsers with `ConfigFile` with the `parser` optional + argument. This was technically supported before, but it was not tested and found + to not actually use the passed in parser once tested. Changed - - The `reset()` method on `ConfigFile` is now called `restore_original()`. - The behavior is the same. This was done to better describe what exactly that method - is doing. Since the file is not written back out with every `set()` or `delete()` - and calling `save()` explicitly is required, `reset()` may have been confused with - resetting the changes you've made rather than deleting and restoring the original - configuration file. +- The `reset()` method on `ConfigFile` is now called `restore_original()`. + The behavior is the same. This was done to better describe what exactly that method + is doing. Since the file is not written back out with every `set()` or `delete()` + and calling `save()` explicitly is required, `reset()` may have been confused with + resetting the changes you've made rather than deleting and restoring the original + configuration file. ## 0.6.0 - 2020-01-24 Changed - - Bumped down python version requirement to 3.6 and now test 3.6, 3.7, and 3.8 on CI. +- Bumped down python version requirement to 3.6 and now test 3.6, 3.7, and 3.8 on CI. ## 0.5.0 - 2020-01-19 Added - - Support for retrieving entire sections as a `dict` with `get()`. +- Support for retrieving entire sections as a `dict` with `get()`. ## 0.4.0 - 2020-01-19 Added - - `reset()` and `save()` methods to `ConfigFile`. This allows you to reset your - configuration file to an "original state," given the original config file path. - However, say you have a `config/config.json` file. Then it will automatically try - to look for `config/config.original.json` if no file path is specified. The `save()` - method should be called after your changes to the config file. It will write them - back out. - - - Raise test coverage to 93%. +- `reset()` and `save()` methods to `ConfigFile`. This allows you to reset your + configuration file to an "original state," given the original config file path. + However, say you have a `config/config.json` file. Then it will automatically try + to look for `config/config.original.json` if no file path is specified. The `save()` + method should be called after your changes to the config file. It will write them + back out. + +- Raise test coverage to 93%. ## 0.3.3 - 2020-01-18 Changed - - `_split_on_dot` is no longer in base parser (now in a utils file). Also, the default +- `_split_on_dot` is no longer in base parser (now in a utils file). Also, the default behavior is now to split on every dot and split on only the last dot if specified - + Fixed - - `ConfigFile` was trying to use `_split_on_dot`, but it no longer inherited from base parser. - - - `TypeError` when setting the key/value pairs since configparser requires the option - to be a string. The parser now just converts the value to a string if it is not one - and then adds the key/value pair. It would still be parsed correctly when retrieving - it. +- `ConfigFile` was trying to use `_split_on_dot`, but it no longer inherited from base parser. + +- `TypeError` when setting the key/value pairs since configparser requires the option + to be a string. The parser now just converts the value to a string if it is not one + and then adds the key/value pair. It would still be parsed correctly when retrieving + it. ## 0.3.1 - 2020-01-17 Fixed - - `isinstance` call for `BaseParser` to be correctly used. +- `isinstance` call for `BaseParser` to be correctly used. ## 0.3.0 - 2020-01-17 Added - - Abstract base parser as a contract for concrete file format parser implementations. - - - Exposed `ConfigFile`, `BaseParser`, and `ParsingError` to the Public API. The base - parser is exposed to allow future custom extensions of the config file and what it +- Abstract base parser as a contract for concrete file format parser implementations. + +- Exposed `ConfigFile`, `BaseParser`, and `ParsingError` to the Public API. The base + parser is exposed to allow future custom extensions of the config file and what it can parse by its users. - - - `has_section` and `has_key` is now changed to a single `has` method which determines - whether you're checking a section or key by the presence of a dot. - - - `IniParser` to support the ini format. It uses configparser internally, but it is - only exposed through the `ConfigFile` object. + +- `has_section` and `has_key` is now changed to a single `has` method which determines + whether you're checking a section or key by the presence of a dot. + +- `IniParser` to support the ini format. It uses configparser internally, but it is + only exposed through the `ConfigFile` object. ## 0.2.0 - 2020-01-04 Added - - Parsing of strings to their native values +- Parsing of strings to their native values ## 0.1.0 - 2020-01-04 Added - - Created the config_file package with only the version inside it - - - toml and pyyaml to dependencies - - - Pre-commit pipeline with autoflake, isort, black, and flake8 - - - MIT license - - - Contributing guidelines - - - bump2version for easily upgrading versions of config file - - - pytest to dev dependencies for testing later on +- Created the config_file package with only the version inside it + +- toml and pyyaml to dependencies + +- Pre-commit pipeline with autoflake, isort, black, and flake8 + +- MIT license + +- Contributing guidelines + +- bump2version for easily upgrading versions of config file + +- pytest to dev dependencies for testing later on diff --git a/config_file/__init__.py b/config_file/__init__.py index 3bae28a..f37019a 100644 --- a/config_file/__init__.py +++ b/config_file/__init__.py @@ -1,10 +1,12 @@ from config_file.config_file import ConfigFile -from config_file.parsers.base_parser import BaseParser, ParsingError +from config_file.parsers.base_parser import BaseParser +from config_file.exceptions import ParsingError, ConfigFileError __version__ = "0.10.0" __all__ = [ "ConfigFile", "ParsingError", + "ConfigFileError", "BaseParser", ] diff --git a/config_file/config_file.py b/config_file/config_file.py index 91202da..805fc49 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -1,15 +1,17 @@ import inspect from pathlib import Path, PurePath from shutil import copyfile +from typing import Type, Union -from config_file.parsers.base_parser import BaseParser, ParsingError +from config_file.parsers.base_parser import BaseParser 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.exceptions import ParsingError class ConfigFile: - def __init__(self, file_path, parser: BaseParser = None): + def __init__(self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser] = None) -> None: """ Saves the config file path and expands it if needed, reads in the file contents, and determines what parser should be used for @@ -17,7 +19,6 @@ class ConfigFile: :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. :raises ValueError: If the specified file path does not have an extension that is supported or it is a directory. @@ -27,7 +28,7 @@ class ConfigFile: self.parser = self.__determine_parser(file_path, parser) @staticmethod - def __create_config_path(file_path, original: bool = False) -> str: + def __create_config_path(file_path: Type[Union[str, PurePath]], original: bool = False) -> str: if isinstance(file_path, PurePath): file_path = str(file_path) @@ -40,11 +41,11 @@ class ConfigFile: file_path = ".".join(file_parts) if Path(file_path).is_dir(): - raise ValueError(f"The specified config file ({file_path}) is a directory.") + raise ValueError("The specified config file ({}) is a directory.".format(file_path)) return file_path - def __determine_parser(self, file_path, parser: BaseParser): + def __determine_parser(self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser]) -> BaseParser: if isinstance(file_path, PurePath): file_path = str(file_path) @@ -58,8 +59,7 @@ class ConfigFile: return JsonParser(self.contents) else: raise ValueError( - f"File path contains an unsupported or " - f"unrecognized file type: {file_path}" + "File path contains an unsupported or unrecognized file type: {}".format(file_path) ) def __read_config_file(self): @@ -134,7 +134,7 @@ class ConfigFile: if not Path(original_file_path).exists(): raise OSError( - f"The {original_file_path} file to restore to does not exist." + "The {} file to restore to does not exist.".format(original_file_path) ) Path(self.path).expanduser().unlink() diff --git a/config_file/exceptions.py b/config_file/exceptions.py new file mode 100644 index 0000000..cd7e236 --- /dev/null +++ b/config_file/exceptions.py @@ -0,0 +1,6 @@ +class ConfigFileError(Exception): + """A generic error from the package.""" + +class ParsingError(ConfigFileError): + """Unable to parse the configuration file.""" + \ No newline at end of file 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""" diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 81f2d1b..31b0dcf 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -1,9 +1,10 @@ import configparser from io import StringIO -from config_file.parsers.base_parser import BaseParser, ParsingError +from config_file.parsers.base_parser import BaseParser from config_file.parsers.parse_value import parse_value from config_file.utils import split_on_dot +from config_file.exceptions import ParsingError class IniParser(BaseParser): @@ -95,7 +96,7 @@ class IniParser(BaseParser): 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." + "Cannot delete {} because it is not in the config file.".format(section_key) ) self.parsed_content.remove_section(section_key) @@ -104,7 +105,7 @@ class IniParser(BaseParser): 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}." + "Cannot delete {section}.{key} because {key} is not in {section}.".format(section = section, key = key) ) self.parsed_content.remove_option(section, key) diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py index 20eb085..4d93ca3 100644 --- a/config_file/parsers/json_parser.py +++ b/config_file/parsers/json_parser.py @@ -7,9 +7,10 @@ from nested_lookup import ( nested_update, ) -from config_file.parsers.base_parser import BaseParser, ParsingError +from config_file.parsers.base_parser import BaseParser from config_file.parsers.parse_value import parse_value from config_file.utils import split_on_dot +from config_file.exceptions import ParsingError class JsonParser(BaseParser): @@ -34,7 +35,7 @@ class JsonParser(BaseParser): >>> { "key2": "foo", "key1": "bar" } parser.get('key1.key2') >>> "foo" - parser.get('key1', get_all=True) + parser.get('key1', all=True) >>> [{ "key2": "foo", "key1": "bar" }, "bar"] """ @@ -48,7 +49,7 @@ class JsonParser(BaseParser): except json.decoder.JSONDecodeError as error: raise ParsingError(error) - def get(self, key: str, parse_types: bool = False, get_all: bool = False): + def get(self, key: str, parse_types: bool = False, all: bool = False): """ Retrieve values using a dot syntax. @@ -56,14 +57,14 @@ class JsonParser(BaseParser): :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 + :param 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: + if all: result = nested_lookup(key, self.parsed_content) elif "." in key: split_keys = split_on_dot(key) @@ -77,15 +78,15 @@ class JsonParser(BaseParser): result = result[key] except TypeError: raise ParsingError( - f"Cannot get {key} because {key_for_error} is not subscriptable." + "Cannot get {} because {} is not subscriptable.".format(key, key_for_error) ) 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: + def set(self, key: str, value, all: bool = False): + if all: nested_update(self.parsed_content, key, value, in_place=True) elif "." in key: indexes = split_on_dot(key) @@ -103,8 +104,8 @@ class JsonParser(BaseParser): return True - def delete(self, key, delete_all: bool = False): - if delete_all: + def delete(self, key, all: bool = False): + if all: nested_delete(self.parsed_content, key, in_place=True) elif "." in key: indexes = split_on_dot(key) diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py new file mode 100644 index 0000000..e69de29 diff --git a/config_file/parsers/yaml_parser.py b/config_file/parsers/yaml_parser.py new file mode 100644 index 0000000..e69de29 diff --git a/poetry.lock b/poetry.lock index 9ee97f5..566d146 100644 --- a/poetry.lock +++ b/poetry.lock @@ -9,18 +9,20 @@ version = "0.7.12" [[package]] category = "dev" description = "A small Python module for determining appropriate platform-specific dirs, e.g. a \"user data dir\"." +marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "appdirs" optional = false python-versions = "*" -version = "1.4.3" +version = "1.4.4" [[package]] category = "dev" description = "Utilities for refactoring imports in python-like syntax." +marker = "python_version >= \"3.6.1\" and python_version < \"4.0.0\"" name = "aspy.refactor-imports" optional = false -python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "1.1.0" +python-versions = ">=3.6.1" +version = "2.1.1" [package.dependencies] cached-property = "*" @@ -43,7 +45,7 @@ marker = "sys_platform == \"win32\"" name = "atomicwrites" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "1.3.0" +version = "1.4.0" [[package]] category = "dev" @@ -84,6 +86,7 @@ pytz = ">=2015.7" [[package]] category = "dev" description = "The uncompromising code formatter." +marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "black" optional = false python-versions = ">=3.6" @@ -112,6 +115,7 @@ version = "1.0.0" [[package]] category = "dev" description = "A decorator for caching properties in classes." +marker = "python_version >= \"3.6.1\" and python_version < \"4.0.0\"" name = "cached-property" optional = false python-versions = "*" @@ -123,15 +127,18 @@ description = "Python package for providing Mozilla's CA Bundle." name = "certifi" optional = false python-versions = "*" -version = "2019.11.28" +version = "2020.4.5.2" [[package]] category = "dev" description = "Validate configuration and produce human readable error messages." name = "cfgv" optional = false -python-versions = ">=3.6" -version = "3.0.0" +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" +version = "2.0.1" + +[package.dependencies] +six = "*" [[package]] category = "dev" @@ -144,10 +151,11 @@ version = "3.0.4" [[package]] category = "dev" description = "Composable command line interface toolkit" +marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "click" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" -version = "7.1.1" +version = "7.1.2" [[package]] category = "dev" @@ -164,19 +172,11 @@ description = "Code coverage measurement for Python" name = "coverage" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4" -version = "5.0.4" +version = "5.1" [package.extras] toml = ["toml"] -[[package]] -category = "dev" -description = "Distribution utilities" -name = "distlib" -optional = false -python-versions = "*" -version = "0.3.0" - [[package]] category = "dev" description = "Docutils -- Python Documentation Utilities" @@ -187,33 +187,20 @@ version = "0.16" [[package]] category = "dev" -description = "Discover and load entry points from installed packages." -name = "entrypoints" -optional = false -python-versions = ">=2.7" -version = "0.3" - -[[package]] -category = "dev" -description = "A platform independent file lock." -name = "filelock" -optional = false -python-versions = "*" -version = "3.0.12" - -[[package]] -category = "dev" -description = "the modular source code checker: pep8, pyflakes and co" +description = "the modular source code checker: pep8 pyflakes and co" name = "flake8" optional = false -python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "3.7.9" +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" +version = "3.8.3" [package.dependencies] -entrypoints = ">=0.3.0,<0.4.0" mccabe = ">=0.6.0,<0.7.0" -pycodestyle = ">=2.5.0,<2.6.0" -pyflakes = ">=2.1.0,<2.2.0" +pycodestyle = ">=2.6.0a1,<2.7.0" +pyflakes = ">=2.2.0,<2.3.0" + +[package.dependencies.importlib-metadata] +python = "<3.8" +version = "*" [[package]] category = "dev" @@ -221,7 +208,7 @@ description = "File identification library for Python" name = "identify" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" -version = "1.4.13" +version = "1.4.19" [package.extras] license = ["editdistance"] @@ -249,14 +236,14 @@ marker = "python_version < \"3.8\"" name = "importlib-metadata" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7" -version = "1.6.0" +version = "1.6.1" [package.dependencies] zipp = ">=0.5" [package.extras] docs = ["sphinx", "rst.linker"] -testing = ["packaging", "importlib-resources"] +testing = ["packaging", "pep517", "importlib-resources (>=1.3)"] [[package]] category = "dev" @@ -265,7 +252,7 @@ marker = "python_version < \"3.7\"" name = "importlib-resources" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7" -version = "1.4.0" +version = "2.0.0" [package.dependencies] [package.dependencies.importlib-metadata] @@ -299,7 +286,7 @@ description = "A very fast and expressive template engine." name = "jinja2" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" -version = "2.11.1" +version = "2.11.2" [package.dependencies] MarkupSafe = ">=0.23" @@ -329,7 +316,7 @@ description = "More routines for operating on iterables, beyond itertools" name = "more-itertools" optional = false python-versions = ">=3.5" -version = "8.2.0" +version = "8.3.0" [[package]] category = "main" @@ -348,7 +335,7 @@ description = "Node.js virtual environment builder" name = "nodeenv" optional = false python-versions = "*" -version = "1.3.5" +version = "1.4.0" [[package]] category = "dev" @@ -356,19 +343,32 @@ description = "Core utilities for Python packages" name = "packaging" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "20.3" +version = "20.4" [package.dependencies] pyparsing = ">=2.0.2" six = "*" +[[package]] +category = "dev" +description = "Object-oriented filesystem paths" +marker = "python_version < \"3.6\"" +name = "pathlib2" +optional = false +python-versions = "*" +version = "2.3.5" + +[package.dependencies] +six = "*" + [[package]] category = "dev" description = "Utility library for gitignore style pattern matching of file paths." +marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "pathspec" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" -version = "0.7.0" +version = "0.8.0" [[package]] category = "dev" @@ -426,7 +426,7 @@ description = "Python style guide checker" name = "pycodestyle" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "2.5.0" +version = "2.6.0" [[package]] category = "dev" @@ -434,7 +434,7 @@ description = "passive checker of Python programs" name = "pyflakes" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "2.1.1" +version = "2.2.0" [[package]] category = "dev" @@ -450,7 +450,7 @@ description = "Python parsing module" name = "pyparsing" optional = false python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*" -version = "2.4.6" +version = "2.4.7" [[package]] category = "dev" @@ -458,7 +458,7 @@ description = "pytest: simple powerful testing with Python" name = "pytest" optional = false python-versions = ">=3.5" -version = "5.4.1" +version = "5.4.3" [package.dependencies] atomicwrites = ">=1.0" @@ -474,6 +474,10 @@ wcwidth = "*" python = "<3.8" version = ">=0.12" +[package.dependencies.pathlib2] +python = "<3.6" +version = ">=2.2.0" + [package.extras] checkqa-mypy = ["mypy (v0.761)"] testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "requests", "xmlschema"] @@ -483,15 +487,28 @@ category = "dev" description = "Pytest plugin for measuring coverage." name = "pytest-cov" optional = false -python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "2.8.1" +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +version = "2.9.0" [package.dependencies] coverage = ">=4.4" pytest = ">=3.6" [package.extras] -testing = ["fields", "hunter", "process-tests (2.0.2)", "six", "virtualenv"] +testing = ["fields", "hunter", "process-tests (2.0.2)", "six", "pytest-xdist", "virtualenv"] + +[[package]] +category = "dev" +description = "pytest-sugar is a plugin for pytest that changes the default look and feel of pytest (e.g. progressbar, show tests that fail instantly)." +name = "pytest-sugar" +optional = false +python-versions = "*" +version = "0.9.3" + +[package.dependencies] +packaging = ">=14.1" +pytest = ">=2.9" +termcolor = ">=1.1.0" [[package]] category = "dev" @@ -499,7 +516,7 @@ description = "World timezone definitions, modern and historical" name = "pytz" optional = false python-versions = "*" -version = "2019.3" +version = "2020.1" [[package]] category = "main" @@ -512,10 +529,11 @@ version = "5.3.1" [[package]] category = "dev" description = "Alternative regular expression module, to replace re." +marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "regex" optional = false python-versions = "*" -version = "2020.2.20" +version = "2020.6.8" [[package]] category = "dev" @@ -538,10 +556,11 @@ socks = ["PySocks (>=1.5.6,<1.5.7 || >1.5.7)", "win-inet-pton"] [[package]] category = "dev" description = "Statically populate the `known_third_party` `isort` setting." +marker = "python_version >= \"3.6.1\" and python_version < \"4.0.0\"" name = "seed-isort-config" optional = false -python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" -version = "1.9.4" +python-versions = ">=3.6.1" +version = "2.2.0" [package.dependencies] "aspy.refactor-imports" = "*" @@ -552,7 +571,7 @@ description = "Python 2 and 3 compatibility utilities" name = "six" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*" -version = "1.14.0" +version = "1.15.0" [[package]] category = "dev" @@ -678,6 +697,7 @@ test = ["pytest"] [[package]] category = "dev" description = "tasks runner for python projects" +marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "taskipy" optional = false python-versions = ">=3.6,<4.0" @@ -686,17 +706,26 @@ version = "1.2.1" [package.dependencies] toml = ">=0.10.0,<0.11.0" +[[package]] +category = "dev" +description = "ANSII Color formatting for output in terminal." +name = "termcolor" +optional = false +python-versions = "*" +version = "1.1.0" + [[package]] category = "main" description = "Python Library for Tom's Obvious, Minimal Language" name = "toml" optional = false python-versions = "*" -version = "0.10.0" +version = "0.10.1" [[package]] category = "dev" description = "a fork of Python 2 and 3 ast modules with type comment support" +marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "typed-ast" optional = false python-versions = "*" @@ -708,11 +737,11 @@ description = "HTTP library with thread-safe connection pooling, file post, and name = "urllib3" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4" -version = "1.25.8" +version = "1.25.9" [package.extras] brotli = ["brotlipy (>=0.6.0)"] -secure = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "certifi", "ipaddress"] +secure = ["certifi", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "pyOpenSSL (>=0.14)", "ipaddress"] socks = ["PySocks (>=1.5.6,<1.5.7 || >1.5.7,<2.0)"] [[package]] @@ -721,33 +750,19 @@ description = "Virtual Python Environment builder" name = "virtualenv" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" -version = "20.0.15" - -[package.dependencies] -appdirs = ">=1.4.3,<2" -distlib = ">=0.3.0,<1" -filelock = ">=3.0.0,<4" -six = ">=1.9.0,<2" - -[package.dependencies.importlib-metadata] -python = "<3.8" -version = ">=0.12,<2" - -[package.dependencies.importlib-resources] -python = "<3.7" -version = ">=1.0,<2" +version = "16.7.10" [package.extras] -docs = ["sphinx (>=2.0.0,<3)", "sphinx-argparse (>=0.2.5,<1)", "sphinx-rtd-theme (>=0.4.3,<1)", "towncrier (>=19.9.0rc1)", "proselint (>=0.10.2,<1)"] -testing = ["pytest (>=4.0.0,<6)", "coverage (>=4.5.1,<6)", "pytest-mock (>=2.0.0,<3)", "pytest-env (>=0.6.2,<1)", "pytest-timeout (>=1.3.4,<2)", "packaging (>=20.0)", "xonsh (>=0.9.13,<1)"] +docs = ["sphinx (>=1.8.0,<2)", "towncrier (>=18.5.0)", "sphinx-rtd-theme (>=0.4.2,<1)"] +testing = ["pytest (>=4.0.0,<5)", "coverage (>=4.5.0,<5)", "pytest-timeout (>=1.3.0,<2)", "six (>=1.10.0,<2)", "pytest-xdist", "pytest-localserver", "pypiserver", "mock", "xonsh"] [[package]] category = "dev" -description = "Measures number of Terminal column cells of wide-character codes" +description = "Measures the displayed width of unicode strings in a terminal" name = "wcwidth" optional = false python-versions = "*" -version = "0.1.9" +version = "0.2.4" [[package]] category = "dev" @@ -755,20 +770,20 @@ description = "Backport of pathlib-compatible object wrapper for zip files" marker = "python_version < \"3.8\"" name = "zipp" optional = false -python-versions = ">=3.6" -version = "3.1.0" +python-versions = ">=2.7" +version = "1.2.0" [package.extras] docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"] -testing = ["jaraco.itertools", "func-timeout"] +testing = ["pathlib2", "unittest2", "jaraco.itertools", "func-timeout"] [extras] toml = ["toml"] yaml = ["pyyaml"] [metadata] -content-hash = "08e43891b71c2bd97878564d8084ce7950165cfcccea39d0ad362363de3055c3" -python-versions = "^3.6" +content-hash = "cbcbacf7c64fd8aec4372a6979e3ef11a81c049e7baf24b1b396023209b960cd" +python-versions = "^3.5" [metadata.files] alabaster = [ @@ -776,20 +791,20 @@ alabaster = [ {file = "alabaster-0.7.12.tar.gz", hash = "sha256:a661d72d58e6ea8a57f7a86e37d86716863ee5e92788398526d58b26a4e4dc02"}, ] appdirs = [ - {file = "appdirs-1.4.3-py2.py3-none-any.whl", hash = "sha256:d8b24664561d0d34ddfaec54636d502d7cea6e29c3eaf68f3df6180863e2166e"}, - {file = "appdirs-1.4.3.tar.gz", hash = "sha256:9e5896d1372858f8dd3344faf4e5014d21849c756c8d5701f78f8a103b372d92"}, + {file = "appdirs-1.4.4-py2.py3-none-any.whl", hash = "sha256:a841dacd6b99318a741b166adb07e19ee71a274450e68237b4650ca1055ab128"}, + {file = "appdirs-1.4.4.tar.gz", hash = "sha256:7d5d0167b2b1ba821647616af46a749d1c653740dd0d2415100fe26e27afdf41"}, ] "aspy.refactor-imports" = [ - {file = "aspy.refactor_imports-1.1.0-py2.py3-none-any.whl", hash = "sha256:6d465c18cadad7e5a87810ecf8e516cb6f78e91871f4f55d0f228c6c376bd16a"}, - {file = "aspy.refactor_imports-1.1.0.tar.gz", hash = "sha256:88418d9bd1a0d50b9d3836504469dce6f26ee8b2d78bd58b805d73dd8b381d55"}, + {file = "aspy.refactor_imports-2.1.1-py2.py3-none-any.whl", hash = "sha256:9df76bf19ef81620068b785a386740ab3c8939fcbdcebf20c4a4e0057230d782"}, + {file = "aspy.refactor_imports-2.1.1.tar.gz", hash = "sha256:eec8d1a73bedf64ffb8b589ad919a030c1fb14acf7d1ce0ab192f6eedae895c5"}, ] "aspy.yaml" = [ {file = "aspy.yaml-1.3.0-py2.py3-none-any.whl", hash = "sha256:463372c043f70160a9ec950c3f1e4c3a82db5fca01d334b6bc89c7164d744bdc"}, {file = "aspy.yaml-1.3.0.tar.gz", hash = "sha256:e7c742382eff2caed61f87a39d13f99109088e5e93f04d76eb8d4b28aa143f45"}, ] atomicwrites = [ - {file = "atomicwrites-1.3.0-py2.py3-none-any.whl", hash = "sha256:03472c30eb2c5d1ba9227e4c2ca66ab8287fbfbbda3888aa93dc2e28fc6811b4"}, - {file = "atomicwrites-1.3.0.tar.gz", hash = "sha256:75a9445bac02d8d058d5e1fe689654ba5a6556a1dfd8ce6ec55a0ed79866cfa6"}, + {file = "atomicwrites-1.4.0-py2.py3-none-any.whl", hash = "sha256:6d1784dea7c0c8d4a5172b6c620f40b6e4cbfdf96d783691f2e1302a7b88e197"}, + {file = "atomicwrites-1.4.0.tar.gz", hash = "sha256:ae70396ad1a434f9c7046fd2dd196fc04b12f9e91ffb859164193be8b6168a7a"}, ] attrs = [ {file = "attrs-19.3.0-py2.py3-none-any.whl", hash = "sha256:08a96c641c3a74e44eb59afb61a24f2cb9f4d7188748e76ba4bb5edfa3cb7d1c"}, @@ -815,80 +830,69 @@ cached-property = [ {file = "cached_property-1.5.1-py2.py3-none-any.whl", hash = "sha256:3a026f1a54135677e7da5ce819b0c690f156f37976f3e30c5430740725203d7f"}, ] certifi = [ - {file = "certifi-2019.11.28-py2.py3-none-any.whl", hash = "sha256:017c25db2a153ce562900032d5bc68e9f191e44e9a0f762f373977de9df1fbb3"}, - {file = "certifi-2019.11.28.tar.gz", hash = "sha256:25b64c7da4cd7479594d035c08c2d809eb4aab3a26e5a990ea98cc450c320f1f"}, + {file = "certifi-2020.4.5.2-py2.py3-none-any.whl", hash = "sha256:9cd41137dc19af6a5e03b630eefe7d1f458d964d406342dd3edf625839b944cc"}, + {file = "certifi-2020.4.5.2.tar.gz", hash = "sha256:5ad7e9a056d25ffa5082862e36f119f7f7cec6457fa07ee2f8c339814b80c9b1"}, ] cfgv = [ - {file = "cfgv-3.0.0-py2.py3-none-any.whl", hash = "sha256:f22b426ed59cd2ab2b54ff96608d846c33dfb8766a67f0b4a6ce130ce244414f"}, - {file = "cfgv-3.0.0.tar.gz", hash = "sha256:04b093b14ddf9fd4d17c53ebfd55582d27b76ed30050193c14e560770c5360eb"}, + {file = "cfgv-2.0.1-py2.py3-none-any.whl", hash = "sha256:fbd93c9ab0a523bf7daec408f3be2ed99a980e20b2d19b50fc184ca6b820d289"}, + {file = "cfgv-2.0.1.tar.gz", hash = "sha256:edb387943b665bf9c434f717bf630fa78aecd53d5900d2e05da6ad6048553144"}, ] chardet = [ {file = "chardet-3.0.4-py2.py3-none-any.whl", hash = "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"}, {file = "chardet-3.0.4.tar.gz", hash = "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae"}, ] click = [ - {file = "click-7.1.1-py2.py3-none-any.whl", hash = "sha256:e345d143d80bf5ee7534056164e5e112ea5e22716bbb1ce727941f4c8b471b9a"}, - {file = "click-7.1.1.tar.gz", hash = "sha256:8a18b4ea89d8820c5d0c7da8a64b2c324b4dabb695804dbfea19b9be9d88c0cc"}, + {file = "click-7.1.2-py2.py3-none-any.whl", hash = "sha256:dacca89f4bfadd5de3d7489b7c8a566eee0d3676333fbb50030263894c38c0dc"}, + {file = "click-7.1.2.tar.gz", hash = "sha256:d2b5255c7c6349bc1bd1e59e08cd12acbbd63ce649f2588755783aa94dfb6b1a"}, ] colorama = [ {file = "colorama-0.4.3-py2.py3-none-any.whl", hash = "sha256:7d73d2a99753107a36ac6b455ee49046802e59d9d076ef8e47b61499fa29afff"}, {file = "colorama-0.4.3.tar.gz", hash = "sha256:e96da0d330793e2cb9485e9ddfd918d456036c7149416295932478192f4436a1"}, ] coverage = [ - {file = "coverage-5.0.4-cp27-cp27m-macosx_10_12_x86_64.whl", hash = "sha256:8a620767b8209f3446197c0e29ba895d75a1e272a36af0786ec70fe7834e4307"}, - {file = "coverage-5.0.4-cp27-cp27m-macosx_10_13_intel.whl", hash = "sha256:73aa6e86034dad9f00f4bbf5a666a889d17d79db73bc5af04abd6c20a014d9c8"}, - {file = "coverage-5.0.4-cp27-cp27m-manylinux1_i686.whl", hash = "sha256:408ce64078398b2ee2ec08199ea3fcf382828d2f8a19c5a5ba2946fe5ddc6c31"}, - {file = "coverage-5.0.4-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:cda33311cb9fb9323958a69499a667bd728a39a7aa4718d7622597a44c4f1441"}, - {file = "coverage-5.0.4-cp27-cp27m-win32.whl", hash = "sha256:5f587dfd83cb669933186661a351ad6fc7166273bc3e3a1531ec5c783d997aac"}, - {file = "coverage-5.0.4-cp27-cp27m-win_amd64.whl", hash = "sha256:9fad78c13e71546a76c2f8789623eec8e499f8d2d799f4b4547162ce0a4df435"}, - {file = "coverage-5.0.4-cp27-cp27mu-manylinux1_i686.whl", hash = "sha256:2e08c32cbede4a29e2a701822291ae2bc9b5220a971bba9d1e7615312efd3037"}, - {file = "coverage-5.0.4-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:922fb9ef2c67c3ab20e22948dcfd783397e4c043a5c5fa5ff5e9df5529074b0a"}, - {file = "coverage-5.0.4-cp35-cp35m-macosx_10_12_x86_64.whl", hash = "sha256:c3fc325ce4cbf902d05a80daa47b645d07e796a80682c1c5800d6ac5045193e5"}, - {file = "coverage-5.0.4-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:046a1a742e66d065d16fb564a26c2a15867f17695e7f3d358d7b1ad8a61bca30"}, - {file = "coverage-5.0.4-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:6ad6ca45e9e92c05295f638e78cd42bfaaf8ee07878c9ed73e93190b26c125f7"}, - {file = "coverage-5.0.4-cp35-cp35m-win32.whl", hash = "sha256:eda55e6e9ea258f5e4add23bcf33dc53b2c319e70806e180aecbff8d90ea24de"}, - {file = "coverage-5.0.4-cp35-cp35m-win_amd64.whl", hash = "sha256:4a8a259bf990044351baf69d3b23e575699dd60b18460c71e81dc565f5819ac1"}, - {file = "coverage-5.0.4-cp36-cp36m-macosx_10_13_x86_64.whl", hash = "sha256:f372cdbb240e09ee855735b9d85e7f50730dcfb6296b74b95a3e5dea0615c4c1"}, - {file = "coverage-5.0.4-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:a37c6233b28e5bc340054cf6170e7090a4e85069513320275a4dc929144dccf0"}, - {file = "coverage-5.0.4-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:443be7602c790960b9514567917af538cac7807a7c0c0727c4d2bbd4014920fd"}, - {file = "coverage-5.0.4-cp36-cp36m-win32.whl", hash = "sha256:165a48268bfb5a77e2d9dbb80de7ea917332a79c7adb747bd005b3a07ff8caf0"}, - {file = "coverage-5.0.4-cp36-cp36m-win_amd64.whl", hash = "sha256:0a907199566269e1cfa304325cc3b45c72ae341fbb3253ddde19fa820ded7a8b"}, - {file = "coverage-5.0.4-cp37-cp37m-macosx_10_13_x86_64.whl", hash = "sha256:513e6526e0082c59a984448f4104c9bf346c2da9961779ede1fc458e8e8a1f78"}, - {file = "coverage-5.0.4-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:3844c3dab800ca8536f75ae89f3cf566848a3eb2af4d9f7b1103b4f4f7a5dad6"}, - {file = "coverage-5.0.4-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:641e329e7f2c01531c45c687efcec8aeca2a78a4ff26d49184dce3d53fc35014"}, - {file = "coverage-5.0.4-cp37-cp37m-win32.whl", hash = "sha256:db1d4e38c9b15be1521722e946ee24f6db95b189d1447fa9ff18dd16ba89f732"}, - {file = "coverage-5.0.4-cp37-cp37m-win_amd64.whl", hash = "sha256:62061e87071497951155cbccee487980524d7abea647a1b2a6eb6b9647df9006"}, - {file = "coverage-5.0.4-cp38-cp38-macosx_10_13_x86_64.whl", hash = "sha256:65a7e00c00472cd0f59ae09d2fb8a8aaae7f4a0cf54b2b74f3138d9f9ceb9cb2"}, - {file = "coverage-5.0.4-cp38-cp38-manylinux1_i686.whl", hash = "sha256:1f66cf263ec77af5b8fe14ef14c5e46e2eb4a795ac495ad7c03adc72ae43fafe"}, - {file = "coverage-5.0.4-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:85596aa5d9aac1bf39fe39d9fa1051b0f00823982a1de5766e35d495b4a36ca9"}, - {file = "coverage-5.0.4-cp38-cp38-win32.whl", hash = "sha256:86a0ea78fd851b313b2e712266f663e13b6bc78c2fb260b079e8b67d970474b1"}, - {file = "coverage-5.0.4-cp38-cp38-win_amd64.whl", hash = "sha256:03f630aba2b9b0d69871c2e8d23a69b7fe94a1e2f5f10df5049c0df99db639a0"}, - {file = "coverage-5.0.4-cp39-cp39-win32.whl", hash = "sha256:7c9762f80a25d8d0e4ab3cb1af5d9dffbddb3ee5d21c43e3474c84bf5ff941f7"}, - {file = "coverage-5.0.4-cp39-cp39-win_amd64.whl", hash = "sha256:4482f69e0701139d0f2c44f3c395d1d1d37abd81bfafbf9b6efbe2542679d892"}, - {file = "coverage-5.0.4.tar.gz", hash = "sha256:1b60a95fc995649464e0cd48cecc8288bac5f4198f21d04b8229dc4097d76823"}, -] -distlib = [ - {file = "distlib-0.3.0.zip", hash = "sha256:2e166e231a26b36d6dfe35a48c4464346620f8645ed0ace01ee31822b288de21"}, + {file = "coverage-5.1-cp27-cp27m-macosx_10_12_x86_64.whl", hash = "sha256:0cb4be7e784dcdc050fc58ef05b71aa8e89b7e6636b99967fadbdba694cf2b65"}, + {file = "coverage-5.1-cp27-cp27m-macosx_10_13_intel.whl", hash = "sha256:c317eaf5ff46a34305b202e73404f55f7389ef834b8dbf4da09b9b9b37f76dd2"}, + {file = "coverage-5.1-cp27-cp27m-manylinux1_i686.whl", hash = "sha256:b83835506dfc185a319031cf853fa4bb1b3974b1f913f5bb1a0f3d98bdcded04"}, + {file = "coverage-5.1-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:5f2294dbf7875b991c381e3d5af2bcc3494d836affa52b809c91697449d0eda6"}, + {file = "coverage-5.1-cp27-cp27m-win32.whl", hash = "sha256:de807ae933cfb7f0c7d9d981a053772452217df2bf38e7e6267c9cbf9545a796"}, + {file = "coverage-5.1-cp27-cp27m-win_amd64.whl", hash = "sha256:bf9cb9a9fd8891e7efd2d44deb24b86d647394b9705b744ff6f8261e6f29a730"}, + {file = "coverage-5.1-cp27-cp27mu-manylinux1_i686.whl", hash = "sha256:acf3763ed01af8410fc36afea23707d4ea58ba7e86a8ee915dfb9ceff9ef69d0"}, + {file = "coverage-5.1-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:dec5202bfe6f672d4511086e125db035a52b00f1648d6407cc8e526912c0353a"}, + {file = "coverage-5.1-cp35-cp35m-macosx_10_12_x86_64.whl", hash = "sha256:7a5bdad4edec57b5fb8dae7d3ee58622d626fd3a0be0dfceda162a7035885ecf"}, + {file = "coverage-5.1-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:1601e480b9b99697a570cea7ef749e88123c04b92d84cedaa01e117436b4a0a9"}, + {file = "coverage-5.1-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:dbe8c6ae7534b5b024296464f387d57c13caa942f6d8e6e0346f27e509f0f768"}, + {file = "coverage-5.1-cp35-cp35m-win32.whl", hash = "sha256:a027ef0492ede1e03a8054e3c37b8def89a1e3c471482e9f046906ba4f2aafd2"}, + {file = "coverage-5.1-cp35-cp35m-win_amd64.whl", hash = "sha256:0e61d9803d5851849c24f78227939c701ced6704f337cad0a91e0972c51c1ee7"}, + {file = "coverage-5.1-cp36-cp36m-macosx_10_13_x86_64.whl", hash = "sha256:2d27a3f742c98e5c6b461ee6ef7287400a1956c11421eb574d843d9ec1f772f0"}, + {file = "coverage-5.1-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:66460ab1599d3cf894bb6baee8c684788819b71a5dc1e8fa2ecc152e5d752019"}, + {file = "coverage-5.1-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:5c542d1e62eece33c306d66fe0a5c4f7f7b3c08fecc46ead86d7916684b36d6c"}, + {file = "coverage-5.1-cp36-cp36m-win32.whl", hash = "sha256:2742c7515b9eb368718cd091bad1a1b44135cc72468c731302b3d641895b83d1"}, + {file = "coverage-5.1-cp36-cp36m-win_amd64.whl", hash = "sha256:dead2ddede4c7ba6cb3a721870f5141c97dc7d85a079edb4bd8d88c3ad5b20c7"}, + {file = "coverage-5.1-cp37-cp37m-macosx_10_13_x86_64.whl", hash = "sha256:01333e1bd22c59713ba8a79f088b3955946e293114479bbfc2e37d522be03355"}, + {file = "coverage-5.1-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:e1ea316102ea1e1770724db01998d1603ed921c54a86a2efcb03428d5417e489"}, + {file = "coverage-5.1-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:adeb4c5b608574a3d647011af36f7586811a2c1197c861aedb548dd2453b41cd"}, + {file = "coverage-5.1-cp37-cp37m-win32.whl", hash = "sha256:782caea581a6e9ff75eccda79287daefd1d2631cc09d642b6ee2d6da21fc0a4e"}, + {file = "coverage-5.1-cp37-cp37m-win_amd64.whl", hash = "sha256:00f1d23f4336efc3b311ed0d807feb45098fc86dee1ca13b3d6768cdab187c8a"}, + {file = "coverage-5.1-cp38-cp38-macosx_10_13_x86_64.whl", hash = "sha256:402e1744733df483b93abbf209283898e9f0d67470707e3c7516d84f48524f55"}, + {file = "coverage-5.1-cp38-cp38-manylinux1_i686.whl", hash = "sha256:a3f3654d5734a3ece152636aad89f58afc9213c6520062db3978239db122f03c"}, + {file = "coverage-5.1-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:6402bd2fdedabbdb63a316308142597534ea8e1895f4e7d8bf7476c5e8751fef"}, + {file = "coverage-5.1-cp38-cp38-win32.whl", hash = "sha256:8fa0cbc7ecad630e5b0f4f35b0f6ad419246b02bc750de7ac66db92667996d24"}, + {file = "coverage-5.1-cp38-cp38-win_amd64.whl", hash = "sha256:79a3cfd6346ce6c13145731d39db47b7a7b859c0272f02cdb89a3bdcbae233a0"}, + {file = "coverage-5.1-cp39-cp39-win32.whl", hash = "sha256:a82b92b04a23d3c8a581fc049228bafde988abacba397d57ce95fe95e0338ab4"}, + {file = "coverage-5.1-cp39-cp39-win_amd64.whl", hash = "sha256:bb28a7245de68bf29f6fb199545d072d1036a1917dca17a1e75bbb919e14ee8e"}, + {file = "coverage-5.1.tar.gz", hash = "sha256:f90bfc4ad18450c80b024036eaf91e4a246ae287701aaa88eaebebf150868052"}, ] docutils = [ {file = "docutils-0.16-py2.py3-none-any.whl", hash = "sha256:0c5b78adfbf7762415433f5515cd5c9e762339e23369dbe8000d84a4bf4ab3af"}, {file = "docutils-0.16.tar.gz", hash = "sha256:c2de3a60e9e7d07be26b7f2b00ca0309c207e06c100f9cc2a94931fc75a478fc"}, ] -entrypoints = [ - {file = "entrypoints-0.3-py2.py3-none-any.whl", hash = "sha256:589f874b313739ad35be6e0cd7efde2a4e9b6fea91edcc34e58ecbb8dbe56d19"}, - {file = "entrypoints-0.3.tar.gz", hash = "sha256:c70dd71abe5a8c85e55e12c19bd91ccfeec11a6e99044204511f9ed547d48451"}, -] -filelock = [ - {file = "filelock-3.0.12-py3-none-any.whl", hash = "sha256:929b7d63ec5b7d6b71b0fa5ac14e030b3f70b75747cef1b10da9b879fef15836"}, - {file = "filelock-3.0.12.tar.gz", hash = "sha256:18d82244ee114f543149c66a6e0c14e9c4f8a1044b5cdaadd0f82159d6a6ff59"}, -] flake8 = [ - {file = "flake8-3.7.9-py2.py3-none-any.whl", hash = "sha256:49356e766643ad15072a789a20915d3c91dc89fd313ccd71802303fd67e4deca"}, - {file = "flake8-3.7.9.tar.gz", hash = "sha256:45681a117ecc81e870cbf1262835ae4af5e7a8b08e40b944a8a6e6b895914cfb"}, + {file = "flake8-3.8.3-py2.py3-none-any.whl", hash = "sha256:15e351d19611c887e482fb960eae4d44845013cc142d42896e9862f775d8cf5c"}, + {file = "flake8-3.8.3.tar.gz", hash = "sha256:f04b9fcbac03b0a3e58c0ab3a0ecc462e023a9faf046d57794184028123aa208"}, ] identify = [ - {file = "identify-1.4.13-py2.py3-none-any.whl", hash = "sha256:a7577a1f55cee1d21953a5cf11a3c839ab87f5ef909a4cba6cf52ed72b4c6059"}, - {file = "identify-1.4.13.tar.gz", hash = "sha256:ab246293e6585a1c6361a505b68d5b501a0409310932b7de2c2ead667b564d89"}, + {file = "identify-1.4.19-py2.py3-none-any.whl", hash = "sha256:781fd3401f5d2b17b22a8b18b493a48d5d948e3330634e82742e23f9c20234ef"}, + {file = "identify-1.4.19.tar.gz", hash = "sha256:249ebc7e2066d6393d27c1b1be3b70433f824a120b1d8274d362f1eb419e3b52"}, ] idna = [ {file = "idna-2.9-py2.py3-none-any.whl", hash = "sha256:a068a21ceac8a4d63dbfd964670474107f541babbd2250d61922f029858365fa"}, @@ -899,20 +903,20 @@ imagesize = [ {file = "imagesize-1.2.0.tar.gz", hash = "sha256:b1f6b5a4eab1f73479a50fb79fcf729514a900c341d8503d62a62dbc4127a2b1"}, ] importlib-metadata = [ - {file = "importlib_metadata-1.6.0-py2.py3-none-any.whl", hash = "sha256:2a688cbaa90e0cc587f1df48bdc97a6eadccdcd9c35fb3f976a09e3b5016d90f"}, - {file = "importlib_metadata-1.6.0.tar.gz", hash = "sha256:34513a8a0c4962bc66d35b359558fd8a5e10cd472d37aec5f66858addef32c1e"}, + {file = "importlib_metadata-1.6.1-py2.py3-none-any.whl", hash = "sha256:15ec6c0fd909e893e3a08b3a7c76ecb149122fb14b7efe1199ddd4c7c57ea958"}, + {file = "importlib_metadata-1.6.1.tar.gz", hash = "sha256:0505dd08068cfec00f53a74a0ad927676d7757da81b7436a6eefe4c7cf75c545"}, ] importlib-resources = [ - {file = "importlib_resources-1.4.0-py2.py3-none-any.whl", hash = "sha256:dd98ceeef3f5ad2ef4cc287b8586da4ebad15877f351e9688987ad663a0a29b8"}, - {file = "importlib_resources-1.4.0.tar.gz", hash = "sha256:4019b6a9082d8ada9def02bece4a76b131518866790d58fdda0b5f8c603b36c2"}, + {file = "importlib_resources-2.0.0-py2.py3-none-any.whl", hash = "sha256:a86462cf34a6d391d1d5d598a5e2f5aac9fc00b265d40542e1196328f015d1f6"}, + {file = "importlib_resources-2.0.0.tar.gz", hash = "sha256:7f6aae2ed252ba10f5d1af5676b0e35f3b3eb7d3cc103b8365cc92aec0c79258"}, ] isort = [ {file = "isort-4.3.21-py2.py3-none-any.whl", hash = "sha256:6e811fcb295968434526407adb8796944f1988c5b65e8139058f2014cbe100fd"}, {file = "isort-4.3.21.tar.gz", hash = "sha256:54da7e92468955c4fceacd0c86bd0ec997b0e1ee80d97f67c35a78b719dccab1"}, ] jinja2 = [ - {file = "Jinja2-2.11.1-py2.py3-none-any.whl", hash = "sha256:b0eaf100007721b5c16c1fc1eecb87409464edc10469ddc9a22a27a99123be49"}, - {file = "Jinja2-2.11.1.tar.gz", hash = "sha256:93187ffbc7808079673ef52771baa950426fd664d3aad1d0fa3e95644360e250"}, + {file = "Jinja2-2.11.2-py2.py3-none-any.whl", hash = "sha256:f0a4641d3cf955324a89c04f3d94663aa4d638abe8f733ecd3582848e1c37035"}, + {file = "Jinja2-2.11.2.tar.gz", hash = "sha256:89aab215427ef59c34ad58735269eb58b1a5808103067f7bb9d5836c651b3bb0"}, ] markupsafe = [ {file = "MarkupSafe-1.1.1-cp27-cp27m-macosx_10_6_intel.whl", hash = "sha256:09027a7803a62ca78792ad89403b1b7a73a01c8cb65909cd876f7fcebd79b161"}, @@ -954,22 +958,26 @@ mccabe = [ {file = "mccabe-0.6.1.tar.gz", hash = "sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f"}, ] more-itertools = [ - {file = "more-itertools-8.2.0.tar.gz", hash = "sha256:b1ddb932186d8a6ac451e1d95844b382f55e12686d51ca0c68b6f61f2ab7a507"}, - {file = "more_itertools-8.2.0-py3-none-any.whl", hash = "sha256:5dd8bcf33e5f9513ffa06d5ad33d78f31e1931ac9a18f33d37e77a180d393a7c"}, + {file = "more-itertools-8.3.0.tar.gz", hash = "sha256:558bb897a2232f5e4f8e2399089e35aecb746e1f9191b6584a151647e89267be"}, + {file = "more_itertools-8.3.0-py3-none-any.whl", hash = "sha256:7818f596b1e87be009031c7653d01acc46ed422e6656b394b0f765ce66ed4982"}, ] nested-lookup = [ {file = "nested-lookup-0.2.21.tar.gz", hash = "sha256:23789e328bd1d0b3f9db93cf51b7103a978dd0d8a834770d2c19b365e934ab96"}, ] nodeenv = [ - {file = "nodeenv-1.3.5-py2.py3-none-any.whl", hash = "sha256:5b2438f2e42af54ca968dd1b374d14a1194848955187b0e5e4be1f73813a5212"}, + {file = "nodeenv-1.4.0-py2.py3-none-any.whl", hash = "sha256:4b0b77afa3ba9b54f4b6396e60b0c83f59eaeb2d63dc3cc7a70f7f4af96c82bc"}, ] packaging = [ - {file = "packaging-20.3-py2.py3-none-any.whl", hash = "sha256:82f77b9bee21c1bafbf35a84905d604d5d1223801d639cf3ed140bd651c08752"}, - {file = "packaging-20.3.tar.gz", hash = "sha256:3c292b474fda1671ec57d46d739d072bfd495a4f51ad01a055121d81e952b7a3"}, + {file = "packaging-20.4-py2.py3-none-any.whl", hash = "sha256:998416ba6962ae7fbd6596850b80e17859a5753ba17c32284f67bfff33784181"}, + {file = "packaging-20.4.tar.gz", hash = "sha256:4357f74f47b9c12db93624a82154e9b120fa8293699949152b22065d556079f8"}, +] +pathlib2 = [ + {file = "pathlib2-2.3.5-py2.py3-none-any.whl", hash = "sha256:0ec8205a157c80d7acc301c0b18fbd5d44fe655968f5d947b6ecef5290fc35db"}, + {file = "pathlib2-2.3.5.tar.gz", hash = "sha256:6cd9a47b597b37cc57de1c05e56fb1a1c9cc9fab04fe78c29acd090418529868"}, ] pathspec = [ - {file = "pathspec-0.7.0-py2.py3-none-any.whl", hash = "sha256:163b0632d4e31cef212976cf57b43d9fd6b0bac6e67c26015d611a647d5e7424"}, - {file = "pathspec-0.7.0.tar.gz", hash = "sha256:562aa70af2e0d434367d9790ad37aed893de47f1693e4201fd1d3dca15d19b96"}, + {file = "pathspec-0.8.0-py2.py3-none-any.whl", hash = "sha256:7d91249d21749788d07a2d0f94147accd8f845507400749ea19c1ec9054a12b0"}, + {file = "pathspec-0.8.0.tar.gz", hash = "sha256:da45173eb3a6f2a5a487efba21f050af2b41948be6ab52b6a1e3ff22bb8b7061"}, ] pluggy = [ {file = "pluggy-0.13.1-py2.py3-none-any.whl", hash = "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"}, @@ -984,32 +992,35 @@ py = [ {file = "py-1.8.1.tar.gz", hash = "sha256:5e27081401262157467ad6e7f851b7aa402c5852dbcb3dae06768434de5752aa"}, ] pycodestyle = [ - {file = "pycodestyle-2.5.0-py2.py3-none-any.whl", hash = "sha256:95a2219d12372f05704562a14ec30bc76b05a5b297b21a5dfe3f6fac3491ae56"}, - {file = "pycodestyle-2.5.0.tar.gz", hash = "sha256:e40a936c9a450ad81df37f549d676d127b1b66000a6c500caa2b085bc0ca976c"}, + {file = "pycodestyle-2.6.0-py2.py3-none-any.whl", hash = "sha256:2295e7b2f6b5bd100585ebcb1f616591b652db8a741695b3d8f5d28bdc934367"}, + {file = "pycodestyle-2.6.0.tar.gz", hash = "sha256:c58a7d2815e0e8d7972bf1803331fb0152f867bd89adf8a01dfd55085434192e"}, ] pyflakes = [ - {file = "pyflakes-2.1.1-py2.py3-none-any.whl", hash = "sha256:17dbeb2e3f4d772725c777fabc446d5634d1038f234e77343108ce445ea69ce0"}, - {file = "pyflakes-2.1.1.tar.gz", hash = "sha256:d976835886f8c5b31d47970ed689944a0262b5f3afa00a5a7b4dc81e5449f8a2"}, + {file = "pyflakes-2.2.0-py2.py3-none-any.whl", hash = "sha256:0d94e0e05a19e57a99444b6ddcf9a6eb2e5c68d3ca1e98e90707af8152c90a92"}, + {file = "pyflakes-2.2.0.tar.gz", hash = "sha256:35b2d75ee967ea93b55750aa9edbbf72813e06a66ba54438df2cfac9e3c27fc8"}, ] pygments = [ {file = "Pygments-2.6.1-py3-none-any.whl", hash = "sha256:ff7a40b4860b727ab48fad6360eb351cc1b33cbf9b15a0f689ca5353e9463324"}, {file = "Pygments-2.6.1.tar.gz", hash = "sha256:647344a061c249a3b74e230c739f434d7ea4d8b1d5f3721bc0f3558049b38f44"}, ] pyparsing = [ - {file = "pyparsing-2.4.6-py2.py3-none-any.whl", hash = "sha256:c342dccb5250c08d45fd6f8b4a559613ca603b57498511740e65cd11a2e7dcec"}, - {file = "pyparsing-2.4.6.tar.gz", hash = "sha256:4c830582a84fb022400b85429791bc551f1f4871c33f23e44f353119e92f969f"}, + {file = "pyparsing-2.4.7-py2.py3-none-any.whl", hash = "sha256:ef9d7589ef3c200abe66653d3f1ab1033c3c419ae9b9bdb1240a85b024efc88b"}, + {file = "pyparsing-2.4.7.tar.gz", hash = "sha256:c203ec8783bf771a155b207279b9bccb8dea02d8f0c9e5f8ead507bc3246ecc1"}, ] pytest = [ - {file = "pytest-5.4.1-py3-none-any.whl", hash = "sha256:0e5b30f5cb04e887b91b1ee519fa3d89049595f428c1db76e73bd7f17b09b172"}, - {file = "pytest-5.4.1.tar.gz", hash = "sha256:84dde37075b8805f3d1f392cc47e38a0e59518fb46a431cfdaf7cf1ce805f970"}, + {file = "pytest-5.4.3-py3-none-any.whl", hash = "sha256:5c0db86b698e8f170ba4582a492248919255fcd4c79b1ee64ace34301fb589a1"}, + {file = "pytest-5.4.3.tar.gz", hash = "sha256:7979331bfcba207414f5e1263b5a0f8f521d0f457318836a7355531ed1a4c7d8"}, ] pytest-cov = [ - {file = "pytest-cov-2.8.1.tar.gz", hash = "sha256:cc6742d8bac45070217169f5f72ceee1e0e55b0221f54bcf24845972d3a47f2b"}, - {file = "pytest_cov-2.8.1-py2.py3-none-any.whl", hash = "sha256:cdbdef4f870408ebdbfeb44e63e07eb18bb4619fae852f6e760645fa36172626"}, + {file = "pytest-cov-2.9.0.tar.gz", hash = "sha256:b6a814b8ed6247bd81ff47f038511b57fe1ce7f4cc25b9106f1a4b106f1d9322"}, + {file = "pytest_cov-2.9.0-py2.py3-none-any.whl", hash = "sha256:c87dfd8465d865655a8213859f1b4749b43448b5fae465cb981e16d52a811424"}, +] +pytest-sugar = [ + {file = "pytest-sugar-0.9.3.tar.gz", hash = "sha256:1630b5b7ea3624919b73fde37cffb87965c5087a4afab8a43074ff44e0d810c4"}, ] pytz = [ - {file = "pytz-2019.3-py2.py3-none-any.whl", hash = "sha256:1c557d7d0e871de1f5ccd5833f60fb2550652da6be2693c1e02300743d21500d"}, - {file = "pytz-2019.3.tar.gz", hash = "sha256:b02c06db6cf09c12dd25137e563b31700d3b80fcc4ad23abb7a315f2789819be"}, + {file = "pytz-2020.1-py2.py3-none-any.whl", hash = "sha256:a494d53b6d39c3c6e44c3bec237336e14305e4f29bbf800b599253057fbb79ed"}, + {file = "pytz-2020.1.tar.gz", hash = "sha256:c35965d010ce31b23eeb663ed3cc8c906275d6be1a34393a1d73a41febf4a048"}, ] pyyaml = [ {file = "PyYAML-5.3.1-cp27-cp27m-win32.whl", hash = "sha256:74809a57b329d6cc0fdccee6318f44b9b8649961fa73144a98735b0aaf029f1f"}, @@ -1025,39 +1036,39 @@ pyyaml = [ {file = "PyYAML-5.3.1.tar.gz", hash = "sha256:b8eac752c5e14d3eca0e6dd9199cd627518cb5ec06add0de9d32baeee6fe645d"}, ] regex = [ - {file = "regex-2020.2.20-cp27-cp27m-win32.whl", hash = "sha256:99272d6b6a68c7ae4391908fc15f6b8c9a6c345a46b632d7fdb7ef6c883a2bbb"}, - {file = "regex-2020.2.20-cp27-cp27m-win_amd64.whl", hash = "sha256:974535648f31c2b712a6b2595969f8ab370834080e00ab24e5dbb9d19b8bfb74"}, - {file = "regex-2020.2.20-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:5de40649d4f88a15c9489ed37f88f053c15400257eeb18425ac7ed0a4e119400"}, - {file = "regex-2020.2.20-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:82469a0c1330a4beb3d42568f82dffa32226ced006e0b063719468dcd40ffdf0"}, - {file = "regex-2020.2.20-cp36-cp36m-manylinux2010_i686.whl", hash = "sha256:d58a4fa7910102500722defbde6e2816b0372a4fcc85c7e239323767c74f5cbc"}, - {file = "regex-2020.2.20-cp36-cp36m-manylinux2010_x86_64.whl", hash = "sha256:f1ac2dc65105a53c1c2d72b1d3e98c2464a133b4067a51a3d2477b28449709a0"}, - {file = "regex-2020.2.20-cp36-cp36m-win32.whl", hash = "sha256:8c2b7fa4d72781577ac45ab658da44c7518e6d96e2a50d04ecb0fd8f28b21d69"}, - {file = "regex-2020.2.20-cp36-cp36m-win_amd64.whl", hash = "sha256:269f0c5ff23639316b29f31df199f401e4cb87529eafff0c76828071635d417b"}, - {file = "regex-2020.2.20-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:bed7986547ce54d230fd8721aba6fd19459cdc6d315497b98686d0416efaff4e"}, - {file = "regex-2020.2.20-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:046e83a8b160aff37e7034139a336b660b01dbfe58706f9d73f5cdc6b3460242"}, - {file = "regex-2020.2.20-cp37-cp37m-manylinux2010_i686.whl", hash = "sha256:b33ebcd0222c1d77e61dbcd04a9fd139359bded86803063d3d2d197b796c63ce"}, - {file = "regex-2020.2.20-cp37-cp37m-manylinux2010_x86_64.whl", hash = "sha256:bba52d72e16a554d1894a0cc74041da50eea99a8483e591a9edf1025a66843ab"}, - {file = "regex-2020.2.20-cp37-cp37m-win32.whl", hash = "sha256:01b2d70cbaed11f72e57c1cfbaca71b02e3b98f739ce33f5f26f71859ad90431"}, - {file = "regex-2020.2.20-cp37-cp37m-win_amd64.whl", hash = "sha256:113309e819634f499d0006f6200700c8209a2a8bf6bd1bdc863a4d9d6776a5d1"}, - {file = "regex-2020.2.20-cp38-cp38-manylinux1_i686.whl", hash = "sha256:25f4ce26b68425b80a233ce7b6218743c71cf7297dbe02feab1d711a2bf90045"}, - {file = "regex-2020.2.20-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:9b64a4cc825ec4df262050c17e18f60252cdd94742b4ba1286bcfe481f1c0f26"}, - {file = "regex-2020.2.20-cp38-cp38-manylinux2010_i686.whl", hash = "sha256:9ff16d994309b26a1cdf666a6309c1ef51ad4f72f99d3392bcd7b7139577a1f2"}, - {file = "regex-2020.2.20-cp38-cp38-manylinux2010_x86_64.whl", hash = "sha256:c7f58a0e0e13fb44623b65b01052dae8e820ed9b8b654bb6296bc9c41f571b70"}, - {file = "regex-2020.2.20-cp38-cp38-win32.whl", hash = "sha256:200539b5124bc4721247a823a47d116a7a23e62cc6695744e3eb5454a8888e6d"}, - {file = "regex-2020.2.20-cp38-cp38-win_amd64.whl", hash = "sha256:7f78f963e62a61e294adb6ff5db901b629ef78cb2a1cfce3cf4eeba80c1c67aa"}, - {file = "regex-2020.2.20.tar.gz", hash = "sha256:9e9624440d754733eddbcd4614378c18713d2d9d0dc647cf9c72f64e39671be5"}, + {file = "regex-2020.6.8-cp27-cp27m-win32.whl", hash = "sha256:fbff901c54c22425a5b809b914a3bfaf4b9570eee0e5ce8186ac71eb2025191c"}, + {file = "regex-2020.6.8-cp27-cp27m-win_amd64.whl", hash = "sha256:112e34adf95e45158c597feea65d06a8124898bdeac975c9087fe71b572bd938"}, + {file = "regex-2020.6.8-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:92d8a043a4241a710c1cf7593f5577fbb832cf6c3a00ff3fc1ff2052aff5dd89"}, + {file = "regex-2020.6.8-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:bae83f2a56ab30d5353b47f9b2a33e4aac4de9401fb582b55c42b132a8ac3868"}, + {file = "regex-2020.6.8-cp36-cp36m-manylinux2010_i686.whl", hash = "sha256:b2ba0f78b3ef375114856cbdaa30559914d081c416b431f2437f83ce4f8b7f2f"}, + {file = "regex-2020.6.8-cp36-cp36m-manylinux2010_x86_64.whl", hash = "sha256:95fa7726d073c87141f7bbfb04c284901f8328e2d430eeb71b8ffdd5742a5ded"}, + {file = "regex-2020.6.8-cp36-cp36m-win32.whl", hash = "sha256:e3cdc9423808f7e1bb9c2e0bdb1c9dc37b0607b30d646ff6faf0d4e41ee8fee3"}, + {file = "regex-2020.6.8-cp36-cp36m-win_amd64.whl", hash = "sha256:c78e66a922de1c95a208e4ec02e2e5cf0bb83a36ceececc10a72841e53fbf2bd"}, + {file = "regex-2020.6.8-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:08997a37b221a3e27d68ffb601e45abfb0093d39ee770e4257bd2f5115e8cb0a"}, + {file = "regex-2020.6.8-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:2f6f211633ee8d3f7706953e9d3edc7ce63a1d6aad0be5dcee1ece127eea13ae"}, + {file = "regex-2020.6.8-cp37-cp37m-manylinux2010_i686.whl", hash = "sha256:55b4c25cbb3b29f8d5e63aeed27b49fa0f8476b0d4e1b3171d85db891938cc3a"}, + {file = "regex-2020.6.8-cp37-cp37m-manylinux2010_x86_64.whl", hash = "sha256:89cda1a5d3e33ec9e231ece7307afc101b5217523d55ef4dc7fb2abd6de71ba3"}, + {file = "regex-2020.6.8-cp37-cp37m-win32.whl", hash = "sha256:690f858d9a94d903cf5cada62ce069b5d93b313d7d05456dbcd99420856562d9"}, + {file = "regex-2020.6.8-cp37-cp37m-win_amd64.whl", hash = "sha256:1700419d8a18c26ff396b3b06ace315b5f2a6e780dad387e4c48717a12a22c29"}, + {file = "regex-2020.6.8-cp38-cp38-manylinux1_i686.whl", hash = "sha256:654cb773b2792e50151f0e22be0f2b6e1c3a04c5328ff1d9d59c0398d37ef610"}, + {file = "regex-2020.6.8-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:52e1b4bef02f4040b2fd547357a170fc1146e60ab310cdbdd098db86e929b387"}, + {file = "regex-2020.6.8-cp38-cp38-manylinux2010_i686.whl", hash = "sha256:cf59bbf282b627130f5ba68b7fa3abdb96372b24b66bdf72a4920e8153fc7910"}, + {file = "regex-2020.6.8-cp38-cp38-manylinux2010_x86_64.whl", hash = "sha256:5aaa5928b039ae440d775acea11d01e42ff26e1561c0ffcd3d805750973c6baf"}, + {file = "regex-2020.6.8-cp38-cp38-win32.whl", hash = "sha256:97712e0d0af05febd8ab63d2ef0ab2d0cd9deddf4476f7aa153f76feef4b2754"}, + {file = "regex-2020.6.8-cp38-cp38-win_amd64.whl", hash = "sha256:6ad8663c17db4c5ef438141f99e291c4d4edfeaacc0ce28b5bba2b0bf273d9b5"}, + {file = "regex-2020.6.8.tar.gz", hash = "sha256:e9b64e609d37438f7d6e68c2546d2cb8062f3adb27e6336bc129b51be20773ac"}, ] requests = [ {file = "requests-2.23.0-py2.py3-none-any.whl", hash = "sha256:43999036bfa82904b6af1d99e4882b560e5e2c68e5c4b0aa03b655f3d7d73fee"}, {file = "requests-2.23.0.tar.gz", hash = "sha256:b3f43d496c6daba4493e7c431722aeb7dbc6288f52a6e04e7b6023b0247817e6"}, ] seed-isort-config = [ - {file = "seed_isort_config-1.9.4-py2.py3-none-any.whl", hash = "sha256:81f7adf8dd0bc24e03d0f5eef87561e170757fc7a36050c01e4c29d150757783"}, - {file = "seed_isort_config-1.9.4.tar.gz", hash = "sha256:cecccb911b8d01ecdca16e17d083bf4cd78e928db4d7906125b0373b81f1c7ec"}, + {file = "seed_isort_config-2.2.0-py2.py3-none-any.whl", hash = "sha256:8601fb715a5a4aac39256bbf73c2da6a81f964da9c9d9897ab9074db3663526f"}, + {file = "seed_isort_config-2.2.0.tar.gz", hash = "sha256:be4cfef8f9a3fe8ea1817069c6b624538ac0b429636ec746edeb27e98ed628c8"}, ] six = [ - {file = "six-1.14.0-py2.py3-none-any.whl", hash = "sha256:8f3cd2e254d8f793e7f3d6d9df77b92252b52637291d0f0da013c76ea2724b6c"}, - {file = "six-1.14.0.tar.gz", hash = "sha256:236bdbdce46e6e6a3d61a337c0f8b763ca1e8717c03b369e87a7ec7ce1319c0a"}, + {file = "six-1.15.0-py2.py3-none-any.whl", hash = "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced"}, + {file = "six-1.15.0.tar.gz", hash = "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259"}, ] snowballstemmer = [ {file = "snowballstemmer-2.0.0-py2.py3-none-any.whl", hash = "sha256:209f257d7533fdb3cb73bdbd24f436239ca3b2fa67d56f6ff88e86be08cc5ef0"}, @@ -1099,10 +1110,12 @@ taskipy = [ {file = "taskipy-1.2.1-py3-none-any.whl", hash = "sha256:99bdaf5b19791c2345806847147e0fc2d28e1ac9446058def5a8b6b3fc9f23e2"}, {file = "taskipy-1.2.1.tar.gz", hash = "sha256:5eb2c3b1606c896c7fa799848e71e8883b880759224958d07ba760e5db263175"}, ] +termcolor = [ + {file = "termcolor-1.1.0.tar.gz", hash = "sha256:1d6d69ce66211143803fbc56652b41d73b4a400a2891d7bf7a1cdf4c02de613b"}, +] toml = [ - {file = "toml-0.10.0-py2.7.egg", hash = "sha256:f1db651f9657708513243e61e6cc67d101a39bad662eaa9b5546f789338e07a3"}, - {file = "toml-0.10.0-py2.py3-none-any.whl", hash = "sha256:235682dd292d5899d361a811df37e04a8828a5b1da3115886b73cf81ebc9100e"}, - {file = "toml-0.10.0.tar.gz", hash = "sha256:229f81c57791a41d65e399fc06bf0848bab550a9dfd5ed66df18ce5f05e73d5c"}, + {file = "toml-0.10.1-py2.py3-none-any.whl", hash = "sha256:bda89d5935c2eac546d648028b9901107a595863cb36bae0c73ac804a9b4ce88"}, + {file = "toml-0.10.1.tar.gz", hash = "sha256:926b612be1e5ce0634a2ca03470f95169cf16f939018233a670519cb4ac58b0f"}, ] typed-ast = [ {file = "typed_ast-1.4.1-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:73d785a950fc82dd2a25897d525d003f6378d1cb23ab305578394694202a58c3"}, @@ -1128,18 +1141,18 @@ typed-ast = [ {file = "typed_ast-1.4.1.tar.gz", hash = "sha256:8c8aaad94455178e3187ab22c8b01a3837f8ee50e09cf31f1ba129eb293ec30b"}, ] urllib3 = [ - {file = "urllib3-1.25.8-py2.py3-none-any.whl", hash = "sha256:2f3db8b19923a873b3e5256dc9c2dedfa883e33d87c690d9c7913e1f40673cdc"}, - {file = "urllib3-1.25.8.tar.gz", hash = "sha256:87716c2d2a7121198ebcb7ce7cccf6ce5e9ba539041cfbaeecfb641dc0bf6acc"}, + {file = "urllib3-1.25.9-py2.py3-none-any.whl", hash = "sha256:88206b0eb87e6d677d424843ac5209e3fb9d0190d0ee169599165ec25e9d9115"}, + {file = "urllib3-1.25.9.tar.gz", hash = "sha256:3018294ebefce6572a474f0604c2021e33b3fd8006ecd11d62107a5d2a963527"}, ] virtualenv = [ - {file = "virtualenv-20.0.15-py2.py3-none-any.whl", hash = "sha256:4e399f48c6b71228bf79f5febd27e3bbb753d9d5905776a86667bc61ab628a25"}, - {file = "virtualenv-20.0.15.tar.gz", hash = "sha256:9e81279f4a9d16d1c0654a127c2c86e5bca2073585341691882c1e66e31ef8a5"}, + {file = "virtualenv-16.7.10-py2.py3-none-any.whl", hash = "sha256:105893c8dc66b7817691c7371439ec18e3b6c5e323a304b5ed96cdd2e75cc1ec"}, + {file = "virtualenv-16.7.10.tar.gz", hash = "sha256:e88fdcb08b0ecb11da97868f463dd06275923f50d87f4b9c8b2fc0994eec40f4"}, ] wcwidth = [ - {file = "wcwidth-0.1.9-py2.py3-none-any.whl", hash = "sha256:cafe2186b3c009a04067022ce1dcd79cb38d8d65ee4f4791b8888d6599d1bbe1"}, - {file = "wcwidth-0.1.9.tar.gz", hash = "sha256:ee73862862a156bf77ff92b09034fc4825dd3af9cf81bc5b360668d425f3c5f1"}, + {file = "wcwidth-0.2.4-py2.py3-none-any.whl", hash = "sha256:79375666b9954d4a1a10739315816324c3e73110af9d0e102d906fdb0aec009f"}, + {file = "wcwidth-0.2.4.tar.gz", hash = "sha256:8c6b5b6ee1360b842645f336d9e5d68c55817c26d3050f46b235ef2bc650e48f"}, ] zipp = [ - {file = "zipp-3.1.0-py3-none-any.whl", hash = "sha256:aa36550ff0c0b7ef7fa639055d797116ee891440eac1a56f378e2d3179e0320b"}, - {file = "zipp-3.1.0.tar.gz", hash = "sha256:c599e4d75c98f6798c509911d08a22e6c021d074469042177c8c86fb92eefd96"}, + {file = "zipp-1.2.0-py2.py3-none-any.whl", hash = "sha256:e0d9e63797e483a30d27e09fffd308c59a700d365ec34e93cc100844168bf921"}, + {file = "zipp-1.2.0.tar.gz", hash = "sha256:c70410551488251b0fee67b460fb9a536af8d6f9f008ad10ac51f615b6a521b1"}, ] diff --git a/pyproject.toml b/pyproject.toml index 9cf335a..97c98c7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,10 @@ classifiers = [ "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", + "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", "Topic :: Software Development :: Libraries", "Topic :: Software Development :: Libraries :: Python Modules", ] @@ -20,8 +23,8 @@ classifiers = [ test = "poetry run pytest -v --cov=config_file/ --cov-report term-missing tests/" [tool.poetry.dependencies] -python = "^3.6" -nested-lookup = "^0.2.19" +python = "^3.5" +nested-lookup = "^0.2.21" toml = { version = "^0.10.0", optional = true } pyyaml = { version = "^5.3.1", optional = true } @@ -33,19 +36,20 @@ yaml = ["pyyaml"] pre-commit = "^1.20" autoflake = "^1.3.1" isort = "^4.3.21" -seed-isort-config = "^1.9" +seed-isort-config = { version = "^2.1.1", python = "^3.6.1" } pytest = "^5.4.1" pytest-cov = "^2.8.1" +pytest-sugar = "^0.9.3" sphinx = "^2.4.4" sphinx_rtd_theme = "^0.4.3" -taskipy = "^1.2.1" +taskipy = { version = "^1.2.1", python = "^3.6" } bump2version = "^1.0.0" flake8 = "^3.7.9" -black = "^19.10b0" +black = { version = "^19.10b0", python = "^3.6" } [tool.black] line-length = 88 -target_version = ['py38'] +target_version = ['py35'] include = '\.pyi?$' [tool.isort] diff --git a/tests/test_config_file.py b/tests/test_config_file.py index f083538..fe8539f 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -3,8 +3,7 @@ from pathlib import Path import pytest -from config_file.config_file import ConfigFile -from config_file.parsers.base_parser import BaseParser +from config_file import ConfigFile, BaseParser @pytest.mark.parametrize( @@ -49,14 +48,14 @@ def test_missing_config_files_during_restore( tmpdir, file_extension, specify_original_config_path ): with pytest.raises(OSError): - temp_path = tmpdir / f"config.{file_extension}" + temp_path = tmpdir / "config.{}".format(file_extension) temp_path.write("") config = ConfigFile(str(temp_path)) if specify_original_config_path: - specify_original_config_path = f"does_not_exist.{file_extension}" + specify_original_config_path = "does_not_exist.{}".format(file_extension) else: - config.path = f"does_not_exist.{file_extension}" + config.path = "does_not_exist.{}".format(file_extension) specify_original_config_path = None config.restore_original(original_file_path=specify_original_config_path) @@ -160,7 +159,7 @@ def test_that_config_file_can_get( def test_that_config_file_can_find_sections_and_keys( tmpdir, file_extension, file_contents, section_key, expected_result ): - config_path = tmpdir / f"config.{file_extension}" + config_path = tmpdir / "config.{}".format(file_extension) config_path.write_text(file_contents, encoding="utf-8") config = ConfigFile(str(config_path)) assert config.has(section_key) == expected_result diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index 7cdeea8..1b2fd3b 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,6 +1,7 @@ import pytest -from config_file.parsers.ini_parser import IniParser, ParsingError +from config_file.parsers.ini_parser import IniParser +from config_file.exceptions import ParsingError @pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"]) diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index a5e38a1..9a8ac09 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -2,7 +2,8 @@ import json import pytest -from config_file.parsers.json_parser import JsonParser, ParsingError +from config_file.parsers.json_parser import JsonParser +from config_file.exceptions import ParsingError @pytest.mark.parametrize("test_json", ["{'test': }", "[section]\nblah = value\n\n"]) @@ -162,7 +163,7 @@ def test_that_json_parser_can_delete_keys(key, test_json, expected_json): ) def test_that_json_parser_can_retrieve_all_keys(test_json, key, expected_output): parser = JsonParser(json.dumps(test_json)) - assert parser.get(key, get_all=True) == expected_output + assert parser.get(key, all=True) == expected_output @pytest.mark.parametrize( @@ -178,8 +179,8 @@ def test_that_json_parser_can_retrieve_all_keys(test_json, key, expected_output) ) def test_that_json_parser_can_set_all_keys(test_json, key, value, expected_output): parser = JsonParser(json.dumps(test_json)) - parser.set(key, value, set_all=True) - assert parser.get(key, get_all=True) == expected_output + parser.set(key, value, all=True) + assert parser.get(key, all=True) == expected_output @pytest.mark.parametrize( @@ -194,5 +195,5 @@ def test_that_json_parser_can_set_all_keys(test_json, key, value, expected_outpu ) def test_that_json_parser_can_delete_all_keys(test_json, key, expected_output): parser = JsonParser(json.dumps(test_json)) - parser.delete(key, delete_all=True) + parser.delete(key, all=True) assert parser.stringify() == json.dumps(expected_output) diff --git a/tox.ini b/tox.ini index 1098700..1a1512f 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] isolated_build = true -envlist = py{36, 37, 38} +envlist = py{35, 36, 37, 38} recreate = True [testenv] -- cgit v1.2.3 From 6924f9027ccb0a97e67929961b6d87bf647eaf88 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 13:44:38 -0500 Subject: Run travis CI on python 3.5 --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 507c5e1..e04fe9a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ language: python os: linux python: + - "3.5" - "3.6" - "3.7" - "3.8" @@ -10,4 +11,4 @@ install: - curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - source $HOME/.poetry/env - pip install --upgrade pip tox-travis codecov -script: tox \ No newline at end of file +script: tox -- cgit v1.2.3 From d69d842faba3308a71fac31d19af55595d3e23dd Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 13:47:27 -0500 Subject: Remove taskipy from tox & add default to parser in __determine_parser Taskipy uses f-strings which break on 3.5. --- config_file/config_file.py | 20 +++++++++++++++----- tox.ini | 4 ++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/config_file/config_file.py b/config_file/config_file.py index 805fc49..1cddd20 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -11,7 +11,9 @@ from config_file.exceptions import ParsingError class ConfigFile: - def __init__(self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser] = None) -> None: + def __init__( + self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser] = None + ) -> None: """ Saves the config file path and expands it if needed, reads in the file contents, and determines what parser should be used for @@ -28,7 +30,9 @@ class ConfigFile: self.parser = self.__determine_parser(file_path, parser) @staticmethod - def __create_config_path(file_path: Type[Union[str, PurePath]], original: bool = False) -> str: + def __create_config_path( + file_path: Type[Union[str, PurePath]], original: bool = False + ) -> str: if isinstance(file_path, PurePath): file_path = str(file_path) @@ -41,11 +45,15 @@ class ConfigFile: file_path = ".".join(file_parts) if Path(file_path).is_dir(): - raise ValueError("The specified config file ({}) is a directory.".format(file_path)) + raise ValueError( + "The specified config file ({}) is a directory.".format(file_path) + ) return file_path - def __determine_parser(self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser]) -> BaseParser: + def __determine_parser( + self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser] = None + ) -> BaseParser: if isinstance(file_path, PurePath): file_path = str(file_path) @@ -59,7 +67,9 @@ class ConfigFile: return JsonParser(self.contents) else: raise ValueError( - "File path contains an unsupported or unrecognized file type: {}".format(file_path) + "File path contains an unsupported or unrecognized file type: {}".format( + file_path + ) ) def __read_config_file(self): diff --git a/tox.ini b/tox.ini index 1a1512f..6b849bc 100644 --- a/tox.ini +++ b/tox.ini @@ -9,6 +9,6 @@ deps = codecov>=1.4.0 whitelist_externals = poetry commands = poetry install -v --no-dev -E toml -E yaml - pip install pytest pytest-cov requests taskipy - poetry run task test + pip install pytest pytest-cov requests + poetry run pytest -v --cov=config_file/ --cov-report term-missing tests/ codecov -e TOXENV -- cgit v1.2.3 From 792d5422b663f5669b37e1085200d69821aa3906 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 15:13:53 -0500 Subject: Change path, parser, and contents to be properties Also, restore_original now raises a FileNotFoundError. --- .pre-commit-config.yaml | 42 +++++++++++++-------- CHANGELOG.md | 3 ++ config_file/config_file.py | 94 +++++++++++++++++++++++----------------------- config_file/utils.py | 22 +++++++---- tests/test_config_file.py | 45 +++++++++------------- 5 files changed, 108 insertions(+), 98 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9975877..879742c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,30 +1,42 @@ repos: + - repo: git@github.com:humitos/mirrors-autoflake.git + rev: v1.3 + hooks: + - id: autoflake + args: + [ + "--in-place", + "--remove-all-unused-imports", + "--remove-unused-variable", + ] -- repo: git@github.com:humitos/mirrors-autoflake.git - rev: v1.3 - hooks: - - id: autoflake - args: ['--in-place', '--remove-all-unused-imports', '--remove-unused-variable'] - -- repo: https://github.com/asottile/seed-isort-config + - repo: https://github.com/asottile/seed-isort-config rev: v1.9.4 hooks: - - id: seed-isort-config + - id: seed-isort-config -- repo: https://github.com/pre-commit/mirrors-isort + - repo: https://github.com/pre-commit/mirrors-isort rev: v4.3.21 hooks: - - id: isort + - id: isort additional_dependencies: - toml -- repo: https://github.com/ambv/black + - repo: https://github.com/ambv/black rev: stable hooks: - - id: black - language_version: python3.8 + - id: black + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.5.0 + hooks: + - id: trailing-whitespace + - id: check-docstring-first + - id: check-case-conflict + - id: check-yaml + - id: check-toml -- repo: https://github.com/pre-commit/pre-commit-hooks + - repo: https://github.com/pre-commit/pre-commit-hooks rev: v1.2.3 hooks: - - id: flake8 + - id: flake8 diff --git a/CHANGELOG.md b/CHANGELOG.md index e6ac91f..2c9e591 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,9 @@ Changed - The `get_all`, `set_all`, and `delete_all` optional parameter options in `JsonParser` have been renamed to just `all`. This shouldn't have any effect since `JsonParser` is not intended to be used directly, and that option is not in `ConfigFile`. +- `path`, `parser`, and `contents` on the `ConfigFile` are now all properties and can only be retrieved. +- `restore_original` in `ConfigFile` now raises a `FileNotFoundError` instead of an `OSError` if the original + file path does not exist. ## 0.10.0 - 2020-05-10 diff --git a/config_file/config_file.py b/config_file/config_file.py index 1cddd20..d4131de 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -1,18 +1,18 @@ import inspect -from pathlib import Path, PurePath +from pathlib import Path from shutil import copyfile from typing import Type, Union +from config_file.exceptions import ParsingError from config_file.parsers.base_parser import BaseParser 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.exceptions import ParsingError +from config_file.utils import read_file, split_on_dot class ConfigFile: def __init__( - self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser] = None + self, file_path: Type[Union[str, Path]], parser: Type[BaseParser] = None ) -> None: """ Saves the config file path and expands it if needed, reads in @@ -25,19 +25,29 @@ class ConfigFile: :raises ValueError: If the specified file path does not have an extension that is supported or it is a directory. """ - self.path = self.__create_config_path(file_path) - self.contents = self.__read_config_file() - self.parser = self.__determine_parser(file_path, parser) + if type(file_path) is not Path: + file_path = Path(file_path) - @staticmethod - def __create_config_path( - file_path: Type[Union[str, PurePath]], original: bool = False - ) -> str: - if isinstance(file_path, PurePath): - file_path = str(file_path) + self.__path = self.__create_config_path(file_path) + self.__contents = read_file(self.__path) + self.__parser = self.__determine_parser(file_path, parser) + + @property + def path(self) -> Path: + return self.__path + + @property + def contents(self) -> str: + return self.__contents - if len(file_path) > 1 and file_path[0] == "~": - return str(Path(file_path).expanduser()) + @property + def parser(self) -> Type[BaseParser]: + return self.__parser + + @staticmethod + def __create_config_path(file_path: Path, original: bool = False) -> str: + if len(file_path.parts) >= 1 and file_path.parts[0] == "~": + return file_path.expanduser() if original: file_parts = split_on_dot(file_path, only_last_dot=True) @@ -52,30 +62,21 @@ class ConfigFile: return file_path def __determine_parser( - self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser] = None + self, file_path: Path, parser: Type[BaseParser] = None ) -> 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) + return parser(self.__contents) file_type = split_on_dot(file_path, only_last_dot=True)[-1] if file_type == "ini": - return IniParser(self.contents) + return IniParser(self.__contents) elif file_type == "json": - return JsonParser(self.contents) + return JsonParser(self.__contents) else: raise ValueError( - "File path contains an unsupported or unrecognized file type: {}".format( - file_path - ) + "File path contains an unrecognized file type: {}".format(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): """ Retrieve the value of a key. @@ -92,10 +93,10 @@ class ConfigFile: :return: The value of the key. """ if default is None: - key_value = self.parser.get(key, parse_types=parse_types) + key_value = self.__parser.get(key, parse_types=parse_types) else: try: - key_value = self.parser.get(key, parse_types=parse_types) + key_value = self.__parser.get(key, parse_types=parse_types) except ParsingError: return default @@ -103,15 +104,15 @@ class ConfigFile: def set(self, key: str, value): """Sets the value of a key.""" - return self.parser.set(key, value) + return self.__parser.set(key, value) def delete(self, section_key: str): """Deletes a key/value pair or entire sections.""" - return self.parser.delete(section_key) + return self.__parser.delete(section_key) def stringify(self) -> str: """Convert the parsed internal representation of the file back into a string.""" - return self.parser.stringify() + return self.__parser.stringify() def has(self, section_key: str) -> bool: """ @@ -121,7 +122,7 @@ class ConfigFile: Some formats, like JSON, do not have sections and therefore, it would only be checking if the key exists. """ - return self.parser.has(section_key) + return self.__parser.has(section_key) def restore_original(self, original_file_path=None): """ @@ -134,23 +135,22 @@ class ConfigFile: method would look for a config.original.json in the same folder by default. :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. + :raises FileNotFoundError: If the original configuration file does not exist + :raises OSError: If self.path is not writable + :raises SameFileError: If self.path and original_file_path are the same file. """ if original_file_path is None: - original_file_path = self.__create_config_path( - str(self.path), original=True - ) + original_file_path = self.__create_config_path(self.__path, original=True) if not Path(original_file_path).exists(): - raise OSError( + raise FileNotFoundError( "The {} file to restore to does not exist.".format(original_file_path) ) - 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) + self.__path.expanduser().unlink() + copyfile(original_file_path, self.__path) + self.__contents = read_file(self.path) + self.__parser.parsed_content = self.__parser.parse(self.__contents) return True @@ -159,7 +159,7 @@ class ConfigFile: Save the configuration changes by writing the file out to the specified path :return: True if the save succeeded. """ - with open(self.path, "w") as config_file: - config_file.write(self.parser.stringify()) + with open(self.__path, "w") as config_file: + config_file.write(self.__parser.stringify()) return True diff --git a/config_file/utils.py b/config_file/utils.py index 61f0ae0..05c3a7a 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -1,4 +1,8 @@ -def split_on_dot(line: str, only_last_dot=False): +from pathlib import Path +from typing import Union + + +def split_on_dot(line: Union[str, Path], only_last_dot=False): """ Split a string on the dot character (.). @@ -7,15 +11,17 @@ def split_on_dot(line: str, only_last_dot=False): :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'" + "The given string does not have a dot to split on: {}".format(line) ) return line.rsplit(".", 1) if only_last_dot else line.split(".") + + +def read_file(path: Union[str, Path]): + with open(path, "r") as file: + return file.read() diff --git a/tests/test_config_file.py b/tests/test_config_file.py index fe8539f..38cdf38 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -3,7 +3,7 @@ from pathlib import Path import pytest -from config_file import ConfigFile, BaseParser +from config_file import BaseParser, ConfigFile @pytest.mark.parametrize( @@ -14,7 +14,7 @@ def test_that_config_file_initializes_correctly(tmp_path, file_contents, file_na temp.write_text(file_contents) config = ConfigFile(temp) - assert config.path == str(temp) + assert config.path == temp assert config.contents == temp.read_text() assert isinstance(config.parser, BaseParser) @@ -26,39 +26,27 @@ def test_that_config_file_initializes_correctly(tmp_path, file_contents, file_na def test_that_config_file_can_restore_the_original( tmpdir, file_contents, file_name, original_file_name ): - config_path = tmpdir / file_name + config_path = Path(tmpdir / file_name) config_path.write_text(file_contents, encoding="utf-8") - - original_config_path = tmpdir / original_file_name + original_config_path = Path(tmpdir / original_file_name) original_config_path.write_text(file_contents, encoding="utf-8") - config = ConfigFile(str(config_path)) - config.set("calendar.sunday_index", 0) + config = ConfigFile(config_path) + config.set("calendar.sunday_index", 5) config.restore_original() - assert config.stringify() == ConfigFile(str(original_config_path)).stringify() - assert config_path.read_text(encoding="utf-8") == original_config_path.read_text( - encoding="utf-8" - ) + assert config.stringify() == ConfigFile(original_config_path).stringify() -@pytest.mark.parametrize( - "file_extension, specify_original_config_path", [("ini", False), ("ini", True)] -) -def test_missing_config_files_during_restore( - tmpdir, file_extension, specify_original_config_path -): - with pytest.raises(OSError): - temp_path = tmpdir / "config.{}".format(file_extension) - temp_path.write("") - config = ConfigFile(str(temp_path)) - if specify_original_config_path: - specify_original_config_path = "does_not_exist.{}".format(file_extension) - else: - config.path = "does_not_exist.{}".format(file_extension) - specify_original_config_path = None +def test_missing_config_files_during_restore(tmpdir): + config_path = Path(tmpdir / "config.ini") + config_path.write_text("") + config = ConfigFile(config_path) - config.restore_original(original_file_path=specify_original_config_path) + with pytest.raises(FileNotFoundError) as error: + config.restore_original(original_file_path="jhklasdfhjlkasjej") + + assert "file to restore to does not exist" in str(error) @pytest.mark.parametrize( @@ -228,6 +216,7 @@ def test_that_custom_parser_can_be_used(tmpdir): config_path = tmpdir / "config.conf" config_path.write_text("", encoding="utf-8") config = ConfigFile(str(config_path), parser=CustomParser) + assert isinstance(config.parser, CustomParser) assert config.get("key") == "key" assert config.set("key", "value") == ("key", "value") @@ -242,6 +231,6 @@ def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): try: Path(config_path).expanduser().touch() - assert ConfigFile(config_path).path == str(Path.home() / config_name) + assert ConfigFile(config_path).path == Path.home() / config_name finally: Path(config_path).expanduser().unlink() -- cgit v1.2.3 From a4d252e5dbcf8d366707ab0c4ddac8a81963a801 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 15:30:11 -0500 Subject: Revert Python 3.5 support --- CHANGELOG.md | 1 - CONTRIBUTING.md | 35 ++++++++++++++++++----------------- config_file/config_file.py | 10 +++++----- config_file/parsers/ini_parser.py | 6 +++--- config_file/parsers/json_parser.py | 4 ++-- config_file/utils.py | 14 ++++++-------- pyproject.toml | 9 ++++----- tox.ini | 6 +++--- 8 files changed, 41 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c9e591..3f89271 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Added -- Support for Python 3.5 by removing use of f-strings from codebase. - A generic `ConfigFileError`. - More type hints from `typings` have been added to `ConfigFile` for better type information. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8669fd0..902c669 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -14,33 +14,34 @@ Here are some guidelines to keep in mind when submitting a pull request: paragraphs of explanation as you need. This is the place to clarify any subtleties you have in your implemeÆ’ntation, document other approaches you tried that didn't end up working, any limitations on your implementation, etc. The most important - part here is to describe *why* you made the change you did, not simply *what* the + part here is to describe _why_ you made the change you did, not simply _what_ the change you made is. - Changelog: Please ensure to update the changelog by adding a new bullet under - an ``Added``, ``Changed``, ``Deprecated``, ``Removed``, ``Fixed``, or ``Security`` section - headers under the ``Unreleased`` version. If any of those sections are not present, + an `Added`, `Changed`, `Deprecated`, `Removed`, `Fixed`, or `Security` section + headers under the `Unreleased` version. If any of those sections are not present, feel free to add the one you need. See [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) if you need guidance on what makes a good entry since this project follows those principles. If you're not comfortable with your English, we'd be happy to write it for you. -- Ensure the tests pass: ``poetry run task test`` to run all tests. +- Ensure the tests pass: `poetry run task test` to run all tests. - New features should be accompanied with tests for them: + - Unit tests are written using [pytest](https://docs.pytest.org/en/latest/). - Pre-commit pipeline: We use a pre-commit pipeline to ensure standard code format. - - Unused imports are automatically removed using `autoflake `_ - - - The remaining imports are sorted using `isort `_. - - - All code is automatically formatted with `black `_ - - - Lastly, everything is checked by `flake8 `_. - - Make sure to install pre-commit before making commits. + - Unused imports are automatically removed using `autoflake `\_ + + - The remaining imports are sorted using `isort `\_. + + - All code is automatically formatted with `black `\_ + + - Lastly, everything is checked by `flake8 `\_. + + Make sure to install pre-commit before making commits. Note: if any of the items had to do any reformatting, sorting, etc., the commit will fail. You'll have to re-add the items it fixed and try again. @@ -48,11 +49,11 @@ Here are some guidelines to keep in mind when submitting a pull request: ```bash pre-commit install ``` - + - CI Pipeline: There is a CI pipeline that is run by Travis CI on commits to master and on pull requests. - + - All it does is ensure that all the tests pass. -To get started, make sure you have poetry installed and run ``poetry install`` and -``poetry shell`` to enter the virtual environment. \ No newline at end of file +To get started, make sure you have poetry installed and run `poetry install` and +`poetry shell` to enter the virtual environment. diff --git a/config_file/config_file.py b/config_file/config_file.py index d4131de..3af1083 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -55,9 +55,7 @@ class ConfigFile: file_path = ".".join(file_parts) if Path(file_path).is_dir(): - raise ValueError( - "The specified config file ({}) is a directory.".format(file_path) - ) + raise ValueError(f"The specified config file ({file_path}) is a directory.") return file_path @@ -74,7 +72,7 @@ class ConfigFile: return JsonParser(self.__contents) else: raise ValueError( - "File path contains an unrecognized file type: {}".format(file_path) + f"File path contains an unrecognized file type: {file_path}" ) def get(self, key: str, parse_types: bool = False, return_type=None, default=None): @@ -91,6 +89,8 @@ class ConfigFile: :param default: The default value to return if the value of the key is empty. :return: The value of the key. + + :raises 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) @@ -144,7 +144,7 @@ class ConfigFile: if not Path(original_file_path).exists(): raise FileNotFoundError( - "The {} file to restore to does not exist.".format(original_file_path) + f"The {original_file_path} file to restore to does not exist." ) self.__path.expanduser().unlink() diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 31b0dcf..2db1ae8 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -1,10 +1,10 @@ import configparser from io import StringIO +from config_file.exceptions import ParsingError from config_file.parsers.base_parser import BaseParser from config_file.parsers.parse_value import parse_value from config_file.utils import split_on_dot -from config_file.exceptions import ParsingError class IniParser(BaseParser): @@ -96,7 +96,7 @@ class IniParser(BaseParser): if "." not in section_key: if not self.parsed_content.has_section(section_key): raise ValueError( - "Cannot delete {} because it is not in the config file.".format(section_key) + f"Cannot delete {section_key} because it is not in the config file." ) self.parsed_content.remove_section(section_key) @@ -105,7 +105,7 @@ class IniParser(BaseParser): section, key = split_on_dot(section_key, only_last_dot=True) if key not in self.parsed_content[section]: raise ValueError( - "Cannot delete {section}.{key} because {key} is not in {section}.".format(section = section, key = key) + f"Cannot delete {section}.{key} because {key} is not in {section}." ) self.parsed_content.remove_option(section, key) diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py index 4d93ca3..10d5afc 100644 --- a/config_file/parsers/json_parser.py +++ b/config_file/parsers/json_parser.py @@ -7,10 +7,10 @@ from nested_lookup import ( nested_update, ) +from config_file.exceptions import ParsingError from config_file.parsers.base_parser import BaseParser from config_file.parsers.parse_value import parse_value from config_file.utils import split_on_dot -from config_file.exceptions import ParsingError class JsonParser(BaseParser): @@ -78,7 +78,7 @@ class JsonParser(BaseParser): result = result[key] except TypeError: raise ParsingError( - "Cannot get {} because {} is not subscriptable.".format(key, key_for_error) + f"Cannot get {key} because {key_for_error} is not subscriptable." ) else: result = self.parsed_content[key] diff --git a/config_file/utils.py b/config_file/utils.py index 05c3a7a..9ed09b5 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -1,8 +1,8 @@ -from pathlib import Path -from typing import Union +from pathlib import PurePath +from typing import Type, Union -def split_on_dot(line: Union[str, Path], only_last_dot=False): +def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False): """ Split a string on the dot character (.). @@ -11,17 +11,15 @@ def split_on_dot(line: Union[str, Path], only_last_dot=False): :raises ValueError: if the line does not have a dot. """ - if isinstance(line, Path): + if isinstance(line, PurePath): line = str(line) if "." not in line: - raise ValueError( - "The given string does not have a dot to split on: {}".format(line) - ) + 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(".") -def read_file(path: Union[str, Path]): +def read_file(path: Union[str, Type[PurePath]]): with open(path, "r") as file: return file.read() diff --git a/pyproject.toml b/pyproject.toml index 97c98c7..0f3bf5f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,6 @@ classifiers = [ "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", - "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", @@ -23,7 +22,7 @@ classifiers = [ test = "poetry run pytest -v --cov=config_file/ --cov-report term-missing tests/" [tool.poetry.dependencies] -python = "^3.5" +python = "^3.6" nested-lookup = "^0.2.21" toml = { version = "^0.10.0", optional = true } pyyaml = { version = "^5.3.1", optional = true } @@ -42,14 +41,14 @@ pytest-cov = "^2.8.1" pytest-sugar = "^0.9.3" sphinx = "^2.4.4" sphinx_rtd_theme = "^0.4.3" -taskipy = { version = "^1.2.1", python = "^3.6" } +taskipy = "^1.2.1" bump2version = "^1.0.0" flake8 = "^3.7.9" -black = { version = "^19.10b0", python = "^3.6" } +black = "^19.10b0" [tool.black] line-length = 88 -target_version = ['py35'] +target_version = ['py36'] include = '\.pyi?$' [tool.isort] diff --git a/tox.ini b/tox.ini index 6b849bc..1098700 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] isolated_build = true -envlist = py{35, 36, 37, 38} +envlist = py{36, 37, 38} recreate = True [testenv] @@ -9,6 +9,6 @@ deps = codecov>=1.4.0 whitelist_externals = poetry commands = poetry install -v --no-dev -E toml -E yaml - pip install pytest pytest-cov requests - poetry run pytest -v --cov=config_file/ --cov-report term-missing tests/ + pip install pytest pytest-cov requests taskipy + poetry run task test codecov -e TOXENV -- cgit v1.2.3 From 3fc0b5a1a4281b21132ceb2fb6b1859c55be6e04 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 --- README.md | 8 ++++---- config_file/__init__.py | 2 +- config_file/config_file.py | 28 ++++++++++++++++++---------- config_file/exceptions.py | 2 +- config_file/parsers/base_parser.py | 1 - tests/test_ini_parser.py | 2 +- tests/test_json_parser.py | 2 +- 7 files changed, 26 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index b7ef6ef..223f3e7 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ ## About Config File -The Config File project is designed to allow you to easily manipulate your +The Config File project is designed to allow you to easily manipulate your configuration files with the same simple API whether they are in INI, JSON, YAML, or TOML. For the time being, it only supports INI and JSON. @@ -49,7 +49,7 @@ from config_file import ConfigFile ORIGINAL_CONFIG_PATH = Path("~/some-project/some-other-config-file.ini") CONFIG_PATH = Path("~/some-project/config.ini") -# Our path can be a string or a Path object. +# Our path can be a string or a Path object. # The "~" will be automatically expanded to the full path for us. config = ConfigFile(CONFIG_PATH) @@ -66,7 +66,7 @@ print(config.get("section")) print(config.get("section", parse_types=True)) >>> {'first_key': 5, 'second_key': 'blah', 'third_key': True} -# Sometimes we want to retrieve a key but don't know whether or not +# Sometimes we want to retrieve a key but don't know whether or not # it will be set. In that case, we can set a default. print(config.get("section.unknown", default=False)) >>> False @@ -96,7 +96,7 @@ print(config.restore_original(original_file_path=ORIGINAL_CONFIG_PATH)) >>> True # Otherwise, a config.original.ini file will automatically be looked for in the -# current directory (because our configuration file we passed in was +# current directory (because our configuration file we passed in was # named config.ini). print(config.restore_original()) >>> True diff --git a/config_file/__init__.py b/config_file/__init__.py index f37019a..5fb800a 100644 --- a/config_file/__init__.py +++ b/config_file/__init__.py @@ -1,6 +1,6 @@ from config_file.config_file import ConfigFile +from config_file.exceptions import ConfigFileError, ParsingError from config_file.parsers.base_parser import BaseParser -from config_file.exceptions import ParsingError, ConfigFileError __version__ = "0.10.0" diff --git a/config_file/config_file.py b/config_file/config_file.py index 3af1083..431521a 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -1,7 +1,7 @@ import inspect -from pathlib import Path +from pathlib import Path, PurePath from shutil import copyfile -from typing import Type, Union +from typing import Any, Type, Union from config_file.exceptions import ParsingError from config_file.parsers.base_parser import BaseParser @@ -12,7 +12,9 @@ from config_file.utils import read_file, split_on_dot class ConfigFile: def __init__( - self, file_path: Type[Union[str, Path]], parser: Type[BaseParser] = None + self, + file_path: Type[Union[str, Type[PurePath]]], + parser: Type[BaseParser] = None, ) -> None: """ Saves the config file path and expands it if needed, reads in @@ -33,7 +35,7 @@ class ConfigFile: self.__parser = self.__determine_parser(file_path, parser) @property - def path(self) -> Path: + def path(self) -> Type[PurePath]: return self.__path @property @@ -45,7 +47,7 @@ class ConfigFile: return self.__parser @staticmethod - def __create_config_path(file_path: Path, original: bool = False) -> str: + def __create_config_path(file_path: Type[PurePath], original: bool = False) -> str: if len(file_path.parts) >= 1 and file_path.parts[0] == "~": return file_path.expanduser() @@ -60,7 +62,7 @@ class ConfigFile: return file_path def __determine_parser( - self, file_path: Path, parser: Type[BaseParser] = None + self, file_path: Type[PurePath], parser: Type[BaseParser] = None ) -> BaseParser: if parser is not None and not inspect.isabstract(parser): return parser(self.__contents) @@ -75,7 +77,13 @@ class ConfigFile: f"File path contains an unrecognized file type: {file_path}" ) - def get(self, key: str, parse_types: bool = False, return_type=None, default=None): + def get( + self, + key: str, + parse_types: bool = False, + return_type: Any = None, + default: Any = None, + ) -> Any: """ Retrieve the value of a key. @@ -102,7 +110,7 @@ class ConfigFile: return return_type(key_value) if return_type else key_value - def set(self, key: str, value): + def set(self, key: str, value: Any) -> bool: """Sets the value of a key.""" return self.__parser.set(key, value) @@ -124,7 +132,7 @@ class ConfigFile: """ return self.__parser.has(section_key) - def restore_original(self, original_file_path=None): + def restore_original(self, original_file_path: Union[str, Type[PurePath]] = None): """ 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 @@ -154,7 +162,7 @@ class ConfigFile: return True - def save(self): + def save(self) -> bool: """ Save the configuration changes by writing the file out to the specified path :return: True if the save succeeded. diff --git a/config_file/exceptions.py b/config_file/exceptions.py index cd7e236..e1af755 100644 --- a/config_file/exceptions.py +++ b/config_file/exceptions.py @@ -1,6 +1,6 @@ class ConfigFileError(Exception): """A generic error from the package.""" + class ParsingError(ConfigFileError): """Unable to parse the configuration file.""" - \ No newline at end of file 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 - diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index 1b2fd3b..fb92373 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,7 +1,7 @@ import pytest -from config_file.parsers.ini_parser import IniParser from config_file.exceptions import ParsingError +from config_file.parsers.ini_parser import IniParser @pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"]) diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index 9a8ac09..42a0ba9 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -2,8 +2,8 @@ import json import pytest -from config_file.parsers.json_parser import JsonParser from config_file.exceptions import ParsingError +from config_file.parsers.json_parser import JsonParser @pytest.mark.parametrize("test_json", ["{'test': }", "[section]\nblah = value\n\n"]) -- cgit v1.2.3 From dcc86d388f5305b8d2c16a6aaf8e5031be358c02 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 15:36:02 -0500 Subject: Convert string to f-string in test --- tests/test_config_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 38cdf38..861b169 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -147,7 +147,7 @@ def test_that_config_file_can_get( def test_that_config_file_can_find_sections_and_keys( tmpdir, file_extension, file_contents, section_key, expected_result ): - config_path = tmpdir / "config.{}".format(file_extension) + config_path = tmpdir / f"config.{file_extension}" config_path.write_text(file_contents, encoding="utf-8") config = ConfigFile(str(config_path)) assert config.has(section_key) == expected_result -- cgit v1.2.3 From 0793757c3a617ad2b736c879216ccf802e2ae921 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 15:56:03 -0500 Subject: Use xml cov report for pytest --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 0f3bf5f..5819885 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,7 +19,7 @@ classifiers = [ ] [tool.taskipy.tasks] -test = "poetry run pytest -v --cov=config_file/ --cov-report term-missing tests/" +test = "poetry run pytest -v --cov=config_file/ --cov-report=xml term-missing tests/" [tool.poetry.dependencies] python = "^3.6" -- cgit v1.2.3 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. --- CHANGELOG.md | 195 +++++++++-------- config_file/__init__.py | 4 +- config_file/config_file.py | 18 +- config_file/exceptions.py | 6 + config_file/parsers/base_parser.py | 3 - config_file/parsers/ini_parser.py | 7 +- config_file/parsers/json_parser.py | 21 +- config_file/parsers/toml_parser.py | 0 config_file/parsers/yaml_parser.py | 0 poetry.lock | 425 +++++++++++++++++++------------------ pyproject.toml | 16 +- tests/test_config_file.py | 11 +- tests/test_ini_parser.py | 3 +- tests/test_json_parser.py | 11 +- tox.ini | 2 +- 15 files changed, 381 insertions(+), 341 deletions(-) create mode 100644 config_file/exceptions.py create mode 100644 config_file/parsers/toml_parser.py create mode 100644 config_file/parsers/yaml_parser.py diff --git a/CHANGELOG.md b/CHANGELOG.md index e0a3ce8..e6ac91f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,167 +5,182 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +Added + +- Support for Python 3.5 by removing use of f-strings from codebase. +- A generic `ConfigFileError`. +- More type hints from `typings` have been added to `ConfigFile` for better type information. + +Changed + +- `ParsingError` now inherits from `ConfigFileError` rather than `Exception`. +- Errors have been moved to `exceptions.py`. +- The `get_all`, `set_all`, and `delete_all` optional parameter options in `JsonParser` have + been renamed to just `all`. This shouldn't have any effect since `JsonParser` is not intended + to be used directly, and that option is not in `ConfigFile`. + ## 0.10.0 - 2020-05-10 Changed - - `toml` and `pyyaml` are now optional extra dependencies. This allows you to - not have to install them if you aren't using them. - - - `retrieve_all` in `JsonParser`'s `get` method is now called `get_all` for - consistency. It isn't publicly available yet. The thought is to write a custom - ini parser first that supports multiple of the same keys and subsections. - This way you can have the `get_all`, `set_all`, `delete_all`, etc. methods - for all file type parsers. I would have to weight if that added complexity - of not using the built-in configparser and roll our own for some added - features is worth it or see if there are ways to work around it with - configparser that are ideal. +- `toml` and `pyyaml` are now optional extra dependencies. This allows you to + not have to install them if you aren't using them. -Fixed +- `retrieve_all` in `JsonParser`'s `get` method is now called `get_all` for + consistency. It isn't publicly available yet. The thought is to write a custom + ini parser first that supports multiple of the same keys and subsections. + This way you can have the `get_all`, `set_all`, `delete_all`, etc. methods + for all file type parsers. I would have to weight if that added complexity + of not using the built-in configparser and roll our own for some added + features is worth it or see if there are ways to work around it with + configparser that are ideal. - - You can now specify a default and still coerce your return type. Previously, - if you specified a default, there was no logic in that branch to coerce your - return type as well. +Fixed +- You can now specify a default and still coerce your return type. Previously, + if you specified a default, there was no logic in that branch to coerce your + return type as well. ## 0.9.0 - 2020-02-09 Added - - `default` optional parameter to the `get()` method of `ConfigFile`. - The allows a default value to be fallen back to if the given key is missing. - - - `return_type` optional parameter to `get()` method of `ConfigFile`. This - allows you to coerce the return type to one of your choosing by feeding it - the return value. +- `default` optional parameter to the `get()` method of `ConfigFile`. + The allows a default value to be fallen back to if the given key is missing. + +- `return_type` optional parameter to `get()` method of `ConfigFile`. This + allows you to coerce the return type to one of your choosing by feeding it + the return value. Changed - - Automatic type parsing is now off by default. This is because of the addition of - the `return_type` optional parameter. After using the package more, I think - the explicitness of specifying the type you're after or that you'd like to - automatically parse the type to one of the basic types is more maintainable. - However, I think the option to automatically parse or parse a whole section of - values is still a useful one. - - - The `parse_type` parameter to `ConfigFile`'s `get()` method is now called - `parse_types`. +- Automatic type parsing is now off by default. This is because of the addition of + the `return_type` optional parameter. After using the package more, I think + the explicitness of specifying the type you're after or that you'd like to + automatically parse the type to one of the basic types is more maintainable. + However, I think the option to automatically parse or parse a whole section of + values is still a useful one. + +- The `parse_type` parameter to `ConfigFile`'s `get()` method is now called + `parse_types`. ## 0.8.0 - 2020-02-02 Added - - More type hints to `ConfigFile` and `IniParser`. - - `nested_lookup` dependency to help with modifying deeply nested structures - (JSON + YAML) - - `JsonParser` so you can now specify `.json` files to be parsed. - +- More type hints to `ConfigFile` and `IniParser`. +- `nested_lookup` dependency to help with modifying deeply nested structures + (JSON + YAML) +- `JsonParser` so you can now specify `.json` files to be parsed. + Changed - - The original content of the passed in file is now called `content` - instead of `contents`. This is for consistency since the parsed version is - called `parsed_content`. +- The original content of the passed in file is now called `content` + instead of `contents`. This is for consistency since the parsed version is + called `parsed_content`. ## 0.7.0 - 2020-01-27 Fixed - - Support for using custom parsers with `ConfigFile` with the `parser` optional - argument. This was technically supported before, but it was not tested and found - to not actually use the passed in parser once tested. +- Support for using custom parsers with `ConfigFile` with the `parser` optional + argument. This was technically supported before, but it was not tested and found + to not actually use the passed in parser once tested. Changed - - The `reset()` method on `ConfigFile` is now called `restore_original()`. - The behavior is the same. This was done to better describe what exactly that method - is doing. Since the file is not written back out with every `set()` or `delete()` - and calling `save()` explicitly is required, `reset()` may have been confused with - resetting the changes you've made rather than deleting and restoring the original - configuration file. +- The `reset()` method on `ConfigFile` is now called `restore_original()`. + The behavior is the same. This was done to better describe what exactly that method + is doing. Since the file is not written back out with every `set()` or `delete()` + and calling `save()` explicitly is required, `reset()` may have been confused with + resetting the changes you've made rather than deleting and restoring the original + configuration file. ## 0.6.0 - 2020-01-24 Changed - - Bumped down python version requirement to 3.6 and now test 3.6, 3.7, and 3.8 on CI. +- Bumped down python version requirement to 3.6 and now test 3.6, 3.7, and 3.8 on CI. ## 0.5.0 - 2020-01-19 Added - - Support for retrieving entire sections as a `dict` with `get()`. +- Support for retrieving entire sections as a `dict` with `get()`. ## 0.4.0 - 2020-01-19 Added - - `reset()` and `save()` methods to `ConfigFile`. This allows you to reset your - configuration file to an "original state," given the original config file path. - However, say you have a `config/config.json` file. Then it will automatically try - to look for `config/config.original.json` if no file path is specified. The `save()` - method should be called after your changes to the config file. It will write them - back out. - - - Raise test coverage to 93%. +- `reset()` and `save()` methods to `ConfigFile`. This allows you to reset your + configuration file to an "original state," given the original config file path. + However, say you have a `config/config.json` file. Then it will automatically try + to look for `config/config.original.json` if no file path is specified. The `save()` + method should be called after your changes to the config file. It will write them + back out. + +- Raise test coverage to 93%. ## 0.3.3 - 2020-01-18 Changed - - `_split_on_dot` is no longer in base parser (now in a utils file). Also, the default +- `_split_on_dot` is no longer in base parser (now in a utils file). Also, the default behavior is now to split on every dot and split on only the last dot if specified - + Fixed - - `ConfigFile` was trying to use `_split_on_dot`, but it no longer inherited from base parser. - - - `TypeError` when setting the key/value pairs since configparser requires the option - to be a string. The parser now just converts the value to a string if it is not one - and then adds the key/value pair. It would still be parsed correctly when retrieving - it. +- `ConfigFile` was trying to use `_split_on_dot`, but it no longer inherited from base parser. + +- `TypeError` when setting the key/value pairs since configparser requires the option + to be a string. The parser now just converts the value to a string if it is not one + and then adds the key/value pair. It would still be parsed correctly when retrieving + it. ## 0.3.1 - 2020-01-17 Fixed - - `isinstance` call for `BaseParser` to be correctly used. +- `isinstance` call for `BaseParser` to be correctly used. ## 0.3.0 - 2020-01-17 Added - - Abstract base parser as a contract for concrete file format parser implementations. - - - Exposed `ConfigFile`, `BaseParser`, and `ParsingError` to the Public API. The base - parser is exposed to allow future custom extensions of the config file and what it +- Abstract base parser as a contract for concrete file format parser implementations. + +- Exposed `ConfigFile`, `BaseParser`, and `ParsingError` to the Public API. The base + parser is exposed to allow future custom extensions of the config file and what it can parse by its users. - - - `has_section` and `has_key` is now changed to a single `has` method which determines - whether you're checking a section or key by the presence of a dot. - - - `IniParser` to support the ini format. It uses configparser internally, but it is - only exposed through the `ConfigFile` object. + +- `has_section` and `has_key` is now changed to a single `has` method which determines + whether you're checking a section or key by the presence of a dot. + +- `IniParser` to support the ini format. It uses configparser internally, but it is + only exposed through the `ConfigFile` object. ## 0.2.0 - 2020-01-04 Added - - Parsing of strings to their native values +- Parsing of strings to their native values ## 0.1.0 - 2020-01-04 Added - - Created the config_file package with only the version inside it - - - toml and pyyaml to dependencies - - - Pre-commit pipeline with autoflake, isort, black, and flake8 - - - MIT license - - - Contributing guidelines - - - bump2version for easily upgrading versions of config file - - - pytest to dev dependencies for testing later on +- Created the config_file package with only the version inside it + +- toml and pyyaml to dependencies + +- Pre-commit pipeline with autoflake, isort, black, and flake8 + +- MIT license + +- Contributing guidelines + +- bump2version for easily upgrading versions of config file + +- pytest to dev dependencies for testing later on diff --git a/config_file/__init__.py b/config_file/__init__.py index 3bae28a..f37019a 100644 --- a/config_file/__init__.py +++ b/config_file/__init__.py @@ -1,10 +1,12 @@ from config_file.config_file import ConfigFile -from config_file.parsers.base_parser import BaseParser, ParsingError +from config_file.parsers.base_parser import BaseParser +from config_file.exceptions import ParsingError, ConfigFileError __version__ = "0.10.0" __all__ = [ "ConfigFile", "ParsingError", + "ConfigFileError", "BaseParser", ] diff --git a/config_file/config_file.py b/config_file/config_file.py index 91202da..805fc49 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -1,15 +1,17 @@ import inspect from pathlib import Path, PurePath from shutil import copyfile +from typing import Type, Union -from config_file.parsers.base_parser import BaseParser, ParsingError +from config_file.parsers.base_parser import BaseParser 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.exceptions import ParsingError class ConfigFile: - def __init__(self, file_path, parser: BaseParser = None): + def __init__(self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser] = None) -> None: """ Saves the config file path and expands it if needed, reads in the file contents, and determines what parser should be used for @@ -17,7 +19,6 @@ class ConfigFile: :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. :raises ValueError: If the specified file path does not have an extension that is supported or it is a directory. @@ -27,7 +28,7 @@ class ConfigFile: self.parser = self.__determine_parser(file_path, parser) @staticmethod - def __create_config_path(file_path, original: bool = False) -> str: + def __create_config_path(file_path: Type[Union[str, PurePath]], original: bool = False) -> str: if isinstance(file_path, PurePath): file_path = str(file_path) @@ -40,11 +41,11 @@ class ConfigFile: file_path = ".".join(file_parts) if Path(file_path).is_dir(): - raise ValueError(f"The specified config file ({file_path}) is a directory.") + raise ValueError("The specified config file ({}) is a directory.".format(file_path)) return file_path - def __determine_parser(self, file_path, parser: BaseParser): + def __determine_parser(self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser]) -> BaseParser: if isinstance(file_path, PurePath): file_path = str(file_path) @@ -58,8 +59,7 @@ class ConfigFile: return JsonParser(self.contents) else: raise ValueError( - f"File path contains an unsupported or " - f"unrecognized file type: {file_path}" + "File path contains an unsupported or unrecognized file type: {}".format(file_path) ) def __read_config_file(self): @@ -134,7 +134,7 @@ class ConfigFile: if not Path(original_file_path).exists(): raise OSError( - f"The {original_file_path} file to restore to does not exist." + "The {} file to restore to does not exist.".format(original_file_path) ) Path(self.path).expanduser().unlink() diff --git a/config_file/exceptions.py b/config_file/exceptions.py new file mode 100644 index 0000000..cd7e236 --- /dev/null +++ b/config_file/exceptions.py @@ -0,0 +1,6 @@ +class ConfigFileError(Exception): + """A generic error from the package.""" + +class ParsingError(ConfigFileError): + """Unable to parse the configuration file.""" + \ No newline at end of file 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""" diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 81f2d1b..31b0dcf 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -1,9 +1,10 @@ import configparser from io import StringIO -from config_file.parsers.base_parser import BaseParser, ParsingError +from config_file.parsers.base_parser import BaseParser from config_file.parsers.parse_value import parse_value from config_file.utils import split_on_dot +from config_file.exceptions import ParsingError class IniParser(BaseParser): @@ -95,7 +96,7 @@ class IniParser(BaseParser): 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." + "Cannot delete {} because it is not in the config file.".format(section_key) ) self.parsed_content.remove_section(section_key) @@ -104,7 +105,7 @@ class IniParser(BaseParser): 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}." + "Cannot delete {section}.{key} because {key} is not in {section}.".format(section = section, key = key) ) self.parsed_content.remove_option(section, key) diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py index 20eb085..4d93ca3 100644 --- a/config_file/parsers/json_parser.py +++ b/config_file/parsers/json_parser.py @@ -7,9 +7,10 @@ from nested_lookup import ( nested_update, ) -from config_file.parsers.base_parser import BaseParser, ParsingError +from config_file.parsers.base_parser import BaseParser from config_file.parsers.parse_value import parse_value from config_file.utils import split_on_dot +from config_file.exceptions import ParsingError class JsonParser(BaseParser): @@ -34,7 +35,7 @@ class JsonParser(BaseParser): >>> { "key2": "foo", "key1": "bar" } parser.get('key1.key2') >>> "foo" - parser.get('key1', get_all=True) + parser.get('key1', all=True) >>> [{ "key2": "foo", "key1": "bar" }, "bar"] """ @@ -48,7 +49,7 @@ class JsonParser(BaseParser): except json.decoder.JSONDecodeError as error: raise ParsingError(error) - def get(self, key: str, parse_types: bool = False, get_all: bool = False): + def get(self, key: str, parse_types: bool = False, all: bool = False): """ Retrieve values using a dot syntax. @@ -56,14 +57,14 @@ class JsonParser(BaseParser): :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 + :param 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: + if all: result = nested_lookup(key, self.parsed_content) elif "." in key: split_keys = split_on_dot(key) @@ -77,15 +78,15 @@ class JsonParser(BaseParser): result = result[key] except TypeError: raise ParsingError( - f"Cannot get {key} because {key_for_error} is not subscriptable." + "Cannot get {} because {} is not subscriptable.".format(key, key_for_error) ) 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: + def set(self, key: str, value, all: bool = False): + if all: nested_update(self.parsed_content, key, value, in_place=True) elif "." in key: indexes = split_on_dot(key) @@ -103,8 +104,8 @@ class JsonParser(BaseParser): return True - def delete(self, key, delete_all: bool = False): - if delete_all: + def delete(self, key, all: bool = False): + if all: nested_delete(self.parsed_content, key, in_place=True) elif "." in key: indexes = split_on_dot(key) diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py new file mode 100644 index 0000000..e69de29 diff --git a/config_file/parsers/yaml_parser.py b/config_file/parsers/yaml_parser.py new file mode 100644 index 0000000..e69de29 diff --git a/poetry.lock b/poetry.lock index 9ee97f5..566d146 100644 --- a/poetry.lock +++ b/poetry.lock @@ -9,18 +9,20 @@ version = "0.7.12" [[package]] category = "dev" description = "A small Python module for determining appropriate platform-specific dirs, e.g. a \"user data dir\"." +marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "appdirs" optional = false python-versions = "*" -version = "1.4.3" +version = "1.4.4" [[package]] category = "dev" description = "Utilities for refactoring imports in python-like syntax." +marker = "python_version >= \"3.6.1\" and python_version < \"4.0.0\"" name = "aspy.refactor-imports" optional = false -python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "1.1.0" +python-versions = ">=3.6.1" +version = "2.1.1" [package.dependencies] cached-property = "*" @@ -43,7 +45,7 @@ marker = "sys_platform == \"win32\"" name = "atomicwrites" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "1.3.0" +version = "1.4.0" [[package]] category = "dev" @@ -84,6 +86,7 @@ pytz = ">=2015.7" [[package]] category = "dev" description = "The uncompromising code formatter." +marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "black" optional = false python-versions = ">=3.6" @@ -112,6 +115,7 @@ version = "1.0.0" [[package]] category = "dev" description = "A decorator for caching properties in classes." +marker = "python_version >= \"3.6.1\" and python_version < \"4.0.0\"" name = "cached-property" optional = false python-versions = "*" @@ -123,15 +127,18 @@ description = "Python package for providing Mozilla's CA Bundle." name = "certifi" optional = false python-versions = "*" -version = "2019.11.28" +version = "2020.4.5.2" [[package]] category = "dev" description = "Validate configuration and produce human readable error messages." name = "cfgv" optional = false -python-versions = ">=3.6" -version = "3.0.0" +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" +version = "2.0.1" + +[package.dependencies] +six = "*" [[package]] category = "dev" @@ -144,10 +151,11 @@ version = "3.0.4" [[package]] category = "dev" description = "Composable command line interface toolkit" +marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "click" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" -version = "7.1.1" +version = "7.1.2" [[package]] category = "dev" @@ -164,19 +172,11 @@ description = "Code coverage measurement for Python" name = "coverage" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4" -version = "5.0.4" +version = "5.1" [package.extras] toml = ["toml"] -[[package]] -category = "dev" -description = "Distribution utilities" -name = "distlib" -optional = false -python-versions = "*" -version = "0.3.0" - [[package]] category = "dev" description = "Docutils -- Python Documentation Utilities" @@ -187,33 +187,20 @@ version = "0.16" [[package]] category = "dev" -description = "Discover and load entry points from installed packages." -name = "entrypoints" -optional = false -python-versions = ">=2.7" -version = "0.3" - -[[package]] -category = "dev" -description = "A platform independent file lock." -name = "filelock" -optional = false -python-versions = "*" -version = "3.0.12" - -[[package]] -category = "dev" -description = "the modular source code checker: pep8, pyflakes and co" +description = "the modular source code checker: pep8 pyflakes and co" name = "flake8" optional = false -python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "3.7.9" +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" +version = "3.8.3" [package.dependencies] -entrypoints = ">=0.3.0,<0.4.0" mccabe = ">=0.6.0,<0.7.0" -pycodestyle = ">=2.5.0,<2.6.0" -pyflakes = ">=2.1.0,<2.2.0" +pycodestyle = ">=2.6.0a1,<2.7.0" +pyflakes = ">=2.2.0,<2.3.0" + +[package.dependencies.importlib-metadata] +python = "<3.8" +version = "*" [[package]] category = "dev" @@ -221,7 +208,7 @@ description = "File identification library for Python" name = "identify" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" -version = "1.4.13" +version = "1.4.19" [package.extras] license = ["editdistance"] @@ -249,14 +236,14 @@ marker = "python_version < \"3.8\"" name = "importlib-metadata" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7" -version = "1.6.0" +version = "1.6.1" [package.dependencies] zipp = ">=0.5" [package.extras] docs = ["sphinx", "rst.linker"] -testing = ["packaging", "importlib-resources"] +testing = ["packaging", "pep517", "importlib-resources (>=1.3)"] [[package]] category = "dev" @@ -265,7 +252,7 @@ marker = "python_version < \"3.7\"" name = "importlib-resources" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7" -version = "1.4.0" +version = "2.0.0" [package.dependencies] [package.dependencies.importlib-metadata] @@ -299,7 +286,7 @@ description = "A very fast and expressive template engine." name = "jinja2" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" -version = "2.11.1" +version = "2.11.2" [package.dependencies] MarkupSafe = ">=0.23" @@ -329,7 +316,7 @@ description = "More routines for operating on iterables, beyond itertools" name = "more-itertools" optional = false python-versions = ">=3.5" -version = "8.2.0" +version = "8.3.0" [[package]] category = "main" @@ -348,7 +335,7 @@ description = "Node.js virtual environment builder" name = "nodeenv" optional = false python-versions = "*" -version = "1.3.5" +version = "1.4.0" [[package]] category = "dev" @@ -356,19 +343,32 @@ description = "Core utilities for Python packages" name = "packaging" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "20.3" +version = "20.4" [package.dependencies] pyparsing = ">=2.0.2" six = "*" +[[package]] +category = "dev" +description = "Object-oriented filesystem paths" +marker = "python_version < \"3.6\"" +name = "pathlib2" +optional = false +python-versions = "*" +version = "2.3.5" + +[package.dependencies] +six = "*" + [[package]] category = "dev" description = "Utility library for gitignore style pattern matching of file paths." +marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "pathspec" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" -version = "0.7.0" +version = "0.8.0" [[package]] category = "dev" @@ -426,7 +426,7 @@ description = "Python style guide checker" name = "pycodestyle" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "2.5.0" +version = "2.6.0" [[package]] category = "dev" @@ -434,7 +434,7 @@ description = "passive checker of Python programs" name = "pyflakes" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "2.1.1" +version = "2.2.0" [[package]] category = "dev" @@ -450,7 +450,7 @@ description = "Python parsing module" name = "pyparsing" optional = false python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*" -version = "2.4.6" +version = "2.4.7" [[package]] category = "dev" @@ -458,7 +458,7 @@ description = "pytest: simple powerful testing with Python" name = "pytest" optional = false python-versions = ">=3.5" -version = "5.4.1" +version = "5.4.3" [package.dependencies] atomicwrites = ">=1.0" @@ -474,6 +474,10 @@ wcwidth = "*" python = "<3.8" version = ">=0.12" +[package.dependencies.pathlib2] +python = "<3.6" +version = ">=2.2.0" + [package.extras] checkqa-mypy = ["mypy (v0.761)"] testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "requests", "xmlschema"] @@ -483,15 +487,28 @@ category = "dev" description = "Pytest plugin for measuring coverage." name = "pytest-cov" optional = false -python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "2.8.1" +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +version = "2.9.0" [package.dependencies] coverage = ">=4.4" pytest = ">=3.6" [package.extras] -testing = ["fields", "hunter", "process-tests (2.0.2)", "six", "virtualenv"] +testing = ["fields", "hunter", "process-tests (2.0.2)", "six", "pytest-xdist", "virtualenv"] + +[[package]] +category = "dev" +description = "pytest-sugar is a plugin for pytest that changes the default look and feel of pytest (e.g. progressbar, show tests that fail instantly)." +name = "pytest-sugar" +optional = false +python-versions = "*" +version = "0.9.3" + +[package.dependencies] +packaging = ">=14.1" +pytest = ">=2.9" +termcolor = ">=1.1.0" [[package]] category = "dev" @@ -499,7 +516,7 @@ description = "World timezone definitions, modern and historical" name = "pytz" optional = false python-versions = "*" -version = "2019.3" +version = "2020.1" [[package]] category = "main" @@ -512,10 +529,11 @@ version = "5.3.1" [[package]] category = "dev" description = "Alternative regular expression module, to replace re." +marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "regex" optional = false python-versions = "*" -version = "2020.2.20" +version = "2020.6.8" [[package]] category = "dev" @@ -538,10 +556,11 @@ socks = ["PySocks (>=1.5.6,<1.5.7 || >1.5.7)", "win-inet-pton"] [[package]] category = "dev" description = "Statically populate the `known_third_party` `isort` setting." +marker = "python_version >= \"3.6.1\" and python_version < \"4.0.0\"" name = "seed-isort-config" optional = false -python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" -version = "1.9.4" +python-versions = ">=3.6.1" +version = "2.2.0" [package.dependencies] "aspy.refactor-imports" = "*" @@ -552,7 +571,7 @@ description = "Python 2 and 3 compatibility utilities" name = "six" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*" -version = "1.14.0" +version = "1.15.0" [[package]] category = "dev" @@ -678,6 +697,7 @@ test = ["pytest"] [[package]] category = "dev" description = "tasks runner for python projects" +marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "taskipy" optional = false python-versions = ">=3.6,<4.0" @@ -686,17 +706,26 @@ version = "1.2.1" [package.dependencies] toml = ">=0.10.0,<0.11.0" +[[package]] +category = "dev" +description = "ANSII Color formatting for output in terminal." +name = "termcolor" +optional = false +python-versions = "*" +version = "1.1.0" + [[package]] category = "main" description = "Python Library for Tom's Obvious, Minimal Language" name = "toml" optional = false python-versions = "*" -version = "0.10.0" +version = "0.10.1" [[package]] category = "dev" description = "a fork of Python 2 and 3 ast modules with type comment support" +marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "typed-ast" optional = false python-versions = "*" @@ -708,11 +737,11 @@ description = "HTTP library with thread-safe connection pooling, file post, and name = "urllib3" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4" -version = "1.25.8" +version = "1.25.9" [package.extras] brotli = ["brotlipy (>=0.6.0)"] -secure = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "certifi", "ipaddress"] +secure = ["certifi", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "pyOpenSSL (>=0.14)", "ipaddress"] socks = ["PySocks (>=1.5.6,<1.5.7 || >1.5.7,<2.0)"] [[package]] @@ -721,33 +750,19 @@ description = "Virtual Python Environment builder" name = "virtualenv" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" -version = "20.0.15" - -[package.dependencies] -appdirs = ">=1.4.3,<2" -distlib = ">=0.3.0,<1" -filelock = ">=3.0.0,<4" -six = ">=1.9.0,<2" - -[package.dependencies.importlib-metadata] -python = "<3.8" -version = ">=0.12,<2" - -[package.dependencies.importlib-resources] -python = "<3.7" -version = ">=1.0,<2" +version = "16.7.10" [package.extras] -docs = ["sphinx (>=2.0.0,<3)", "sphinx-argparse (>=0.2.5,<1)", "sphinx-rtd-theme (>=0.4.3,<1)", "towncrier (>=19.9.0rc1)", "proselint (>=0.10.2,<1)"] -testing = ["pytest (>=4.0.0,<6)", "coverage (>=4.5.1,<6)", "pytest-mock (>=2.0.0,<3)", "pytest-env (>=0.6.2,<1)", "pytest-timeout (>=1.3.4,<2)", "packaging (>=20.0)", "xonsh (>=0.9.13,<1)"] +docs = ["sphinx (>=1.8.0,<2)", "towncrier (>=18.5.0)", "sphinx-rtd-theme (>=0.4.2,<1)"] +testing = ["pytest (>=4.0.0,<5)", "coverage (>=4.5.0,<5)", "pytest-timeout (>=1.3.0,<2)", "six (>=1.10.0,<2)", "pytest-xdist", "pytest-localserver", "pypiserver", "mock", "xonsh"] [[package]] category = "dev" -description = "Measures number of Terminal column cells of wide-character codes" +description = "Measures the displayed width of unicode strings in a terminal" name = "wcwidth" optional = false python-versions = "*" -version = "0.1.9" +version = "0.2.4" [[package]] category = "dev" @@ -755,20 +770,20 @@ description = "Backport of pathlib-compatible object wrapper for zip files" marker = "python_version < \"3.8\"" name = "zipp" optional = false -python-versions = ">=3.6" -version = "3.1.0" +python-versions = ">=2.7" +version = "1.2.0" [package.extras] docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"] -testing = ["jaraco.itertools", "func-timeout"] +testing = ["pathlib2", "unittest2", "jaraco.itertools", "func-timeout"] [extras] toml = ["toml"] yaml = ["pyyaml"] [metadata] -content-hash = "08e43891b71c2bd97878564d8084ce7950165cfcccea39d0ad362363de3055c3" -python-versions = "^3.6" +content-hash = "cbcbacf7c64fd8aec4372a6979e3ef11a81c049e7baf24b1b396023209b960cd" +python-versions = "^3.5" [metadata.files] alabaster = [ @@ -776,20 +791,20 @@ alabaster = [ {file = "alabaster-0.7.12.tar.gz", hash = "sha256:a661d72d58e6ea8a57f7a86e37d86716863ee5e92788398526d58b26a4e4dc02"}, ] appdirs = [ - {file = "appdirs-1.4.3-py2.py3-none-any.whl", hash = "sha256:d8b24664561d0d34ddfaec54636d502d7cea6e29c3eaf68f3df6180863e2166e"}, - {file = "appdirs-1.4.3.tar.gz", hash = "sha256:9e5896d1372858f8dd3344faf4e5014d21849c756c8d5701f78f8a103b372d92"}, + {file = "appdirs-1.4.4-py2.py3-none-any.whl", hash = "sha256:a841dacd6b99318a741b166adb07e19ee71a274450e68237b4650ca1055ab128"}, + {file = "appdirs-1.4.4.tar.gz", hash = "sha256:7d5d0167b2b1ba821647616af46a749d1c653740dd0d2415100fe26e27afdf41"}, ] "aspy.refactor-imports" = [ - {file = "aspy.refactor_imports-1.1.0-py2.py3-none-any.whl", hash = "sha256:6d465c18cadad7e5a87810ecf8e516cb6f78e91871f4f55d0f228c6c376bd16a"}, - {file = "aspy.refactor_imports-1.1.0.tar.gz", hash = "sha256:88418d9bd1a0d50b9d3836504469dce6f26ee8b2d78bd58b805d73dd8b381d55"}, + {file = "aspy.refactor_imports-2.1.1-py2.py3-none-any.whl", hash = "sha256:9df76bf19ef81620068b785a386740ab3c8939fcbdcebf20c4a4e0057230d782"}, + {file = "aspy.refactor_imports-2.1.1.tar.gz", hash = "sha256:eec8d1a73bedf64ffb8b589ad919a030c1fb14acf7d1ce0ab192f6eedae895c5"}, ] "aspy.yaml" = [ {file = "aspy.yaml-1.3.0-py2.py3-none-any.whl", hash = "sha256:463372c043f70160a9ec950c3f1e4c3a82db5fca01d334b6bc89c7164d744bdc"}, {file = "aspy.yaml-1.3.0.tar.gz", hash = "sha256:e7c742382eff2caed61f87a39d13f99109088e5e93f04d76eb8d4b28aa143f45"}, ] atomicwrites = [ - {file = "atomicwrites-1.3.0-py2.py3-none-any.whl", hash = "sha256:03472c30eb2c5d1ba9227e4c2ca66ab8287fbfbbda3888aa93dc2e28fc6811b4"}, - {file = "atomicwrites-1.3.0.tar.gz", hash = "sha256:75a9445bac02d8d058d5e1fe689654ba5a6556a1dfd8ce6ec55a0ed79866cfa6"}, + {file = "atomicwrites-1.4.0-py2.py3-none-any.whl", hash = "sha256:6d1784dea7c0c8d4a5172b6c620f40b6e4cbfdf96d783691f2e1302a7b88e197"}, + {file = "atomicwrites-1.4.0.tar.gz", hash = "sha256:ae70396ad1a434f9c7046fd2dd196fc04b12f9e91ffb859164193be8b6168a7a"}, ] attrs = [ {file = "attrs-19.3.0-py2.py3-none-any.whl", hash = "sha256:08a96c641c3a74e44eb59afb61a24f2cb9f4d7188748e76ba4bb5edfa3cb7d1c"}, @@ -815,80 +830,69 @@ cached-property = [ {file = "cached_property-1.5.1-py2.py3-none-any.whl", hash = "sha256:3a026f1a54135677e7da5ce819b0c690f156f37976f3e30c5430740725203d7f"}, ] certifi = [ - {file = "certifi-2019.11.28-py2.py3-none-any.whl", hash = "sha256:017c25db2a153ce562900032d5bc68e9f191e44e9a0f762f373977de9df1fbb3"}, - {file = "certifi-2019.11.28.tar.gz", hash = "sha256:25b64c7da4cd7479594d035c08c2d809eb4aab3a26e5a990ea98cc450c320f1f"}, + {file = "certifi-2020.4.5.2-py2.py3-none-any.whl", hash = "sha256:9cd41137dc19af6a5e03b630eefe7d1f458d964d406342dd3edf625839b944cc"}, + {file = "certifi-2020.4.5.2.tar.gz", hash = "sha256:5ad7e9a056d25ffa5082862e36f119f7f7cec6457fa07ee2f8c339814b80c9b1"}, ] cfgv = [ - {file = "cfgv-3.0.0-py2.py3-none-any.whl", hash = "sha256:f22b426ed59cd2ab2b54ff96608d846c33dfb8766a67f0b4a6ce130ce244414f"}, - {file = "cfgv-3.0.0.tar.gz", hash = "sha256:04b093b14ddf9fd4d17c53ebfd55582d27b76ed30050193c14e560770c5360eb"}, + {file = "cfgv-2.0.1-py2.py3-none-any.whl", hash = "sha256:fbd93c9ab0a523bf7daec408f3be2ed99a980e20b2d19b50fc184ca6b820d289"}, + {file = "cfgv-2.0.1.tar.gz", hash = "sha256:edb387943b665bf9c434f717bf630fa78aecd53d5900d2e05da6ad6048553144"}, ] chardet = [ {file = "chardet-3.0.4-py2.py3-none-any.whl", hash = "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"}, {file = "chardet-3.0.4.tar.gz", hash = "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae"}, ] click = [ - {file = "click-7.1.1-py2.py3-none-any.whl", hash = "sha256:e345d143d80bf5ee7534056164e5e112ea5e22716bbb1ce727941f4c8b471b9a"}, - {file = "click-7.1.1.tar.gz", hash = "sha256:8a18b4ea89d8820c5d0c7da8a64b2c324b4dabb695804dbfea19b9be9d88c0cc"}, + {file = "click-7.1.2-py2.py3-none-any.whl", hash = "sha256:dacca89f4bfadd5de3d7489b7c8a566eee0d3676333fbb50030263894c38c0dc"}, + {file = "click-7.1.2.tar.gz", hash = "sha256:d2b5255c7c6349bc1bd1e59e08cd12acbbd63ce649f2588755783aa94dfb6b1a"}, ] colorama = [ {file = "colorama-0.4.3-py2.py3-none-any.whl", hash = "sha256:7d73d2a99753107a36ac6b455ee49046802e59d9d076ef8e47b61499fa29afff"}, {file = "colorama-0.4.3.tar.gz", hash = "sha256:e96da0d330793e2cb9485e9ddfd918d456036c7149416295932478192f4436a1"}, ] coverage = [ - {file = "coverage-5.0.4-cp27-cp27m-macosx_10_12_x86_64.whl", hash = "sha256:8a620767b8209f3446197c0e29ba895d75a1e272a36af0786ec70fe7834e4307"}, - {file = "coverage-5.0.4-cp27-cp27m-macosx_10_13_intel.whl", hash = "sha256:73aa6e86034dad9f00f4bbf5a666a889d17d79db73bc5af04abd6c20a014d9c8"}, - {file = "coverage-5.0.4-cp27-cp27m-manylinux1_i686.whl", hash = "sha256:408ce64078398b2ee2ec08199ea3fcf382828d2f8a19c5a5ba2946fe5ddc6c31"}, - {file = "coverage-5.0.4-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:cda33311cb9fb9323958a69499a667bd728a39a7aa4718d7622597a44c4f1441"}, - {file = "coverage-5.0.4-cp27-cp27m-win32.whl", hash = "sha256:5f587dfd83cb669933186661a351ad6fc7166273bc3e3a1531ec5c783d997aac"}, - {file = "coverage-5.0.4-cp27-cp27m-win_amd64.whl", hash = "sha256:9fad78c13e71546a76c2f8789623eec8e499f8d2d799f4b4547162ce0a4df435"}, - {file = "coverage-5.0.4-cp27-cp27mu-manylinux1_i686.whl", hash = "sha256:2e08c32cbede4a29e2a701822291ae2bc9b5220a971bba9d1e7615312efd3037"}, - {file = "coverage-5.0.4-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:922fb9ef2c67c3ab20e22948dcfd783397e4c043a5c5fa5ff5e9df5529074b0a"}, - {file = "coverage-5.0.4-cp35-cp35m-macosx_10_12_x86_64.whl", hash = "sha256:c3fc325ce4cbf902d05a80daa47b645d07e796a80682c1c5800d6ac5045193e5"}, - {file = "coverage-5.0.4-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:046a1a742e66d065d16fb564a26c2a15867f17695e7f3d358d7b1ad8a61bca30"}, - {file = "coverage-5.0.4-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:6ad6ca45e9e92c05295f638e78cd42bfaaf8ee07878c9ed73e93190b26c125f7"}, - {file = "coverage-5.0.4-cp35-cp35m-win32.whl", hash = "sha256:eda55e6e9ea258f5e4add23bcf33dc53b2c319e70806e180aecbff8d90ea24de"}, - {file = "coverage-5.0.4-cp35-cp35m-win_amd64.whl", hash = "sha256:4a8a259bf990044351baf69d3b23e575699dd60b18460c71e81dc565f5819ac1"}, - {file = "coverage-5.0.4-cp36-cp36m-macosx_10_13_x86_64.whl", hash = "sha256:f372cdbb240e09ee855735b9d85e7f50730dcfb6296b74b95a3e5dea0615c4c1"}, - {file = "coverage-5.0.4-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:a37c6233b28e5bc340054cf6170e7090a4e85069513320275a4dc929144dccf0"}, - {file = "coverage-5.0.4-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:443be7602c790960b9514567917af538cac7807a7c0c0727c4d2bbd4014920fd"}, - {file = "coverage-5.0.4-cp36-cp36m-win32.whl", hash = "sha256:165a48268bfb5a77e2d9dbb80de7ea917332a79c7adb747bd005b3a07ff8caf0"}, - {file = "coverage-5.0.4-cp36-cp36m-win_amd64.whl", hash = "sha256:0a907199566269e1cfa304325cc3b45c72ae341fbb3253ddde19fa820ded7a8b"}, - {file = "coverage-5.0.4-cp37-cp37m-macosx_10_13_x86_64.whl", hash = "sha256:513e6526e0082c59a984448f4104c9bf346c2da9961779ede1fc458e8e8a1f78"}, - {file = "coverage-5.0.4-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:3844c3dab800ca8536f75ae89f3cf566848a3eb2af4d9f7b1103b4f4f7a5dad6"}, - {file = "coverage-5.0.4-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:641e329e7f2c01531c45c687efcec8aeca2a78a4ff26d49184dce3d53fc35014"}, - {file = "coverage-5.0.4-cp37-cp37m-win32.whl", hash = "sha256:db1d4e38c9b15be1521722e946ee24f6db95b189d1447fa9ff18dd16ba89f732"}, - {file = "coverage-5.0.4-cp37-cp37m-win_amd64.whl", hash = "sha256:62061e87071497951155cbccee487980524d7abea647a1b2a6eb6b9647df9006"}, - {file = "coverage-5.0.4-cp38-cp38-macosx_10_13_x86_64.whl", hash = "sha256:65a7e00c00472cd0f59ae09d2fb8a8aaae7f4a0cf54b2b74f3138d9f9ceb9cb2"}, - {file = "coverage-5.0.4-cp38-cp38-manylinux1_i686.whl", hash = "sha256:1f66cf263ec77af5b8fe14ef14c5e46e2eb4a795ac495ad7c03adc72ae43fafe"}, - {file = "coverage-5.0.4-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:85596aa5d9aac1bf39fe39d9fa1051b0f00823982a1de5766e35d495b4a36ca9"}, - {file = "coverage-5.0.4-cp38-cp38-win32.whl", hash = "sha256:86a0ea78fd851b313b2e712266f663e13b6bc78c2fb260b079e8b67d970474b1"}, - {file = "coverage-5.0.4-cp38-cp38-win_amd64.whl", hash = "sha256:03f630aba2b9b0d69871c2e8d23a69b7fe94a1e2f5f10df5049c0df99db639a0"}, - {file = "coverage-5.0.4-cp39-cp39-win32.whl", hash = "sha256:7c9762f80a25d8d0e4ab3cb1af5d9dffbddb3ee5d21c43e3474c84bf5ff941f7"}, - {file = "coverage-5.0.4-cp39-cp39-win_amd64.whl", hash = "sha256:4482f69e0701139d0f2c44f3c395d1d1d37abd81bfafbf9b6efbe2542679d892"}, - {file = "coverage-5.0.4.tar.gz", hash = "sha256:1b60a95fc995649464e0cd48cecc8288bac5f4198f21d04b8229dc4097d76823"}, -] -distlib = [ - {file = "distlib-0.3.0.zip", hash = "sha256:2e166e231a26b36d6dfe35a48c4464346620f8645ed0ace01ee31822b288de21"}, + {file = "coverage-5.1-cp27-cp27m-macosx_10_12_x86_64.whl", hash = "sha256:0cb4be7e784dcdc050fc58ef05b71aa8e89b7e6636b99967fadbdba694cf2b65"}, + {file = "coverage-5.1-cp27-cp27m-macosx_10_13_intel.whl", hash = "sha256:c317eaf5ff46a34305b202e73404f55f7389ef834b8dbf4da09b9b9b37f76dd2"}, + {file = "coverage-5.1-cp27-cp27m-manylinux1_i686.whl", hash = "sha256:b83835506dfc185a319031cf853fa4bb1b3974b1f913f5bb1a0f3d98bdcded04"}, + {file = "coverage-5.1-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:5f2294dbf7875b991c381e3d5af2bcc3494d836affa52b809c91697449d0eda6"}, + {file = "coverage-5.1-cp27-cp27m-win32.whl", hash = "sha256:de807ae933cfb7f0c7d9d981a053772452217df2bf38e7e6267c9cbf9545a796"}, + {file = "coverage-5.1-cp27-cp27m-win_amd64.whl", hash = "sha256:bf9cb9a9fd8891e7efd2d44deb24b86d647394b9705b744ff6f8261e6f29a730"}, + {file = "coverage-5.1-cp27-cp27mu-manylinux1_i686.whl", hash = "sha256:acf3763ed01af8410fc36afea23707d4ea58ba7e86a8ee915dfb9ceff9ef69d0"}, + {file = "coverage-5.1-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:dec5202bfe6f672d4511086e125db035a52b00f1648d6407cc8e526912c0353a"}, + {file = "coverage-5.1-cp35-cp35m-macosx_10_12_x86_64.whl", hash = "sha256:7a5bdad4edec57b5fb8dae7d3ee58622d626fd3a0be0dfceda162a7035885ecf"}, + {file = "coverage-5.1-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:1601e480b9b99697a570cea7ef749e88123c04b92d84cedaa01e117436b4a0a9"}, + {file = "coverage-5.1-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:dbe8c6ae7534b5b024296464f387d57c13caa942f6d8e6e0346f27e509f0f768"}, + {file = "coverage-5.1-cp35-cp35m-win32.whl", hash = "sha256:a027ef0492ede1e03a8054e3c37b8def89a1e3c471482e9f046906ba4f2aafd2"}, + {file = "coverage-5.1-cp35-cp35m-win_amd64.whl", hash = "sha256:0e61d9803d5851849c24f78227939c701ced6704f337cad0a91e0972c51c1ee7"}, + {file = "coverage-5.1-cp36-cp36m-macosx_10_13_x86_64.whl", hash = "sha256:2d27a3f742c98e5c6b461ee6ef7287400a1956c11421eb574d843d9ec1f772f0"}, + {file = "coverage-5.1-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:66460ab1599d3cf894bb6baee8c684788819b71a5dc1e8fa2ecc152e5d752019"}, + {file = "coverage-5.1-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:5c542d1e62eece33c306d66fe0a5c4f7f7b3c08fecc46ead86d7916684b36d6c"}, + {file = "coverage-5.1-cp36-cp36m-win32.whl", hash = "sha256:2742c7515b9eb368718cd091bad1a1b44135cc72468c731302b3d641895b83d1"}, + {file = "coverage-5.1-cp36-cp36m-win_amd64.whl", hash = "sha256:dead2ddede4c7ba6cb3a721870f5141c97dc7d85a079edb4bd8d88c3ad5b20c7"}, + {file = "coverage-5.1-cp37-cp37m-macosx_10_13_x86_64.whl", hash = "sha256:01333e1bd22c59713ba8a79f088b3955946e293114479bbfc2e37d522be03355"}, + {file = "coverage-5.1-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:e1ea316102ea1e1770724db01998d1603ed921c54a86a2efcb03428d5417e489"}, + {file = "coverage-5.1-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:adeb4c5b608574a3d647011af36f7586811a2c1197c861aedb548dd2453b41cd"}, + {file = "coverage-5.1-cp37-cp37m-win32.whl", hash = "sha256:782caea581a6e9ff75eccda79287daefd1d2631cc09d642b6ee2d6da21fc0a4e"}, + {file = "coverage-5.1-cp37-cp37m-win_amd64.whl", hash = "sha256:00f1d23f4336efc3b311ed0d807feb45098fc86dee1ca13b3d6768cdab187c8a"}, + {file = "coverage-5.1-cp38-cp38-macosx_10_13_x86_64.whl", hash = "sha256:402e1744733df483b93abbf209283898e9f0d67470707e3c7516d84f48524f55"}, + {file = "coverage-5.1-cp38-cp38-manylinux1_i686.whl", hash = "sha256:a3f3654d5734a3ece152636aad89f58afc9213c6520062db3978239db122f03c"}, + {file = "coverage-5.1-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:6402bd2fdedabbdb63a316308142597534ea8e1895f4e7d8bf7476c5e8751fef"}, + {file = "coverage-5.1-cp38-cp38-win32.whl", hash = "sha256:8fa0cbc7ecad630e5b0f4f35b0f6ad419246b02bc750de7ac66db92667996d24"}, + {file = "coverage-5.1-cp38-cp38-win_amd64.whl", hash = "sha256:79a3cfd6346ce6c13145731d39db47b7a7b859c0272f02cdb89a3bdcbae233a0"}, + {file = "coverage-5.1-cp39-cp39-win32.whl", hash = "sha256:a82b92b04a23d3c8a581fc049228bafde988abacba397d57ce95fe95e0338ab4"}, + {file = "coverage-5.1-cp39-cp39-win_amd64.whl", hash = "sha256:bb28a7245de68bf29f6fb199545d072d1036a1917dca17a1e75bbb919e14ee8e"}, + {file = "coverage-5.1.tar.gz", hash = "sha256:f90bfc4ad18450c80b024036eaf91e4a246ae287701aaa88eaebebf150868052"}, ] docutils = [ {file = "docutils-0.16-py2.py3-none-any.whl", hash = "sha256:0c5b78adfbf7762415433f5515cd5c9e762339e23369dbe8000d84a4bf4ab3af"}, {file = "docutils-0.16.tar.gz", hash = "sha256:c2de3a60e9e7d07be26b7f2b00ca0309c207e06c100f9cc2a94931fc75a478fc"}, ] -entrypoints = [ - {file = "entrypoints-0.3-py2.py3-none-any.whl", hash = "sha256:589f874b313739ad35be6e0cd7efde2a4e9b6fea91edcc34e58ecbb8dbe56d19"}, - {file = "entrypoints-0.3.tar.gz", hash = "sha256:c70dd71abe5a8c85e55e12c19bd91ccfeec11a6e99044204511f9ed547d48451"}, -] -filelock = [ - {file = "filelock-3.0.12-py3-none-any.whl", hash = "sha256:929b7d63ec5b7d6b71b0fa5ac14e030b3f70b75747cef1b10da9b879fef15836"}, - {file = "filelock-3.0.12.tar.gz", hash = "sha256:18d82244ee114f543149c66a6e0c14e9c4f8a1044b5cdaadd0f82159d6a6ff59"}, -] flake8 = [ - {file = "flake8-3.7.9-py2.py3-none-any.whl", hash = "sha256:49356e766643ad15072a789a20915d3c91dc89fd313ccd71802303fd67e4deca"}, - {file = "flake8-3.7.9.tar.gz", hash = "sha256:45681a117ecc81e870cbf1262835ae4af5e7a8b08e40b944a8a6e6b895914cfb"}, + {file = "flake8-3.8.3-py2.py3-none-any.whl", hash = "sha256:15e351d19611c887e482fb960eae4d44845013cc142d42896e9862f775d8cf5c"}, + {file = "flake8-3.8.3.tar.gz", hash = "sha256:f04b9fcbac03b0a3e58c0ab3a0ecc462e023a9faf046d57794184028123aa208"}, ] identify = [ - {file = "identify-1.4.13-py2.py3-none-any.whl", hash = "sha256:a7577a1f55cee1d21953a5cf11a3c839ab87f5ef909a4cba6cf52ed72b4c6059"}, - {file = "identify-1.4.13.tar.gz", hash = "sha256:ab246293e6585a1c6361a505b68d5b501a0409310932b7de2c2ead667b564d89"}, + {file = "identify-1.4.19-py2.py3-none-any.whl", hash = "sha256:781fd3401f5d2b17b22a8b18b493a48d5d948e3330634e82742e23f9c20234ef"}, + {file = "identify-1.4.19.tar.gz", hash = "sha256:249ebc7e2066d6393d27c1b1be3b70433f824a120b1d8274d362f1eb419e3b52"}, ] idna = [ {file = "idna-2.9-py2.py3-none-any.whl", hash = "sha256:a068a21ceac8a4d63dbfd964670474107f541babbd2250d61922f029858365fa"}, @@ -899,20 +903,20 @@ imagesize = [ {file = "imagesize-1.2.0.tar.gz", hash = "sha256:b1f6b5a4eab1f73479a50fb79fcf729514a900c341d8503d62a62dbc4127a2b1"}, ] importlib-metadata = [ - {file = "importlib_metadata-1.6.0-py2.py3-none-any.whl", hash = "sha256:2a688cbaa90e0cc587f1df48bdc97a6eadccdcd9c35fb3f976a09e3b5016d90f"}, - {file = "importlib_metadata-1.6.0.tar.gz", hash = "sha256:34513a8a0c4962bc66d35b359558fd8a5e10cd472d37aec5f66858addef32c1e"}, + {file = "importlib_metadata-1.6.1-py2.py3-none-any.whl", hash = "sha256:15ec6c0fd909e893e3a08b3a7c76ecb149122fb14b7efe1199ddd4c7c57ea958"}, + {file = "importlib_metadata-1.6.1.tar.gz", hash = "sha256:0505dd08068cfec00f53a74a0ad927676d7757da81b7436a6eefe4c7cf75c545"}, ] importlib-resources = [ - {file = "importlib_resources-1.4.0-py2.py3-none-any.whl", hash = "sha256:dd98ceeef3f5ad2ef4cc287b8586da4ebad15877f351e9688987ad663a0a29b8"}, - {file = "importlib_resources-1.4.0.tar.gz", hash = "sha256:4019b6a9082d8ada9def02bece4a76b131518866790d58fdda0b5f8c603b36c2"}, + {file = "importlib_resources-2.0.0-py2.py3-none-any.whl", hash = "sha256:a86462cf34a6d391d1d5d598a5e2f5aac9fc00b265d40542e1196328f015d1f6"}, + {file = "importlib_resources-2.0.0.tar.gz", hash = "sha256:7f6aae2ed252ba10f5d1af5676b0e35f3b3eb7d3cc103b8365cc92aec0c79258"}, ] isort = [ {file = "isort-4.3.21-py2.py3-none-any.whl", hash = "sha256:6e811fcb295968434526407adb8796944f1988c5b65e8139058f2014cbe100fd"}, {file = "isort-4.3.21.tar.gz", hash = "sha256:54da7e92468955c4fceacd0c86bd0ec997b0e1ee80d97f67c35a78b719dccab1"}, ] jinja2 = [ - {file = "Jinja2-2.11.1-py2.py3-none-any.whl", hash = "sha256:b0eaf100007721b5c16c1fc1eecb87409464edc10469ddc9a22a27a99123be49"}, - {file = "Jinja2-2.11.1.tar.gz", hash = "sha256:93187ffbc7808079673ef52771baa950426fd664d3aad1d0fa3e95644360e250"}, + {file = "Jinja2-2.11.2-py2.py3-none-any.whl", hash = "sha256:f0a4641d3cf955324a89c04f3d94663aa4d638abe8f733ecd3582848e1c37035"}, + {file = "Jinja2-2.11.2.tar.gz", hash = "sha256:89aab215427ef59c34ad58735269eb58b1a5808103067f7bb9d5836c651b3bb0"}, ] markupsafe = [ {file = "MarkupSafe-1.1.1-cp27-cp27m-macosx_10_6_intel.whl", hash = "sha256:09027a7803a62ca78792ad89403b1b7a73a01c8cb65909cd876f7fcebd79b161"}, @@ -954,22 +958,26 @@ mccabe = [ {file = "mccabe-0.6.1.tar.gz", hash = "sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f"}, ] more-itertools = [ - {file = "more-itertools-8.2.0.tar.gz", hash = "sha256:b1ddb932186d8a6ac451e1d95844b382f55e12686d51ca0c68b6f61f2ab7a507"}, - {file = "more_itertools-8.2.0-py3-none-any.whl", hash = "sha256:5dd8bcf33e5f9513ffa06d5ad33d78f31e1931ac9a18f33d37e77a180d393a7c"}, + {file = "more-itertools-8.3.0.tar.gz", hash = "sha256:558bb897a2232f5e4f8e2399089e35aecb746e1f9191b6584a151647e89267be"}, + {file = "more_itertools-8.3.0-py3-none-any.whl", hash = "sha256:7818f596b1e87be009031c7653d01acc46ed422e6656b394b0f765ce66ed4982"}, ] nested-lookup = [ {file = "nested-lookup-0.2.21.tar.gz", hash = "sha256:23789e328bd1d0b3f9db93cf51b7103a978dd0d8a834770d2c19b365e934ab96"}, ] nodeenv = [ - {file = "nodeenv-1.3.5-py2.py3-none-any.whl", hash = "sha256:5b2438f2e42af54ca968dd1b374d14a1194848955187b0e5e4be1f73813a5212"}, + {file = "nodeenv-1.4.0-py2.py3-none-any.whl", hash = "sha256:4b0b77afa3ba9b54f4b6396e60b0c83f59eaeb2d63dc3cc7a70f7f4af96c82bc"}, ] packaging = [ - {file = "packaging-20.3-py2.py3-none-any.whl", hash = "sha256:82f77b9bee21c1bafbf35a84905d604d5d1223801d639cf3ed140bd651c08752"}, - {file = "packaging-20.3.tar.gz", hash = "sha256:3c292b474fda1671ec57d46d739d072bfd495a4f51ad01a055121d81e952b7a3"}, + {file = "packaging-20.4-py2.py3-none-any.whl", hash = "sha256:998416ba6962ae7fbd6596850b80e17859a5753ba17c32284f67bfff33784181"}, + {file = "packaging-20.4.tar.gz", hash = "sha256:4357f74f47b9c12db93624a82154e9b120fa8293699949152b22065d556079f8"}, +] +pathlib2 = [ + {file = "pathlib2-2.3.5-py2.py3-none-any.whl", hash = "sha256:0ec8205a157c80d7acc301c0b18fbd5d44fe655968f5d947b6ecef5290fc35db"}, + {file = "pathlib2-2.3.5.tar.gz", hash = "sha256:6cd9a47b597b37cc57de1c05e56fb1a1c9cc9fab04fe78c29acd090418529868"}, ] pathspec = [ - {file = "pathspec-0.7.0-py2.py3-none-any.whl", hash = "sha256:163b0632d4e31cef212976cf57b43d9fd6b0bac6e67c26015d611a647d5e7424"}, - {file = "pathspec-0.7.0.tar.gz", hash = "sha256:562aa70af2e0d434367d9790ad37aed893de47f1693e4201fd1d3dca15d19b96"}, + {file = "pathspec-0.8.0-py2.py3-none-any.whl", hash = "sha256:7d91249d21749788d07a2d0f94147accd8f845507400749ea19c1ec9054a12b0"}, + {file = "pathspec-0.8.0.tar.gz", hash = "sha256:da45173eb3a6f2a5a487efba21f050af2b41948be6ab52b6a1e3ff22bb8b7061"}, ] pluggy = [ {file = "pluggy-0.13.1-py2.py3-none-any.whl", hash = "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"}, @@ -984,32 +992,35 @@ py = [ {file = "py-1.8.1.tar.gz", hash = "sha256:5e27081401262157467ad6e7f851b7aa402c5852dbcb3dae06768434de5752aa"}, ] pycodestyle = [ - {file = "pycodestyle-2.5.0-py2.py3-none-any.whl", hash = "sha256:95a2219d12372f05704562a14ec30bc76b05a5b297b21a5dfe3f6fac3491ae56"}, - {file = "pycodestyle-2.5.0.tar.gz", hash = "sha256:e40a936c9a450ad81df37f549d676d127b1b66000a6c500caa2b085bc0ca976c"}, + {file = "pycodestyle-2.6.0-py2.py3-none-any.whl", hash = "sha256:2295e7b2f6b5bd100585ebcb1f616591b652db8a741695b3d8f5d28bdc934367"}, + {file = "pycodestyle-2.6.0.tar.gz", hash = "sha256:c58a7d2815e0e8d7972bf1803331fb0152f867bd89adf8a01dfd55085434192e"}, ] pyflakes = [ - {file = "pyflakes-2.1.1-py2.py3-none-any.whl", hash = "sha256:17dbeb2e3f4d772725c777fabc446d5634d1038f234e77343108ce445ea69ce0"}, - {file = "pyflakes-2.1.1.tar.gz", hash = "sha256:d976835886f8c5b31d47970ed689944a0262b5f3afa00a5a7b4dc81e5449f8a2"}, + {file = "pyflakes-2.2.0-py2.py3-none-any.whl", hash = "sha256:0d94e0e05a19e57a99444b6ddcf9a6eb2e5c68d3ca1e98e90707af8152c90a92"}, + {file = "pyflakes-2.2.0.tar.gz", hash = "sha256:35b2d75ee967ea93b55750aa9edbbf72813e06a66ba54438df2cfac9e3c27fc8"}, ] pygments = [ {file = "Pygments-2.6.1-py3-none-any.whl", hash = "sha256:ff7a40b4860b727ab48fad6360eb351cc1b33cbf9b15a0f689ca5353e9463324"}, {file = "Pygments-2.6.1.tar.gz", hash = "sha256:647344a061c249a3b74e230c739f434d7ea4d8b1d5f3721bc0f3558049b38f44"}, ] pyparsing = [ - {file = "pyparsing-2.4.6-py2.py3-none-any.whl", hash = "sha256:c342dccb5250c08d45fd6f8b4a559613ca603b57498511740e65cd11a2e7dcec"}, - {file = "pyparsing-2.4.6.tar.gz", hash = "sha256:4c830582a84fb022400b85429791bc551f1f4871c33f23e44f353119e92f969f"}, + {file = "pyparsing-2.4.7-py2.py3-none-any.whl", hash = "sha256:ef9d7589ef3c200abe66653d3f1ab1033c3c419ae9b9bdb1240a85b024efc88b"}, + {file = "pyparsing-2.4.7.tar.gz", hash = "sha256:c203ec8783bf771a155b207279b9bccb8dea02d8f0c9e5f8ead507bc3246ecc1"}, ] pytest = [ - {file = "pytest-5.4.1-py3-none-any.whl", hash = "sha256:0e5b30f5cb04e887b91b1ee519fa3d89049595f428c1db76e73bd7f17b09b172"}, - {file = "pytest-5.4.1.tar.gz", hash = "sha256:84dde37075b8805f3d1f392cc47e38a0e59518fb46a431cfdaf7cf1ce805f970"}, + {file = "pytest-5.4.3-py3-none-any.whl", hash = "sha256:5c0db86b698e8f170ba4582a492248919255fcd4c79b1ee64ace34301fb589a1"}, + {file = "pytest-5.4.3.tar.gz", hash = "sha256:7979331bfcba207414f5e1263b5a0f8f521d0f457318836a7355531ed1a4c7d8"}, ] pytest-cov = [ - {file = "pytest-cov-2.8.1.tar.gz", hash = "sha256:cc6742d8bac45070217169f5f72ceee1e0e55b0221f54bcf24845972d3a47f2b"}, - {file = "pytest_cov-2.8.1-py2.py3-none-any.whl", hash = "sha256:cdbdef4f870408ebdbfeb44e63e07eb18bb4619fae852f6e760645fa36172626"}, + {file = "pytest-cov-2.9.0.tar.gz", hash = "sha256:b6a814b8ed6247bd81ff47f038511b57fe1ce7f4cc25b9106f1a4b106f1d9322"}, + {file = "pytest_cov-2.9.0-py2.py3-none-any.whl", hash = "sha256:c87dfd8465d865655a8213859f1b4749b43448b5fae465cb981e16d52a811424"}, +] +pytest-sugar = [ + {file = "pytest-sugar-0.9.3.tar.gz", hash = "sha256:1630b5b7ea3624919b73fde37cffb87965c5087a4afab8a43074ff44e0d810c4"}, ] pytz = [ - {file = "pytz-2019.3-py2.py3-none-any.whl", hash = "sha256:1c557d7d0e871de1f5ccd5833f60fb2550652da6be2693c1e02300743d21500d"}, - {file = "pytz-2019.3.tar.gz", hash = "sha256:b02c06db6cf09c12dd25137e563b31700d3b80fcc4ad23abb7a315f2789819be"}, + {file = "pytz-2020.1-py2.py3-none-any.whl", hash = "sha256:a494d53b6d39c3c6e44c3bec237336e14305e4f29bbf800b599253057fbb79ed"}, + {file = "pytz-2020.1.tar.gz", hash = "sha256:c35965d010ce31b23eeb663ed3cc8c906275d6be1a34393a1d73a41febf4a048"}, ] pyyaml = [ {file = "PyYAML-5.3.1-cp27-cp27m-win32.whl", hash = "sha256:74809a57b329d6cc0fdccee6318f44b9b8649961fa73144a98735b0aaf029f1f"}, @@ -1025,39 +1036,39 @@ pyyaml = [ {file = "PyYAML-5.3.1.tar.gz", hash = "sha256:b8eac752c5e14d3eca0e6dd9199cd627518cb5ec06add0de9d32baeee6fe645d"}, ] regex = [ - {file = "regex-2020.2.20-cp27-cp27m-win32.whl", hash = "sha256:99272d6b6a68c7ae4391908fc15f6b8c9a6c345a46b632d7fdb7ef6c883a2bbb"}, - {file = "regex-2020.2.20-cp27-cp27m-win_amd64.whl", hash = "sha256:974535648f31c2b712a6b2595969f8ab370834080e00ab24e5dbb9d19b8bfb74"}, - {file = "regex-2020.2.20-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:5de40649d4f88a15c9489ed37f88f053c15400257eeb18425ac7ed0a4e119400"}, - {file = "regex-2020.2.20-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:82469a0c1330a4beb3d42568f82dffa32226ced006e0b063719468dcd40ffdf0"}, - {file = "regex-2020.2.20-cp36-cp36m-manylinux2010_i686.whl", hash = "sha256:d58a4fa7910102500722defbde6e2816b0372a4fcc85c7e239323767c74f5cbc"}, - {file = "regex-2020.2.20-cp36-cp36m-manylinux2010_x86_64.whl", hash = "sha256:f1ac2dc65105a53c1c2d72b1d3e98c2464a133b4067a51a3d2477b28449709a0"}, - {file = "regex-2020.2.20-cp36-cp36m-win32.whl", hash = "sha256:8c2b7fa4d72781577ac45ab658da44c7518e6d96e2a50d04ecb0fd8f28b21d69"}, - {file = "regex-2020.2.20-cp36-cp36m-win_amd64.whl", hash = "sha256:269f0c5ff23639316b29f31df199f401e4cb87529eafff0c76828071635d417b"}, - {file = "regex-2020.2.20-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:bed7986547ce54d230fd8721aba6fd19459cdc6d315497b98686d0416efaff4e"}, - {file = "regex-2020.2.20-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:046e83a8b160aff37e7034139a336b660b01dbfe58706f9d73f5cdc6b3460242"}, - {file = "regex-2020.2.20-cp37-cp37m-manylinux2010_i686.whl", hash = "sha256:b33ebcd0222c1d77e61dbcd04a9fd139359bded86803063d3d2d197b796c63ce"}, - {file = "regex-2020.2.20-cp37-cp37m-manylinux2010_x86_64.whl", hash = "sha256:bba52d72e16a554d1894a0cc74041da50eea99a8483e591a9edf1025a66843ab"}, - {file = "regex-2020.2.20-cp37-cp37m-win32.whl", hash = "sha256:01b2d70cbaed11f72e57c1cfbaca71b02e3b98f739ce33f5f26f71859ad90431"}, - {file = "regex-2020.2.20-cp37-cp37m-win_amd64.whl", hash = "sha256:113309e819634f499d0006f6200700c8209a2a8bf6bd1bdc863a4d9d6776a5d1"}, - {file = "regex-2020.2.20-cp38-cp38-manylinux1_i686.whl", hash = "sha256:25f4ce26b68425b80a233ce7b6218743c71cf7297dbe02feab1d711a2bf90045"}, - {file = "regex-2020.2.20-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:9b64a4cc825ec4df262050c17e18f60252cdd94742b4ba1286bcfe481f1c0f26"}, - {file = "regex-2020.2.20-cp38-cp38-manylinux2010_i686.whl", hash = "sha256:9ff16d994309b26a1cdf666a6309c1ef51ad4f72f99d3392bcd7b7139577a1f2"}, - {file = "regex-2020.2.20-cp38-cp38-manylinux2010_x86_64.whl", hash = "sha256:c7f58a0e0e13fb44623b65b01052dae8e820ed9b8b654bb6296bc9c41f571b70"}, - {file = "regex-2020.2.20-cp38-cp38-win32.whl", hash = "sha256:200539b5124bc4721247a823a47d116a7a23e62cc6695744e3eb5454a8888e6d"}, - {file = "regex-2020.2.20-cp38-cp38-win_amd64.whl", hash = "sha256:7f78f963e62a61e294adb6ff5db901b629ef78cb2a1cfce3cf4eeba80c1c67aa"}, - {file = "regex-2020.2.20.tar.gz", hash = "sha256:9e9624440d754733eddbcd4614378c18713d2d9d0dc647cf9c72f64e39671be5"}, + {file = "regex-2020.6.8-cp27-cp27m-win32.whl", hash = "sha256:fbff901c54c22425a5b809b914a3bfaf4b9570eee0e5ce8186ac71eb2025191c"}, + {file = "regex-2020.6.8-cp27-cp27m-win_amd64.whl", hash = "sha256:112e34adf95e45158c597feea65d06a8124898bdeac975c9087fe71b572bd938"}, + {file = "regex-2020.6.8-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:92d8a043a4241a710c1cf7593f5577fbb832cf6c3a00ff3fc1ff2052aff5dd89"}, + {file = "regex-2020.6.8-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:bae83f2a56ab30d5353b47f9b2a33e4aac4de9401fb582b55c42b132a8ac3868"}, + {file = "regex-2020.6.8-cp36-cp36m-manylinux2010_i686.whl", hash = "sha256:b2ba0f78b3ef375114856cbdaa30559914d081c416b431f2437f83ce4f8b7f2f"}, + {file = "regex-2020.6.8-cp36-cp36m-manylinux2010_x86_64.whl", hash = "sha256:95fa7726d073c87141f7bbfb04c284901f8328e2d430eeb71b8ffdd5742a5ded"}, + {file = "regex-2020.6.8-cp36-cp36m-win32.whl", hash = "sha256:e3cdc9423808f7e1bb9c2e0bdb1c9dc37b0607b30d646ff6faf0d4e41ee8fee3"}, + {file = "regex-2020.6.8-cp36-cp36m-win_amd64.whl", hash = "sha256:c78e66a922de1c95a208e4ec02e2e5cf0bb83a36ceececc10a72841e53fbf2bd"}, + {file = "regex-2020.6.8-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:08997a37b221a3e27d68ffb601e45abfb0093d39ee770e4257bd2f5115e8cb0a"}, + {file = "regex-2020.6.8-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:2f6f211633ee8d3f7706953e9d3edc7ce63a1d6aad0be5dcee1ece127eea13ae"}, + {file = "regex-2020.6.8-cp37-cp37m-manylinux2010_i686.whl", hash = "sha256:55b4c25cbb3b29f8d5e63aeed27b49fa0f8476b0d4e1b3171d85db891938cc3a"}, + {file = "regex-2020.6.8-cp37-cp37m-manylinux2010_x86_64.whl", hash = "sha256:89cda1a5d3e33ec9e231ece7307afc101b5217523d55ef4dc7fb2abd6de71ba3"}, + {file = "regex-2020.6.8-cp37-cp37m-win32.whl", hash = "sha256:690f858d9a94d903cf5cada62ce069b5d93b313d7d05456dbcd99420856562d9"}, + {file = "regex-2020.6.8-cp37-cp37m-win_amd64.whl", hash = "sha256:1700419d8a18c26ff396b3b06ace315b5f2a6e780dad387e4c48717a12a22c29"}, + {file = "regex-2020.6.8-cp38-cp38-manylinux1_i686.whl", hash = "sha256:654cb773b2792e50151f0e22be0f2b6e1c3a04c5328ff1d9d59c0398d37ef610"}, + {file = "regex-2020.6.8-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:52e1b4bef02f4040b2fd547357a170fc1146e60ab310cdbdd098db86e929b387"}, + {file = "regex-2020.6.8-cp38-cp38-manylinux2010_i686.whl", hash = "sha256:cf59bbf282b627130f5ba68b7fa3abdb96372b24b66bdf72a4920e8153fc7910"}, + {file = "regex-2020.6.8-cp38-cp38-manylinux2010_x86_64.whl", hash = "sha256:5aaa5928b039ae440d775acea11d01e42ff26e1561c0ffcd3d805750973c6baf"}, + {file = "regex-2020.6.8-cp38-cp38-win32.whl", hash = "sha256:97712e0d0af05febd8ab63d2ef0ab2d0cd9deddf4476f7aa153f76feef4b2754"}, + {file = "regex-2020.6.8-cp38-cp38-win_amd64.whl", hash = "sha256:6ad8663c17db4c5ef438141f99e291c4d4edfeaacc0ce28b5bba2b0bf273d9b5"}, + {file = "regex-2020.6.8.tar.gz", hash = "sha256:e9b64e609d37438f7d6e68c2546d2cb8062f3adb27e6336bc129b51be20773ac"}, ] requests = [ {file = "requests-2.23.0-py2.py3-none-any.whl", hash = "sha256:43999036bfa82904b6af1d99e4882b560e5e2c68e5c4b0aa03b655f3d7d73fee"}, {file = "requests-2.23.0.tar.gz", hash = "sha256:b3f43d496c6daba4493e7c431722aeb7dbc6288f52a6e04e7b6023b0247817e6"}, ] seed-isort-config = [ - {file = "seed_isort_config-1.9.4-py2.py3-none-any.whl", hash = "sha256:81f7adf8dd0bc24e03d0f5eef87561e170757fc7a36050c01e4c29d150757783"}, - {file = "seed_isort_config-1.9.4.tar.gz", hash = "sha256:cecccb911b8d01ecdca16e17d083bf4cd78e928db4d7906125b0373b81f1c7ec"}, + {file = "seed_isort_config-2.2.0-py2.py3-none-any.whl", hash = "sha256:8601fb715a5a4aac39256bbf73c2da6a81f964da9c9d9897ab9074db3663526f"}, + {file = "seed_isort_config-2.2.0.tar.gz", hash = "sha256:be4cfef8f9a3fe8ea1817069c6b624538ac0b429636ec746edeb27e98ed628c8"}, ] six = [ - {file = "six-1.14.0-py2.py3-none-any.whl", hash = "sha256:8f3cd2e254d8f793e7f3d6d9df77b92252b52637291d0f0da013c76ea2724b6c"}, - {file = "six-1.14.0.tar.gz", hash = "sha256:236bdbdce46e6e6a3d61a337c0f8b763ca1e8717c03b369e87a7ec7ce1319c0a"}, + {file = "six-1.15.0-py2.py3-none-any.whl", hash = "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced"}, + {file = "six-1.15.0.tar.gz", hash = "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259"}, ] snowballstemmer = [ {file = "snowballstemmer-2.0.0-py2.py3-none-any.whl", hash = "sha256:209f257d7533fdb3cb73bdbd24f436239ca3b2fa67d56f6ff88e86be08cc5ef0"}, @@ -1099,10 +1110,12 @@ taskipy = [ {file = "taskipy-1.2.1-py3-none-any.whl", hash = "sha256:99bdaf5b19791c2345806847147e0fc2d28e1ac9446058def5a8b6b3fc9f23e2"}, {file = "taskipy-1.2.1.tar.gz", hash = "sha256:5eb2c3b1606c896c7fa799848e71e8883b880759224958d07ba760e5db263175"}, ] +termcolor = [ + {file = "termcolor-1.1.0.tar.gz", hash = "sha256:1d6d69ce66211143803fbc56652b41d73b4a400a2891d7bf7a1cdf4c02de613b"}, +] toml = [ - {file = "toml-0.10.0-py2.7.egg", hash = "sha256:f1db651f9657708513243e61e6cc67d101a39bad662eaa9b5546f789338e07a3"}, - {file = "toml-0.10.0-py2.py3-none-any.whl", hash = "sha256:235682dd292d5899d361a811df37e04a8828a5b1da3115886b73cf81ebc9100e"}, - {file = "toml-0.10.0.tar.gz", hash = "sha256:229f81c57791a41d65e399fc06bf0848bab550a9dfd5ed66df18ce5f05e73d5c"}, + {file = "toml-0.10.1-py2.py3-none-any.whl", hash = "sha256:bda89d5935c2eac546d648028b9901107a595863cb36bae0c73ac804a9b4ce88"}, + {file = "toml-0.10.1.tar.gz", hash = "sha256:926b612be1e5ce0634a2ca03470f95169cf16f939018233a670519cb4ac58b0f"}, ] typed-ast = [ {file = "typed_ast-1.4.1-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:73d785a950fc82dd2a25897d525d003f6378d1cb23ab305578394694202a58c3"}, @@ -1128,18 +1141,18 @@ typed-ast = [ {file = "typed_ast-1.4.1.tar.gz", hash = "sha256:8c8aaad94455178e3187ab22c8b01a3837f8ee50e09cf31f1ba129eb293ec30b"}, ] urllib3 = [ - {file = "urllib3-1.25.8-py2.py3-none-any.whl", hash = "sha256:2f3db8b19923a873b3e5256dc9c2dedfa883e33d87c690d9c7913e1f40673cdc"}, - {file = "urllib3-1.25.8.tar.gz", hash = "sha256:87716c2d2a7121198ebcb7ce7cccf6ce5e9ba539041cfbaeecfb641dc0bf6acc"}, + {file = "urllib3-1.25.9-py2.py3-none-any.whl", hash = "sha256:88206b0eb87e6d677d424843ac5209e3fb9d0190d0ee169599165ec25e9d9115"}, + {file = "urllib3-1.25.9.tar.gz", hash = "sha256:3018294ebefce6572a474f0604c2021e33b3fd8006ecd11d62107a5d2a963527"}, ] virtualenv = [ - {file = "virtualenv-20.0.15-py2.py3-none-any.whl", hash = "sha256:4e399f48c6b71228bf79f5febd27e3bbb753d9d5905776a86667bc61ab628a25"}, - {file = "virtualenv-20.0.15.tar.gz", hash = "sha256:9e81279f4a9d16d1c0654a127c2c86e5bca2073585341691882c1e66e31ef8a5"}, + {file = "virtualenv-16.7.10-py2.py3-none-any.whl", hash = "sha256:105893c8dc66b7817691c7371439ec18e3b6c5e323a304b5ed96cdd2e75cc1ec"}, + {file = "virtualenv-16.7.10.tar.gz", hash = "sha256:e88fdcb08b0ecb11da97868f463dd06275923f50d87f4b9c8b2fc0994eec40f4"}, ] wcwidth = [ - {file = "wcwidth-0.1.9-py2.py3-none-any.whl", hash = "sha256:cafe2186b3c009a04067022ce1dcd79cb38d8d65ee4f4791b8888d6599d1bbe1"}, - {file = "wcwidth-0.1.9.tar.gz", hash = "sha256:ee73862862a156bf77ff92b09034fc4825dd3af9cf81bc5b360668d425f3c5f1"}, + {file = "wcwidth-0.2.4-py2.py3-none-any.whl", hash = "sha256:79375666b9954d4a1a10739315816324c3e73110af9d0e102d906fdb0aec009f"}, + {file = "wcwidth-0.2.4.tar.gz", hash = "sha256:8c6b5b6ee1360b842645f336d9e5d68c55817c26d3050f46b235ef2bc650e48f"}, ] zipp = [ - {file = "zipp-3.1.0-py3-none-any.whl", hash = "sha256:aa36550ff0c0b7ef7fa639055d797116ee891440eac1a56f378e2d3179e0320b"}, - {file = "zipp-3.1.0.tar.gz", hash = "sha256:c599e4d75c98f6798c509911d08a22e6c021d074469042177c8c86fb92eefd96"}, + {file = "zipp-1.2.0-py2.py3-none-any.whl", hash = "sha256:e0d9e63797e483a30d27e09fffd308c59a700d365ec34e93cc100844168bf921"}, + {file = "zipp-1.2.0.tar.gz", hash = "sha256:c70410551488251b0fee67b460fb9a536af8d6f9f008ad10ac51f615b6a521b1"}, ] diff --git a/pyproject.toml b/pyproject.toml index 9cf335a..97c98c7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,10 @@ classifiers = [ "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", + "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", "Topic :: Software Development :: Libraries", "Topic :: Software Development :: Libraries :: Python Modules", ] @@ -20,8 +23,8 @@ classifiers = [ test = "poetry run pytest -v --cov=config_file/ --cov-report term-missing tests/" [tool.poetry.dependencies] -python = "^3.6" -nested-lookup = "^0.2.19" +python = "^3.5" +nested-lookup = "^0.2.21" toml = { version = "^0.10.0", optional = true } pyyaml = { version = "^5.3.1", optional = true } @@ -33,19 +36,20 @@ yaml = ["pyyaml"] pre-commit = "^1.20" autoflake = "^1.3.1" isort = "^4.3.21" -seed-isort-config = "^1.9" +seed-isort-config = { version = "^2.1.1", python = "^3.6.1" } pytest = "^5.4.1" pytest-cov = "^2.8.1" +pytest-sugar = "^0.9.3" sphinx = "^2.4.4" sphinx_rtd_theme = "^0.4.3" -taskipy = "^1.2.1" +taskipy = { version = "^1.2.1", python = "^3.6" } bump2version = "^1.0.0" flake8 = "^3.7.9" -black = "^19.10b0" +black = { version = "^19.10b0", python = "^3.6" } [tool.black] line-length = 88 -target_version = ['py38'] +target_version = ['py35'] include = '\.pyi?$' [tool.isort] diff --git a/tests/test_config_file.py b/tests/test_config_file.py index f083538..fe8539f 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -3,8 +3,7 @@ from pathlib import Path import pytest -from config_file.config_file import ConfigFile -from config_file.parsers.base_parser import BaseParser +from config_file import ConfigFile, BaseParser @pytest.mark.parametrize( @@ -49,14 +48,14 @@ def test_missing_config_files_during_restore( tmpdir, file_extension, specify_original_config_path ): with pytest.raises(OSError): - temp_path = tmpdir / f"config.{file_extension}" + temp_path = tmpdir / "config.{}".format(file_extension) temp_path.write("") config = ConfigFile(str(temp_path)) if specify_original_config_path: - specify_original_config_path = f"does_not_exist.{file_extension}" + specify_original_config_path = "does_not_exist.{}".format(file_extension) else: - config.path = f"does_not_exist.{file_extension}" + config.path = "does_not_exist.{}".format(file_extension) specify_original_config_path = None config.restore_original(original_file_path=specify_original_config_path) @@ -160,7 +159,7 @@ def test_that_config_file_can_get( def test_that_config_file_can_find_sections_and_keys( tmpdir, file_extension, file_contents, section_key, expected_result ): - config_path = tmpdir / f"config.{file_extension}" + config_path = tmpdir / "config.{}".format(file_extension) config_path.write_text(file_contents, encoding="utf-8") config = ConfigFile(str(config_path)) assert config.has(section_key) == expected_result diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index 7cdeea8..1b2fd3b 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,6 +1,7 @@ import pytest -from config_file.parsers.ini_parser import IniParser, ParsingError +from config_file.parsers.ini_parser import IniParser +from config_file.exceptions import ParsingError @pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"]) diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index a5e38a1..9a8ac09 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -2,7 +2,8 @@ import json import pytest -from config_file.parsers.json_parser import JsonParser, ParsingError +from config_file.parsers.json_parser import JsonParser +from config_file.exceptions import ParsingError @pytest.mark.parametrize("test_json", ["{'test': }", "[section]\nblah = value\n\n"]) @@ -162,7 +163,7 @@ def test_that_json_parser_can_delete_keys(key, test_json, expected_json): ) def test_that_json_parser_can_retrieve_all_keys(test_json, key, expected_output): parser = JsonParser(json.dumps(test_json)) - assert parser.get(key, get_all=True) == expected_output + assert parser.get(key, all=True) == expected_output @pytest.mark.parametrize( @@ -178,8 +179,8 @@ def test_that_json_parser_can_retrieve_all_keys(test_json, key, expected_output) ) def test_that_json_parser_can_set_all_keys(test_json, key, value, expected_output): parser = JsonParser(json.dumps(test_json)) - parser.set(key, value, set_all=True) - assert parser.get(key, get_all=True) == expected_output + parser.set(key, value, all=True) + assert parser.get(key, all=True) == expected_output @pytest.mark.parametrize( @@ -194,5 +195,5 @@ def test_that_json_parser_can_set_all_keys(test_json, key, value, expected_outpu ) def test_that_json_parser_can_delete_all_keys(test_json, key, expected_output): parser = JsonParser(json.dumps(test_json)) - parser.delete(key, delete_all=True) + parser.delete(key, all=True) assert parser.stringify() == json.dumps(expected_output) diff --git a/tox.ini b/tox.ini index 1098700..1a1512f 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] isolated_build = true -envlist = py{36, 37, 38} +envlist = py{35, 36, 37, 38} recreate = True [testenv] -- cgit v1.2.3 From d7b4d0e54bf2c26262ce803478c50822a867e2dd Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 13:44:38 -0500 Subject: Run travis CI on python 3.5 --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 507c5e1..e04fe9a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ language: python os: linux python: + - "3.5" - "3.6" - "3.7" - "3.8" @@ -10,4 +11,4 @@ install: - curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - source $HOME/.poetry/env - pip install --upgrade pip tox-travis codecov -script: tox \ No newline at end of file +script: tox -- cgit v1.2.3 From 8d377a1edf2ac2343fa374cb51a07a72a0316ed2 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 13:47:27 -0500 Subject: Remove taskipy from tox & add default to parser in __determine_parser Taskipy uses f-strings which break on 3.5. --- config_file/config_file.py | 20 +++++++++++++++----- tox.ini | 4 ++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/config_file/config_file.py b/config_file/config_file.py index 805fc49..1cddd20 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -11,7 +11,9 @@ from config_file.exceptions import ParsingError class ConfigFile: - def __init__(self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser] = None) -> None: + def __init__( + self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser] = None + ) -> None: """ Saves the config file path and expands it if needed, reads in the file contents, and determines what parser should be used for @@ -28,7 +30,9 @@ class ConfigFile: self.parser = self.__determine_parser(file_path, parser) @staticmethod - def __create_config_path(file_path: Type[Union[str, PurePath]], original: bool = False) -> str: + def __create_config_path( + file_path: Type[Union[str, PurePath]], original: bool = False + ) -> str: if isinstance(file_path, PurePath): file_path = str(file_path) @@ -41,11 +45,15 @@ class ConfigFile: file_path = ".".join(file_parts) if Path(file_path).is_dir(): - raise ValueError("The specified config file ({}) is a directory.".format(file_path)) + raise ValueError( + "The specified config file ({}) is a directory.".format(file_path) + ) return file_path - def __determine_parser(self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser]) -> BaseParser: + def __determine_parser( + self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser] = None + ) -> BaseParser: if isinstance(file_path, PurePath): file_path = str(file_path) @@ -59,7 +67,9 @@ class ConfigFile: return JsonParser(self.contents) else: raise ValueError( - "File path contains an unsupported or unrecognized file type: {}".format(file_path) + "File path contains an unsupported or unrecognized file type: {}".format( + file_path + ) ) def __read_config_file(self): diff --git a/tox.ini b/tox.ini index 1a1512f..6b849bc 100644 --- a/tox.ini +++ b/tox.ini @@ -9,6 +9,6 @@ deps = codecov>=1.4.0 whitelist_externals = poetry commands = poetry install -v --no-dev -E toml -E yaml - pip install pytest pytest-cov requests taskipy - poetry run task test + pip install pytest pytest-cov requests + poetry run pytest -v --cov=config_file/ --cov-report term-missing tests/ codecov -e TOXENV -- cgit v1.2.3 From 95c47a183409fabed81f078a70967f053679737e Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 15:13:53 -0500 Subject: Change path, parser, and contents to be properties Also, restore_original now raises a FileNotFoundError. --- .pre-commit-config.yaml | 42 +++++++++++++-------- CHANGELOG.md | 3 ++ config_file/config_file.py | 94 +++++++++++++++++++++++----------------------- config_file/utils.py | 22 +++++++---- tests/test_config_file.py | 45 +++++++++------------- 5 files changed, 108 insertions(+), 98 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9975877..879742c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,30 +1,42 @@ repos: + - repo: git@github.com:humitos/mirrors-autoflake.git + rev: v1.3 + hooks: + - id: autoflake + args: + [ + "--in-place", + "--remove-all-unused-imports", + "--remove-unused-variable", + ] -- repo: git@github.com:humitos/mirrors-autoflake.git - rev: v1.3 - hooks: - - id: autoflake - args: ['--in-place', '--remove-all-unused-imports', '--remove-unused-variable'] - -- repo: https://github.com/asottile/seed-isort-config + - repo: https://github.com/asottile/seed-isort-config rev: v1.9.4 hooks: - - id: seed-isort-config + - id: seed-isort-config -- repo: https://github.com/pre-commit/mirrors-isort + - repo: https://github.com/pre-commit/mirrors-isort rev: v4.3.21 hooks: - - id: isort + - id: isort additional_dependencies: - toml -- repo: https://github.com/ambv/black + - repo: https://github.com/ambv/black rev: stable hooks: - - id: black - language_version: python3.8 + - id: black + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.5.0 + hooks: + - id: trailing-whitespace + - id: check-docstring-first + - id: check-case-conflict + - id: check-yaml + - id: check-toml -- repo: https://github.com/pre-commit/pre-commit-hooks + - repo: https://github.com/pre-commit/pre-commit-hooks rev: v1.2.3 hooks: - - id: flake8 + - id: flake8 diff --git a/CHANGELOG.md b/CHANGELOG.md index e6ac91f..2c9e591 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,9 @@ Changed - The `get_all`, `set_all`, and `delete_all` optional parameter options in `JsonParser` have been renamed to just `all`. This shouldn't have any effect since `JsonParser` is not intended to be used directly, and that option is not in `ConfigFile`. +- `path`, `parser`, and `contents` on the `ConfigFile` are now all properties and can only be retrieved. +- `restore_original` in `ConfigFile` now raises a `FileNotFoundError` instead of an `OSError` if the original + file path does not exist. ## 0.10.0 - 2020-05-10 diff --git a/config_file/config_file.py b/config_file/config_file.py index 1cddd20..d4131de 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -1,18 +1,18 @@ import inspect -from pathlib import Path, PurePath +from pathlib import Path from shutil import copyfile from typing import Type, Union +from config_file.exceptions import ParsingError from config_file.parsers.base_parser import BaseParser 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.exceptions import ParsingError +from config_file.utils import read_file, split_on_dot class ConfigFile: def __init__( - self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser] = None + self, file_path: Type[Union[str, Path]], parser: Type[BaseParser] = None ) -> None: """ Saves the config file path and expands it if needed, reads in @@ -25,19 +25,29 @@ class ConfigFile: :raises ValueError: If the specified file path does not have an extension that is supported or it is a directory. """ - self.path = self.__create_config_path(file_path) - self.contents = self.__read_config_file() - self.parser = self.__determine_parser(file_path, parser) + if type(file_path) is not Path: + file_path = Path(file_path) - @staticmethod - def __create_config_path( - file_path: Type[Union[str, PurePath]], original: bool = False - ) -> str: - if isinstance(file_path, PurePath): - file_path = str(file_path) + self.__path = self.__create_config_path(file_path) + self.__contents = read_file(self.__path) + self.__parser = self.__determine_parser(file_path, parser) + + @property + def path(self) -> Path: + return self.__path + + @property + def contents(self) -> str: + return self.__contents - if len(file_path) > 1 and file_path[0] == "~": - return str(Path(file_path).expanduser()) + @property + def parser(self) -> Type[BaseParser]: + return self.__parser + + @staticmethod + def __create_config_path(file_path: Path, original: bool = False) -> str: + if len(file_path.parts) >= 1 and file_path.parts[0] == "~": + return file_path.expanduser() if original: file_parts = split_on_dot(file_path, only_last_dot=True) @@ -52,30 +62,21 @@ class ConfigFile: return file_path def __determine_parser( - self, file_path: Type[Union[str, PurePath]], parser: Type[BaseParser] = None + self, file_path: Path, parser: Type[BaseParser] = None ) -> 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) + return parser(self.__contents) file_type = split_on_dot(file_path, only_last_dot=True)[-1] if file_type == "ini": - return IniParser(self.contents) + return IniParser(self.__contents) elif file_type == "json": - return JsonParser(self.contents) + return JsonParser(self.__contents) else: raise ValueError( - "File path contains an unsupported or unrecognized file type: {}".format( - file_path - ) + "File path contains an unrecognized file type: {}".format(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): """ Retrieve the value of a key. @@ -92,10 +93,10 @@ class ConfigFile: :return: The value of the key. """ if default is None: - key_value = self.parser.get(key, parse_types=parse_types) + key_value = self.__parser.get(key, parse_types=parse_types) else: try: - key_value = self.parser.get(key, parse_types=parse_types) + key_value = self.__parser.get(key, parse_types=parse_types) except ParsingError: return default @@ -103,15 +104,15 @@ class ConfigFile: def set(self, key: str, value): """Sets the value of a key.""" - return self.parser.set(key, value) + return self.__parser.set(key, value) def delete(self, section_key: str): """Deletes a key/value pair or entire sections.""" - return self.parser.delete(section_key) + return self.__parser.delete(section_key) def stringify(self) -> str: """Convert the parsed internal representation of the file back into a string.""" - return self.parser.stringify() + return self.__parser.stringify() def has(self, section_key: str) -> bool: """ @@ -121,7 +122,7 @@ class ConfigFile: Some formats, like JSON, do not have sections and therefore, it would only be checking if the key exists. """ - return self.parser.has(section_key) + return self.__parser.has(section_key) def restore_original(self, original_file_path=None): """ @@ -134,23 +135,22 @@ class ConfigFile: method would look for a config.original.json in the same folder by default. :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. + :raises FileNotFoundError: If the original configuration file does not exist + :raises OSError: If self.path is not writable + :raises SameFileError: If self.path and original_file_path are the same file. """ if original_file_path is None: - original_file_path = self.__create_config_path( - str(self.path), original=True - ) + original_file_path = self.__create_config_path(self.__path, original=True) if not Path(original_file_path).exists(): - raise OSError( + raise FileNotFoundError( "The {} file to restore to does not exist.".format(original_file_path) ) - 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) + self.__path.expanduser().unlink() + copyfile(original_file_path, self.__path) + self.__contents = read_file(self.path) + self.__parser.parsed_content = self.__parser.parse(self.__contents) return True @@ -159,7 +159,7 @@ class ConfigFile: Save the configuration changes by writing the file out to the specified path :return: True if the save succeeded. """ - with open(self.path, "w") as config_file: - config_file.write(self.parser.stringify()) + with open(self.__path, "w") as config_file: + config_file.write(self.__parser.stringify()) return True diff --git a/config_file/utils.py b/config_file/utils.py index 61f0ae0..05c3a7a 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -1,4 +1,8 @@ -def split_on_dot(line: str, only_last_dot=False): +from pathlib import Path +from typing import Union + + +def split_on_dot(line: Union[str, Path], only_last_dot=False): """ Split a string on the dot character (.). @@ -7,15 +11,17 @@ def split_on_dot(line: str, only_last_dot=False): :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'" + "The given string does not have a dot to split on: {}".format(line) ) return line.rsplit(".", 1) if only_last_dot else line.split(".") + + +def read_file(path: Union[str, Path]): + with open(path, "r") as file: + return file.read() diff --git a/tests/test_config_file.py b/tests/test_config_file.py index fe8539f..38cdf38 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -3,7 +3,7 @@ from pathlib import Path import pytest -from config_file import ConfigFile, BaseParser +from config_file import BaseParser, ConfigFile @pytest.mark.parametrize( @@ -14,7 +14,7 @@ def test_that_config_file_initializes_correctly(tmp_path, file_contents, file_na temp.write_text(file_contents) config = ConfigFile(temp) - assert config.path == str(temp) + assert config.path == temp assert config.contents == temp.read_text() assert isinstance(config.parser, BaseParser) @@ -26,39 +26,27 @@ def test_that_config_file_initializes_correctly(tmp_path, file_contents, file_na def test_that_config_file_can_restore_the_original( tmpdir, file_contents, file_name, original_file_name ): - config_path = tmpdir / file_name + config_path = Path(tmpdir / file_name) config_path.write_text(file_contents, encoding="utf-8") - - original_config_path = tmpdir / original_file_name + original_config_path = Path(tmpdir / original_file_name) original_config_path.write_text(file_contents, encoding="utf-8") - config = ConfigFile(str(config_path)) - config.set("calendar.sunday_index", 0) + config = ConfigFile(config_path) + config.set("calendar.sunday_index", 5) config.restore_original() - assert config.stringify() == ConfigFile(str(original_config_path)).stringify() - assert config_path.read_text(encoding="utf-8") == original_config_path.read_text( - encoding="utf-8" - ) + assert config.stringify() == ConfigFile(original_config_path).stringify() -@pytest.mark.parametrize( - "file_extension, specify_original_config_path", [("ini", False), ("ini", True)] -) -def test_missing_config_files_during_restore( - tmpdir, file_extension, specify_original_config_path -): - with pytest.raises(OSError): - temp_path = tmpdir / "config.{}".format(file_extension) - temp_path.write("") - config = ConfigFile(str(temp_path)) - if specify_original_config_path: - specify_original_config_path = "does_not_exist.{}".format(file_extension) - else: - config.path = "does_not_exist.{}".format(file_extension) - specify_original_config_path = None +def test_missing_config_files_during_restore(tmpdir): + config_path = Path(tmpdir / "config.ini") + config_path.write_text("") + config = ConfigFile(config_path) - config.restore_original(original_file_path=specify_original_config_path) + with pytest.raises(FileNotFoundError) as error: + config.restore_original(original_file_path="jhklasdfhjlkasjej") + + assert "file to restore to does not exist" in str(error) @pytest.mark.parametrize( @@ -228,6 +216,7 @@ def test_that_custom_parser_can_be_used(tmpdir): config_path = tmpdir / "config.conf" config_path.write_text("", encoding="utf-8") config = ConfigFile(str(config_path), parser=CustomParser) + assert isinstance(config.parser, CustomParser) assert config.get("key") == "key" assert config.set("key", "value") == ("key", "value") @@ -242,6 +231,6 @@ def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): try: Path(config_path).expanduser().touch() - assert ConfigFile(config_path).path == str(Path.home() / config_name) + assert ConfigFile(config_path).path == Path.home() / config_name finally: Path(config_path).expanduser().unlink() -- cgit v1.2.3 From 58808235ea6880c3a0ecfa976a700c81431dc902 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 15:30:11 -0500 Subject: Revert Python 3.5 support --- CHANGELOG.md | 1 - CONTRIBUTING.md | 35 ++++++++++++++++++----------------- config_file/config_file.py | 10 +++++----- config_file/parsers/ini_parser.py | 6 +++--- config_file/parsers/json_parser.py | 4 ++-- config_file/utils.py | 14 ++++++-------- pyproject.toml | 9 ++++----- tox.ini | 6 +++--- 8 files changed, 41 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c9e591..3f89271 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Added -- Support for Python 3.5 by removing use of f-strings from codebase. - A generic `ConfigFileError`. - More type hints from `typings` have been added to `ConfigFile` for better type information. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8669fd0..902c669 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -14,33 +14,34 @@ Here are some guidelines to keep in mind when submitting a pull request: paragraphs of explanation as you need. This is the place to clarify any subtleties you have in your implemeÆ’ntation, document other approaches you tried that didn't end up working, any limitations on your implementation, etc. The most important - part here is to describe *why* you made the change you did, not simply *what* the + part here is to describe _why_ you made the change you did, not simply _what_ the change you made is. - Changelog: Please ensure to update the changelog by adding a new bullet under - an ``Added``, ``Changed``, ``Deprecated``, ``Removed``, ``Fixed``, or ``Security`` section - headers under the ``Unreleased`` version. If any of those sections are not present, + an `Added`, `Changed`, `Deprecated`, `Removed`, `Fixed`, or `Security` section + headers under the `Unreleased` version. If any of those sections are not present, feel free to add the one you need. See [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) if you need guidance on what makes a good entry since this project follows those principles. If you're not comfortable with your English, we'd be happy to write it for you. -- Ensure the tests pass: ``poetry run task test`` to run all tests. +- Ensure the tests pass: `poetry run task test` to run all tests. - New features should be accompanied with tests for them: + - Unit tests are written using [pytest](https://docs.pytest.org/en/latest/). - Pre-commit pipeline: We use a pre-commit pipeline to ensure standard code format. - - Unused imports are automatically removed using `autoflake `_ - - - The remaining imports are sorted using `isort `_. - - - All code is automatically formatted with `black `_ - - - Lastly, everything is checked by `flake8 `_. - - Make sure to install pre-commit before making commits. + - Unused imports are automatically removed using `autoflake `\_ + + - The remaining imports are sorted using `isort `\_. + + - All code is automatically formatted with `black `\_ + + - Lastly, everything is checked by `flake8 `\_. + + Make sure to install pre-commit before making commits. Note: if any of the items had to do any reformatting, sorting, etc., the commit will fail. You'll have to re-add the items it fixed and try again. @@ -48,11 +49,11 @@ Here are some guidelines to keep in mind when submitting a pull request: ```bash pre-commit install ``` - + - CI Pipeline: There is a CI pipeline that is run by Travis CI on commits to master and on pull requests. - + - All it does is ensure that all the tests pass. -To get started, make sure you have poetry installed and run ``poetry install`` and -``poetry shell`` to enter the virtual environment. \ No newline at end of file +To get started, make sure you have poetry installed and run `poetry install` and +`poetry shell` to enter the virtual environment. diff --git a/config_file/config_file.py b/config_file/config_file.py index d4131de..3af1083 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -55,9 +55,7 @@ class ConfigFile: file_path = ".".join(file_parts) if Path(file_path).is_dir(): - raise ValueError( - "The specified config file ({}) is a directory.".format(file_path) - ) + raise ValueError(f"The specified config file ({file_path}) is a directory.") return file_path @@ -74,7 +72,7 @@ class ConfigFile: return JsonParser(self.__contents) else: raise ValueError( - "File path contains an unrecognized file type: {}".format(file_path) + f"File path contains an unrecognized file type: {file_path}" ) def get(self, key: str, parse_types: bool = False, return_type=None, default=None): @@ -91,6 +89,8 @@ class ConfigFile: :param default: The default value to return if the value of the key is empty. :return: The value of the key. + + :raises 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) @@ -144,7 +144,7 @@ class ConfigFile: if not Path(original_file_path).exists(): raise FileNotFoundError( - "The {} file to restore to does not exist.".format(original_file_path) + f"The {original_file_path} file to restore to does not exist." ) self.__path.expanduser().unlink() diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 31b0dcf..2db1ae8 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -1,10 +1,10 @@ import configparser from io import StringIO +from config_file.exceptions import ParsingError from config_file.parsers.base_parser import BaseParser from config_file.parsers.parse_value import parse_value from config_file.utils import split_on_dot -from config_file.exceptions import ParsingError class IniParser(BaseParser): @@ -96,7 +96,7 @@ class IniParser(BaseParser): if "." not in section_key: if not self.parsed_content.has_section(section_key): raise ValueError( - "Cannot delete {} because it is not in the config file.".format(section_key) + f"Cannot delete {section_key} because it is not in the config file." ) self.parsed_content.remove_section(section_key) @@ -105,7 +105,7 @@ class IniParser(BaseParser): section, key = split_on_dot(section_key, only_last_dot=True) if key not in self.parsed_content[section]: raise ValueError( - "Cannot delete {section}.{key} because {key} is not in {section}.".format(section = section, key = key) + f"Cannot delete {section}.{key} because {key} is not in {section}." ) self.parsed_content.remove_option(section, key) diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py index 4d93ca3..10d5afc 100644 --- a/config_file/parsers/json_parser.py +++ b/config_file/parsers/json_parser.py @@ -7,10 +7,10 @@ from nested_lookup import ( nested_update, ) +from config_file.exceptions import ParsingError from config_file.parsers.base_parser import BaseParser from config_file.parsers.parse_value import parse_value from config_file.utils import split_on_dot -from config_file.exceptions import ParsingError class JsonParser(BaseParser): @@ -78,7 +78,7 @@ class JsonParser(BaseParser): result = result[key] except TypeError: raise ParsingError( - "Cannot get {} because {} is not subscriptable.".format(key, key_for_error) + f"Cannot get {key} because {key_for_error} is not subscriptable." ) else: result = self.parsed_content[key] diff --git a/config_file/utils.py b/config_file/utils.py index 05c3a7a..9ed09b5 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -1,8 +1,8 @@ -from pathlib import Path -from typing import Union +from pathlib import PurePath +from typing import Type, Union -def split_on_dot(line: Union[str, Path], only_last_dot=False): +def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False): """ Split a string on the dot character (.). @@ -11,17 +11,15 @@ def split_on_dot(line: Union[str, Path], only_last_dot=False): :raises ValueError: if the line does not have a dot. """ - if isinstance(line, Path): + if isinstance(line, PurePath): line = str(line) if "." not in line: - raise ValueError( - "The given string does not have a dot to split on: {}".format(line) - ) + 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(".") -def read_file(path: Union[str, Path]): +def read_file(path: Union[str, Type[PurePath]]): with open(path, "r") as file: return file.read() diff --git a/pyproject.toml b/pyproject.toml index 97c98c7..0f3bf5f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,6 @@ classifiers = [ "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", - "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", @@ -23,7 +22,7 @@ classifiers = [ test = "poetry run pytest -v --cov=config_file/ --cov-report term-missing tests/" [tool.poetry.dependencies] -python = "^3.5" +python = "^3.6" nested-lookup = "^0.2.21" toml = { version = "^0.10.0", optional = true } pyyaml = { version = "^5.3.1", optional = true } @@ -42,14 +41,14 @@ pytest-cov = "^2.8.1" pytest-sugar = "^0.9.3" sphinx = "^2.4.4" sphinx_rtd_theme = "^0.4.3" -taskipy = { version = "^1.2.1", python = "^3.6" } +taskipy = "^1.2.1" bump2version = "^1.0.0" flake8 = "^3.7.9" -black = { version = "^19.10b0", python = "^3.6" } +black = "^19.10b0" [tool.black] line-length = 88 -target_version = ['py35'] +target_version = ['py36'] include = '\.pyi?$' [tool.isort] diff --git a/tox.ini b/tox.ini index 6b849bc..1098700 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] isolated_build = true -envlist = py{35, 36, 37, 38} +envlist = py{36, 37, 38} recreate = True [testenv] @@ -9,6 +9,6 @@ deps = codecov>=1.4.0 whitelist_externals = poetry commands = poetry install -v --no-dev -E toml -E yaml - pip install pytest pytest-cov requests - poetry run pytest -v --cov=config_file/ --cov-report term-missing tests/ + pip install pytest pytest-cov requests taskipy + poetry run task test codecov -e TOXENV -- 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 --- README.md | 8 ++++---- config_file/__init__.py | 2 +- config_file/config_file.py | 28 ++++++++++++++++++---------- config_file/exceptions.py | 2 +- config_file/parsers/base_parser.py | 1 - tests/test_ini_parser.py | 2 +- tests/test_json_parser.py | 2 +- 7 files changed, 26 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index b7ef6ef..223f3e7 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ ## About Config File -The Config File project is designed to allow you to easily manipulate your +The Config File project is designed to allow you to easily manipulate your configuration files with the same simple API whether they are in INI, JSON, YAML, or TOML. For the time being, it only supports INI and JSON. @@ -49,7 +49,7 @@ from config_file import ConfigFile ORIGINAL_CONFIG_PATH = Path("~/some-project/some-other-config-file.ini") CONFIG_PATH = Path("~/some-project/config.ini") -# Our path can be a string or a Path object. +# Our path can be a string or a Path object. # The "~" will be automatically expanded to the full path for us. config = ConfigFile(CONFIG_PATH) @@ -66,7 +66,7 @@ print(config.get("section")) print(config.get("section", parse_types=True)) >>> {'first_key': 5, 'second_key': 'blah', 'third_key': True} -# Sometimes we want to retrieve a key but don't know whether or not +# Sometimes we want to retrieve a key but don't know whether or not # it will be set. In that case, we can set a default. print(config.get("section.unknown", default=False)) >>> False @@ -96,7 +96,7 @@ print(config.restore_original(original_file_path=ORIGINAL_CONFIG_PATH)) >>> True # Otherwise, a config.original.ini file will automatically be looked for in the -# current directory (because our configuration file we passed in was +# current directory (because our configuration file we passed in was # named config.ini). print(config.restore_original()) >>> True diff --git a/config_file/__init__.py b/config_file/__init__.py index f37019a..5fb800a 100644 --- a/config_file/__init__.py +++ b/config_file/__init__.py @@ -1,6 +1,6 @@ from config_file.config_file import ConfigFile +from config_file.exceptions import ConfigFileError, ParsingError from config_file.parsers.base_parser import BaseParser -from config_file.exceptions import ParsingError, ConfigFileError __version__ = "0.10.0" diff --git a/config_file/config_file.py b/config_file/config_file.py index 3af1083..431521a 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -1,7 +1,7 @@ import inspect -from pathlib import Path +from pathlib import Path, PurePath from shutil import copyfile -from typing import Type, Union +from typing import Any, Type, Union from config_file.exceptions import ParsingError from config_file.parsers.base_parser import BaseParser @@ -12,7 +12,9 @@ from config_file.utils import read_file, split_on_dot class ConfigFile: def __init__( - self, file_path: Type[Union[str, Path]], parser: Type[BaseParser] = None + self, + file_path: Type[Union[str, Type[PurePath]]], + parser: Type[BaseParser] = None, ) -> None: """ Saves the config file path and expands it if needed, reads in @@ -33,7 +35,7 @@ class ConfigFile: self.__parser = self.__determine_parser(file_path, parser) @property - def path(self) -> Path: + def path(self) -> Type[PurePath]: return self.__path @property @@ -45,7 +47,7 @@ class ConfigFile: return self.__parser @staticmethod - def __create_config_path(file_path: Path, original: bool = False) -> str: + def __create_config_path(file_path: Type[PurePath], original: bool = False) -> str: if len(file_path.parts) >= 1 and file_path.parts[0] == "~": return file_path.expanduser() @@ -60,7 +62,7 @@ class ConfigFile: return file_path def __determine_parser( - self, file_path: Path, parser: Type[BaseParser] = None + self, file_path: Type[PurePath], parser: Type[BaseParser] = None ) -> BaseParser: if parser is not None and not inspect.isabstract(parser): return parser(self.__contents) @@ -75,7 +77,13 @@ class ConfigFile: f"File path contains an unrecognized file type: {file_path}" ) - def get(self, key: str, parse_types: bool = False, return_type=None, default=None): + def get( + self, + key: str, + parse_types: bool = False, + return_type: Any = None, + default: Any = None, + ) -> Any: """ Retrieve the value of a key. @@ -102,7 +110,7 @@ class ConfigFile: return return_type(key_value) if return_type else key_value - def set(self, key: str, value): + def set(self, key: str, value: Any) -> bool: """Sets the value of a key.""" return self.__parser.set(key, value) @@ -124,7 +132,7 @@ class ConfigFile: """ return self.__parser.has(section_key) - def restore_original(self, original_file_path=None): + def restore_original(self, original_file_path: Union[str, Type[PurePath]] = None): """ 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 @@ -154,7 +162,7 @@ class ConfigFile: return True - def save(self): + def save(self) -> bool: """ Save the configuration changes by writing the file out to the specified path :return: True if the save succeeded. diff --git a/config_file/exceptions.py b/config_file/exceptions.py index cd7e236..e1af755 100644 --- a/config_file/exceptions.py +++ b/config_file/exceptions.py @@ -1,6 +1,6 @@ class ConfigFileError(Exception): """A generic error from the package.""" + class ParsingError(ConfigFileError): """Unable to parse the configuration file.""" - \ No newline at end of file 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 - diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index 1b2fd3b..fb92373 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,7 +1,7 @@ import pytest -from config_file.parsers.ini_parser import IniParser from config_file.exceptions import ParsingError +from config_file.parsers.ini_parser import IniParser @pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"]) diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index 9a8ac09..42a0ba9 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -2,8 +2,8 @@ import json import pytest -from config_file.parsers.json_parser import JsonParser from config_file.exceptions import ParsingError +from config_file.parsers.json_parser import JsonParser @pytest.mark.parametrize("test_json", ["{'test': }", "[section]\nblah = value\n\n"]) -- cgit v1.2.3 From a63105f958b9519a25d6c75b9b772d952efce9ff Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 15:36:02 -0500 Subject: Convert string to f-string in test --- tests/test_config_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 38cdf38..861b169 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -147,7 +147,7 @@ def test_that_config_file_can_get( def test_that_config_file_can_find_sections_and_keys( tmpdir, file_extension, file_contents, section_key, expected_result ): - config_path = tmpdir / "config.{}".format(file_extension) + config_path = tmpdir / f"config.{file_extension}" config_path.write_text(file_contents, encoding="utf-8") config = ConfigFile(str(config_path)) assert config.has(section_key) == expected_result -- cgit v1.2.3 From 484050afe95aa60f5e415e1c73e4ecf614bbc162 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 15:56:03 -0500 Subject: Use xml cov report for pytest --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 0f3bf5f..5819885 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,7 +19,7 @@ classifiers = [ ] [tool.taskipy.tasks] -test = "poetry run pytest -v --cov=config_file/ --cov-report term-missing tests/" +test = "poetry run pytest -v --cov=config_file/ --cov-report=xml term-missing tests/" [tool.poetry.dependencies] python = "^3.6" -- cgit v1.2.3 From 8d86ca0c8a00767257e7595c6f307827051faf7c Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 16:00:35 -0500 Subject: Add lint task and remove xml cov report --- pyproject.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 5819885..28f2acd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,7 +19,8 @@ classifiers = [ ] [tool.taskipy.tasks] -test = "poetry run pytest -v --cov=config_file/ --cov-report=xml term-missing tests/" +test = "poetry run pytest -v --cov=config_file/ --cov-report term-missing tests/" +lint = "poetry run pre-commit run --all" [tool.poetry.dependencies] python = "^3.6" -- cgit v1.2.3 From 47a60b821ced6a9377bdfdb31d844ce52b2b017d Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 16:02:11 -0500 Subject: Remove linting step from github action --- .github/workflows/python-package-ci.yml | 70 +++++++++++++++------------------ 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/.github/workflows/python-package-ci.yml b/.github/workflows/python-package-ci.yml index d9f8836..305af60 100644 --- a/.github/workflows/python-package-ci.yml +++ b/.github/workflows/python-package-ci.yml @@ -2,51 +2,45 @@ name: Python package CI on: push: - branches: [ master, dev ] + branches: [master, dev] pull_request: - branches: [ master, dev ] + branches: [master, dev] jobs: build: - runs-on: ubuntu-latest strategy: matrix: python-version: [3.6, 3.7, 3.8] steps: - - uses: actions/checkout@v2 - - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 - with: - python-version: ${{ matrix.python-version }} - - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install poetry codecov requests - poetry install -v -E toml -E yaml - - - name: Run linters - run: | - poetry run pre-commit run --all - - - name: Run tests - run: | - poetry run task test - - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v1 - with: - token: ${{ secrets.CODECOV_TOKEN }} - file: ./coverage.xml - flags: unittests - env_vars: OS,PYTHON - name: codecov-umbrella - fail_ci_if_error: true - - - name: Build Package - run: | - poetry build - + - uses: actions/checkout@v2 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install poetry pytest pytest-cov codecov requests + poetry install --no-dev -v -E toml -E yaml + + - name: Run tests + run: | + poetry run task test + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v1 + with: + token: ${{ secrets.CODECOV_TOKEN }} + file: ./coverage.xml + flags: unittests + env_vars: OS,PYTHON + name: codecov-umbrella + fail_ci_if_error: true + + - name: Build Package + run: | + poetry build -- cgit v1.2.3 From d5cc42b3f49e80b33a064e16df5d88c56118c03b Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 16:03:08 -0500 Subject: Add taskipy to deps install in package ci --- .github/workflows/python-package-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-package-ci.yml b/.github/workflows/python-package-ci.yml index 305af60..dcda5df 100644 --- a/.github/workflows/python-package-ci.yml +++ b/.github/workflows/python-package-ci.yml @@ -24,7 +24,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install poetry pytest pytest-cov codecov requests + pip install poetry pytest pytest-cov codecov requests taskipy poetry install --no-dev -v -E toml -E yaml - name: Run tests -- cgit v1.2.3 From e01788cd62ee27486b656e8f1c500f1389521514 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 16:04:58 -0500 Subject: Remove 3.5 from travis --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e04fe9a..8adb511 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ language: python os: linux python: - - "3.5" - "3.6" - "3.7" - "3.8" -- cgit v1.2.3 From 650e56bc222848ba6a4a96ec1bb4c98f6e15beed Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 16:05:27 -0500 Subject: Remove --no-dev from package ci --- .github/workflows/python-package-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-package-ci.yml b/.github/workflows/python-package-ci.yml index dcda5df..8a1432e 100644 --- a/.github/workflows/python-package-ci.yml +++ b/.github/workflows/python-package-ci.yml @@ -24,8 +24,8 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install poetry pytest pytest-cov codecov requests taskipy - poetry install --no-dev -v -E toml -E yaml + pip install poetry codecov requests + poetry install -v -E toml -E yaml - name: Run tests run: | -- cgit v1.2.3 From e6d7aeeead256cf98b8fb4947424cf8ad26d0b9a Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 16:10:07 -0500 Subject: Add test_ci taskipy task --- .github/workflows/python-package-ci.yml | 2 +- pyproject.toml | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-package-ci.yml b/.github/workflows/python-package-ci.yml index 8a1432e..6cbfbb1 100644 --- a/.github/workflows/python-package-ci.yml +++ b/.github/workflows/python-package-ci.yml @@ -29,7 +29,7 @@ jobs: - name: Run tests run: | - poetry run task test + poetry run task test_ci - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 diff --git a/pyproject.toml b/pyproject.toml index 28f2acd..bdf9ba3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,8 +19,11 @@ classifiers = [ ] [tool.taskipy.tasks] -test = "poetry run pytest -v --cov=config_file/ --cov-report term-missing tests/" +test = "task test_cov -- -v --cov-report term-missing" +test_cov = "task pytest -- --cov=config_file/" +test_ci = "task test_cov -- --cov-report=xml" lint = "poetry run pre-commit run --all" +pytest = "poetry run pytest tests/" [tool.poetry.dependencies] python = "^3.6" -- cgit v1.2.3 From ca7482827d61bc22bf255258fd0a5397aff63969 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 16:14:00 -0500 Subject: Remove travis and tox in favor of github actions --- .github/workflows/python-package-ci.yml | 1 - .travis.yml | 13 ------------- tox.ini | 14 -------------- 3 files changed, 28 deletions(-) delete mode 100644 .travis.yml delete mode 100644 tox.ini diff --git a/.github/workflows/python-package-ci.yml b/.github/workflows/python-package-ci.yml index 6cbfbb1..ab6778b 100644 --- a/.github/workflows/python-package-ci.yml +++ b/.github/workflows/python-package-ci.yml @@ -37,7 +37,6 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} file: ./coverage.xml flags: unittests - env_vars: OS,PYTHON name: codecov-umbrella fail_ci_if_error: true diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 8adb511..0000000 --- a/.travis.yml +++ /dev/null @@ -1,13 +0,0 @@ -language: python -os: linux - -python: - - "3.6" - - "3.7" - - "3.8" - -install: - - curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - - source $HOME/.poetry/env - - pip install --upgrade pip tox-travis codecov -script: tox diff --git a/tox.ini b/tox.ini deleted file mode 100644 index 1098700..0000000 --- a/tox.ini +++ /dev/null @@ -1,14 +0,0 @@ -[tox] -isolated_build = true -envlist = py{36, 37, 38} -recreate = True - -[testenv] -passenv = TOXENV CI TRAVIS TRAVIS_* CODECOV_* -deps = codecov>=1.4.0 -whitelist_externals = poetry -commands = - poetry install -v --no-dev -E toml -E yaml - pip install pytest pytest-cov requests taskipy - poetry run task test - codecov -e TOXENV -- cgit v1.2.3 From 8f3d8539d4e0d9123b96815ff15e72f1996ccd56 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 16:17:42 -0500 Subject: Add linting phase to package ci --- .github/workflows/python-package-ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/python-package-ci.yml b/.github/workflows/python-package-ci.yml index ab6778b..8722898 100644 --- a/.github/workflows/python-package-ci.yml +++ b/.github/workflows/python-package-ci.yml @@ -27,6 +27,9 @@ jobs: pip install poetry codecov requests poetry install -v -E toml -E yaml + - name: Run linter + uses: pre-commit/action@v2.0.0 + - name: Run tests run: | poetry run task test_ci -- cgit v1.2.3 From b9dade88ba7c2ab65fa4eda17bf13bcba8f0c7cf Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 16:20:05 -0500 Subject: Remove linting phase --- .github/workflows/python-package-ci.yml | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/.github/workflows/python-package-ci.yml b/.github/workflows/python-package-ci.yml index 8722898..0790104 100644 --- a/.github/workflows/python-package-ci.yml +++ b/.github/workflows/python-package-ci.yml @@ -27,12 +27,8 @@ jobs: pip install poetry codecov requests poetry install -v -E toml -E yaml - - name: Run linter - uses: pre-commit/action@v2.0.0 - - name: Run tests - run: | - poetry run task test_ci + run: poetry run task test_ci - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 @@ -44,5 +40,4 @@ jobs: fail_ci_if_error: true - name: Build Package - run: | - poetry build + run: poetry build -- 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 --- CHANGELOG.md | 2 +- README.md | 4 ++-- config_file/config_file.py | 1 + config_file/parsers/base_parser.py | 12 ++++++------ 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f89271..1e52d9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ Changed - Errors have been moved to `exceptions.py`. - The `get_all`, `set_all`, and `delete_all` optional parameter options in `JsonParser` have been renamed to just `all`. This shouldn't have any effect since `JsonParser` is not intended - to be used directly, and that option is not in `ConfigFile`. + to be used directly, and that option was not in `ConfigFile`. - `path`, `parser`, and `contents` on the `ConfigFile` are now all properties and can only be retrieved. - `restore_original` in `ConfigFile` now raises a `FileNotFoundError` instead of an `OSError` if the original file path does not exist. diff --git a/README.md b/README.md index 223f3e7..1af08fc 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![Version](https://img.shields.io/pypi/v/config-file)](https://pypi.org/project/config-file/) [![Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://pypi.org/project/black/) [![Documentation Status](https://readthedocs.org/projects/config-file/badge/?version=latest)](https://config-file.readthedocs.io/en/latest/?badge=latest) -[![Build Status](https://travis-ci.com/eugenetriguba/config-file.svg?branch=master)](https://travis-ci.com/eugenetriguba/config-file) +[![Build Status](https://github.com/eugenetriguba/config-file/workflows/Python%20package%20CI/badge.svg?branch=master)](https://github.com/eugenetriguba/config-file/actions/) [![Codecov](https://codecov.io/gh/eugenetriguba/config-file/graph/badge.svg)](https://codecov.io/gh/eugenetriguba/config-file) ## About Config File @@ -102,7 +102,7 @@ print(config.restore_original()) >>> True ``` -## Documentation +## Documentation Full documentation and API reference is available at https://config-file.readthedocs.io diff --git a/config_file/config_file.py b/config_file/config_file.py index 431521a..6ded72b 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -83,6 +83,7 @@ class ConfigFile: parse_types: bool = False, return_type: Any = None, default: Any = None, + all: bool = False, ) -> Any: """ Retrieve the value of a key. 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 50403a631179398f7c60c7622029d0bf2d2b4cc3 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 16:33:55 -0500 Subject: Use lowercase action name --- .github/workflows/python-package-ci.yml | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-package-ci.yml b/.github/workflows/python-package-ci.yml index 0790104..dc0d104 100644 --- a/.github/workflows/python-package-ci.yml +++ b/.github/workflows/python-package-ci.yml @@ -1,4 +1,4 @@ -name: Python package CI +name: python package CI on: push: diff --git a/README.md b/README.md index 1af08fc..c7eebda 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![Version](https://img.shields.io/pypi/v/config-file)](https://pypi.org/project/config-file/) [![Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://pypi.org/project/black/) [![Documentation Status](https://readthedocs.org/projects/config-file/badge/?version=latest)](https://config-file.readthedocs.io/en/latest/?badge=latest) -[![Build Status](https://github.com/eugenetriguba/config-file/workflows/Python%20package%20CI/badge.svg?branch=master)](https://github.com/eugenetriguba/config-file/actions/) +[![Build Status](https://github.com/eugenetriguba/config-file/workflows/python%20package%20CI/badge.svg?branch=master)](https://github.com/eugenetriguba/config-file/actions/) [![Codecov](https://codecov.io/gh/eugenetriguba/config-file/graph/badge.svg)](https://codecov.io/gh/eugenetriguba/config-file) ## About Config File -- cgit v1.2.3 From 2c293cc0ac6678253c6af8101ba93866785f64c0 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 21 Jun 2020 11:12:26 -0500 Subject: WIP for refactoring tests --- CHANGELOG.md | 3 +- config_file/config_file.py | 33 +----- config_file/parsers/__init__.py | 6 ++ config_file/parsers/json_parser.py | 2 +- config_file/utils.py | 19 +++- tests/conftest.py | 67 +++++++++++++ tests/templates/ini/default.ini | 7 ++ tests/templates/json/default.json | 13 +++ tests/templates/json/glossary.json | 35 +++++++ tests/templates/json/invalid.json | 1 + tests/test_base_parser.py | 2 +- tests/test_config_file.py | 195 ++++++++++++++++------------------- tests/test_json_parser.py | 201 ++++++++++++++++--------------------- 13 files changed, 324 insertions(+), 260 deletions(-) create mode 100644 tests/conftest.py create mode 100644 tests/templates/ini/default.ini create mode 100644 tests/templates/json/default.json create mode 100644 tests/templates/json/glossary.json create mode 100644 tests/templates/json/invalid.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e52d9b..29e4663 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,8 @@ Changed - The `get_all`, `set_all`, and `delete_all` optional parameter options in `JsonParser` have been renamed to just `all`. This shouldn't have any effect since `JsonParser` is not intended to be used directly, and that option was not in `ConfigFile`. -- `path`, `parser`, and `contents` on the `ConfigFile` are now all properties and can only be retrieved. +- `path` on the `ConfigFile` is now a property that can only be retrieved. `contents` is now private + but you can use `.stringify()` instead to get the contents. `parser` is now also private. - `restore_original` in `ConfigFile` now raises a `FileNotFoundError` instead of an `OSError` if the original file path does not exist. diff --git a/config_file/config_file.py b/config_file/config_file.py index 6ded72b..e037a82 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -4,10 +4,8 @@ from shutil import copyfile from typing import Any, Type, Union from config_file.exceptions import ParsingError -from config_file.parsers.base_parser import BaseParser -from config_file.parsers.ini_parser import IniParser -from config_file.parsers.json_parser import JsonParser -from config_file.utils import read_file, split_on_dot +from config_file.parsers import BaseParser, IniParser, JsonParser +from config_file.utils import create_config_path, read_file, split_on_dot class ConfigFile: @@ -30,7 +28,7 @@ class ConfigFile: if type(file_path) is not Path: file_path = Path(file_path) - self.__path = self.__create_config_path(file_path) + self.__path = create_config_path(file_path) self.__contents = read_file(self.__path) self.__parser = self.__determine_parser(file_path, parser) @@ -38,29 +36,6 @@ class ConfigFile: def path(self) -> Type[PurePath]: return self.__path - @property - def contents(self) -> str: - return self.__contents - - @property - def parser(self) -> Type[BaseParser]: - return self.__parser - - @staticmethod - def __create_config_path(file_path: Type[PurePath], original: bool = False) -> str: - if len(file_path.parts) >= 1 and file_path.parts[0] == "~": - return 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: Type[PurePath], parser: Type[BaseParser] = None ) -> BaseParser: @@ -149,7 +124,7 @@ class ConfigFile: :raises SameFileError: If self.path and original_file_path are the same file. """ if original_file_path is None: - original_file_path = self.__create_config_path(self.__path, original=True) + original_file_path = create_config_path(self.__path, original=True) if not Path(original_file_path).exists(): raise FileNotFoundError( diff --git a/config_file/parsers/__init__.py b/config_file/parsers/__init__.py index e69de29..415f4f9 100644 --- a/config_file/parsers/__init__.py +++ b/config_file/parsers/__init__.py @@ -0,0 +1,6 @@ +from .base_parser import BaseParser +from .ini_parser import IniParser +from .json_parser import JsonParser +from .parse_value import parse_value + +__all__ = ["BaseParser", "IniParser", "JsonParser", "parse_value"] diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py index 10d5afc..584a2d0 100644 --- a/config_file/parsers/json_parser.py +++ b/config_file/parsers/json_parser.py @@ -124,7 +124,7 @@ class JsonParser(BaseParser): return True def stringify(self) -> str: - return json.dumps(self.parsed_content) + return json.dumps(self.parsed_content, indent=4) def has(self, search_key: str) -> bool: return get_occurrence_of_key(self.parsed_content, key=search_key) > 0 diff --git a/config_file/utils.py b/config_file/utils.py index 9ed09b5..4a83a40 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -1,8 +1,8 @@ -from pathlib import PurePath +from pathlib import Path, PurePath from typing import Type, Union -def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False): +def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False) -> str: """ Split a string on the dot character (.). @@ -23,3 +23,18 @@ def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False): def read_file(path: Union[str, Type[PurePath]]): with open(path, "r") as file: return file.read() + + +def create_config_path(file_path: Type[PurePath], original: bool = False) -> Path: + if len(file_path.parts) >= 1 and file_path.parts[0] == "~": + return file_path.expanduser() + + if original: + file_parts = split_on_dot(file_path, only_last_dot=True) + file_parts.insert(-1, "original") + file_path = Path(".".join(file_parts)) + + if file_path.is_dir(): + raise ValueError(f"The specified config file ({file_path}) is a directory.") + + return file_path diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..bb16405 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,67 @@ +from pathlib import Path +from typing import Callable, Type + +import pytest + +from config_file.parsers import BaseParser, IniParser, JsonParser +from config_file.utils import create_config_path + + +@pytest.fixture +def tmp_file(tmp_path) -> Callable[[str, str], Path]: + def func(file_name: str, file_contents: str) -> Path: + file = tmp_path / file_name + file.touch() + file.write_text(file_contents, encoding="utf-8") + return file + + return func + + +@pytest.fixture +def template_file(tmp_file: Callable[[str, str], Path]) -> Callable[[str, str], Path]: + template_dir = Path(__file__).parent / "templates" + + def func(file_type: str, template_name: str = "default") -> Path: + file_name = f"{template_name}.{file_type}" + template = template_dir / file_type / file_name + + return tmp_file(file_name, template.read_text(encoding="utf-8")) + + return func + + +@pytest.fixture +def template_original_file() -> Callable[[Path], Path]: + def func(template_path: str): + original_path = create_config_path(template_path, original=True) + original_path.touch() + original_path.write_text(template_path.read_text(encoding="utf-8")) + + return original_path + + return func + + +@pytest.fixture +def template_and_parser(template_file: Callable[[str, str], Path]) -> tuple: + def func( + file_type: str, template_name: str = "default" + ) -> Callable[[str, str], Type[BaseParser]]: + test_file = template_file(file_type, template_name) + text = test_file.read_text(encoding="utf-8") + + if file_type == "ini": + return test_file, IniParser(text) + elif file_type == "json": + return test_file, JsonParser(text) + + return func + + +@pytest.fixture +def templated_parser(template_and_parser) -> Callable[[str, str], Type[BaseParser]]: + def func(file_type: str, template_name: str = "default") -> Type[BaseParser]: + return template_and_parser(file_type, template_name)[1] + + return func diff --git a/tests/templates/ini/default.ini b/tests/templates/ini/default.ini new file mode 100644 index 0000000..4f711db --- /dev/null +++ b/tests/templates/ini/default.ini @@ -0,0 +1,7 @@ +[header_one] +number_key = 0 + +[header_two] +str_key = "string" +list_key = [1, 2, 3] + diff --git a/tests/templates/json/default.json b/tests/templates/json/default.json new file mode 100644 index 0000000..83d9b1e --- /dev/null +++ b/tests/templates/json/default.json @@ -0,0 +1,13 @@ +{ + "header_one": { + "number_key": 0 + }, + "header_two": { + "str_key": "string", + "list_key": [ + 1, + 2, + 3 + ] + } +} \ No newline at end of file diff --git a/tests/templates/json/glossary.json b/tests/templates/json/glossary.json new file mode 100644 index 0000000..189d8a4 --- /dev/null +++ b/tests/templates/json/glossary.json @@ -0,0 +1,35 @@ +{ + "glossary": { + "title": "str_value", + "GlossDiv": { + "title": "5", + "GlossList": { + "GlossEntry": { + "ID": "false", + "SortAs": "SGML", + "GlossTerm": "Standard Generalized Markup Language", + "Acronym": "SGML", + "Abbrev": "ISO 8879:1986", + "GlossDef": { + "para": "A meta-markup language, used to create markup languages such as DocBook.", + "GlossSeeAlso": ["GML", "XML"] + }, + "GlossSee": "5.5" + } + } + }, + "dict": { + "key": "str_value", + "str_number": "5" + }, + "dict_within_dict": { + "str_number": "16", + "another_dict": { + "str_bool": "false", + "list_of_values": [ + "5", "false", "trUe" + ] + } + } + } +} \ No newline at end of file diff --git a/tests/templates/json/invalid.json b/tests/templates/json/invalid.json new file mode 100644 index 0000000..8e12c39 --- /dev/null +++ b/tests/templates/json/invalid.json @@ -0,0 +1 @@ +{'test': } \ No newline at end of file diff --git a/tests/test_base_parser.py b/tests/test_base_parser.py index 1127a61..6bb58e7 100644 --- a/tests/test_base_parser.py +++ b/tests/test_base_parser.py @@ -1,6 +1,6 @@ import pytest -from config_file.parsers.base_parser import BaseParser +from config_file import BaseParser class ConcreteParser(BaseParser): diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 861b169..d89edc6 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -5,61 +5,98 @@ import pytest from config_file import BaseParser, ConfigFile +SUPPORTED_FILE_TYPES = ["ini", "json"] -@pytest.mark.parametrize( - "file_contents, file_name", [("[calendar]\nsunday_index = 0\n\n", "config.ini")] -) -def test_that_config_file_initializes_correctly(tmp_path, file_contents, file_name): - temp = tmp_path / file_name - temp.write_text(file_contents) - config = ConfigFile(temp) - assert config.path == temp - assert config.contents == temp.read_text() - assert isinstance(config.parser, BaseParser) +@pytest.fixture(params=SUPPORTED_FILE_TYPES) +def template_and_config_file(request, template_file): + def func(template_name: str = "default", parser: BaseParser = None): + template = template_file(request.param, template_name) + return (template, ConfigFile(template, parser=parser)) + + return func + + +@pytest.fixture +def templated_config_file(template_and_config_file): + def func(template_name: str = "default", parser: BaseParser = None): + return template_and_config_file(template_name, parser)[1] + + return func + + +def test_that_config_file_is_initialized_correctly(template_and_config_file): + template_file, config = template_and_config_file() + + assert config.path == template_file + assert config.stringify() == template_file.read_text() + assert isinstance(config._ConfigFile__parser, BaseParser) + + +def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): + config_name = "temp.ini" + config_path = "~/" + config_name + + try: + Path(config_path).expanduser().touch() + assert ConfigFile(config_path).path == Path.home() / config_name + finally: + Path(config_path).expanduser().unlink() + + +@pytest.mark.skip(msg="needs fixing up") +@pytest.mark.parametrize("path", ["", "invalid", "invalid.conf"]) +def test_invalid_config_paths(path): + with pytest.raises(ValueError): + ConfigFile(path) + + +def test_that_config_file_can_save(template_and_config_file): + template_file, config = template_and_config_file() + + config.set("header_one.number_key", 25) + config.save() + + assert config.stringify() == template_file.read_text() -@pytest.mark.parametrize( - "file_contents, file_name, original_file_name", - [("[calendar]\nsunday_index = 0\n\n", "config.ini", "config.original.ini")], -) def test_that_config_file_can_restore_the_original( - tmpdir, file_contents, file_name, original_file_name + templated_config_file, template_original_file ): - config_path = Path(tmpdir / file_name) - config_path.write_text(file_contents, encoding="utf-8") - original_config_path = Path(tmpdir / original_file_name) - original_config_path.write_text(file_contents, encoding="utf-8") + config = templated_config_file() + original_file = template_original_file(config.path) - config = ConfigFile(config_path) - config.set("calendar.sunday_index", 5) + config.set("header_one.number_key", 5) config.restore_original() - assert config.stringify() == ConfigFile(original_config_path).stringify() + assert config.stringify() == original_file.read_text() -def test_missing_config_files_during_restore(tmpdir): - config_path = Path(tmpdir / "config.ini") - config_path.write_text("") - config = ConfigFile(config_path) +def test_missing_config_file_during_restore(templated_config_file): + config = templated_config_file() with pytest.raises(FileNotFoundError) as error: - config.restore_original(original_file_path="jhklasdfhjlkasjej") + config.restore_original() assert "file to restore to does not exist" in str(error) @pytest.mark.parametrize( - "file_contents, file_name", [("[calendar]\nsunday_index = 0\n\n", "config.ini")] + "file_extension, file_contents, section_key, expected_result", + [ + ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar.sunday_index", True), + ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar.blah", False), + ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar", True), + ("json", json.dumps({"calendar": {"sunday_index": 0}}), "calendar", True), + ("json", json.dumps({"calendar": {"sunday_index": 0}}), "blah", False), + ("json", json.dumps({"calendar": {"sunday_index": 0}}), "sunday_index", True), + ], ) -def test_that_config_file_can_save(tmpdir, file_contents, file_name): - config_path = tmpdir / file_name - config_path.write_text(file_contents, encoding="utf-8") - - config = ConfigFile(str(config_path)) - config.set("calendar.sunday_index", 1) - config.save() - assert config.stringify() == Path(config_path).read_text(encoding="utf-8") +def test_that_config_file_can_find_sections_and_keys( + tmp_file, file_extension, file_contents, section_key, expected_result +): + config = ConfigFile(tmp_file(f"file.{file_extension}", file_contents)) + assert config.has(section_key) == expected_result @pytest.mark.parametrize( @@ -124,7 +161,7 @@ def test_that_config_file_can_get( ): config_path = tmpdir / file_name config_path.write_text(file_contents, encoding="utf-8") - config = ConfigFile(str(config_path)) + config = ConfigFile(config_path) ret_val = config.get(section_key, parse_types=parse_type, default=default) if return_type is not None: @@ -133,68 +170,22 @@ def test_that_config_file_can_get( assert ret_val == value -@pytest.mark.parametrize( - "file_extension, file_contents, section_key, expected_result", - [ - ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar.sunday_index", True), - ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar.blah", False), - ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar", True), - ("json", json.dumps({"calendar": {"sunday_index": 0}}), "calendar", True), - ("json", json.dumps({"calendar": {"sunday_index": 0}}), "blah", False), - ("json", json.dumps({"calendar": {"sunday_index": 0}}), "sunday_index", True), - ], -) -def test_that_config_file_can_find_sections_and_keys( - tmpdir, file_extension, file_contents, section_key, expected_result -): - config_path = tmpdir / f"config.{file_extension}" - config_path.write_text(file_contents, encoding="utf-8") - config = ConfigFile(str(config_path)) - assert config.has(section_key) == expected_result - - -@pytest.mark.parametrize( - "section_key, file_name, file_contents, deleted_file_contents", - [ - ( - "calendar.sunday_index", - "config.ini", - "[calendar]\nsunday_index = 0\n\n", - "[calendar]\n\n", - ), - ( - "calendar.sunday_index", - "config.json", - json.dumps({"calendar": {"sunday_index": 0}}), - json.dumps({"calendar": {}}), - ), - ], -) -def test_that_config_file_can_delete( - tmpdir, section_key, file_name, file_contents, deleted_file_contents -): - config_path = tmpdir / file_name - config_path.write_text(file_contents, encoding="utf-8") - config = ConfigFile(str(config_path)) - config.delete(section_key) - assert config.stringify() == deleted_file_contents +def test_that_config_file_can_delete(templated_config_file): + config = templated_config_file() + config.delete("header_one.number_key") -@pytest.mark.parametrize( - "path, is_dir", [("invalid", False), ("invalid", True), ("invalid.conf", False)] -) -def test_invalid_config_paths(tmpdir, path, is_dir): - with pytest.raises(ValueError): - temp_path = tmpdir / path - temp_path.write("") - ConfigFile(str(tmpdir)) if is_dir else ConfigFile(str(temp_path)) + assert config.has("header_one.number_key") is False -def test_that_custom_parser_can_be_used(tmpdir): +def test_that_custom_parser_can_be_used(template_and_config_file): class CustomParser(BaseParser): def __init__(self, file_contents): super().__init__(file_contents) + def parse(self, file_contents: str): + return file_contents + def get(self, key, parse_types=True): return key @@ -210,27 +201,11 @@ def test_that_custom_parser_can_be_used(tmpdir): def stringify(self) -> str: return str(self.parsed_content) - def parse(self, file_contents: str): - return file_contents - - config_path = tmpdir / "config.conf" - config_path.write_text("", encoding="utf-8") - config = ConfigFile(str(config_path), parser=CustomParser) + template_file, config = template_and_config_file(parser=CustomParser) - assert isinstance(config.parser, CustomParser) + assert isinstance(config._ConfigFile__parser, CustomParser) assert config.get("key") == "key" assert config.set("key", "value") == ("key", "value") assert config.delete("section_key") == "section_key" assert config.has("blah") - assert config.stringify() == "" - - -def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): - config_name = "fjlajbkfdajfdioanfks.ini" - config_path = "~/" + config_name - - try: - Path(config_path).expanduser().touch() - assert ConfigFile(config_path).path == Path.home() / config_name - finally: - Path(config_path).expanduser().unlink() + assert config.stringify() == template_file.read_text() diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index 42a0ba9..18a2b6f 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -6,155 +6,124 @@ from config_file.exceptions import ParsingError from config_file.parsers.json_parser import JsonParser -@pytest.mark.parametrize("test_json", ["{'test': }", "[section]\nblah = value\n\n"]) -def test_invalid_json(test_json): +def test_invalid_json(templated_parser): with pytest.raises(ParsingError): - JsonParser(test_json) + templated_parser("json", "invalid") -def test_that_json_parser_can_stringify(): - json_str = '{"test": 5, "blah": true}' - parser = JsonParser(json_str) - assert parser.stringify() == json_str +def test_that_json_parser_can_stringify(template_and_parser): + template, parser = template_and_parser("json") + assert parser.stringify() == template.read_text() @pytest.mark.parametrize( - "test_input,expected_result", + "key", [ - ("glossary", True), - ("title", True), - ("example glossary", False), - ("GlossDiv", True), - ("title", True), - ("5", False), - ("GlossList", True), - ("GlossEntry", True), - ("ID", True), - ("false", False), - ("GlossTerm", True), - ("Standard Generalized Markup Language", False), - ("Acronym", True), - ("ISO 8879:1986", False), - ("GlossDef", True), - ("para", True), - ( - "A meta-markup language, used to create markup languages " - "such as DocBook.", - False, - ), - ("GlossSeeAlso", True), - ("GML", False), - ("XML", False), - ("GlossSee", True), - ("5.5", False), + "glossary", + "title", + "GlossDiv", + "title", + "GlossList", + "GlossEntry", + "ID", + "GlossTerm", + "Acronym", + "GlossDef", + "para", + "GlossSeeAlso", + "GlossSee", ], ) -def test_that_json_parser_can_find_keys(test_input, expected_result): - test_json = { - "glossary": { - "title": "example glossary", - "GlossDiv": { - "title": "5", - "GlossList": { - "GlossEntry": { - "ID": "false", - "SortAs": "SGML", - "GlossTerm": "Standard Generalized Markup Language", - "Acronym": "SGML", - "Abbrev": "ISO 8879:1986", - "GlossDef": { - "para": "A meta-markup language, used to create markup " - + "languages such as DocBook.", - "GlossSeeAlso": ["GML", "XML"], - }, - "GlossSee": "5.5", - } - }, - }, - } - } +def test_that_json_parser_can_find_keys(key, templated_parser): + parser = templated_parser("json", "glossary") + assert parser.has(key) - parser = JsonParser(json.dumps(test_json)) - assert parser.has(test_input) == expected_result + +@pytest.mark.parametrize( + "value", + [ + "str_value", + "5", + "false", + "Standard Generalized Markup Language", + "ISO 8879:1986", + "A meta-markup language, used to create markup languages such as DocBook.", + "GML", + "XML", + "5.5", + ], +) +def test_that_json_parser_does_not_find_value_with_has(value, templated_parser): + parser = templated_parser("json", "glossary") + assert not parser.has(value) @pytest.mark.parametrize( - "test_json, section_key, parse_type, expected_result", + "key, value", [ + ("glossary.title", "str_value"), + ("glossary.dict", {"key": "str_value", "str_number": "5"}), + ], +) +def test_that_json_parser_can_get_keys_without_parsing_types( + templated_parser, key, value +): + parser = templated_parser("json", "glossary") + assert parser.get(key) == value + + +@pytest.mark.parametrize( + "key, value", + [ + ("glossary.title", "str_value"), + ("glossary.dict.str_number", 5), ( - {"glossary": {"title": "example glossary"}}, - "glossary.title", - False, - "example glossary", - ), - ( - {"glossary": {"title": "example glossary"}}, - "glossary.title", - True, - "example glossary", - ), - ( - {"glossary": {"title": "example glossary", "dict": {"blah": "5"}}}, - "glossary.dict", - False, - {"blah": "5"}, - ), - ( - {"glossary": {"title": "example glossary", "dict": {"blah": "5"}}}, - "glossary.dict", - True, - {"blah": 5}, - ), - ( + "glossary.dict_within_dict", { - "glossary": { - "title": "example glossary", - "dict": {"blah": "5", "second": {"third": "5"}}, - } + "str_number": 16, + "another_dict": {"str_bool": False, "list_of_values": [5, False, True]}, }, - "glossary.dict", - True, - {"blah": 5, "second": {"third": 5}}, ), ], ) -def test_that_json_parser_can_get_keys( - test_json, section_key, parse_type, expected_result -): - parser = JsonParser(json.dumps(test_json)) - assert parser.get(section_key, parse_types=parse_type) == expected_result +def test_that_json_parser_can_get_keys_with_parsing_types(templated_parser, key, value): + parser = templated_parser("json", "glossary") + assert parser.get(key, parse_types=True) == value @pytest.mark.parametrize( "key, value", [("zip", "zipzip"), ("foo.bar.foobar", 5), ("foo.baz", "bar")] ) -def test_that_json_parser_can_set_keys(key, value): - test_json = { - "foo": { - "baz": 5, - "boo": "4.4", - "bar": {"test_key": "foobar", "foobar": {"test": True}}, - }, - "bar": {"baz": 10}, - "zip": "piz", - } +def test_that_json_parser_can_set_keys(templated_parser, key, value): + parser = templated_parser("json") + + # test_json = { + # "foo": { + # "baz": 5, + # "boo": "4.4", + # "bar": {"test_key": "foobar", "foobar": {"test": True}}, + # }, + # "bar": {"baz": 10}, + # "zip": "piz", + # } + # parser = JsonParser(json.dumps(test_json)) - parser = JsonParser(json.dumps(test_json)) parser.set(key, value) assert parser.get(key) == value @pytest.mark.parametrize( - "key, test_json, expected_json", - [ - ("foo", {"foo": "bar", "baz": 5}, {"baz": 5}), - ("foo.bar", {"foo": {"bar": 5}}, {"foo": {}}), - ], + "key", + ["glossary", "dict_within_dict", "glossary.GlossDiv.GlossList.GlossEntry.Abbrev"], ) -def test_that_json_parser_can_delete_keys(key, test_json, expected_json): - parser = JsonParser(json.dumps(test_json)) +def test_that_json_parser_can_delete_keys( + template_and_parser, key, test_json, expected_json +): + template, parser = template_and_parser("json", "glossary") + parser.delete(key) - assert parser.stringify() == json.dumps(expected_json) + + assert parser.stringify() == template.read_text(encoding="utf-8") @pytest.mark.parametrize( -- cgit v1.2.3 From 3a5336c20a983a01bf1495914932f490e97e8d23 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 10 Jul 2020 20:29:46 -0500 Subject: Add empty yaml and toml parsers --- config_file/parsers/toml_parser.py | 4 ++++ config_file/parsers/yaml_parser.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py index e69de29..2b3a48f 100644 --- a/config_file/parsers/toml_parser.py +++ b/config_file/parsers/toml_parser.py @@ -0,0 +1,4 @@ +import toml + +class TomlParser(BaseParser): + pass \ No newline at end of file diff --git a/config_file/parsers/yaml_parser.py b/config_file/parsers/yaml_parser.py index e69de29..e7e88b5 100644 --- a/config_file/parsers/yaml_parser.py +++ b/config_file/parsers/yaml_parser.py @@ -0,0 +1,4 @@ + + +class YamlParser: + pass \ No newline at end of file -- cgit v1.2.3 From 2d6b50509cc29516f9ef379619d9a3ac15c5c629 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 11 Jul 2020 14:56:53 -0500 Subject: Fix imports in tests/ --- tests/test_base_parser.py | 2 +- tests/test_config_file.py | 3 ++- tests/test_ini_parser.py | 2 +- tests/test_json_parser.py | 2 +- tests/test_parse_value.py | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/test_base_parser.py b/tests/test_base_parser.py index 6bb58e7..95c0c39 100644 --- a/tests/test_base_parser.py +++ b/tests/test_base_parser.py @@ -1,6 +1,6 @@ import pytest -from config_file import BaseParser +from config_file.parsers import BaseParser class ConcreteParser(BaseParser): diff --git a/tests/test_config_file.py b/tests/test_config_file.py index d89edc6..899e644 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -3,7 +3,8 @@ from pathlib import Path import pytest -from config_file import BaseParser, ConfigFile +from config_file.config_file import ConfigFile +from config_file.parsers import BaseParser SUPPORTED_FILE_TYPES = ["ini", "json"] diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index fb92373..d39758e 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,7 +1,7 @@ import pytest from config_file.exceptions import ParsingError -from config_file.parsers.ini_parser import IniParser +from config_file.parsers import IniParser @pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"]) diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index 18a2b6f..b622f01 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -3,7 +3,7 @@ import json import pytest from config_file.exceptions import ParsingError -from config_file.parsers.json_parser import JsonParser +from config_file.parsers import JsonParser def test_invalid_json(templated_parser): diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py index 81db472..37c68b5 100644 --- a/tests/test_parse_value.py +++ b/tests/test_parse_value.py @@ -1,6 +1,6 @@ import pytest -from config_file.parsers.parse_value import can_be_parsed_as_bool, parse_value +from config_file.parsers import can_be_parsed_as_bool, parse_value @pytest.mark.parametrize( -- cgit v1.2.3 From fbf798d86b1c2a5d70f39f084ee4e2a376fe6914 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 11 Jul 2020 14:57:24 -0500 Subject: Add dummy toml and yaml parsers --- CHANGELOG.md | 55 +++++++++++++++----------------------- config_file/parsers/toml_parser.py | 2 ++ config_file/parsers/yaml_parser.py | 6 ++--- 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29e4663..037dcc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,12 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased -Added +### Added - A generic `ConfigFileError`. - More type hints from `typings` have been added to `ConfigFile` for better type information. +- `YamlParser` and `TomlParser`. This means `ConfigFile` can now accept yaml and toml files, + as long as `PyYaml` is also installed if you're trying to use a yaml file and `toml` is + installed if you're trying to use a toml file. -Changed +### Changed - `ParsingError` now inherits from `ConfigFileError` rather than `Exception`. - Errors have been moved to `exceptions.py`. @@ -26,7 +29,7 @@ Changed ## 0.10.0 - 2020-05-10 -Changed +### Changed - `toml` and `pyyaml` are now optional extra dependencies. This allows you to not have to install them if you aren't using them. @@ -40,7 +43,7 @@ Changed features is worth it or see if there are ways to work around it with configparser that are ideal. -Fixed +### Fixed - You can now specify a default and still coerce your return type. Previously, if you specified a default, there was no logic in that branch to coerce your @@ -48,7 +51,7 @@ Fixed ## 0.9.0 - 2020-02-09 -Added +### Added - `default` optional parameter to the `get()` method of `ConfigFile`. The allows a default value to be fallen back to if the given key is missing. @@ -57,7 +60,7 @@ Added allows you to coerce the return type to one of your choosing by feeding it the return value. -Changed +### Changed - Automatic type parsing is now off by default. This is because of the addition of the `return_type` optional parameter. After using the package more, I think @@ -71,14 +74,14 @@ Changed ## 0.8.0 - 2020-02-02 -Added +### Added - More type hints to `ConfigFile` and `IniParser`. - `nested_lookup` dependency to help with modifying deeply nested structures (JSON + YAML) - `JsonParser` so you can now specify `.json` files to be parsed. -Changed +### Changed - The original content of the passed in file is now called `content` instead of `contents`. This is for consistency since the parsed version is @@ -86,13 +89,13 @@ Changed ## 0.7.0 - 2020-01-27 -Fixed +### Fixed - Support for using custom parsers with `ConfigFile` with the `parser` optional argument. This was technically supported before, but it was not tested and found to not actually use the passed in parser once tested. -Changed +### Changed - The `reset()` method on `ConfigFile` is now called `restore_original()`. The behavior is the same. This was done to better describe what exactly that method @@ -103,19 +106,19 @@ Changed ## 0.6.0 - 2020-01-24 -Changed +### Changed - Bumped down python version requirement to 3.6 and now test 3.6, 3.7, and 3.8 on CI. ## 0.5.0 - 2020-01-19 -Added +### Added - Support for retrieving entire sections as a `dict` with `get()`. ## 0.4.0 - 2020-01-19 -Added +### Added - `reset()` and `save()` methods to `ConfigFile`. This allows you to reset your configuration file to an "original state," given the original config file path. @@ -128,12 +131,12 @@ Added ## 0.3.3 - 2020-01-18 -Changed +### Changed - `_split_on_dot` is no longer in base parser (now in a utils file). Also, the default behavior is now to split on every dot and split on only the last dot if specified -Fixed +### Fixed - `ConfigFile` was trying to use `_split_on_dot`, but it no longer inherited from base parser. @@ -144,13 +147,13 @@ Fixed ## 0.3.1 - 2020-01-17 -Fixed +### Fixed - `isinstance` call for `BaseParser` to be correctly used. ## 0.3.0 - 2020-01-17 -Added +### Added - Abstract base parser as a contract for concrete file format parser implementations. @@ -166,24 +169,10 @@ Added ## 0.2.0 - 2020-01-04 -Added +### Added - Parsing of strings to their native values ## 0.1.0 - 2020-01-04 -Added - -- Created the config_file package with only the version inside it - -- toml and pyyaml to dependencies - -- Pre-commit pipeline with autoflake, isort, black, and flake8 - -- MIT license - -- Contributing guidelines - -- bump2version for easily upgrading versions of config file - -- pytest to dev dependencies for testing later on + - Initial Release \ No newline at end of file diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py index 2b3a48f..64ab216 100644 --- a/config_file/parsers/toml_parser.py +++ b/config_file/parsers/toml_parser.py @@ -1,3 +1,5 @@ +from config_file.parsers import BaseParser + import toml class TomlParser(BaseParser): diff --git a/config_file/parsers/yaml_parser.py b/config_file/parsers/yaml_parser.py index e7e88b5..48722b5 100644 --- a/config_file/parsers/yaml_parser.py +++ b/config_file/parsers/yaml_parser.py @@ -1,4 +1,4 @@ +from config_file.parsers import BaseParser - -class YamlParser: - pass \ No newline at end of file +class YamlParser(BaseParser): + pass -- cgit v1.2.3 From 34d81c75ee3e98c0455393a07329296f461cad53 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 12 Jul 2020 09:10:37 -0500 Subject: Add more type hints to ConfigFile --- config_file/config_file.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/config_file/config_file.py b/config_file/config_file.py index e037a82..de8b851 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -25,7 +25,7 @@ class ConfigFile: :raises ValueError: If the specified file path does not have an extension that is supported or it is a directory. """ - if type(file_path) is not Path: + if not isinstance(file_path, Path): file_path = Path(file_path) self.__path = create_config_path(file_path) @@ -38,7 +38,7 @@ class ConfigFile: def __determine_parser( self, file_path: Type[PurePath], parser: Type[BaseParser] = None - ) -> BaseParser: + ) -> Type[BaseParser]: if parser is not None and not inspect.isabstract(parser): return parser(self.__contents) @@ -90,7 +90,7 @@ class ConfigFile: """Sets the value of a key.""" return self.__parser.set(key, value) - def delete(self, section_key: str): + def delete(self, section_key: str) -> bool: """Deletes a key/value pair or entire sections.""" return self.__parser.delete(section_key) @@ -108,7 +108,7 @@ class ConfigFile: """ return self.__parser.has(section_key) - def restore_original(self, original_file_path: Union[str, Type[PurePath]] = None): + def restore_original(self, original_file_path: Union[str, Type[PurePath]] = None) -> 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 @@ -133,7 +133,7 @@ class ConfigFile: self.__path.expanduser().unlink() copyfile(original_file_path, self.__path) - self.__contents = read_file(self.path) + self.__contents = read_file(self.__path) self.__parser.parsed_content = self.__parser.parse(self.__contents) return True -- cgit v1.2.3 From 12dd1e7a627b147cfe407c79a09c3477b4a9c623 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 12 Jul 2020 09:11:06 -0500 Subject: Simplify BaseParser import --- config_file/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config_file/__init__.py b/config_file/__init__.py index 5fb800a..1b2094f 100644 --- a/config_file/__init__.py +++ b/config_file/__init__.py @@ -1,6 +1,6 @@ from config_file.config_file import ConfigFile from config_file.exceptions import ConfigFileError, ParsingError -from config_file.parsers.base_parser import BaseParser +from config_file.parsers import BaseParser __version__ = "0.10.0" -- cgit v1.2.3 From b467d83e7d67dbda82663634ff60e975f722b115 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 12 Jul 2020 09:11:27 -0500 Subject: Add to __all__ --- config_file/parsers/__init__.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/config_file/parsers/__init__.py b/config_file/parsers/__init__.py index 415f4f9..8da520e 100644 --- a/config_file/parsers/__init__.py +++ b/config_file/parsers/__init__.py @@ -1,6 +1,23 @@ from .base_parser import BaseParser from .ini_parser import IniParser from .json_parser import JsonParser -from .parse_value import parse_value +from .toml_parser import TomlParser +from .yaml_parser import YamlParser +from .parse_value import ( + parse_value, + can_be_parsed_as_bool, + can_be_parsed_as_float, + can_be_parsed_as_int +) -__all__ = ["BaseParser", "IniParser", "JsonParser", "parse_value"] +__all__ = [ + "BaseParser", + "IniParser", + "JsonParser", + "TomlParser", + "YamlParser", + "parse_value", + "can_be_parsed_as_bool", + "can_be_parsed_as_float", + "can_be_parsed_as_int" +] -- 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(-) 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 7568a94560b2381f7d7ba77f2c64473cf6842110 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 19 Jul 2020 13:15:24 -0500 Subject: Add more info on installation with poetry and zsh --- README.md | 20 +- config_file/nested-lookup/.gitignore | 11 + config_file/nested-lookup/.travis.yml | 9 + config_file/nested-lookup/CONTRIBUTING.rst | 7 + .../nested-lookup/nested_lookup/__init__.py | 8 + .../nested-lookup/nested_lookup/lookup_api.py | 214 +++++++ .../nested-lookup/nested_lookup/nested_lookup.py | 182 ++++++ config_file/nested_lookup/.gitignore | 11 + config_file/nested_lookup/.travis.yml | 9 + config_file/nested_lookup/CONTRIBUTING.rst | 7 + config_file/nested_lookup/__init__.py | 8 + config_file/nested_lookup/lookup_api.py | 214 +++++++ config_file/nested_lookup/nested_lookup.py | 182 ++++++ .../nested_lookup/nested_lookup/__init__.py | 8 + .../nested_lookup/nested_lookup/lookup_api.py | 214 +++++++ .../nested_lookup/nested_lookup/nested_lookup.py | 182 ++++++ tests/test_lookup_api.py | 619 +++++++++++++++++++++ tests/test_nested_lookup.py | 445 +++++++++++++++ 18 files changed, 2349 insertions(+), 1 deletion(-) create mode 100644 config_file/nested-lookup/.gitignore create mode 100644 config_file/nested-lookup/.travis.yml create mode 100644 config_file/nested-lookup/CONTRIBUTING.rst create mode 100644 config_file/nested-lookup/nested_lookup/__init__.py create mode 100644 config_file/nested-lookup/nested_lookup/lookup_api.py create mode 100644 config_file/nested-lookup/nested_lookup/nested_lookup.py create mode 100644 config_file/nested_lookup/.gitignore create mode 100644 config_file/nested_lookup/.travis.yml create mode 100644 config_file/nested_lookup/CONTRIBUTING.rst create mode 100644 config_file/nested_lookup/__init__.py create mode 100644 config_file/nested_lookup/lookup_api.py create mode 100644 config_file/nested_lookup/nested_lookup.py create mode 100644 config_file/nested_lookup/nested_lookup/__init__.py create mode 100644 config_file/nested_lookup/nested_lookup/lookup_api.py create mode 100644 config_file/nested_lookup/nested_lookup/nested_lookup.py create mode 100644 tests/test_lookup_api.py create mode 100644 tests/test_nested_lookup.py diff --git a/README.md b/README.md index c7eebda..3a54880 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ JSON, YAML, or TOML. For the time being, it only supports INI and JSON. ## Installation -Config File is available to download through PyPI using pip. +Config File is available to download through PyPI. ```bash $ pip install config-file @@ -29,6 +29,24 @@ If you want to manipulate YAML and TOML, you'll want to download the extras as w $ pip install config-file[yaml, toml] ``` +You can also use Poetry. + +```bash +$ poetry install config-file -E yaml -E toml +``` + +If you use ZSH as your shell, brackets (`[]`) are used for glob matching +and you'll have to put the package name in quotes if you're downloading +the extras too. + +```zsh +% pip install 'config-file[yaml, toml]' + +OR + +% 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/nested-lookup/.gitignore b/config_file/nested-lookup/.gitignore new file mode 100644 index 0000000..c9bd7c4 --- /dev/null +++ b/config_file/nested-lookup/.gitignore @@ -0,0 +1,11 @@ +__pycache__* +.pytest_cache* +*.pyc +.coverage +build/* +dist/* +nested_lookup.egg-info/* +.cache +.vscode +git_commit.sh +venv diff --git a/config_file/nested-lookup/.travis.yml b/config_file/nested-lookup/.travis.yml new file mode 100644 index 0000000..5551955 --- /dev/null +++ b/config_file/nested-lookup/.travis.yml @@ -0,0 +1,9 @@ +language: python +python: + - "2.7" + - "3.6" +# command to install dependencies +install: + - pip install . +# command to run tests +script: pytest \ No newline at end of file diff --git a/config_file/nested-lookup/CONTRIBUTING.rst b/config_file/nested-lookup/CONTRIBUTING.rst new file mode 100644 index 0000000..dde9f01 --- /dev/null +++ b/config_file/nested-lookup/CONTRIBUTING.rst @@ -0,0 +1,7 @@ +The ``nested-lookup`` library is public domain and open source. + +To contribute please make sure all tests pass. + +If you are adding new functionality please write a unit test. + +To avoid discussions regarding code formatting, please use `black `_ with default settings. diff --git a/config_file/nested-lookup/nested_lookup/__init__.py b/config_file/nested-lookup/nested_lookup/__init__.py new file mode 100644 index 0000000..0840df6 --- /dev/null +++ b/config_file/nested-lookup/nested_lookup/__init__.py @@ -0,0 +1,8 @@ +from .nested_lookup import ( + nested_lookup, + get_all_keys, + get_occurrence_of_key, + get_occurrence_of_value, + get_occurrences_and_values +) +from .lookup_api import nested_update, nested_delete, nested_alter diff --git a/config_file/nested-lookup/nested_lookup/lookup_api.py b/config_file/nested-lookup/nested_lookup/lookup_api.py new file mode 100644 index 0000000..a0af63a --- /dev/null +++ b/config_file/nested-lookup/nested_lookup/lookup_api.py @@ -0,0 +1,214 @@ +import copy +import warnings +from six import iteritems +from nested_lookup import nested_lookup + + +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 iteritems(document): + _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_list_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 iteritems(document): + 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/nested-lookup/nested_lookup/nested_lookup.py b/config_file/nested-lookup/nested_lookup/nested_lookup.py new file mode 100644 index 0000000..0b11b4d --- /dev/null +++ b/config_file/nested-lookup/nested_lookup/nested_lookup.py @@ -0,0 +1,182 @@ +from six import iteritems + +from collections import defaultdict + + +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 iteritems(document): + 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 iteritems(document): + 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 iteritems(dictionary): + 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] diff --git a/config_file/nested_lookup/.gitignore b/config_file/nested_lookup/.gitignore new file mode 100644 index 0000000..c9bd7c4 --- /dev/null +++ b/config_file/nested_lookup/.gitignore @@ -0,0 +1,11 @@ +__pycache__* +.pytest_cache* +*.pyc +.coverage +build/* +dist/* +nested_lookup.egg-info/* +.cache +.vscode +git_commit.sh +venv diff --git a/config_file/nested_lookup/.travis.yml b/config_file/nested_lookup/.travis.yml new file mode 100644 index 0000000..5551955 --- /dev/null +++ b/config_file/nested_lookup/.travis.yml @@ -0,0 +1,9 @@ +language: python +python: + - "2.7" + - "3.6" +# command to install dependencies +install: + - pip install . +# command to run tests +script: pytest \ No newline at end of file diff --git a/config_file/nested_lookup/CONTRIBUTING.rst b/config_file/nested_lookup/CONTRIBUTING.rst new file mode 100644 index 0000000..dde9f01 --- /dev/null +++ b/config_file/nested_lookup/CONTRIBUTING.rst @@ -0,0 +1,7 @@ +The ``nested-lookup`` library is public domain and open source. + +To contribute please make sure all tests pass. + +If you are adding new functionality please write a unit test. + +To avoid discussions regarding code formatting, please use `black `_ with default settings. diff --git a/config_file/nested_lookup/__init__.py b/config_file/nested_lookup/__init__.py new file mode 100644 index 0000000..0840df6 --- /dev/null +++ b/config_file/nested_lookup/__init__.py @@ -0,0 +1,8 @@ +from .nested_lookup import ( + nested_lookup, + get_all_keys, + get_occurrence_of_key, + get_occurrence_of_value, + get_occurrences_and_values +) +from .lookup_api import nested_update, nested_delete, nested_alter diff --git a/config_file/nested_lookup/lookup_api.py b/config_file/nested_lookup/lookup_api.py new file mode 100644 index 0000000..a0af63a --- /dev/null +++ b/config_file/nested_lookup/lookup_api.py @@ -0,0 +1,214 @@ +import copy +import warnings +from six import iteritems +from nested_lookup import nested_lookup + + +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 iteritems(document): + _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_list_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 iteritems(document): + 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/nested_lookup/nested_lookup.py b/config_file/nested_lookup/nested_lookup.py new file mode 100644 index 0000000..0b11b4d --- /dev/null +++ b/config_file/nested_lookup/nested_lookup.py @@ -0,0 +1,182 @@ +from six import iteritems + +from collections import defaultdict + + +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 iteritems(document): + 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 iteritems(document): + 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 iteritems(dictionary): + 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] diff --git a/config_file/nested_lookup/nested_lookup/__init__.py b/config_file/nested_lookup/nested_lookup/__init__.py new file mode 100644 index 0000000..0840df6 --- /dev/null +++ b/config_file/nested_lookup/nested_lookup/__init__.py @@ -0,0 +1,8 @@ +from .nested_lookup import ( + nested_lookup, + get_all_keys, + get_occurrence_of_key, + get_occurrence_of_value, + get_occurrences_and_values +) +from .lookup_api import nested_update, nested_delete, nested_alter diff --git a/config_file/nested_lookup/nested_lookup/lookup_api.py b/config_file/nested_lookup/nested_lookup/lookup_api.py new file mode 100644 index 0000000..a0af63a --- /dev/null +++ b/config_file/nested_lookup/nested_lookup/lookup_api.py @@ -0,0 +1,214 @@ +import copy +import warnings +from six import iteritems +from nested_lookup import nested_lookup + + +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 iteritems(document): + _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_list_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 iteritems(document): + 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/nested_lookup/nested_lookup/nested_lookup.py b/config_file/nested_lookup/nested_lookup/nested_lookup.py new file mode 100644 index 0000000..0b11b4d --- /dev/null +++ b/config_file/nested_lookup/nested_lookup/nested_lookup.py @@ -0,0 +1,182 @@ +from six import iteritems + +from collections import defaultdict + + +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 iteritems(document): + 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 iteritems(document): + 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 iteritems(dictionary): + 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] diff --git a/tests/test_lookup_api.py b/tests/test_lookup_api.py new file mode 100644 index 0000000..6a5cc87 --- /dev/null +++ b/tests/test_lookup_api.py @@ -0,0 +1,619 @@ +from unittest import TestCase + +from nested_lookup import nested_lookup, nested_update +from nested_lookup import nested_delete, nested_alter + + +class BaseLookUpApi(TestCase): + def setUp(self): + self.sample_data1 = { + "build_version": { + "model_name": "MacBook Pro", + "build_version": { + "processor_name": "Intel Core i7", + "processor_speed": "2.7 GHz", + "core_details": { + "build_version": "4", + "l2_cache(per_core)": "256 KB", + }, + }, + "number_of_cores": "4", + "memory": "256 KB", + }, + "os_details": {"product_version": "10.13.6", "build_version": "17G65"}, + "name": "Test", + "date": "YYYY-MM-DD HH:MM:SS", + } + + self.sample_data2 = { + "hardware_details": { + "model_name": "MacBook Pro", + "processor_details": [ + {"processor_name": "Intel Core i7", "processor_speed": "2.7 GHz"}, + {"total_number_of_cores": "4", "l2_cache(per_core)": "256 KB"}, + ], + "total_number_of_cores": "5", + "memory": "16 GB", + } + } + + self.sample_data3 = { + "values": [ + { + "checks": [ + { + "monitoring_zones": [ + "mzdfw", + "mzfra", + "mzhkg", + "mziad", + "mzlon", + "mzord", + "mzsyd", + ] + } + ] + } + ] + } + + self.sample_data4 = { + "modelversion": "1.1.0", + "vorgangsID": "1", + "versorgungsvorschlagDatum": 1510558834978, + "eingangsdatum": 1510558834978, + "plz": 82269, + "vertragsteile": [ + { + "typ": "1", + "beitragsDaten": { + "endalter": 85, + "brutto": 58.76, + "netto": 58.76, + "zahlungsrhythmus": "MONATLICH", + "plz": 86899, + }, + "beginn": 1512082800000, + "lebenslang": "True", + "ueberschussverwendung": { + "ueberschussverwendung": "2", + "indexoption": "3", + }, + "deckung": [ + { + "typ": "2", + "art": "1", + "leistung": {"value": 7500242424.0, "einheit": "2"}, + "leistungsRhythmus": "1", + } + ], + "zuschlagNachlass": [], + }, + { + "typ": "1", + "beitragsDaten": { + "endalter": 85, + "brutto": 0.6, + "netto": 0.6, + "zahlungsrhythmus": "1", + }, + "zuschlagNachlass": [], + }, + ], + } + + +class TestNestedDelete(BaseLookUpApi): + def test_sample_data1(self): + result = { + "os_details": {"product_version": "10.13.6"}, + "name": "Test", + "date": "YYYY-MM-DD HH:MM:SS", + } + self.assertEqual(result, nested_delete(self.sample_data1, "build_version")) + + def test_sample_data2(self): + result = { + "hardware_details": { + "model_name": "MacBook Pro", + "total_number_of_cores": "5", + "memory": "16 GB", + } + } + self.assertEqual(result, nested_delete(self.sample_data2, "processor_details")) + + def test_sample_data3(self): + result = {"values": [{"checks": [{}]}]} + self.assertEqual(result, nested_delete(self.sample_data3, "monitoring_zones")) + + +class TestNestedUpdate(BaseLookUpApi): + def test_sample_data1(self): + result = { + "build_version": "Test1", + "os_details": {"product_version": "10.13.6", "build_version": "Test1"}, + "name": "Test", + "date": "YYYY-MM-DD HH:MM:SS", + } + self.assertEqual( + result, nested_update(self.sample_data1, "build_version", "Test1") + ) + + def test_sample_data1_list_input_treat_list_as_element_true(self): + result = { + "build_version": ["Test5", "Test6", "Test7"], + "os_details": { + "product_version": "10.13.6", + "build_version": ["Test5", "Test6", "Test7"], + }, + "name": "Test", + "date": "YYYY-MM-DD HH:MM:SS", + } + + self.assertEqual( + result, + nested_update( + self.sample_data1, + "build_version", + ["Test5", "Test6", "Test7"], + treat_as_element=True, + ), + ) + + def test_nested_update_in_place_false(self): + """ + ested_update should mutate and return a copy of the original document + """ + before_id = id(self.sample_data1) + result = nested_update( + self.sample_data1, "build_version", "Test2", in_place=False + ) + after_id = id(result) + # the object ids should _not_ match. + self.assertNotEqual(before_id, after_id) + + def test_nested_update_in_place_true(self): + """ + nested_update should mutate and return the original document + """ + before_id = id(self.sample_data1) + result = nested_update( + self.sample_data1, "build_version", "Test2", in_place=True + ) + after_id = id(result) + # the object ids should match. + self.assertEqual(before_id, after_id) + + def test_nested_update_in_place_true_list_input(self): + doc = self.sample_data4 + # get all instances of the given element + findings = nested_lookup("plz", doc, False, True) + # alter those instances + updated_findings = list() + for key, val in findings.items(): + for elem in val: + updated_findings.append(elem + 300) + # update those instances with the altered results + doc_updated = nested_update( + doc, "plz", updated_findings, treat_as_element=False + ) + elem1 = doc_updated["plz"] # 85269 + # 87199 + elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + self.assertEqual(elem1, 82569) + self.assertEqual(elem2, 87199) + + def test_nested_update_in_place_false_list_input(self): + doc = self.sample_data4 + # get all instances of the given element + findings = nested_lookup("plz", doc, False, True) + # alter those instances + updated_findings = list() + for key, val in findings.items(): + for elem in val: + updated_findings.append(elem + 300) + # update those instances with the altered results + doc_updated = nested_update( + doc, "plz", updated_findings, in_place=False, treat_as_element=False + ) + elem1 = doc_updated["plz"] + elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + self.assertEqual(elem1, 82569) + self.assertEqual(elem2, 87199) + + def test_nested_update_in_place_false_list_input_as_element_false(self): + doc = self.sample_data4 + # get all instances of the given element + list_input = [1, 2, 3, 4, 5] + # update those instances with the altered results + doc_updated = nested_update( + doc, "plz", list_input, in_place=False, treat_as_element=False + ) + elem1 = doc_updated["plz"] + elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + # should not work without specifying "treat_list_as_element = True" + # in nested_update + self.assertNotEqual(elem1, list_input) + self.assertNotEqual(elem2, list_input) + + def test_nested_update_in_place_false_list_input_as_element_true(self): + doc = self.sample_data4 + # get all instances of the given element + list_input = [1, 2, 3, 4, 5] + # update those instances with the altered results + doc_updated = nested_update( + doc, "plz", list_input, in_place=False, treat_as_element=True + ) + elem1 = doc_updated["plz"] + elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + # should not work without specifying "treat_list_as_element = True" + # in nested_update + self.assertEqual(elem1, list_input) + self.assertEqual(elem2, list_input) + + def test_nested_update_in_place_true_list_input_as_element_false(self): + doc = self.sample_data4 + # get all instances of the given element + list_input = [1, 2, 3, 4, 5] + # update those instances with the altered results + doc_updated = nested_update( + doc, "plz", list_input, in_place=True, treat_as_element=False + ) + elem1 = doc_updated["plz"] + elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + # should not work without specifying "treat_list_as_element = True" + # in nested_update + self.assertNotEqual(elem1, list_input) + self.assertNotEqual(elem2, list_input) + + def test_nested_update_in_place_true_list_input_as_element_true(self): + doc = self.sample_data4 + # get all instances of the given element + list_input = [1, 2, 3, 4, 5] + # update those instances with the altered results + doc_updated = nested_update( + doc, "plz", list_input, in_place=True, treat_as_element=True + ) + elem1 = doc_updated["plz"] + elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + # should not work without specifying "treat_list_as_element = True" + # in nested_update + self.assertEqual(elem1, list_input) + self.assertEqual(elem2, list_input) + + def test_nested_delete_in_place_false(self): + """ + nested_delete should mutate and return a copy of the original document + """ + before_id = id(self.sample_data1) + result = nested_delete(self.sample_data1, "build_version", in_place=False) + after_id = id(result) + # the object ids should _not_ match. + self.assertNotEqual(before_id, after_id) + + def test_nested_delete_in_place_true(self): + """nested_delete should mutate and return the original document""" + before_id = id(self.sample_data1) + result = nested_delete(self.sample_data1, "build_version", in_place=True) + after_id = id(result) + # the object ids should match. + self.assertEqual(before_id, after_id) + + def test_nested_update_taco_for_example(self): + document = [{"taco": 42}, {"salsa": [{"burrito": {"taco": 69}}]}] + + updated_document = nested_update( + document, "taco", [100, 200], treat_as_element=False + ) + + self.assertEqual(updated_document[0]["taco"], 100) + # The multi-update version only works for scalar input, + # if you need to adress a list of dicts, you have to + # manually iterate over those and pass them to nested_update + # one by one + self.assertEqual(updated_document[1]["salsa"][0]["burrito"]["taco"], 200) + + def test_nested_update_raise_error(self): + doc = self.sample_data4 + # get all instances of the given element + list_input = 1 + # update those instances with the altered results + self.assertRaises( + Exception, + nested_update, + doc, + "plz", + list_input, + in_place=True, + treat_as_element=False, + ) + + def test_sample_data2(self): + result = { + "hardware_details": { + "model_name": "MacBook Pro", + "processor_details": {"test_key1": "test_value1"}, + "total_number_of_cores": "5", + "memory": "16 GB", + } + } + self.assertEqual( + result, + nested_update( + self.sample_data2, "processor_details", {"test_key1": "test_value1"} + ), + ) + + def test_sample_data3(self): + result = {"values": [{"checks": {"key1": ["value1"], "key2": "value2"}}]} + self.assertEqual( + result, + nested_update( + self.sample_data3, "checks", {"key1": ["value1"], "key2": "value2"} + ), + ) + + def test_sample_data4(self): + result = { + "modelversion": {"key1": ["value1"], "key2": "value2"}, + "vorgangsID": "1", + "versorgungsvorschlagDatum": 1510558834978, + "eingangsdatum": 1510558834978, + "plz": 82269, + "vertragsteile": [ + { + "typ": "1", + "beitragsDaten": { + "endalter": 85, + "brutto": 58.76, + "netto": 58.76, + "zahlungsrhythmus": "MONATLICH", + "plz": 86899, + }, + "beginn": 1512082800000, + "lebenslang": "True", + "ueberschussverwendung": { + "ueberschussverwendung": "2", + "indexoption": "3", + }, + "deckung": [ + { + "typ": "2", + "art": "1", + "leistung": {"value": 7500242424.0, "einheit": "2"}, + "leistungsRhythmus": "1", + } + ], + "zuschlagNachlass": [], + }, + { + "typ": "1", + "beitragsDaten": { + "endalter": 85, + "brutto": 0.6, + "netto": 0.6, + "zahlungsrhythmus": "1", + }, + "zuschlagNachlass": [], + }, + ], + } + self.assertEqual( + result, + nested_update( + self.sample_data4, + "modelversion", + {"key1": ["value1"], "key2": "value2"}, + ), + ) + + +class TestNestedAlter(BaseLookUpApi): + def test_nested_alter_in_place_true(self): + + # callback functions + def callback(data): + return str(data) + "###" + + doc_updated = nested_alter( + self.sample_data4, "vorgangsID", callback, in_place=True + ) + + vorgangsid = doc_updated["vorgangsID"] + + self.assertEqual(vorgangsid, "1###") + + def test_nested_alter_in_place_false(self): + + # callback functions + def callback(data): + return str(data) + "###" + + doc_updated = nested_alter( + self.sample_data4, "vorgangsID", callback, in_place=False + ) + + vorgangsid = doc_updated["vorgangsID"] + + # should not work without specifying + # "treat_list_as_element = True" in nested_update + self.assertEqual(vorgangsid, "1###") + + def test_nested_alter_list_input_in_place_true(self): + + # callback functions + def callback(data): + return str(data) + "###" + + doc_updated = nested_alter( + self.sample_data4, ["plz", "vorgangsID"], callback, in_place=True + ) + + plz1 = doc_updated["plz"] + plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + vorgangsid = doc_updated["vorgangsID"] + + # should not work without specifying + # "treat_list_as_element = True" in nested_update + self.assertEqual(plz1, "82269###") + self.assertEqual(plz2, "86899###") + self.assertEqual(vorgangsid, "1###") + + def test_nested_alter_list_input_with_args_in_place_true(self): + + # callback functions + def callback(data, str1, str2): + return str(data) + str1 + str2 + + doc_updated = nested_alter( + self.sample_data4, + ["plz", "vorgangsID"], + callback, + function_parameters=["abc", "def"], + in_place=True, + ) + + plz1 = doc_updated["plz"] + plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + vorgangsid = doc_updated["vorgangsID"] + + # should not work without specifying + # "treat_list_as_element = True" in nested_update + self.assertEqual(plz1, "82269abcdef") + self.assertEqual(plz2, "86899abcdef") + self.assertEqual(vorgangsid, "1abcdef") + + def test_nested_alter_list_input_with_args_in_place_false(self): + + # callback functions + def callback(data, str1, str2): + return str(data) + str1 + str2 + + doc_updated = nested_alter( + self.sample_data4, + ["plz", "vorgangsID"], + callback, + function_parameters=["abc", "def"], + in_place=False, + ) + + plz1 = doc_updated["plz"] + plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + vorgangsid = doc_updated["vorgangsID"] + + # should not work without specifying + # "treat_list_as_element = True" in nested_update + self.assertEqual(plz1, "82269abcdef") + self.assertEqual(plz2, "86899abcdef") + self.assertEqual(vorgangsid, "1abcdef") + + def test_nested_alter_list_input_in_place_false(self): + + # callback functions + def callback(data): + return str(data) + "###" + + doc_updated = nested_alter( + self.sample_data4, ["plz", "vorgangsID"], callback, in_place=False + ) + + plz1 = doc_updated["plz"] + plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + vorgangsid = doc_updated["vorgangsID"] + + # should not work without specifying + # "treat_list_as_element = True" in nested_update + self.assertEqual(plz1, "82269###") + self.assertEqual(plz2, "86899###") + self.assertEqual(vorgangsid, "1###") + + def test_nested_alter_taco_for_example(self): + documents = [{"taco": 42}, {"salsa": [{"burrito": {"taco": 69}}]}] + + # write a callback function which processes a scalar value. + # Be aware about the possible types which can be passed to + # the callback functions. + # In this example we can be sure that only int will be passed, + # in production you should check the type because it could be + # anything. + def callback(data): + return data + 10 # (data/100*10) + + # The alter-version only works for scalar input (one dict), + # if you need to adress a list of dicts, you have to + # manually iterate over those and pass them to + # nested_update one by one + out = [] + for elem in documents: + altered_document = nested_alter(elem, "taco", callback) + out.append(altered_document) + + self.maxDiff = None + self.assertEqual(out[0]["taco"], 52) + self.assertEqual(out[1]["salsa"][0]["burrito"]["taco"], 79) + + def test_nested_alter_work_with_right_order(self): + document = {"taco": 42, "salsa": [{"burrito":{"key":20}}], "key":50} + + def callback(data): + return data + 100 + + altered_document = nested_alter(document, "key", callback, in_place=True) + + self.assertEqual(altered_document["salsa"][0]["burrito"]["key"], 120) + self.assertEqual(altered_document["key"], 150) + + + def test_sample_data4(self): + + result = { + "modelversion": "1.1.0", + "vorgangsID": "1", + "versorgungsvorschlagDatum": 1510558834978, + "eingangsdatum": 1510558834978, + "plz": 82270, + "vertragsteile": [ + { + "typ": "1", + "beitragsDaten": { + "endalter": 85, + "brutto": 58.76, + "netto": 58.76, + "zahlungsrhythmus": "MONATLICH", + "plz": 86900, + }, + "beginn": 1512082800000, + "lebenslang": "True", + "ueberschussverwendung": { + "ueberschussverwendung": "2", + "indexoption": "3", + }, + "deckung": [ + { + "typ": "2", + "art": "1", + "leistung": {"value": 7500242424.0, "einheit": "2"}, + "leistungsRhythmus": "1", + } + ], + "zuschlagNachlass": [], + }, + { + "typ": "1", + "beitragsDaten": { + "endalter": 85, + "brutto": 0.6, + "netto": 0.6, + "zahlungsrhythmus": "1", + }, + "zuschlagNachlass": [], + }, + ], + } + + # add +1 to all plz + def callback(data): + return data + 1 + + self.maxDiff = None + self.assertEqual(result, nested_alter(self.sample_data4, "plz", callback)) diff --git a/tests/test_nested_lookup.py b/tests/test_nested_lookup.py new file mode 100644 index 0000000..b1bf44b --- /dev/null +++ b/tests/test_nested_lookup.py @@ -0,0 +1,445 @@ +from unittest import TestCase + +from nested_lookup import ( + nested_lookup, + get_all_keys, + get_occurrence_of_key, + get_occurrence_of_value, + get_occurrences_and_values +) + + +class TestNestedLookup(TestCase): + def setUp(self): + self.subject_dict = {"a": 1, "b": {"d": 100}, "c": {"d": 200}} + self.subject_dict2 = { + "name": "Russell Ballestrini", + "email_address": "test1@example.com", + "other": { + "secondary_email": "test2@example.com", + "EMAIL_RECOVERY": "test3@example.com", + "email_address": "test4@example.com", + }, + } + self.subject_dict3 = { + "build_version": { + "model_name": "MacBook Pro", + "build_version": { + "processor_name": "Intel Core i7", + "processor_speed": "2.7 GHz", + "core_details": { + "build_version": "4", + "l2_cache(per_core)": "256 KB", + }, + }, + "number_of_cores": "4", + "memory": "256 KB", + }, + "os_details": {"product_version": "10.13.6", "build_version": "17G65"}, + "name": "Test", + "date": "YYYY-MM-DD HH:MM:SS", + } + self.subject_dict4 = {1: "a", 2: {"b": 44, "C": 55}, 3: "d", 4: "e", "6776": "works"} + + def test_nested_lookup(self): + results = nested_lookup("d", self.subject_dict) + self.assertEqual(2, len(results)) + self.assertIn(100, results) + self.assertIn(200, results) + self.assertSetEqual({100, 200}, set(results)) + + def test_nested_lookup_wrapped_in_list(self): + results = nested_lookup("d", [{}, self.subject_dict, {}]) + self.assertEqual(2, len(results)) + self.assertIn(100, results) + self.assertIn(200, results) + self.assertSetEqual({100, 200}, set(results)) + + def test_nested_lookup_wrapped_in_list_in_dict_in_list(self): + results = nested_lookup("d", [{}, {"H": [self.subject_dict]}]) + self.assertEqual(2, len(results)) + self.assertIn(100, results) + self.assertIn(200, results) + self.assertSetEqual({100, 200}, set(results)) + + def test_nested_lookup_wrapped_in_list_in_list(self): + results = nested_lookup("d", [{}, [self.subject_dict, {}]]) + self.assertEqual(2, len(results)) + self.assertIn(100, results) + self.assertIn(200, results) + self.assertSetEqual({100, 200}, set(results)) + + def test_nested_lookup_key_is_non_str(self): + results = nested_lookup(key=4, document=self.subject_dict4) + self.assertIn("e", results) + + def test_wild_nested_lookup(self): + results = nested_lookup(key="mail", document=self.subject_dict2, wild=True) + self.assertEqual(4, len(results)) + self.assertIn("test1@example.com", results) + self.assertIn("test2@example.com", results) + self.assertIn("test3@example.com", results) + + def test_wild_nested_lookup_integer_keys_in_document(self): + results = nested_lookup(key="c", document=self.subject_dict4, wild=True) + self.assertIn(55, results) + + def test_wild_nested_lookup_integer_key_as_substring(self): + # test that wild works converts integers into strings before substring matching. + results = nested_lookup(key=77, document=self.subject_dict4, wild=True) + self.assertIn("works", results) + + def test_wild_with_keys_nested_lookup(self): + matches = nested_lookup( + key="mail", document=self.subject_dict2, wild=True, with_keys=True + ) + self.assertEqual(3, len(matches)) + self.assertIn("email_address", matches) + self.assertIn("secondary_email", matches) + self.assertIn("EMAIL_RECOVERY", matches) + self.assertSetEqual( + {"test1@example.com", "test4@example.com"}, set(matches["email_address"]) + ) + self.assertIn("test2@example.com", matches["secondary_email"]) + + def test_nested_lookup_with_keys(self): + matches = nested_lookup("d", self.subject_dict, with_keys=True) + self.assertIn("d", matches) + self.assertEqual(2, len(matches["d"])) + self.assertSetEqual({100, 200}, set(matches["d"])) + + def test_after_key_is_found(self): + result = nested_lookup(key="build_version", document=self.subject_dict3) + self.assertEqual(4, len(result)) + self.assertIn("4", result) + self.assertIn("17G65", result) + match1 = { + "processor_name": "Intel Core i7", + "processor_speed": "2.7 GHz", + "core_details": {"build_version": "4", "l2_cache(per_core)": "256 KB"}, + } + self.assertIn(match1, result) + match2 = { + "build_version": { + "processor_name": "Intel Core i7", + "processor_speed": "2.7 GHz", + "core_details": {"build_version": "4", "l2_cache(per_core)": "256 KB"}, + }, + "memory": "256 KB", + "model_name": "MacBook Pro", + "number_of_cores": "4", + } + self.assertIn(match2, result) + + +class TestGetAllKeys(TestCase): + def setUp(self): + self.sample1 = { + "hardware_details": { + "model_name": "MacBook Pro", + "processor_details": { + "processor_name": "Intel Core i7", + "processor_speed": "2.7 GHz", + "core_details": { + "total_numberof_cores": "4", + "l2_cache(per_core)": "256 KB", + }, + }, + "total_number_of_cores": "4", + "memory": "16 GB", + }, + "os_details": {"product_version": "10.13.6", "build_version": "17G65"}, + "name": "Test", + "date": "YYYY-MM-DD HH:MM:SS", + } + self.sample2 = { + "hardware_details": { + "model_name": "MacBook Pro", + "processor_details": [ + { + "processor_name": "Intel Core i7", + "processor_speed": "2.7 GHz", + "core_details": { + "total_numberof_cores": "4", + "l2_cache(per_core)": "256 KB", + }, + } + ], + "total_number_of_cores": "4", + "memory": "16 GB", + } + } + self.sample3 = { + "hardware_details": { + "model_name": "MacBook Pro", + "processor_details": [ + {"processor_name": "Intel Core i7", "processor_speed": "2.7 GHz"}, + {"total_numberof_cores": "4", "l2_cache(per_core)": "256 KB"}, + ], + "total_number_of_cores": "4", + "memory": "16 GB", + } + } + self.sample4 = { + "values": [ + { + "checks": [ + { + "monitoring_zones": [ + "mzdfw", + "mzfra", + "mzhkg", + "mziad", + "mzlon", + "mzord", + "mzsyd", + ] + } + ] + } + ] + } + self.sample5 = [ + { + "listings": [ + { + "name": "title", + "postcode": "postcode", + "full_address": "fulladdress", + "city": "city", + "lat": "latitude", + "lng": "longitude", + } + ] + } + ] + + def test_sample_data1(self): + result = get_all_keys(self.sample1) + self.assertEqual(15, len(result)) + keys_to_verify = [ + "model_name", + "core_details", + "l2_cache(per_core)", + "build_version", + "date", + ] + for key in keys_to_verify: + self.assertIn(key, result) + + def test_sample_data2(self): + result = get_all_keys(self.sample2) + self.assertEqual(10, len(result)) + keys_to_verify = [ + "hardware_details", + "processor_speed", + "total_numberof_cores", + "memory", + ] + for key in keys_to_verify: + self.assertIn(key, result) + + def test_sample_data3(self): + result = get_all_keys(self.sample3) + self.assertEqual(9, len(result)) + keys_to_verify = [ + "processor_details", + "processor_name", + "l2_cache(per_core)", + "total_number_of_cores", + ] + for key in keys_to_verify: + self.assertIn(key, result) + + def test_sample_data4(self): + result = get_all_keys(self.sample4) + self.assertEqual(3, len(result)) + keys_to_verify = ["values", "checks", "monitoring_zones"] + for key in keys_to_verify: + self.assertIn(key, result) + + def test_sample_data5(self): + result = get_all_keys(self.sample5) + self.assertEqual(7, len(result)) + keys_to_verify = [ + "listings", + "name", + "postcode", + "full_address", + "city", + "lat", + "lng", + ] + for key in keys_to_verify: + self.assertIn(key, result) + + +class TestGetOccurrence(TestCase): + def setUp(self): + self.sample1 = { + "build_version": { + "model_name": "MacBook Pro", + "build_version": { + "processor_name": "Intel Core i7", + "processor_speed": "2.7 GHz", + "core_details": { + "build_version": "4", + "l2_cache(per_core)": "256 KB", + }, + }, + "number_of_cores": "4", + "memory": "256 KB", + }, + "os_details": {"product_version": "10.13.6", "build_version": "17G65"}, + "name": "Test", + "date": "YYYY-MM-DD HH:MM:SS", + } + self.sample2 = { + "hardware_details": { + "model_name": "MacBook Pro", + "processor_details": [ + { + "processor_name": "4", + "processor_speed": "2.7 GHz", + "core_details": { + "total_numberof_cores": "4", + "l2_cache(per_core)": "256 KB", + }, + } + ], + "total_number_of_cores": "4", + "memory": "16 GB", + } + } + self.sample3 = { + "hardware_details": { + "model_name": "MacBook Pro", + "processor_details": [ + {"total_number_of_cores": "4", "processor_speed": "2.7 GHz"}, + {"total_number_of_cores": "4", "l2_cache(per_core)": "256 KB"}, + ], + "total_number_of_cores": "4", + "memory": "16 GB", + } + } + self.sample4 = { + "values": [ + { + "checks": [ + { + "monitoring_zones": [ + "mzdfw", + "mzfra", + "mzhkg", + "mziad", + "mzlon", + "mzord", + "mzsyd", + ] + } + ] + } + ] + } + + self.sample5 = { + "hardware_details": { + "model_name": "MacBook Pro", + "total_number_of_cores": 0, + "memory": False, + } + } + + self.sample6 = [ + { + "processor_name": "4", + "processor_speed": "2.7 GHz", + "core_details": { + "total_numberof_cores": "4", + "l2_cache(per_core)": "256 KB", + } + } + ] + + self.sample7 = [ + { + "processor_name": "4", + "processor_speed": "2.7 GHz", + "core_details": { + "total_numberof_cores": "4", + "l2_cache(per_core)": "256 KB", + } + }, + { + "processor_name": "4", + "processor_speed": "2.7 GHz", + "core_details": { + "total_numberof_cores": "4", + "l2_cache(per_core)": "256 KB", + } + } + ] + + def test_sample_data1(self): + result = get_occurrence_of_key(self.sample1, "build_version") + self.assertEqual(4, result) + result = get_occurrence_of_value(self.sample1, "256 KB") + self.assertEqual(2, result) + + def test_sample_data2(self): + result = get_occurrence_of_key(self.sample2, "core_details") + self.assertEqual(1, result) + result = get_occurrence_of_value(self.sample2, "4") + self.assertEqual(3, result) + + def test_sample_data3(self): + result = get_occurrence_of_key(self.sample3, "total_number_of_cores") + self.assertEqual(3, result) + result = get_occurrence_of_value(self.sample3, "4") + self.assertEqual(3, result) + + def test_sample_data4(self): + result = get_occurrence_of_key(self.sample4, "checks") + self.assertEqual(1, result) + result = get_occurrence_of_value(self.sample4, "mziad") + self.assertEqual(1, result) + # Add one more value in key "monitoring_zones" and verify + self.sample4["values"][0]["checks"][0]["monitoring_zones"].append("mziad") + self.assertEqual(2, get_occurrence_of_value(self.sample4, "mziad")) + + def test_sample_data5(self): + self.assertEqual( + 1, get_occurrence_of_key(self.sample5, "total_number_of_cores") + ) + self.assertEqual(1, get_occurrence_of_key(self.sample5, "memory")) + # Add key 'memory' and verify + self.sample5["memory"] = 0 + self.assertEqual(2, get_occurrence_of_key(self.sample5, "memory")) + + def test_sample_data6(self): + value = '4' + result = get_occurrences_and_values(self.sample6, value) + self.assertEqual(2, result[value]['occurrences']) + self.assertEqual(2, len(result[value]['values'])) + + def test_sample_data7(self): + value = '2.7 GHz' + result = get_occurrences_and_values(self.sample6, value) + self.assertEqual(1, result[value]['occurrences']) + self.assertEqual(1, len(result[value]['values'])) + + def test_sample_data8(self): + value = '4' + result = get_occurrences_and_values(self.sample7, value) + self.assertEqual(4, result[value]['occurrences']) + self.assertEqual(4, len(result[value]['values'])) + + def test_sample_data9(self): + value = '5' + result = get_occurrences_and_values(self.sample7, value) + self.assertEqual(0, result[value]['occurrences']) + self.assertEqual(0, len(result[value]['values'])) + + +if __name__ == "__main__": + pass -- cgit v1.2.3 From bc1280444ea3d5d4e5b5afd4e39bfb9365689393 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 19 Jul 2020 13:17:09 -0500 Subject: Add nested-lookup to project russellballestrini is a python project in the public domain. Rather than installing it as a dependency, this adds it as part of the config-file package now, which allows more control. --- config_file/nested-lookup/.gitignore | 11 - config_file/nested-lookup/.travis.yml | 9 - config_file/nested-lookup/CONTRIBUTING.rst | 7 - .../nested-lookup/nested_lookup/__init__.py | 8 - .../nested-lookup/nested_lookup/lookup_api.py | 214 --------------- .../nested-lookup/nested_lookup/nested_lookup.py | 182 ------------- config_file/nested_lookup/.gitignore | 11 - config_file/nested_lookup/.travis.yml | 9 - config_file/nested_lookup/CONTRIBUTING.rst | 7 - config_file/nested_lookup/__init__.py | 6 +- config_file/nested_lookup/lookup_api.py | 16 +- config_file/nested_lookup/nested_lookup.py | 8 +- .../nested_lookup/nested_lookup/__init__.py | 8 - .../nested_lookup/nested_lookup/lookup_api.py | 214 --------------- .../nested_lookup/nested_lookup/nested_lookup.py | 182 ------------- config_file/parsers/json_parser.py | 12 +- config_file/parsers/toml_parser.py | 105 +++++++- poetry.lock | 297 ++++++++++----------- pyproject.toml | 8 +- tests/test_lookup_api.py | 4 +- tests/test_nested_lookup.py | 2 +- 21 files changed, 278 insertions(+), 1042 deletions(-) delete mode 100644 config_file/nested-lookup/.gitignore delete mode 100644 config_file/nested-lookup/.travis.yml delete mode 100644 config_file/nested-lookup/CONTRIBUTING.rst delete mode 100644 config_file/nested-lookup/nested_lookup/__init__.py delete mode 100644 config_file/nested-lookup/nested_lookup/lookup_api.py delete mode 100644 config_file/nested-lookup/nested_lookup/nested_lookup.py delete mode 100644 config_file/nested_lookup/.gitignore delete mode 100644 config_file/nested_lookup/.travis.yml delete mode 100644 config_file/nested_lookup/CONTRIBUTING.rst delete mode 100644 config_file/nested_lookup/nested_lookup/__init__.py delete mode 100644 config_file/nested_lookup/nested_lookup/lookup_api.py delete mode 100644 config_file/nested_lookup/nested_lookup/nested_lookup.py diff --git a/config_file/nested-lookup/.gitignore b/config_file/nested-lookup/.gitignore deleted file mode 100644 index c9bd7c4..0000000 --- a/config_file/nested-lookup/.gitignore +++ /dev/null @@ -1,11 +0,0 @@ -__pycache__* -.pytest_cache* -*.pyc -.coverage -build/* -dist/* -nested_lookup.egg-info/* -.cache -.vscode -git_commit.sh -venv diff --git a/config_file/nested-lookup/.travis.yml b/config_file/nested-lookup/.travis.yml deleted file mode 100644 index 5551955..0000000 --- a/config_file/nested-lookup/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -language: python -python: - - "2.7" - - "3.6" -# command to install dependencies -install: - - pip install . -# command to run tests -script: pytest \ No newline at end of file diff --git a/config_file/nested-lookup/CONTRIBUTING.rst b/config_file/nested-lookup/CONTRIBUTING.rst deleted file mode 100644 index dde9f01..0000000 --- a/config_file/nested-lookup/CONTRIBUTING.rst +++ /dev/null @@ -1,7 +0,0 @@ -The ``nested-lookup`` library is public domain and open source. - -To contribute please make sure all tests pass. - -If you are adding new functionality please write a unit test. - -To avoid discussions regarding code formatting, please use `black `_ with default settings. diff --git a/config_file/nested-lookup/nested_lookup/__init__.py b/config_file/nested-lookup/nested_lookup/__init__.py deleted file mode 100644 index 0840df6..0000000 --- a/config_file/nested-lookup/nested_lookup/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ -from .nested_lookup import ( - nested_lookup, - get_all_keys, - get_occurrence_of_key, - get_occurrence_of_value, - get_occurrences_and_values -) -from .lookup_api import nested_update, nested_delete, nested_alter diff --git a/config_file/nested-lookup/nested_lookup/lookup_api.py b/config_file/nested-lookup/nested_lookup/lookup_api.py deleted file mode 100644 index a0af63a..0000000 --- a/config_file/nested-lookup/nested_lookup/lookup_api.py +++ /dev/null @@ -1,214 +0,0 @@ -import copy -import warnings -from six import iteritems -from nested_lookup import nested_lookup - - -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 iteritems(document): - _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_list_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 iteritems(document): - 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/nested-lookup/nested_lookup/nested_lookup.py b/config_file/nested-lookup/nested_lookup/nested_lookup.py deleted file mode 100644 index 0b11b4d..0000000 --- a/config_file/nested-lookup/nested_lookup/nested_lookup.py +++ /dev/null @@ -1,182 +0,0 @@ -from six import iteritems - -from collections import defaultdict - - -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 iteritems(document): - 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 iteritems(document): - 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 iteritems(dictionary): - 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] diff --git a/config_file/nested_lookup/.gitignore b/config_file/nested_lookup/.gitignore deleted file mode 100644 index c9bd7c4..0000000 --- a/config_file/nested_lookup/.gitignore +++ /dev/null @@ -1,11 +0,0 @@ -__pycache__* -.pytest_cache* -*.pyc -.coverage -build/* -dist/* -nested_lookup.egg-info/* -.cache -.vscode -git_commit.sh -venv diff --git a/config_file/nested_lookup/.travis.yml b/config_file/nested_lookup/.travis.yml deleted file mode 100644 index 5551955..0000000 --- a/config_file/nested_lookup/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -language: python -python: - - "2.7" - - "3.6" -# command to install dependencies -install: - - pip install . -# command to run tests -script: pytest \ No newline at end of file diff --git a/config_file/nested_lookup/CONTRIBUTING.rst b/config_file/nested_lookup/CONTRIBUTING.rst deleted file mode 100644 index dde9f01..0000000 --- a/config_file/nested_lookup/CONTRIBUTING.rst +++ /dev/null @@ -1,7 +0,0 @@ -The ``nested-lookup`` library is public domain and open source. - -To contribute please make sure all tests pass. - -If you are adding new functionality please write a unit test. - -To avoid discussions regarding code formatting, please use `black `_ with default settings. diff --git a/config_file/nested_lookup/__init__.py b/config_file/nested_lookup/__init__.py index 0840df6..768facc 100644 --- a/config_file/nested_lookup/__init__.py +++ b/config_file/nested_lookup/__init__.py @@ -5,4 +5,8 @@ from .nested_lookup import ( get_occurrence_of_value, get_occurrences_and_values ) -from .lookup_api import nested_update, nested_delete, nested_alter +from .lookup_api import ( + nested_update, + nested_delete, + nested_alter +) diff --git a/config_file/nested_lookup/lookup_api.py b/config_file/nested_lookup/lookup_api.py index a0af63a..e103534 100644 --- a/config_file/nested_lookup/lookup_api.py +++ b/config_file/nested_lookup/lookup_api.py @@ -1,7 +1,7 @@ import copy import warnings -from six import iteritems -from nested_lookup import nested_lookup + +from .nested_lookup import nested_lookup def nested_delete(document, key, in_place=False): @@ -26,7 +26,7 @@ def _nested_delete(document, key): elif isinstance(document, dict): if document.get(key): del document[key] - for dict_key, dict_value in iteritems(document): + for dict_key, dict_value in document.items(): _nested_delete(document=dict_value, key=key) return document @@ -34,16 +34,21 @@ def _nested_delete(document, key): 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_list_element (bool): + + 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 @@ -51,6 +56,7 @@ def nested_update(document, key, value, in_place=False, treat_as_element=True): 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. """ @@ -89,7 +95,7 @@ def _nested_update(document, key, value): document=list_items, key=key, value=value ) elif isinstance(document, dict): - for dict_key, dict_value in iteritems(document): + for dict_key, dict_value in document.items(): if dict_key == key: document[key] = value[0] if len(value) > 1: diff --git a/config_file/nested_lookup/nested_lookup.py b/config_file/nested_lookup/nested_lookup.py index 0b11b4d..9454a44 100644 --- a/config_file/nested_lookup/nested_lookup.py +++ b/config_file/nested_lookup/nested_lookup.py @@ -1,5 +1,3 @@ -from six import iteritems - from collections import defaultdict @@ -29,7 +27,7 @@ def _nested_lookup(key, document, wild=False, with_keys=False): yield result if isinstance(document, dict): - for k, v in iteritems(document): + for k, v in document.items(): if key == k or (wild and _is_case_insensitive_substring(key, k)): if with_keys: yield k, v @@ -61,7 +59,7 @@ def get_all_keys(dictionary): for list_items in document: recrusion(document=list_items) elif isinstance(document, dict): - for key, value in iteritems(document): + for key, value in document.items(): result_list.append(key) recrusion(document=value) return @@ -148,7 +146,7 @@ def _recursion(dictionary, item, keyword, occurrence, with_values=False): occurrence[0] += list(dictionary.values()).count(keyword) if with_values: values_list.append(dictionary) - for key, value in iteritems(dictionary): + for key, value in dictionary.items(): if isinstance(value, dict): _recursion(value, item, keyword, occurrence, with_values) elif isinstance(value, list): diff --git a/config_file/nested_lookup/nested_lookup/__init__.py b/config_file/nested_lookup/nested_lookup/__init__.py deleted file mode 100644 index 0840df6..0000000 --- a/config_file/nested_lookup/nested_lookup/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ -from .nested_lookup import ( - nested_lookup, - get_all_keys, - get_occurrence_of_key, - get_occurrence_of_value, - get_occurrences_and_values -) -from .lookup_api import nested_update, nested_delete, nested_alter diff --git a/config_file/nested_lookup/nested_lookup/lookup_api.py b/config_file/nested_lookup/nested_lookup/lookup_api.py deleted file mode 100644 index a0af63a..0000000 --- a/config_file/nested_lookup/nested_lookup/lookup_api.py +++ /dev/null @@ -1,214 +0,0 @@ -import copy -import warnings -from six import iteritems -from nested_lookup import nested_lookup - - -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 iteritems(document): - _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_list_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 iteritems(document): - 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/nested_lookup/nested_lookup/nested_lookup.py b/config_file/nested_lookup/nested_lookup/nested_lookup.py deleted file mode 100644 index 0b11b4d..0000000 --- a/config_file/nested_lookup/nested_lookup/nested_lookup.py +++ /dev/null @@ -1,182 +0,0 @@ -from six import iteritems - -from collections import defaultdict - - -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 iteritems(document): - 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 iteritems(document): - 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 iteritems(dictionary): - 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] diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py index 584a2d0..13b84b5 100644 --- a/config_file/parsers/json_parser.py +++ b/config_file/parsers/json_parser.py @@ -1,6 +1,6 @@ import json -from nested_lookup import ( +from config_file.nested_lookup import ( get_occurrence_of_key, nested_delete, nested_lookup, @@ -8,8 +8,7 @@ from nested_lookup import ( ) from config_file.exceptions import ParsingError -from config_file.parsers.base_parser import BaseParser -from config_file.parsers.parse_value import parse_value +from config_file.parsers import BaseParser, parse_value from config_file.utils import split_on_dot @@ -70,15 +69,14 @@ class JsonParser(BaseParser): 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 + key_causing_error = None try: for key in split_keys: - key_for_error = key + key_causing_error = key result = result[key] except TypeError: raise ParsingError( - f"Cannot get {key} because {key_for_error} is not subscriptable." + f"Cannot get {key} because {key_causing_error} is not subscriptable." ) else: result = self.parsed_content[key] diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py index 64ab216..ad3e7f4 100644 --- a/config_file/parsers/toml_parser.py +++ b/config_file/parsers/toml_parser.py @@ -1,6 +1,105 @@ -from config_file.parsers import BaseParser - import toml +from config_file.nested_lookup import ( + get_occurrence_of_key, + nested_delete, + nested_lookup, + nested_update, +) +from config_file.exceptions import ParsingError +from config_file.parsers import BaseParser, parse_value +from config_file.utils import split_on_dot + + class TomlParser(BaseParser): - pass \ No newline at end of file + """The TomlParser allows use of toml files with ConfigFile.""" + + 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 toml.loads(file_contents) + except toml.TomlDecodeError as error: + raise ParsingError(error) + + def get(self, key: str, parse_types: bool = False, 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 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 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, all: bool = False): + if 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, all: bool = False): + if 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, indent=4) + + def has(self, search_key: str) -> bool: + return get_occurrence_of_key(self.parsed_content, key=search_key) > 0 diff --git a/poetry.lock b/poetry.lock index 566d146..afe6577 100644 --- a/poetry.lock +++ b/poetry.lock @@ -9,7 +9,6 @@ version = "0.7.12" [[package]] category = "dev" description = "A small Python module for determining appropriate platform-specific dirs, e.g. a \"user data dir\"." -marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "appdirs" optional = false python-versions = "*" @@ -86,7 +85,6 @@ pytz = ">=2015.7" [[package]] category = "dev" description = "The uncompromising code formatter." -marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "black" optional = false python-versions = ">=3.6" @@ -127,18 +125,15 @@ description = "Python package for providing Mozilla's CA Bundle." name = "certifi" optional = false python-versions = "*" -version = "2020.4.5.2" +version = "2020.6.20" [[package]] category = "dev" description = "Validate configuration and produce human readable error messages." name = "cfgv" optional = false -python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "2.0.1" - -[package.dependencies] -six = "*" +python-versions = ">=3.6" +version = "3.0.0" [[package]] category = "dev" @@ -151,7 +146,6 @@ version = "3.0.4" [[package]] category = "dev" description = "Composable command line interface toolkit" -marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "click" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" @@ -172,11 +166,19 @@ description = "Code coverage measurement for Python" name = "coverage" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4" -version = "5.1" +version = "5.2" [package.extras] toml = ["toml"] +[[package]] +category = "dev" +description = "Distribution utilities" +name = "distlib" +optional = false +python-versions = "*" +version = "0.3.1" + [[package]] category = "dev" description = "Docutils -- Python Documentation Utilities" @@ -185,6 +187,14 @@ optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" version = "0.16" +[[package]] +category = "dev" +description = "A platform independent file lock." +name = "filelock" +optional = false +python-versions = "*" +version = "3.0.12" + [[package]] category = "dev" description = "the modular source code checker: pep8 pyflakes and co" @@ -208,7 +218,7 @@ description = "File identification library for Python" name = "identify" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" -version = "1.4.19" +version = "1.4.23" [package.extras] license = ["editdistance"] @@ -219,7 +229,7 @@ description = "Internationalized Domain Names in Applications (IDNA)" name = "idna" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "2.9" +version = "2.10" [[package]] category = "dev" @@ -236,7 +246,7 @@ marker = "python_version < \"3.8\"" name = "importlib-metadata" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7" -version = "1.6.1" +version = "1.7.0" [package.dependencies] zipp = ">=0.5" @@ -252,13 +262,9 @@ marker = "python_version < \"3.7\"" name = "importlib-resources" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7" -version = "2.0.0" +version = "3.0.0" [package.dependencies] -[package.dependencies.importlib-metadata] -python = "<3.8" -version = "*" - [package.dependencies.zipp] python = "<3.8" version = ">=0.4" @@ -316,18 +322,7 @@ description = "More routines for operating on iterables, beyond itertools" name = "more-itertools" optional = false python-versions = ">=3.5" -version = "8.3.0" - -[[package]] -category = "main" -description = "Python functions for working with deeply nested documents (lists and dicts)" -name = "nested-lookup" -optional = false -python-versions = "*" -version = "0.2.21" - -[package.dependencies] -six = "*" +version = "8.4.0" [[package]] category = "dev" @@ -349,22 +344,9 @@ version = "20.4" pyparsing = ">=2.0.2" six = "*" -[[package]] -category = "dev" -description = "Object-oriented filesystem paths" -marker = "python_version < \"3.6\"" -name = "pathlib2" -optional = false -python-versions = "*" -version = "2.3.5" - -[package.dependencies] -six = "*" - [[package]] category = "dev" description = "Utility library for gitignore style pattern matching of file paths." -marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "pathspec" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" @@ -418,7 +400,7 @@ description = "library with cross-python path, ini-parsing, io, code, log facili name = "py" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" -version = "1.8.1" +version = "1.9.0" [[package]] category = "dev" @@ -474,10 +456,6 @@ wcwidth = "*" python = "<3.8" version = ">=0.12" -[package.dependencies.pathlib2] -python = "<3.6" -version = ">=2.2.0" - [package.extras] checkqa-mypy = ["mypy (v0.761)"] testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "requests", "xmlschema"] @@ -488,11 +466,11 @@ description = "Pytest plugin for measuring coverage." name = "pytest-cov" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" -version = "2.9.0" +version = "2.10.0" [package.dependencies] coverage = ">=4.4" -pytest = ">=3.6" +pytest = ">=4.6" [package.extras] testing = ["fields", "hunter", "process-tests (2.0.2)", "six", "pytest-xdist", "virtualenv"] @@ -503,7 +481,7 @@ description = "pytest-sugar is a plugin for pytest that changes the default look name = "pytest-sugar" optional = false python-versions = "*" -version = "0.9.3" +version = "0.9.4" [package.dependencies] packaging = ">=14.1" @@ -529,11 +507,10 @@ version = "5.3.1" [[package]] category = "dev" description = "Alternative regular expression module, to replace re." -marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "regex" optional = false python-versions = "*" -version = "2020.6.8" +version = "2020.7.14" [[package]] category = "dev" @@ -541,7 +518,7 @@ description = "Python HTTP for Humans." name = "requests" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" -version = "2.23.0" +version = "2.24.0" [package.dependencies] certifi = ">=2017.4.17" @@ -566,7 +543,7 @@ version = "2.2.0" "aspy.refactor-imports" = "*" [[package]] -category = "main" +category = "dev" description = "Python 2 and 3 compatibility utilities" name = "six" optional = false @@ -697,7 +674,6 @@ test = ["pytest"] [[package]] category = "dev" description = "tasks runner for python projects" -marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "taskipy" optional = false python-versions = ">=3.6,<4.0" @@ -725,7 +701,6 @@ version = "0.10.1" [[package]] category = "dev" description = "a fork of Python 2 and 3 ast modules with type comment support" -marker = "python_version >= \"3.6\" and python_version < \"4.0\"" name = "typed-ast" optional = false python-versions = "*" @@ -750,11 +725,25 @@ description = "Virtual Python Environment builder" name = "virtualenv" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" -version = "16.7.10" +version = "20.0.27" + +[package.dependencies] +appdirs = ">=1.4.3,<2" +distlib = ">=0.3.1,<1" +filelock = ">=3.0.0,<4" +six = ">=1.9.0,<2" + +[package.dependencies.importlib-metadata] +python = "<3.8" +version = ">=0.12,<2" + +[package.dependencies.importlib-resources] +python = "<3.7" +version = ">=1.0" [package.extras] -docs = ["sphinx (>=1.8.0,<2)", "towncrier (>=18.5.0)", "sphinx-rtd-theme (>=0.4.2,<1)"] -testing = ["pytest (>=4.0.0,<5)", "coverage (>=4.5.0,<5)", "pytest-timeout (>=1.3.0,<2)", "six (>=1.10.0,<2)", "pytest-xdist", "pytest-localserver", "pypiserver", "mock", "xonsh"] +docs = ["sphinx (>=3)", "sphinx-argparse (>=0.2.5)", "sphinx-rtd-theme (>=0.4.3)", "towncrier (>=19.9.0rc1)", "proselint (>=0.10.2)"] +testing = ["pytest (>=4)", "coverage (>=5)", "coverage-enable-subprocess (>=1)", "pytest-xdist (>=1.31.0)", "pytest-mock (>=2)", "pytest-env (>=0.6.2)", "pytest-randomly (>=1)", "pytest-timeout (>=1)", "pytest-freezegun (>=0.4.1)", "flaky (>=3)", "packaging (>=20.0)", "xonsh (>=0.9.16)"] [[package]] category = "dev" @@ -762,7 +751,7 @@ description = "Measures the displayed width of unicode strings in a terminal" name = "wcwidth" optional = false python-versions = "*" -version = "0.2.4" +version = "0.2.5" [[package]] category = "dev" @@ -770,20 +759,20 @@ description = "Backport of pathlib-compatible object wrapper for zip files" marker = "python_version < \"3.8\"" name = "zipp" optional = false -python-versions = ">=2.7" -version = "1.2.0" +python-versions = ">=3.6" +version = "3.1.0" [package.extras] docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"] -testing = ["pathlib2", "unittest2", "jaraco.itertools", "func-timeout"] +testing = ["jaraco.itertools", "func-timeout"] [extras] toml = ["toml"] yaml = ["pyyaml"] [metadata] -content-hash = "cbcbacf7c64fd8aec4372a6979e3ef11a81c049e7baf24b1b396023209b960cd" -python-versions = "^3.5" +content-hash = "432d63eaf6fb166650820be893bb3364a954b8fcf81b618bbfb398dbb54de1f6" +python-versions = "^3.6" [metadata.files] alabaster = [ @@ -830,12 +819,12 @@ cached-property = [ {file = "cached_property-1.5.1-py2.py3-none-any.whl", hash = "sha256:3a026f1a54135677e7da5ce819b0c690f156f37976f3e30c5430740725203d7f"}, ] certifi = [ - {file = "certifi-2020.4.5.2-py2.py3-none-any.whl", hash = "sha256:9cd41137dc19af6a5e03b630eefe7d1f458d964d406342dd3edf625839b944cc"}, - {file = "certifi-2020.4.5.2.tar.gz", hash = "sha256:5ad7e9a056d25ffa5082862e36f119f7f7cec6457fa07ee2f8c339814b80c9b1"}, + {file = "certifi-2020.6.20-py2.py3-none-any.whl", hash = "sha256:8fc0819f1f30ba15bdb34cceffb9ef04d99f420f68eb75d901e9560b8749fc41"}, + {file = "certifi-2020.6.20.tar.gz", hash = "sha256:5930595817496dd21bb8dc35dad090f1c2cd0adfaf21204bf6732ca5d8ee34d3"}, ] cfgv = [ - {file = "cfgv-2.0.1-py2.py3-none-any.whl", hash = "sha256:fbd93c9ab0a523bf7daec408f3be2ed99a980e20b2d19b50fc184ca6b820d289"}, - {file = "cfgv-2.0.1.tar.gz", hash = "sha256:edb387943b665bf9c434f717bf630fa78aecd53d5900d2e05da6ad6048553144"}, + {file = "cfgv-3.0.0-py2.py3-none-any.whl", hash = "sha256:f22b426ed59cd2ab2b54ff96608d846c33dfb8766a67f0b4a6ce130ce244414f"}, + {file = "cfgv-3.0.0.tar.gz", hash = "sha256:04b093b14ddf9fd4d17c53ebfd55582d27b76ed30050193c14e560770c5360eb"}, ] chardet = [ {file = "chardet-3.0.4-py2.py3-none-any.whl", hash = "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"}, @@ -850,65 +839,76 @@ colorama = [ {file = "colorama-0.4.3.tar.gz", hash = "sha256:e96da0d330793e2cb9485e9ddfd918d456036c7149416295932478192f4436a1"}, ] coverage = [ - {file = "coverage-5.1-cp27-cp27m-macosx_10_12_x86_64.whl", hash = "sha256:0cb4be7e784dcdc050fc58ef05b71aa8e89b7e6636b99967fadbdba694cf2b65"}, - {file = "coverage-5.1-cp27-cp27m-macosx_10_13_intel.whl", hash = "sha256:c317eaf5ff46a34305b202e73404f55f7389ef834b8dbf4da09b9b9b37f76dd2"}, - {file = "coverage-5.1-cp27-cp27m-manylinux1_i686.whl", hash = "sha256:b83835506dfc185a319031cf853fa4bb1b3974b1f913f5bb1a0f3d98bdcded04"}, - {file = "coverage-5.1-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:5f2294dbf7875b991c381e3d5af2bcc3494d836affa52b809c91697449d0eda6"}, - {file = "coverage-5.1-cp27-cp27m-win32.whl", hash = "sha256:de807ae933cfb7f0c7d9d981a053772452217df2bf38e7e6267c9cbf9545a796"}, - {file = "coverage-5.1-cp27-cp27m-win_amd64.whl", hash = "sha256:bf9cb9a9fd8891e7efd2d44deb24b86d647394b9705b744ff6f8261e6f29a730"}, - {file = "coverage-5.1-cp27-cp27mu-manylinux1_i686.whl", hash = "sha256:acf3763ed01af8410fc36afea23707d4ea58ba7e86a8ee915dfb9ceff9ef69d0"}, - {file = "coverage-5.1-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:dec5202bfe6f672d4511086e125db035a52b00f1648d6407cc8e526912c0353a"}, - {file = "coverage-5.1-cp35-cp35m-macosx_10_12_x86_64.whl", hash = "sha256:7a5bdad4edec57b5fb8dae7d3ee58622d626fd3a0be0dfceda162a7035885ecf"}, - {file = "coverage-5.1-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:1601e480b9b99697a570cea7ef749e88123c04b92d84cedaa01e117436b4a0a9"}, - {file = "coverage-5.1-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:dbe8c6ae7534b5b024296464f387d57c13caa942f6d8e6e0346f27e509f0f768"}, - {file = "coverage-5.1-cp35-cp35m-win32.whl", hash = "sha256:a027ef0492ede1e03a8054e3c37b8def89a1e3c471482e9f046906ba4f2aafd2"}, - {file = "coverage-5.1-cp35-cp35m-win_amd64.whl", hash = "sha256:0e61d9803d5851849c24f78227939c701ced6704f337cad0a91e0972c51c1ee7"}, - {file = "coverage-5.1-cp36-cp36m-macosx_10_13_x86_64.whl", hash = "sha256:2d27a3f742c98e5c6b461ee6ef7287400a1956c11421eb574d843d9ec1f772f0"}, - {file = "coverage-5.1-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:66460ab1599d3cf894bb6baee8c684788819b71a5dc1e8fa2ecc152e5d752019"}, - {file = "coverage-5.1-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:5c542d1e62eece33c306d66fe0a5c4f7f7b3c08fecc46ead86d7916684b36d6c"}, - {file = "coverage-5.1-cp36-cp36m-win32.whl", hash = "sha256:2742c7515b9eb368718cd091bad1a1b44135cc72468c731302b3d641895b83d1"}, - {file = "coverage-5.1-cp36-cp36m-win_amd64.whl", hash = "sha256:dead2ddede4c7ba6cb3a721870f5141c97dc7d85a079edb4bd8d88c3ad5b20c7"}, - {file = "coverage-5.1-cp37-cp37m-macosx_10_13_x86_64.whl", hash = "sha256:01333e1bd22c59713ba8a79f088b3955946e293114479bbfc2e37d522be03355"}, - {file = "coverage-5.1-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:e1ea316102ea1e1770724db01998d1603ed921c54a86a2efcb03428d5417e489"}, - {file = "coverage-5.1-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:adeb4c5b608574a3d647011af36f7586811a2c1197c861aedb548dd2453b41cd"}, - {file = "coverage-5.1-cp37-cp37m-win32.whl", hash = "sha256:782caea581a6e9ff75eccda79287daefd1d2631cc09d642b6ee2d6da21fc0a4e"}, - {file = "coverage-5.1-cp37-cp37m-win_amd64.whl", hash = "sha256:00f1d23f4336efc3b311ed0d807feb45098fc86dee1ca13b3d6768cdab187c8a"}, - {file = "coverage-5.1-cp38-cp38-macosx_10_13_x86_64.whl", hash = "sha256:402e1744733df483b93abbf209283898e9f0d67470707e3c7516d84f48524f55"}, - {file = "coverage-5.1-cp38-cp38-manylinux1_i686.whl", hash = "sha256:a3f3654d5734a3ece152636aad89f58afc9213c6520062db3978239db122f03c"}, - {file = "coverage-5.1-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:6402bd2fdedabbdb63a316308142597534ea8e1895f4e7d8bf7476c5e8751fef"}, - {file = "coverage-5.1-cp38-cp38-win32.whl", hash = "sha256:8fa0cbc7ecad630e5b0f4f35b0f6ad419246b02bc750de7ac66db92667996d24"}, - {file = "coverage-5.1-cp38-cp38-win_amd64.whl", hash = "sha256:79a3cfd6346ce6c13145731d39db47b7a7b859c0272f02cdb89a3bdcbae233a0"}, - {file = "coverage-5.1-cp39-cp39-win32.whl", hash = "sha256:a82b92b04a23d3c8a581fc049228bafde988abacba397d57ce95fe95e0338ab4"}, - {file = "coverage-5.1-cp39-cp39-win_amd64.whl", hash = "sha256:bb28a7245de68bf29f6fb199545d072d1036a1917dca17a1e75bbb919e14ee8e"}, - {file = "coverage-5.1.tar.gz", hash = "sha256:f90bfc4ad18450c80b024036eaf91e4a246ae287701aaa88eaebebf150868052"}, + {file = "coverage-5.2-cp27-cp27m-macosx_10_13_intel.whl", hash = "sha256:d9ad0a988ae20face62520785ec3595a5e64f35a21762a57d115dae0b8fb894a"}, + {file = "coverage-5.2-cp27-cp27m-macosx_10_9_x86_64.whl", hash = "sha256:4bb385a747e6ae8a65290b3df60d6c8a692a5599dc66c9fa3520e667886f2e10"}, + {file = "coverage-5.2-cp27-cp27m-manylinux1_i686.whl", hash = "sha256:9702e2cb1c6dec01fb8e1a64c015817c0800a6eca287552c47a5ee0ebddccf62"}, + {file = "coverage-5.2-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:42fa45a29f1059eda4d3c7b509589cc0343cd6bbf083d6118216830cd1a51613"}, + {file = "coverage-5.2-cp27-cp27m-win32.whl", hash = "sha256:41d88736c42f4a22c494c32cc48a05828236e37c991bd9760f8923415e3169e4"}, + {file = "coverage-5.2-cp27-cp27m-win_amd64.whl", hash = "sha256:bbb387811f7a18bdc61a2ea3d102be0c7e239b0db9c83be7bfa50f095db5b92a"}, + {file = "coverage-5.2-cp27-cp27mu-manylinux1_i686.whl", hash = "sha256:3740b796015b889e46c260ff18b84683fa2e30f0f75a171fb10d2bf9fb91fc70"}, + {file = "coverage-5.2-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:ebf2431b2d457ae5217f3a1179533c456f3272ded16f8ed0b32961a6d90e38ee"}, + {file = "coverage-5.2-cp35-cp35m-macosx_10_13_x86_64.whl", hash = "sha256:d54d7ea74cc00482a2410d63bf10aa34ebe1c49ac50779652106c867f9986d6b"}, + {file = "coverage-5.2-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:87bdc8135b8ee739840eee19b184804e5d57f518578ffc797f5afa2c3c297913"}, + {file = "coverage-5.2-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:ed9a21502e9223f563e071759f769c3d6a2e1ba5328c31e86830368e8d78bc9c"}, + {file = "coverage-5.2-cp35-cp35m-win32.whl", hash = "sha256:509294f3e76d3f26b35083973fbc952e01e1727656d979b11182f273f08aa80b"}, + {file = "coverage-5.2-cp35-cp35m-win_amd64.whl", hash = "sha256:ca63dae130a2e788f2b249200f01d7fa240f24da0596501d387a50e57aa7075e"}, + {file = "coverage-5.2-cp36-cp36m-macosx_10_13_x86_64.whl", hash = "sha256:5c74c5b6045969b07c9fb36b665c9cac84d6c174a809fc1b21bdc06c7836d9a0"}, + {file = "coverage-5.2-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:c32aa13cc3fe86b0f744dfe35a7f879ee33ac0a560684fef0f3e1580352b818f"}, + {file = "coverage-5.2-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:1e58fca3d9ec1a423f1b7f2aa34af4f733cbfa9020c8fe39ca451b6071237405"}, + {file = "coverage-5.2-cp36-cp36m-win32.whl", hash = "sha256:3b2c34690f613525672697910894b60d15800ac7e779fbd0fccf532486c1ba40"}, + {file = "coverage-5.2-cp36-cp36m-win_amd64.whl", hash = "sha256:a4d511012beb967a39580ba7d2549edf1e6865a33e5fe51e4dce550522b3ac0e"}, + {file = "coverage-5.2-cp37-cp37m-macosx_10_13_x86_64.whl", hash = "sha256:32ecee61a43be509b91a526819717d5e5650e009a8d5eda8631a59c721d5f3b6"}, + {file = "coverage-5.2-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:6f91b4492c5cde83bfe462f5b2b997cdf96a138f7c58b1140f05de5751623cf1"}, + {file = "coverage-5.2-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:bfcc811883699ed49afc58b1ed9f80428a18eb9166422bce3c31a53dba00fd1d"}, + {file = "coverage-5.2-cp37-cp37m-win32.whl", hash = "sha256:60a3d36297b65c7f78329b80120f72947140f45b5c7a017ea730f9112b40f2ec"}, + {file = "coverage-5.2-cp37-cp37m-win_amd64.whl", hash = "sha256:12eaccd86d9a373aea59869bc9cfa0ab6ba8b1477752110cb4c10d165474f703"}, + {file = "coverage-5.2-cp38-cp38-macosx_10_13_x86_64.whl", hash = "sha256:d82db1b9a92cb5c67661ca6616bdca6ff931deceebb98eecbd328812dab52032"}, + {file = "coverage-5.2-cp38-cp38-manylinux1_i686.whl", hash = "sha256:214eb2110217f2636a9329bc766507ab71a3a06a8ea30cdeebb47c24dce5972d"}, + {file = "coverage-5.2-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:8a3decd12e7934d0254939e2bf434bf04a5890c5bf91a982685021786a08087e"}, + {file = "coverage-5.2-cp38-cp38-win32.whl", hash = "sha256:1dcebae667b73fd4aa69237e6afb39abc2f27520f2358590c1b13dd90e32abe7"}, + {file = "coverage-5.2-cp38-cp38-win_amd64.whl", hash = "sha256:f50632ef2d749f541ca8e6c07c9928a37f87505ce3a9f20c8446ad310f1aa87b"}, + {file = "coverage-5.2-cp39-cp39-macosx_10_13_x86_64.whl", hash = "sha256:7403675df5e27745571aba1c957c7da2dacb537c21e14007ec3a417bf31f7f3d"}, + {file = "coverage-5.2-cp39-cp39-manylinux1_i686.whl", hash = "sha256:0fc4e0d91350d6f43ef6a61f64a48e917637e1dcfcba4b4b7d543c628ef82c2d"}, + {file = "coverage-5.2-cp39-cp39-manylinux1_x86_64.whl", hash = "sha256:25fe74b5b2f1b4abb11e103bb7984daca8f8292683957d0738cd692f6a7cc64c"}, + {file = "coverage-5.2-cp39-cp39-win32.whl", hash = "sha256:d67599521dff98ec8c34cd9652cbcfe16ed076a2209625fca9dc7419b6370e5c"}, + {file = "coverage-5.2-cp39-cp39-win_amd64.whl", hash = "sha256:10f2a618a6e75adf64329f828a6a5b40244c1c50f5ef4ce4109e904e69c71bd2"}, + {file = "coverage-5.2.tar.gz", hash = "sha256:1874bdc943654ba46d28f179c1846f5710eda3aeb265ff029e0ac2b52daae404"}, +] +distlib = [ + {file = "distlib-0.3.1-py2.py3-none-any.whl", hash = "sha256:8c09de2c67b3e7deef7184574fc060ab8a793e7adbb183d942c389c8b13c52fb"}, + {file = "distlib-0.3.1.zip", hash = "sha256:edf6116872c863e1aa9d5bb7cb5e05a022c519a4594dc703843343a9ddd9bff1"}, ] docutils = [ {file = "docutils-0.16-py2.py3-none-any.whl", hash = "sha256:0c5b78adfbf7762415433f5515cd5c9e762339e23369dbe8000d84a4bf4ab3af"}, {file = "docutils-0.16.tar.gz", hash = "sha256:c2de3a60e9e7d07be26b7f2b00ca0309c207e06c100f9cc2a94931fc75a478fc"}, ] +filelock = [ + {file = "filelock-3.0.12-py3-none-any.whl", hash = "sha256:929b7d63ec5b7d6b71b0fa5ac14e030b3f70b75747cef1b10da9b879fef15836"}, + {file = "filelock-3.0.12.tar.gz", hash = "sha256:18d82244ee114f543149c66a6e0c14e9c4f8a1044b5cdaadd0f82159d6a6ff59"}, +] flake8 = [ {file = "flake8-3.8.3-py2.py3-none-any.whl", hash = "sha256:15e351d19611c887e482fb960eae4d44845013cc142d42896e9862f775d8cf5c"}, {file = "flake8-3.8.3.tar.gz", hash = "sha256:f04b9fcbac03b0a3e58c0ab3a0ecc462e023a9faf046d57794184028123aa208"}, ] identify = [ - {file = "identify-1.4.19-py2.py3-none-any.whl", hash = "sha256:781fd3401f5d2b17b22a8b18b493a48d5d948e3330634e82742e23f9c20234ef"}, - {file = "identify-1.4.19.tar.gz", hash = "sha256:249ebc7e2066d6393d27c1b1be3b70433f824a120b1d8274d362f1eb419e3b52"}, + {file = "identify-1.4.23-py2.py3-none-any.whl", hash = "sha256:882c4b08b4569517b5f2257ecca180e01f38400a17f429f5d0edff55530c41c7"}, + {file = "identify-1.4.23.tar.gz", hash = "sha256:f89add935982d5bc62913ceee16c9297d8ff14b226e9d3072383a4e38136b656"}, ] idna = [ - {file = "idna-2.9-py2.py3-none-any.whl", hash = "sha256:a068a21ceac8a4d63dbfd964670474107f541babbd2250d61922f029858365fa"}, - {file = "idna-2.9.tar.gz", hash = "sha256:7588d1c14ae4c77d74036e8c22ff447b26d0fde8f007354fd48a7814db15b7cb"}, + {file = "idna-2.10-py2.py3-none-any.whl", hash = "sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0"}, + {file = "idna-2.10.tar.gz", hash = "sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6"}, ] imagesize = [ {file = "imagesize-1.2.0-py2.py3-none-any.whl", hash = "sha256:6965f19a6a2039c7d48bca7dba2473069ff854c36ae6f19d2cde309d998228a1"}, {file = "imagesize-1.2.0.tar.gz", hash = "sha256:b1f6b5a4eab1f73479a50fb79fcf729514a900c341d8503d62a62dbc4127a2b1"}, ] importlib-metadata = [ - {file = "importlib_metadata-1.6.1-py2.py3-none-any.whl", hash = "sha256:15ec6c0fd909e893e3a08b3a7c76ecb149122fb14b7efe1199ddd4c7c57ea958"}, - {file = "importlib_metadata-1.6.1.tar.gz", hash = "sha256:0505dd08068cfec00f53a74a0ad927676d7757da81b7436a6eefe4c7cf75c545"}, + {file = "importlib_metadata-1.7.0-py2.py3-none-any.whl", hash = "sha256:dc15b2969b4ce36305c51eebe62d418ac7791e9a157911d58bfb1f9ccd8e2070"}, + {file = "importlib_metadata-1.7.0.tar.gz", hash = "sha256:90bb658cdbbf6d1735b6341ce708fc7024a3e14e99ffdc5783edea9f9b077f83"}, ] importlib-resources = [ - {file = "importlib_resources-2.0.0-py2.py3-none-any.whl", hash = "sha256:a86462cf34a6d391d1d5d598a5e2f5aac9fc00b265d40542e1196328f015d1f6"}, - {file = "importlib_resources-2.0.0.tar.gz", hash = "sha256:7f6aae2ed252ba10f5d1af5676b0e35f3b3eb7d3cc103b8365cc92aec0c79258"}, + {file = "importlib_resources-3.0.0-py2.py3-none-any.whl", hash = "sha256:d028f66b66c0d5732dae86ba4276999855e162a749c92620a38c1d779ed138a7"}, + {file = "importlib_resources-3.0.0.tar.gz", hash = "sha256:19f745a6eca188b490b1428c8d1d4a0d2368759f32370ea8fb89cad2ab1106c3"}, ] isort = [ {file = "isort-4.3.21-py2.py3-none-any.whl", hash = "sha256:6e811fcb295968434526407adb8796944f1988c5b65e8139058f2014cbe100fd"}, @@ -958,11 +958,8 @@ mccabe = [ {file = "mccabe-0.6.1.tar.gz", hash = "sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f"}, ] more-itertools = [ - {file = "more-itertools-8.3.0.tar.gz", hash = "sha256:558bb897a2232f5e4f8e2399089e35aecb746e1f9191b6584a151647e89267be"}, - {file = "more_itertools-8.3.0-py3-none-any.whl", hash = "sha256:7818f596b1e87be009031c7653d01acc46ed422e6656b394b0f765ce66ed4982"}, -] -nested-lookup = [ - {file = "nested-lookup-0.2.21.tar.gz", hash = "sha256:23789e328bd1d0b3f9db93cf51b7103a978dd0d8a834770d2c19b365e934ab96"}, + {file = "more-itertools-8.4.0.tar.gz", hash = "sha256:68c70cc7167bdf5c7c9d8f6954a7837089c6a36bf565383919bb595efb8a17e5"}, + {file = "more_itertools-8.4.0-py3-none-any.whl", hash = "sha256:b78134b2063dd214000685165d81c154522c3ee0a1c0d4d113c80361c234c5a2"}, ] nodeenv = [ {file = "nodeenv-1.4.0-py2.py3-none-any.whl", hash = "sha256:4b0b77afa3ba9b54f4b6396e60b0c83f59eaeb2d63dc3cc7a70f7f4af96c82bc"}, @@ -971,10 +968,6 @@ packaging = [ {file = "packaging-20.4-py2.py3-none-any.whl", hash = "sha256:998416ba6962ae7fbd6596850b80e17859a5753ba17c32284f67bfff33784181"}, {file = "packaging-20.4.tar.gz", hash = "sha256:4357f74f47b9c12db93624a82154e9b120fa8293699949152b22065d556079f8"}, ] -pathlib2 = [ - {file = "pathlib2-2.3.5-py2.py3-none-any.whl", hash = "sha256:0ec8205a157c80d7acc301c0b18fbd5d44fe655968f5d947b6ecef5290fc35db"}, - {file = "pathlib2-2.3.5.tar.gz", hash = "sha256:6cd9a47b597b37cc57de1c05e56fb1a1c9cc9fab04fe78c29acd090418529868"}, -] pathspec = [ {file = "pathspec-0.8.0-py2.py3-none-any.whl", hash = "sha256:7d91249d21749788d07a2d0f94147accd8f845507400749ea19c1ec9054a12b0"}, {file = "pathspec-0.8.0.tar.gz", hash = "sha256:da45173eb3a6f2a5a487efba21f050af2b41948be6ab52b6a1e3ff22bb8b7061"}, @@ -988,8 +981,8 @@ pre-commit = [ {file = "pre_commit-1.21.0.tar.gz", hash = "sha256:8f48d8637bdae6fa70cc97db9c1dd5aa7c5c8bf71968932a380628c25978b850"}, ] py = [ - {file = "py-1.8.1-py2.py3-none-any.whl", hash = "sha256:c20fdd83a5dbc0af9efd622bee9a5564e278f6380fffcacc43ba6f43db2813b0"}, - {file = "py-1.8.1.tar.gz", hash = "sha256:5e27081401262157467ad6e7f851b7aa402c5852dbcb3dae06768434de5752aa"}, + {file = "py-1.9.0-py2.py3-none-any.whl", hash = "sha256:366389d1db726cd2fcfc79732e75410e5fe4d31db13692115529d34069a043c2"}, + {file = "py-1.9.0.tar.gz", hash = "sha256:9ca6883ce56b4e8da7e79ac18787889fa5206c79dcc67fb065376cd2fe03f342"}, ] pycodestyle = [ {file = "pycodestyle-2.6.0-py2.py3-none-any.whl", hash = "sha256:2295e7b2f6b5bd100585ebcb1f616591b652db8a741695b3d8f5d28bdc934367"}, @@ -1012,11 +1005,11 @@ pytest = [ {file = "pytest-5.4.3.tar.gz", hash = "sha256:7979331bfcba207414f5e1263b5a0f8f521d0f457318836a7355531ed1a4c7d8"}, ] pytest-cov = [ - {file = "pytest-cov-2.9.0.tar.gz", hash = "sha256:b6a814b8ed6247bd81ff47f038511b57fe1ce7f4cc25b9106f1a4b106f1d9322"}, - {file = "pytest_cov-2.9.0-py2.py3-none-any.whl", hash = "sha256:c87dfd8465d865655a8213859f1b4749b43448b5fae465cb981e16d52a811424"}, + {file = "pytest-cov-2.10.0.tar.gz", hash = "sha256:1a629dc9f48e53512fcbfda6b07de490c374b0c83c55ff7a1720b3fccff0ac87"}, + {file = "pytest_cov-2.10.0-py2.py3-none-any.whl", hash = "sha256:6e6d18092dce6fad667cd7020deed816f858ad3b49d5b5e2b1cc1c97a4dba65c"}, ] pytest-sugar = [ - {file = "pytest-sugar-0.9.3.tar.gz", hash = "sha256:1630b5b7ea3624919b73fde37cffb87965c5087a4afab8a43074ff44e0d810c4"}, + {file = "pytest-sugar-0.9.4.tar.gz", hash = "sha256:b1b2186b0a72aada6859bea2a5764145e3aaa2c1cfbb23c3a19b5f7b697563d3"}, ] pytz = [ {file = "pytz-2020.1-py2.py3-none-any.whl", hash = "sha256:a494d53b6d39c3c6e44c3bec237336e14305e4f29bbf800b599253057fbb79ed"}, @@ -1036,31 +1029,31 @@ pyyaml = [ {file = "PyYAML-5.3.1.tar.gz", hash = "sha256:b8eac752c5e14d3eca0e6dd9199cd627518cb5ec06add0de9d32baeee6fe645d"}, ] regex = [ - {file = "regex-2020.6.8-cp27-cp27m-win32.whl", hash = "sha256:fbff901c54c22425a5b809b914a3bfaf4b9570eee0e5ce8186ac71eb2025191c"}, - {file = "regex-2020.6.8-cp27-cp27m-win_amd64.whl", hash = "sha256:112e34adf95e45158c597feea65d06a8124898bdeac975c9087fe71b572bd938"}, - {file = "regex-2020.6.8-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:92d8a043a4241a710c1cf7593f5577fbb832cf6c3a00ff3fc1ff2052aff5dd89"}, - {file = "regex-2020.6.8-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:bae83f2a56ab30d5353b47f9b2a33e4aac4de9401fb582b55c42b132a8ac3868"}, - {file = "regex-2020.6.8-cp36-cp36m-manylinux2010_i686.whl", hash = "sha256:b2ba0f78b3ef375114856cbdaa30559914d081c416b431f2437f83ce4f8b7f2f"}, - {file = "regex-2020.6.8-cp36-cp36m-manylinux2010_x86_64.whl", hash = "sha256:95fa7726d073c87141f7bbfb04c284901f8328e2d430eeb71b8ffdd5742a5ded"}, - {file = "regex-2020.6.8-cp36-cp36m-win32.whl", hash = "sha256:e3cdc9423808f7e1bb9c2e0bdb1c9dc37b0607b30d646ff6faf0d4e41ee8fee3"}, - {file = "regex-2020.6.8-cp36-cp36m-win_amd64.whl", hash = "sha256:c78e66a922de1c95a208e4ec02e2e5cf0bb83a36ceececc10a72841e53fbf2bd"}, - {file = "regex-2020.6.8-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:08997a37b221a3e27d68ffb601e45abfb0093d39ee770e4257bd2f5115e8cb0a"}, - {file = "regex-2020.6.8-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:2f6f211633ee8d3f7706953e9d3edc7ce63a1d6aad0be5dcee1ece127eea13ae"}, - {file = "regex-2020.6.8-cp37-cp37m-manylinux2010_i686.whl", hash = "sha256:55b4c25cbb3b29f8d5e63aeed27b49fa0f8476b0d4e1b3171d85db891938cc3a"}, - {file = "regex-2020.6.8-cp37-cp37m-manylinux2010_x86_64.whl", hash = "sha256:89cda1a5d3e33ec9e231ece7307afc101b5217523d55ef4dc7fb2abd6de71ba3"}, - {file = "regex-2020.6.8-cp37-cp37m-win32.whl", hash = "sha256:690f858d9a94d903cf5cada62ce069b5d93b313d7d05456dbcd99420856562d9"}, - {file = "regex-2020.6.8-cp37-cp37m-win_amd64.whl", hash = "sha256:1700419d8a18c26ff396b3b06ace315b5f2a6e780dad387e4c48717a12a22c29"}, - {file = "regex-2020.6.8-cp38-cp38-manylinux1_i686.whl", hash = "sha256:654cb773b2792e50151f0e22be0f2b6e1c3a04c5328ff1d9d59c0398d37ef610"}, - {file = "regex-2020.6.8-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:52e1b4bef02f4040b2fd547357a170fc1146e60ab310cdbdd098db86e929b387"}, - {file = "regex-2020.6.8-cp38-cp38-manylinux2010_i686.whl", hash = "sha256:cf59bbf282b627130f5ba68b7fa3abdb96372b24b66bdf72a4920e8153fc7910"}, - {file = "regex-2020.6.8-cp38-cp38-manylinux2010_x86_64.whl", hash = "sha256:5aaa5928b039ae440d775acea11d01e42ff26e1561c0ffcd3d805750973c6baf"}, - {file = "regex-2020.6.8-cp38-cp38-win32.whl", hash = "sha256:97712e0d0af05febd8ab63d2ef0ab2d0cd9deddf4476f7aa153f76feef4b2754"}, - {file = "regex-2020.6.8-cp38-cp38-win_amd64.whl", hash = "sha256:6ad8663c17db4c5ef438141f99e291c4d4edfeaacc0ce28b5bba2b0bf273d9b5"}, - {file = "regex-2020.6.8.tar.gz", hash = "sha256:e9b64e609d37438f7d6e68c2546d2cb8062f3adb27e6336bc129b51be20773ac"}, + {file = "regex-2020.7.14-cp27-cp27m-win32.whl", hash = "sha256:e46d13f38cfcbb79bfdb2964b0fe12561fe633caf964a77a5f8d4e45fe5d2ef7"}, + {file = "regex-2020.7.14-cp27-cp27m-win_amd64.whl", hash = "sha256:6961548bba529cac7c07af2fd4d527c5b91bb8fe18995fed6044ac22b3d14644"}, + {file = "regex-2020.7.14-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:c50a724d136ec10d920661f1442e4a8b010a4fe5aebd65e0c2241ea41dbe93dc"}, + {file = "regex-2020.7.14-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:8a51f2c6d1f884e98846a0a9021ff6861bdb98457879f412fdc2b42d14494067"}, + {file = "regex-2020.7.14-cp36-cp36m-manylinux2010_i686.whl", hash = "sha256:9c568495e35599625f7b999774e29e8d6b01a6fb684d77dee1f56d41b11b40cd"}, + {file = "regex-2020.7.14-cp36-cp36m-manylinux2010_x86_64.whl", hash = "sha256:51178c738d559a2d1071ce0b0f56e57eb315bcf8f7d4cf127674b533e3101f88"}, + {file = "regex-2020.7.14-cp36-cp36m-win32.whl", hash = "sha256:9eddaafb3c48e0900690c1727fba226c4804b8e6127ea409689c3bb492d06de4"}, + {file = "regex-2020.7.14-cp36-cp36m-win_amd64.whl", hash = "sha256:14a53646369157baa0499513f96091eb70382eb50b2c82393d17d7ec81b7b85f"}, + {file = "regex-2020.7.14-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:1269fef3167bb52631ad4fa7dd27bf635d5a0790b8e6222065d42e91bede4162"}, + {file = "regex-2020.7.14-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:d0a5095d52b90ff38592bbdc2644f17c6d495762edf47d876049cfd2968fbccf"}, + {file = "regex-2020.7.14-cp37-cp37m-manylinux2010_i686.whl", hash = "sha256:4c037fd14c5f4e308b8370b447b469ca10e69427966527edcab07f52d88388f7"}, + {file = "regex-2020.7.14-cp37-cp37m-manylinux2010_x86_64.whl", hash = "sha256:bc3d98f621898b4a9bc7fecc00513eec8f40b5b83913d74ccb445f037d58cd89"}, + {file = "regex-2020.7.14-cp37-cp37m-win32.whl", hash = "sha256:46bac5ca10fb748d6c55843a931855e2727a7a22584f302dd9bb1506e69f83f6"}, + {file = "regex-2020.7.14-cp37-cp37m-win_amd64.whl", hash = "sha256:0dc64ee3f33cd7899f79a8d788abfbec168410be356ed9bd30bbd3f0a23a7204"}, + {file = "regex-2020.7.14-cp38-cp38-manylinux1_i686.whl", hash = "sha256:5ea81ea3dbd6767873c611687141ec7b06ed8bab43f68fad5b7be184a920dc99"}, + {file = "regex-2020.7.14-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:bbb332d45b32df41200380fff14712cb6093b61bd142272a10b16778c418e98e"}, + {file = "regex-2020.7.14-cp38-cp38-manylinux2010_i686.whl", hash = "sha256:c11d6033115dc4887c456565303f540c44197f4fc1a2bfb192224a301534888e"}, + {file = "regex-2020.7.14-cp38-cp38-manylinux2010_x86_64.whl", hash = "sha256:75aaa27aa521a182824d89e5ab0a1d16ca207318a6b65042b046053cfc8ed07a"}, + {file = "regex-2020.7.14-cp38-cp38-win32.whl", hash = "sha256:d6cff2276e502b86a25fd10c2a96973fdb45c7a977dca2138d661417f3728341"}, + {file = "regex-2020.7.14-cp38-cp38-win_amd64.whl", hash = "sha256:7a2dd66d2d4df34fa82c9dc85657c5e019b87932019947faece7983f2089a840"}, + {file = "regex-2020.7.14.tar.gz", hash = "sha256:3a3af27a8d23143c49a3420efe5b3f8cf1a48c6fc8bc6856b03f638abc1833bb"}, ] requests = [ - {file = "requests-2.23.0-py2.py3-none-any.whl", hash = "sha256:43999036bfa82904b6af1d99e4882b560e5e2c68e5c4b0aa03b655f3d7d73fee"}, - {file = "requests-2.23.0.tar.gz", hash = "sha256:b3f43d496c6daba4493e7c431722aeb7dbc6288f52a6e04e7b6023b0247817e6"}, + {file = "requests-2.24.0-py2.py3-none-any.whl", hash = "sha256:fe75cc94a9443b9246fc7049224f75604b113c36acb93f87b80ed42c44cbb898"}, + {file = "requests-2.24.0.tar.gz", hash = "sha256:b3559a131db72c33ee969480840fff4bb6dd111de7dd27c8ee1f820f4f00231b"}, ] seed-isort-config = [ {file = "seed_isort_config-2.2.0-py2.py3-none-any.whl", hash = "sha256:8601fb715a5a4aac39256bbf73c2da6a81f964da9c9d9897ab9074db3663526f"}, @@ -1145,14 +1138,14 @@ urllib3 = [ {file = "urllib3-1.25.9.tar.gz", hash = "sha256:3018294ebefce6572a474f0604c2021e33b3fd8006ecd11d62107a5d2a963527"}, ] virtualenv = [ - {file = "virtualenv-16.7.10-py2.py3-none-any.whl", hash = "sha256:105893c8dc66b7817691c7371439ec18e3b6c5e323a304b5ed96cdd2e75cc1ec"}, - {file = "virtualenv-16.7.10.tar.gz", hash = "sha256:e88fdcb08b0ecb11da97868f463dd06275923f50d87f4b9c8b2fc0994eec40f4"}, + {file = "virtualenv-20.0.27-py2.py3-none-any.whl", hash = "sha256:c51f1ba727d1614ce8fd62457748b469fbedfdab2c7e5dd480c9ae3fbe1233f1"}, + {file = "virtualenv-20.0.27.tar.gz", hash = "sha256:26cdd725a57fef4c7c22060dba4647ebd8ca377e30d1c1cf547b30a0b79c43b4"}, ] wcwidth = [ - {file = "wcwidth-0.2.4-py2.py3-none-any.whl", hash = "sha256:79375666b9954d4a1a10739315816324c3e73110af9d0e102d906fdb0aec009f"}, - {file = "wcwidth-0.2.4.tar.gz", hash = "sha256:8c6b5b6ee1360b842645f336d9e5d68c55817c26d3050f46b235ef2bc650e48f"}, + {file = "wcwidth-0.2.5-py2.py3-none-any.whl", hash = "sha256:beb4802a9cebb9144e99086eff703a642a13d6a0052920003a230f3294bbe784"}, + {file = "wcwidth-0.2.5.tar.gz", hash = "sha256:c4d647b99872929fdb7bdcaa4fbe7f01413ed3d98077df798530e5b04f116c83"}, ] zipp = [ - {file = "zipp-1.2.0-py2.py3-none-any.whl", hash = "sha256:e0d9e63797e483a30d27e09fffd308c59a700d365ec34e93cc100844168bf921"}, - {file = "zipp-1.2.0.tar.gz", hash = "sha256:c70410551488251b0fee67b460fb9a536af8d6f9f008ad10ac51f615b6a521b1"}, + {file = "zipp-3.1.0-py3-none-any.whl", hash = "sha256:aa36550ff0c0b7ef7fa639055d797116ee891440eac1a56f378e2d3179e0320b"}, + {file = "zipp-3.1.0.tar.gz", hash = "sha256:c599e4d75c98f6798c509911d08a22e6c021d074469042177c8c86fb92eefd96"}, ] diff --git a/pyproject.toml b/pyproject.toml index bdf9ba3..dc93234 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,9 +5,10 @@ description = "Modify configuration files of various formats with the same simpl authors = ["Eugene Triguba "] readme = "README.md" repository = "https://github.com/eugenetriguba/config_file" -keywords = ["configuration", "config file", "ini", "json", "toml", "yaml"] +documentation = "https://config-file.readthedocs.io/" +keywords = ["configuration", "ini", "json", "toml", "yaml"] classifiers = [ - "Development Status :: 2 - Pre-Alpha", + "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", @@ -27,7 +28,6 @@ pytest = "poetry run pytest tests/" [tool.poetry.dependencies] python = "^3.6" -nested-lookup = "^0.2.21" toml = { version = "^0.10.0", optional = true } pyyaml = { version = "^5.3.1", optional = true } @@ -59,7 +59,7 @@ include = '\.pyi?$' line_length = 88 multi_line_output = 3 include_trailing_comma = true -known_third_party = ["nested_lookup", "pytest"] +known_third_party = ["pytest", "pyyaml", "toml"] known_first_party = ["config_file"] [build-system] diff --git a/tests/test_lookup_api.py b/tests/test_lookup_api.py index 6a5cc87..f7b7cc3 100644 --- a/tests/test_lookup_api.py +++ b/tests/test_lookup_api.py @@ -1,7 +1,7 @@ from unittest import TestCase -from nested_lookup import nested_lookup, nested_update -from nested_lookup import nested_delete, nested_alter +from config_file.nested_lookup import nested_lookup, nested_update +from config_file.nested_lookup import nested_delete, nested_alter class BaseLookUpApi(TestCase): diff --git a/tests/test_nested_lookup.py b/tests/test_nested_lookup.py index b1bf44b..6c1cc83 100644 --- a/tests/test_nested_lookup.py +++ b/tests/test_nested_lookup.py @@ -1,6 +1,6 @@ from unittest import TestCase -from nested_lookup import ( +from config_file.nested_lookup import ( nested_lookup, get_all_keys, get_occurrence_of_key, -- cgit v1.2.3 From 4846966c6a1d337fc3a7958514ca23d06a9da7e0 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 19 Jul 2020 13:19:29 -0500 Subject: Tweak zsh installation info --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3a54880..bb9a617 100644 --- a/README.md +++ b/README.md @@ -36,14 +36,14 @@ $ poetry install config-file -E yaml -E toml ``` If you use ZSH as your shell, brackets (`[]`) are used for glob matching -and you'll have to put the package name in quotes if you're downloading -the extras too. +so you can work around that if installing extras in one of the following +ways. ```zsh % pip install 'config-file[yaml, toml]' +``` -OR - +```zsh % noglob pip install config-file[yaml, toml] ``` -- 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 --- 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 From 461cb78f0a56b9293b5ade10fe90ec4aa77d23ad Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 19 Jul 2020 15:02:13 -0500 Subject: Change name of BaseParser to AbstractParser --- config_file/__init__.py | 4 +- config_file/config_file.py | 12 ++-- config_file/parsers/__init__.py | 14 ++--- config_file/parsers/abstract_parser.py | 20 +++---- config_file/parsers/ini_parser.py | 4 +- config_file/parsers/json_parser.py | 10 ++-- config_file/parsers/toml_parser.py | 104 +-------------------------------- config_file/parsers/yaml_parser.py | 5 +- pyproject.toml | 2 +- tests/conftest.py | 8 +-- tests/test_base_parser.py | 6 +- tests/test_config_file.py | 10 ++-- 12 files changed, 51 insertions(+), 148 deletions(-) diff --git a/config_file/__init__.py b/config_file/__init__.py index 1b2094f..3c43d33 100644 --- a/config_file/__init__.py +++ b/config_file/__init__.py @@ -1,6 +1,6 @@ from config_file.config_file import ConfigFile from config_file.exceptions import ConfigFileError, ParsingError -from config_file.parsers import BaseParser +from config_file.parsers import AbstractParser __version__ = "0.10.0" @@ -8,5 +8,5 @@ __all__ = [ "ConfigFile", "ParsingError", "ConfigFileError", - "BaseParser", + "AbstractParser", ] diff --git a/config_file/config_file.py b/config_file/config_file.py index de8b851..34df561 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -4,7 +4,7 @@ from shutil import copyfile from typing import Any, Type, Union from config_file.exceptions import ParsingError -from config_file.parsers import BaseParser, IniParser, JsonParser +from config_file.parsers import AbstractParser, IniParser, JsonParser from config_file.utils import create_config_path, read_file, split_on_dot @@ -12,7 +12,7 @@ class ConfigFile: def __init__( self, file_path: Type[Union[str, Type[PurePath]]], - parser: Type[BaseParser] = None, + parser: Type[AbstractParser] = None, ) -> None: """ Saves the config file path and expands it if needed, reads in @@ -37,8 +37,8 @@ class ConfigFile: return self.__path def __determine_parser( - self, file_path: Type[PurePath], parser: Type[BaseParser] = None - ) -> Type[BaseParser]: + self, file_path: Type[PurePath], parser: Type[AbstractParser] = None + ) -> Type[AbstractParser]: if parser is not None and not inspect.isabstract(parser): return parser(self.__contents) @@ -108,7 +108,9 @@ class ConfigFile: """ return self.__parser.has(section_key) - def restore_original(self, original_file_path: Union[str, Type[PurePath]] = None) -> bool: + def restore_original( + self, original_file_path: Union[str, Type[PurePath]] = None + ) -> 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 diff --git a/config_file/parsers/__init__.py b/config_file/parsers/__init__.py index 8da520e..554b624 100644 --- a/config_file/parsers/__init__.py +++ b/config_file/parsers/__init__.py @@ -1,17 +1,17 @@ -from .base_parser import BaseParser +from .abstract_parser import AbstractParser from .ini_parser import IniParser from .json_parser import JsonParser -from .toml_parser import TomlParser -from .yaml_parser import YamlParser from .parse_value import ( - parse_value, can_be_parsed_as_bool, can_be_parsed_as_float, - can_be_parsed_as_int + can_be_parsed_as_int, + parse_value, ) +from .toml_parser import TomlParser +from .yaml_parser import YamlParser __all__ = [ - "BaseParser", + "AbstractParser", "IniParser", "JsonParser", "TomlParser", @@ -19,5 +19,5 @@ __all__ = [ "parse_value", "can_be_parsed_as_bool", "can_be_parsed_as_float", - "can_be_parsed_as_int" + "can_be_parsed_as_int", ] diff --git a/config_file/parsers/abstract_parser.py b/config_file/parsers/abstract_parser.py index da6e90a..8f1bee1 100644 --- a/config_file/parsers/abstract_parser.py +++ b/config_file/parsers/abstract_parser.py @@ -2,7 +2,7 @@ from abc import ABC, abstractmethod from typing import Any -class BaseParser(ABC): +class AbstractParser(ABC): """ The abstract base parser for all other parsers to implement. @@ -25,6 +25,14 @@ class BaseParser(ABC): self.content = file_contents self.parsed_content = self.parse(self.content) + @abstractmethod + def parse(self, file_contents: str) -> Any: + """ + Parse the file_contents into an internal representation + the given parser can work with. + """ + raise NotImplementedError + @abstractmethod def get(self, key: str, parse_types: bool = False, all: bool = False) -> Any: """ @@ -62,14 +70,6 @@ class BaseParser(ABC): 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. + it would only be checking if a certain key exists. """ raise NotImplementedError diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 2db1ae8..164ff6d 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -2,12 +2,12 @@ import configparser from io import StringIO from config_file.exceptions import ParsingError -from config_file.parsers.base_parser import BaseParser +from config_file.parsers.abstract_parser import AbstractParser from config_file.parsers.parse_value import parse_value from config_file.utils import split_on_dot -class IniParser(BaseParser): +class IniParser(AbstractParser): def __init__(self, file_contents: str): """Reads in the file contents into the configparser.""" super().__init__(file_contents) diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py index 13b84b5..237c0b5 100644 --- a/config_file/parsers/json_parser.py +++ b/config_file/parsers/json_parser.py @@ -1,18 +1,17 @@ import json +from config_file.exceptions import ParsingError from config_file.nested_lookup import ( get_occurrence_of_key, nested_delete, nested_lookup, nested_update, ) - -from config_file.exceptions import ParsingError -from config_file.parsers import BaseParser, parse_value +from config_file.parsers import AbstractParser, parse_value from config_file.utils import split_on_dot -class JsonParser(BaseParser): +class JsonParser(AbstractParser): """ The JsonParser allows use of json files with ConfigFile. The syntax it supports is a bit more flexible than the IniParser. @@ -76,7 +75,8 @@ class JsonParser(BaseParser): result = result[key] except TypeError: raise ParsingError( - f"Cannot get {key} because {key_causing_error} is not subscriptable." + f"Cannot get {key} because {key_causing_error} " + "is not subscriptable." ) else: result = self.parsed_content[key] diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py index ad3e7f4..4905aec 100644 --- a/config_file/parsers/toml_parser.py +++ b/config_file/parsers/toml_parser.py @@ -1,105 +1,5 @@ -import toml +from config_file.parsers import AbstractParser -from config_file.nested_lookup import ( - get_occurrence_of_key, - nested_delete, - nested_lookup, - nested_update, -) -from config_file.exceptions import ParsingError -from config_file.parsers import BaseParser, parse_value -from config_file.utils import split_on_dot - -class TomlParser(BaseParser): +class TomlParser(AbstractParser): """The TomlParser allows use of toml files with ConfigFile.""" - - 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 toml.loads(file_contents) - except toml.TomlDecodeError as error: - raise ParsingError(error) - - def get(self, key: str, parse_types: bool = False, 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 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 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, all: bool = False): - if 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, all: bool = False): - if 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, indent=4) - - 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/yaml_parser.py b/config_file/parsers/yaml_parser.py index 48722b5..a45d7f6 100644 --- a/config_file/parsers/yaml_parser.py +++ b/config_file/parsers/yaml_parser.py @@ -1,4 +1,5 @@ -from config_file.parsers import BaseParser +from config_file.parsers import AbstractParser -class YamlParser(BaseParser): + +class YamlParser(AbstractParser): pass diff --git a/pyproject.toml b/pyproject.toml index dc93234..4ac2043 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -59,7 +59,7 @@ include = '\.pyi?$' line_length = 88 multi_line_output = 3 include_trailing_comma = true -known_third_party = ["pytest", "pyyaml", "toml"] +known_third_party = ["pytest"] known_first_party = ["config_file"] [build-system] diff --git a/tests/conftest.py b/tests/conftest.py index bb16405..5fdf5a9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,7 +3,7 @@ from typing import Callable, Type import pytest -from config_file.parsers import BaseParser, IniParser, JsonParser +from config_file.parsers import AbstractParser, IniParser, JsonParser from config_file.utils import create_config_path @@ -47,7 +47,7 @@ def template_original_file() -> Callable[[Path], Path]: def template_and_parser(template_file: Callable[[str, str], Path]) -> tuple: def func( file_type: str, template_name: str = "default" - ) -> Callable[[str, str], Type[BaseParser]]: + ) -> Callable[[str, str], Type[AbstractParser]]: test_file = template_file(file_type, template_name) text = test_file.read_text(encoding="utf-8") @@ -60,8 +60,8 @@ def template_and_parser(template_file: Callable[[str, str], Path]) -> tuple: @pytest.fixture -def templated_parser(template_and_parser) -> Callable[[str, str], Type[BaseParser]]: - def func(file_type: str, template_name: str = "default") -> Type[BaseParser]: +def templated_parser(template_and_parser) -> Callable[[str, str], Type[AbstractParser]]: + def func(file_type: str, template_name: str = "default") -> Type[AbstractParser]: return template_and_parser(file_type, template_name)[1] return func diff --git a/tests/test_base_parser.py b/tests/test_base_parser.py index 95c0c39..4eaa92f 100644 --- a/tests/test_base_parser.py +++ b/tests/test_base_parser.py @@ -1,9 +1,9 @@ import pytest -from config_file.parsers import BaseParser +from config_file.parsers import AbstractParser -class ConcreteParser(BaseParser): +class ConcreteParser(AbstractParser): def __init__(self, file_contents): super().__init__(file_contents) @@ -31,7 +31,7 @@ class ConcreteParser(BaseParser): def test_that_base_parser_can_not_be_instantiated(): with pytest.raises(TypeError): - BaseParser("") + AbstractParser("") @pytest.mark.parametrize( diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 899e644..5430fb8 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -4,14 +4,14 @@ from pathlib import Path import pytest from config_file.config_file import ConfigFile -from config_file.parsers import BaseParser +from config_file.parsers import AbstractParser SUPPORTED_FILE_TYPES = ["ini", "json"] @pytest.fixture(params=SUPPORTED_FILE_TYPES) def template_and_config_file(request, template_file): - def func(template_name: str = "default", parser: BaseParser = None): + def func(template_name: str = "default", parser: AbstractParser = None): template = template_file(request.param, template_name) return (template, ConfigFile(template, parser=parser)) @@ -20,7 +20,7 @@ def template_and_config_file(request, template_file): @pytest.fixture def templated_config_file(template_and_config_file): - def func(template_name: str = "default", parser: BaseParser = None): + def func(template_name: str = "default", parser: AbstractParser = None): return template_and_config_file(template_name, parser)[1] return func @@ -31,7 +31,7 @@ def test_that_config_file_is_initialized_correctly(template_and_config_file): assert config.path == template_file assert config.stringify() == template_file.read_text() - assert isinstance(config._ConfigFile__parser, BaseParser) + assert isinstance(config._ConfigFile__parser, AbstractParser) def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): @@ -180,7 +180,7 @@ def test_that_config_file_can_delete(templated_config_file): def test_that_custom_parser_can_be_used(template_and_config_file): - class CustomParser(BaseParser): + class CustomParser(AbstractParser): def __init__(self, file_contents): super().__init__(file_contents) -- cgit v1.2.3 From 7309c0df45496e66007ea3838442413f4a3056f2 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 19 Jul 2020 20:09:35 -0500 Subject: Add napoleon to transition to Google docstring format --- docs/source/conf.py | 6 +++++- poetry.lock | 33 ++++++++++++++++++++++++++++++++- pyproject.toml | 5 +++-- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 1c16a92..69cfce0 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -31,7 +31,11 @@ release = "0.10.0" # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. -extensions = ["sphinx.ext.autodoc", "sphinx.ext.autosectionlabel"] +extensions = [ + "sphinx.ext.autodoc", + "sphinx.ext.autosectionlabel", + "sphinx.ext.napoleon", +] # Add any paths that contain templates here, relative to this directory. templates_path = ["_templates"] diff --git a/poetry.lock b/poetry.lock index afe6577..ea4dca0 100644 --- a/poetry.lock +++ b/poetry.lock @@ -368,6 +368,17 @@ version = ">=0.12" [package.extras] dev = ["pre-commit", "tox"] +[[package]] +category = "dev" +description = "A collection of helpful Python tools!" +name = "pockets" +optional = false +python-versions = "*" +version = "0.9.1" + +[package.dependencies] +six = ">=1.5.2" + [[package]] category = "dev" description = "A framework for managing and maintaining multi-language pre-commit hooks." @@ -647,6 +658,18 @@ version = "1.0.1" [package.extras] test = ["pytest", "flake8", "mypy"] +[[package]] +category = "dev" +description = "Sphinx \"napoleon\" extension." +name = "sphinxcontrib-napoleon" +optional = false +python-versions = "*" +version = "0.7" + +[package.dependencies] +pockets = ">=0.3" +six = ">=1.5.2" + [[package]] category = "dev" description = "sphinxcontrib-qthelp is a sphinx extension which outputs QtHelp document." @@ -771,7 +794,7 @@ toml = ["toml"] yaml = ["pyyaml"] [metadata] -content-hash = "432d63eaf6fb166650820be893bb3364a954b8fcf81b618bbfb398dbb54de1f6" +content-hash = "c3cc44b7d09a63e8aba8852464f06543d403a4091727bad9f58ee881c60f18dd" python-versions = "^3.6" [metadata.files] @@ -976,6 +999,10 @@ pluggy = [ {file = "pluggy-0.13.1-py2.py3-none-any.whl", hash = "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"}, {file = "pluggy-0.13.1.tar.gz", hash = "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0"}, ] +pockets = [ + {file = "pockets-0.9.1-py2.py3-none-any.whl", hash = "sha256:68597934193c08a08eb2bf6a1d85593f627c22f9b065cc727a4f03f669d96d86"}, + {file = "pockets-0.9.1.tar.gz", hash = "sha256:9320f1a3c6f7a9133fe3b571f283bcf3353cd70249025ae8d618e40e9f7e92b3"}, +] pre-commit = [ {file = "pre_commit-1.21.0-py2.py3-none-any.whl", hash = "sha256:f92a359477f3252452ae2e8d3029de77aec59415c16ae4189bcfba40b757e029"}, {file = "pre_commit-1.21.0.tar.gz", hash = "sha256:8f48d8637bdae6fa70cc97db9c1dd5aa7c5c8bf71968932a380628c25978b850"}, @@ -1091,6 +1118,10 @@ sphinxcontrib-jsmath = [ {file = "sphinxcontrib-jsmath-1.0.1.tar.gz", hash = "sha256:a9925e4a4587247ed2191a22df5f6970656cb8ca2bd6284309578f2153e0c4b8"}, {file = "sphinxcontrib_jsmath-1.0.1-py2.py3-none-any.whl", hash = "sha256:2ec2eaebfb78f3f2078e73666b1415417a116cc848b72e5172e596c871103178"}, ] +sphinxcontrib-napoleon = [ + {file = "sphinxcontrib-napoleon-0.7.tar.gz", hash = "sha256:407382beed396e9f2d7f3043fad6afda95719204a1e1a231ac865f40abcbfcf8"}, + {file = "sphinxcontrib_napoleon-0.7-py2.py3-none-any.whl", hash = "sha256:711e41a3974bdf110a484aec4c1a556799eb0b3f3b897521a018ad7e2db13fef"}, +] sphinxcontrib-qthelp = [ {file = "sphinxcontrib-qthelp-1.0.3.tar.gz", hash = "sha256:4c33767ee058b70dba89a6fc5c1892c0d57a54be67ddd3e7875a18d14cba5a72"}, {file = "sphinxcontrib_qthelp-1.0.3-py2.py3-none-any.whl", hash = "sha256:bd9fc24bcb748a8d51fd4ecaade681350aa63009a347a8c14e637895444dfab6"}, diff --git a/pyproject.toml b/pyproject.toml index 4ac2043..846059c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,12 +43,13 @@ seed-isort-config = { version = "^2.1.1", python = "^3.6.1" } pytest = "^5.4.1" pytest-cov = "^2.8.1" pytest-sugar = "^0.9.3" -sphinx = "^2.4.4" -sphinx_rtd_theme = "^0.4.3" taskipy = "^1.2.1" bump2version = "^1.0.0" flake8 = "^3.7.9" black = "^19.10b0" +sphinx = "^2.4.4" +sphinx_rtd_theme = "^0.4.3" +sphinxcontrib-napoleon = "^0.7" [tool.black] line-length = 88 -- cgit v1.2.3 From 6eab76b24dfc831b8c41c445d98175c3276b20a5 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 19 Jul 2020 20:28:40 -0500 Subject: Add UnrecognizedFileError and MissingDependencyError --- config_file/__init__.py | 13 ++++++++++--- config_file/exceptions.py | 13 +++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/config_file/__init__.py b/config_file/__init__.py index 3c43d33..668c0a9 100644 --- a/config_file/__init__.py +++ b/config_file/__init__.py @@ -1,12 +1,19 @@ from config_file.config_file import ConfigFile -from config_file.exceptions import ConfigFileError, ParsingError +from config_file.exceptions import ( + ConfigFileError, + MissingDependencyError, + ParsingError, + UnrecognizedFileError, +) from config_file.parsers import AbstractParser __version__ = "0.10.0" __all__ = [ "ConfigFile", - "ParsingError", - "ConfigFileError", "AbstractParser", + "ConfigFileError", + "ParsingError", + "UnrecognizedFileError", + "MissingDependencyError", ] diff --git a/config_file/exceptions.py b/config_file/exceptions.py index e1af755..03ef052 100644 --- a/config_file/exceptions.py +++ b/config_file/exceptions.py @@ -4,3 +4,16 @@ class ConfigFileError(Exception): class ParsingError(ConfigFileError): """Unable to parse the configuration file.""" + + +class MissingDependencyError(ConfigFileError): + """ + PyYaml or toml is not installed, but the parsers + for them were attempted to be used. + """ + + +class UnrecognizedFileError(ConfigFileError): + """ + A missing file extension or a file type not supported. + """ -- cgit v1.2.3 From 706ffca38a30ff30ac93287d90db4b31e5bc90ea Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 19 Jul 2020 20:34:13 -0500 Subject: Run CI on all OSs and simplify dependencies installed in CI --- .github/workflows/python-package-ci.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/python-package-ci.yml b/.github/workflows/python-package-ci.yml index dc0d104..a8ab5c1 100644 --- a/.github/workflows/python-package-ci.yml +++ b/.github/workflows/python-package-ci.yml @@ -8,10 +8,11 @@ on: jobs: build: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} strategy: matrix: python-version: [3.6, 3.7, 3.8] + os: [macos-latest, ubuntu-latest, windows-latest] steps: - uses: actions/checkout@v2 @@ -24,8 +25,8 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install poetry codecov requests - poetry install -v -E toml -E yaml + pip install poetry codecov requests pytest pytest-cov taskipy + poetry install --no-dev -vvv -E toml -E yaml - name: Run tests run: poetry run task test_ci -- cgit v1.2.3 From d0acdbfc11ac7ea9efdb9158da62d426421058d8 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 19 Jul 2020 20:47:02 -0500 Subject: Update example with dev changes --- README.md | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 6b3b781..c0fc8a3 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,8 @@ ## About Config File The Config File project is designed to allow you to easily manipulate your -configuration files with the same simple API whether they are in INI, -JSON, YAML, or TOML. For the time being, it only supports INI and JSON. +configuration files with the same simple API whether they are in INI, JSON, +YAML, or TOML. ## Installation @@ -52,6 +52,8 @@ third_key = true Then we can manipulate it as follows. ```python +from pathlib import Path + from config_file import ConfigFile ORIGINAL_CONFIG_PATH = Path("~/some-project/some-other-config-file.ini") @@ -68,7 +70,7 @@ print(config.get("section.first_key", return_type=int)) >>> 5 # This holds true when we retrieve entire sections as well. However, we can -# also recursively parse the entire section is desired. +# also recursively parse the entire section if desired. print(config.get("section")) >>> {'first_key': '5', 'second_key': 'blah', 'third_key': 'true'} print(config.get("section", parse_types=True)) @@ -80,34 +82,28 @@ print(config.get("section.unknown", default=False)) >>> False # Setting, deleting, and checking if a key exists is just as easy. -print(config.set("section.first_key", 10)) ->>> True - -print(config.delete("section.third_key")) ->>> True +config.set("section.first_key", 10) +config.delete("section.third_key") print(config.has("section.third_key")) >>> False # We can also convert the entire configuration file to a string. print(config.stringify()) ->>> '[section]\nfirst_key = 5\nsecond_key = blah\nthird_key = true\n\n' +>>> '[section]\nfirst_key = 10\nsecond_key = blah\n\n' -# Lastly, we need to make sure we save our changes. Nothing is written -# out until we do so. -print(config.save()) ->>> True +# Lastly, we need to make sure we save our changes. +# Nothing is written out until we do so. +config.save() # If we have, say, a default config file and a user config file, we can easily # restore default one. We can specify the file path to it. -print(config.restore_original(original_file_path=ORIGINAL_CONFIG_PATH)) ->>> True +config.restore_original(original_file_path=ORIGINAL_CONFIG_PATH) # Otherwise, a config.original.ini file will automatically be looked for in the # current directory (because our configuration file we passed in was # named config.ini). -print(config.restore_original()) ->>> True +config.restore_original() ``` ## Documentation -- cgit v1.2.3 From 608d5e397228318c91a461858098c1f58b8c1b1e Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 18:34:38 -0500 Subject: Run CI only on ubuntu-latest and on every push --- .github/workflows/python-package-ci.yml | 9 ++------- .github/workflows/python-security.yml | 0 2 files changed, 2 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/python-security.yml diff --git a/.github/workflows/python-package-ci.yml b/.github/workflows/python-package-ci.yml index a8ab5c1..5f7d348 100644 --- a/.github/workflows/python-package-ci.yml +++ b/.github/workflows/python-package-ci.yml @@ -1,18 +1,13 @@ name: python package CI -on: - push: - branches: [master, dev] - pull_request: - branches: [master, dev] +on: [push] jobs: build: - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest strategy: matrix: python-version: [3.6, 3.7, 3.8] - os: [macos-latest, ubuntu-latest, windows-latest] steps: - uses: actions/checkout@v2 diff --git a/.github/workflows/python-security.yml b/.github/workflows/python-security.yml new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3 From e1d7e1fd0b1e8974d65e78e83a542d9b37b4bfb7 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 18:34:56 -0500 Subject: Add security check to CI --- .github/workflows/python-security.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/python-security.yml b/.github/workflows/python-security.yml index e69de29..c5339e4 100644 --- a/.github/workflows/python-security.yml +++ b/.github/workflows/python-security.yml @@ -0,0 +1,10 @@ +on: [push] + +jobs: + security_checks: + runs-on: ubuntu-latest + name: Execute the pycharm-security action + steps: + - uses: actions/checkout@v1 + - name: Run PyCharm Security + uses: tonybaloney/pycharm-security@master \ No newline at end of file -- cgit v1.2.3 From bc05c79f915e2b45b7f13f143b4c01f4c49013f8 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 21:59:22 -0500 Subject: Add FLOAT_REGEX constant --- config_file/parsers/parse_value.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/config_file/parsers/parse_value.py b/config_file/parsers/parse_value.py index e58781a..26bdf2e 100644 --- a/config_file/parsers/parse_value.py +++ b/config_file/parsers/parse_value.py @@ -1,5 +1,5 @@ """ -Parse types allows you to check or parse a string into its native type. +This module allows you to parse a string into its native type. Example: parse_value('5') -> 5 (int) @@ -17,6 +17,7 @@ Example: "5".isdigit() -> True "-5".isdigit() -> False can_be_parsed_as_int("-5") -> True +parse_value("-5") -> -5 """ import re from distutils.util import strtobool @@ -65,6 +66,8 @@ def can_be_parsed_as_int(value) -> bool: def can_be_parsed_as_float(value) -> bool: + FLOAT_REGEX = r"^\d*\.\d+$" + if type(value) is float: return True @@ -72,9 +75,9 @@ def can_be_parsed_as_float(value) -> bool: return False if value.startswith("-"): - return bool(re.match(r"^\d*\.\d+$", value[1:])) + return bool(re.match(FLOAT_REGEX, value[1:])) - return bool(re.match(r"^\d*\.\d+$", value)) + return bool(re.match(FLOAT_REGEX, value)) def can_be_parsed_as_bool(value) -> bool: -- cgit v1.2.3 From 18e714b63f5dbb3394a21baf51544f39b6ccf7ef Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 22:06:28 -0500 Subject: Use tomlkit instead of toml & specify packages to include This constrains the public api to only what is in the __init__.py in config_file/, rather than all the other python packages in it. --- poetry.lock | 24 ++++++++++++++++++------ pyproject.toml | 9 +++++++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/poetry.lock b/poetry.lock index ea4dca0..35b1683 100644 --- a/poetry.lock +++ b/poetry.lock @@ -218,7 +218,7 @@ description = "File identification library for Python" name = "identify" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" -version = "1.4.23" +version = "1.4.24" [package.extras] license = ["editdistance"] @@ -714,13 +714,21 @@ python-versions = "*" version = "1.1.0" [[package]] -category = "main" +category = "dev" description = "Python Library for Tom's Obvious, Minimal Language" name = "toml" optional = false python-versions = "*" version = "0.10.1" +[[package]] +category = "main" +description = "Style preserving TOML library" +name = "tomlkit" +optional = true +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" +version = "0.6.0" + [[package]] category = "dev" description = "a fork of Python 2 and 3 ast modules with type comment support" @@ -790,11 +798,11 @@ docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"] testing = ["jaraco.itertools", "func-timeout"] [extras] -toml = ["toml"] +toml = ["tomlkit"] yaml = ["pyyaml"] [metadata] -content-hash = "c3cc44b7d09a63e8aba8852464f06543d403a4091727bad9f58ee881c60f18dd" +content-hash = "c3a4ce6bced99835c134bb9950d5c54b7b4bcd76e40fe95f279f918e879d0772" python-versions = "^3.6" [metadata.files] @@ -914,8 +922,8 @@ flake8 = [ {file = "flake8-3.8.3.tar.gz", hash = "sha256:f04b9fcbac03b0a3e58c0ab3a0ecc462e023a9faf046d57794184028123aa208"}, ] identify = [ - {file = "identify-1.4.23-py2.py3-none-any.whl", hash = "sha256:882c4b08b4569517b5f2257ecca180e01f38400a17f429f5d0edff55530c41c7"}, - {file = "identify-1.4.23.tar.gz", hash = "sha256:f89add935982d5bc62913ceee16c9297d8ff14b226e9d3072383a4e38136b656"}, + {file = "identify-1.4.24-py2.py3-none-any.whl", hash = "sha256:5519601b70c831011fb425ffd214101df7639ba3980f24dc283f7675b19127b3"}, + {file = "identify-1.4.24.tar.gz", hash = "sha256:06b4373546ae55eaaefdac54f006951dbd968fe2912846c00e565b09cfaed101"}, ] idna = [ {file = "idna-2.10-py2.py3-none-any.whl", hash = "sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0"}, @@ -1141,6 +1149,10 @@ toml = [ {file = "toml-0.10.1-py2.py3-none-any.whl", hash = "sha256:bda89d5935c2eac546d648028b9901107a595863cb36bae0c73ac804a9b4ce88"}, {file = "toml-0.10.1.tar.gz", hash = "sha256:926b612be1e5ce0634a2ca03470f95169cf16f939018233a670519cb4ac58b0f"}, ] +tomlkit = [ + {file = "tomlkit-0.6.0-py2.py3-none-any.whl", hash = "sha256:e5d5f20809c2b09276a6c5d98fb0202325aee441a651db84ac12e0812ab7e569"}, + {file = "tomlkit-0.6.0.tar.gz", hash = "sha256:74f976908030ff164c0aa1edabe3bf83ea004b3daa5b0940b9c86a060c004e9a"}, +] typed-ast = [ {file = "typed_ast-1.4.1-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:73d785a950fc82dd2a25897d525d003f6378d1cb23ab305578394694202a58c3"}, {file = "typed_ast-1.4.1-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:aaee9905aee35ba5905cfb3c62f3e83b3bec7b39413f0a7f19be4e547ea01ebb"}, diff --git a/pyproject.toml b/pyproject.toml index 846059c..0e883c7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,6 +19,11 @@ classifiers = [ "Topic :: Software Development :: Libraries :: Python Modules", ] +packages = [ + { include = "config_file" }, + { include = "tests", format = "sdist" } +] + [tool.taskipy.tasks] test = "task test_cov -- -v --cov-report term-missing" test_cov = "task pytest -- --cov=config_file/" @@ -28,11 +33,11 @@ pytest = "poetry run pytest tests/" [tool.poetry.dependencies] python = "^3.6" -toml = { version = "^0.10.0", optional = true } +tomlkit = { version = "^0.6.0", optional = true } pyyaml = { version = "^5.3.1", optional = true } [tool.poetry.extras] -toml = ["toml"] +toml = ["tomlkit"] yaml = ["pyyaml"] [tool.poetry.dev-dependencies] -- cgit v1.2.3 From 8c28da679092d9debf7140974fa7d28dfeda8e21 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 22:49:58 -0500 Subject: Redo docstrings and api for abstract parser --- config_file/parsers/abstract_parser.py | 122 ++++++++++++++++++++++----------- 1 file changed, 82 insertions(+), 40 deletions(-) diff --git a/config_file/parsers/abstract_parser.py b/config_file/parsers/abstract_parser.py index 8f1bee1..ed9430b 100644 --- a/config_file/parsers/abstract_parser.py +++ b/config_file/parsers/abstract_parser.py @@ -3,73 +3,115 @@ from typing import Any class AbstractParser(ABC): - """ - The abstract base parser for all other parsers to implement. + """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. - 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. + 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: - """ - 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) + self.file_contents = file_contents @abstractmethod - def parse(self, file_contents: str) -> Any: - """ - Parse the file_contents into an internal representation - the given parser can work with. + 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, 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. + def get(self, key: str) -> Any: + """Retrieve the value of a key from the working file. + + Args: + key: The key to retrieve. - If parse_types is set to False, all values will be returned - back as strings. + Returns: + Whatever value the given key happens to contain. + + Raises: + MissingKeyError: If the key to retrieve does not exist. """ raise NotImplementedError @abstractmethod - def set(self, key: str, value: Any) -> bool: - """Sets the value of a key.""" + 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, section_key: str) -> bool: - """Deletes a key/value pair or entire sections.""" + def delete(self, key: str) -> None: + """Deletes a key from the working file. + + Args: + key: The key we would like to remove. + + Raises: + MissingKeyError: If the key to remove does not exist. + """ raise NotImplementedError @abstractmethod def stringify(self) -> str: - """ - Using self.__contents, converts the parsed internal representation - of the file back into a string. + """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, section_key: str) -> bool: - """ - Check if a section, sub-section, or key exists in the file - using a section.key format. + def has(self, key: str) -> 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. - Some formats, like JSON, do not have sections and therefore, - it would only be checking if a certain key exists. + Returns: + True if the working file has the key. False otherwise. """ raise NotImplementedError -- cgit v1.2.3 From ce71e05c2615350dba997b671ee63f8bd607cf03 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 22:50:39 -0500 Subject: Remove parser imports from __init__ --- config_file/parsers/__init__.py | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/config_file/parsers/__init__.py b/config_file/parsers/__init__.py index 554b624..e69de29 100644 --- a/config_file/parsers/__init__.py +++ b/config_file/parsers/__init__.py @@ -1,23 +0,0 @@ -from .abstract_parser import AbstractParser -from .ini_parser import IniParser -from .json_parser import JsonParser -from .parse_value import ( - can_be_parsed_as_bool, - can_be_parsed_as_float, - can_be_parsed_as_int, - parse_value, -) -from .toml_parser import TomlParser -from .yaml_parser import YamlParser - -__all__ = [ - "AbstractParser", - "IniParser", - "JsonParser", - "TomlParser", - "YamlParser", - "parse_value", - "can_be_parsed_as_bool", - "can_be_parsed_as_float", - "can_be_parsed_as_int", -] -- cgit v1.2.3 From ba5d3e2541670829697d794943322dd5f2a453af Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 22:51:14 -0500 Subject: Add toml and yaml parsers --- config_file/parsers/toml_parser.py | 29 ++++++++++++++++++++++++++--- config_file/parsers/yaml_parser.py | 30 +++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py index 4905aec..d369003 100644 --- a/config_file/parsers/toml_parser.py +++ b/config_file/parsers/toml_parser.py @@ -1,5 +1,28 @@ -from config_file.parsers import AbstractParser +try: + import tomlkit + TOML_AVAILABLE = True +except ImportError: + TOML_AVAILABLE = False -class TomlParser(AbstractParser): - """The TomlParser allows use of toml files with ConfigFile.""" +from config_file.exceptions import MissingDependencyError + +from .base_parser import BaseParser + + +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 " + "file was attempted to be used. Install the `toml` " + "extra first with `pip install config-file[toml]`." + ) + + super().__init__( + file_contents=file_contents, + module=tomlkit, + # tomlkit doesn't expose it's exceptions, but + # it's parsing error is a subclass of ValueError + decode_error=ValueError, + ) diff --git a/config_file/parsers/yaml_parser.py b/config_file/parsers/yaml_parser.py index a45d7f6..77b59fe 100644 --- a/config_file/parsers/yaml_parser.py +++ b/config_file/parsers/yaml_parser.py @@ -1,5 +1,29 @@ -from config_file.parsers import AbstractParser +try: + import yaml + YAML_AVAILABLE = True -class YamlParser(AbstractParser): - pass + # Conform yaml to json and + # toml package syntax + yaml.loads = yaml.load + yaml.dumps = yaml.dump +except ImportError: + YAML_AVAILABLE = False + +from config_file.exceptions import MissingDependencyError + +from .base_parser import BaseParser + + +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]`." + ) + + super().__init__( + file_contents=file_contents, module=yaml, decode_error=yaml.YAMLError + ) -- 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 ++++++++++++++++++++++ config_file/parsers/ini_parser.py | 145 ++++++++++--------------------------- config_file/parsers/json_parser.py | 127 ++------------------------------ 3 files changed, 130 insertions(+), 228 deletions(-) create mode 100644 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 diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 164ff6d..2a29ba7 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -1,124 +1,53 @@ import configparser from io import StringIO -from config_file.exceptions import ParsingError -from config_file.parsers.abstract_parser import AbstractParser -from config_file.parsers.parse_value import parse_value -from config_file.utils import split_on_dot +from .base_parser import BaseParser -class IniParser(AbstractParser): - 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. +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. - :param section_key: The key to set from the config file. e.g. 'section1.key' + It is used as the `module` parameter + for BaseParser, which requires us to have a + `dumps` and `loads` method. + """ - :param value: The value to set the key to. It can be any type that can - be converted to a string. + @staticmethod + def loads(file_contents: str) -> dict: + parser = configparser.ConfigParser() + parser.read_string(file_contents) - :return: True if the setting was successful. + return IniParserCompatibility.__create_configparser_dict(parser) - :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. + @staticmethod + def dumps(file_contents: dict) -> str: + parser = configparser.ConfigParser() + buffer = StringIO() - :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." - ) + parser.read_dict(file_contents) + parser.write(buffer) - self.parsed_content.remove_section(section_key) - return True + return buffer.getvalue() - 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}." - ) + @staticmethod + def __create_configparser_dict(parser: configparser.ConfigParser) -> dict: + result = {} + items = dict(parser.items()) - self.parsed_content.remove_option(section, key) - return True + for section in parser.sections(): + result.update({str(section): dict(items[section])}) - def stringify(self) -> str: - buffer = StringIO() - self.parsed_content.write(buffer) - return buffer.getvalue() + return result - 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) +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/parsers/json_parser.py b/config_file/parsers/json_parser.py index 237c0b5..5314cd7 100644 --- a/config_file/parsers/json_parser.py +++ b/config_file/parsers/json_parser.py @@ -1,128 +1,15 @@ import json -from config_file.exceptions import ParsingError -from config_file.nested_lookup import ( - get_occurrence_of_key, - nested_delete, - nested_lookup, - nested_update, -) -from config_file.parsers import AbstractParser, parse_value -from config_file.utils import split_on_dot +from .base_parser import BaseParser -class JsonParser(AbstractParser): - """ - 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', all=True) - >>> [{ "key2": "foo", "key1": "bar" }, "bar"] - """ - +class JsonParser(BaseParser): 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, 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 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 all: - result = nested_lookup(key, self.parsed_content) - elif "." in key: - split_keys = split_on_dot(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 {key} because {key_causing_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, all: bool = False): - if 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, all: bool = False): - if 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 + 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) - - def has(self, search_key: str) -> bool: - return get_occurrence_of_key(self.parsed_content, key=search_key) > 0 -- cgit v1.2.3 From 92593a3895c8e045e059d6371027aa99e39a153f Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 22:53:38 -0500 Subject: Add MissingKeyError --- config_file/__init__.py | 4 +++- config_file/exceptions.py | 15 +++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/config_file/__init__.py b/config_file/__init__.py index 668c0a9..3825140 100644 --- a/config_file/__init__.py +++ b/config_file/__init__.py @@ -2,10 +2,11 @@ from config_file.config_file import ConfigFile from config_file.exceptions import ( ConfigFileError, MissingDependencyError, + MissingKeyError, ParsingError, UnrecognizedFileError, ) -from config_file.parsers import AbstractParser +from config_file.parsers.abstract_parser import AbstractParser __version__ = "0.10.0" @@ -16,4 +17,5 @@ __all__ = [ "ParsingError", "UnrecognizedFileError", "MissingDependencyError", + "MissingKeyError", ] diff --git a/config_file/exceptions.py b/config_file/exceptions.py index 03ef052..d11e373 100644 --- a/config_file/exceptions.py +++ b/config_file/exceptions.py @@ -2,10 +2,6 @@ class ConfigFileError(Exception): """A generic error from the package.""" -class ParsingError(ConfigFileError): - """Unable to parse the configuration file.""" - - class MissingDependencyError(ConfigFileError): """ PyYaml or toml is not installed, but the parsers @@ -17,3 +13,14 @@ class UnrecognizedFileError(ConfigFileError): """ A missing file extension or a file type not supported. """ + + +class ParsingError(ConfigFileError): + """Unable to parse the configuration file.""" + + +class MissingKeyError(ParsingError): + """ + A key that was specified (to retrieve, delete, etc.) + is not in the file. + """ -- cgit v1.2.3 From 9010575de1cd91c9fb4152835704931255e3f30d Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 22:54:03 -0500 Subject: Fix type hints and add Default class --- config_file/utils.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/config_file/utils.py b/config_file/utils.py index 4a83a40..de31c69 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -1,8 +1,8 @@ -from pathlib import Path, PurePath -from typing import Type, Union +from pathlib import Path +from typing import List, Union -def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False) -> str: +def split_on_dot(line: Union[str, Path], only_last_dot=False) -> List[str]: """ Split a string on the dot character (.). @@ -11,7 +11,7 @@ def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False) -> str: :raises ValueError: if the line does not have a dot. """ - if isinstance(line, PurePath): + if isinstance(line, Path): line = str(line) if "." not in line: @@ -20,12 +20,12 @@ def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False) -> str: return line.rsplit(".", 1) if only_last_dot else line.split(".") -def read_file(path: Union[str, Type[PurePath]]): +def read_file(path: Union[str, Path]) -> str: with open(path, "r") as file: return file.read() -def create_config_path(file_path: Type[PurePath], original: bool = False) -> Path: +def create_config_path(file_path: Path, original: bool = False) -> Path: if len(file_path.parts) >= 1 and file_path.parts[0] == "~": return file_path.expanduser() @@ -38,3 +38,11 @@ def create_config_path(file_path: Type[PurePath], original: bool = False) -> Pat raise ValueError(f"The specified config file ({file_path}) is a directory.") return file_path + + +class Default: + def __init__(self, value): + self.value = value + + def __repr__(self): + return "Default Value: {} ({})".format(self.value, type(self.value)) -- cgit v1.2.3 From 7929171083f9d79e4e6b62d7f09189e4de520fe4 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 22:54:55 -0500 Subject: Remove `all`, start convert to google docstring, add other parsers --- config_file/config_file.py | 168 +++++++++++++++++++++++++++++---------------- 1 file changed, 110 insertions(+), 58 deletions(-) diff --git a/config_file/config_file.py b/config_file/config_file.py index 34df561..3ac4b91 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -1,18 +1,23 @@ import inspect -from pathlib import Path, PurePath +from pathlib import Path from shutil import copyfile -from typing import Any, Type, Union +from typing import Any, Optional, Type, Union -from config_file.exceptions import ParsingError -from config_file.parsers import AbstractParser, IniParser, JsonParser -from config_file.utils import create_config_path, read_file, split_on_dot +from config_file.exceptions import ParsingError, UnrecognizedFileError +from config_file.parsers.abstract_parser import AbstractParser +from config_file.parsers.ini_parser import IniParser +from config_file.parsers.json_parser import JsonParser +from config_file.parsers.parse_value import parse_value +from config_file.parsers.toml_parser import TomlParser +from config_file.parsers.yaml_parser import YamlParser +from config_file.utils import Default, create_config_path, read_file, split_on_dot class ConfigFile: def __init__( self, - file_path: Type[Union[str, Type[PurePath]]], - parser: Type[AbstractParser] = None, + file_path: Union[str, Path], + parser: Optional[Type[AbstractParser]] = None, ) -> None: """ Saves the config file path and expands it if needed, reads in @@ -33,23 +38,42 @@ class ConfigFile: self.__parser = self.__determine_parser(file_path, parser) @property - def path(self) -> Type[PurePath]: + def path(self) -> Path: return self.__path def __determine_parser( - self, file_path: Type[PurePath], parser: Type[AbstractParser] = None + self, file_path: Path, parser: Optional[Type[AbstractParser]] = None ) -> Type[AbstractParser]: 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] + try: + file_type = split_on_dot(file_path, only_last_dot=True)[-1] + except ValueError: + raise UnrecognizedFileError( + "Tried to determine a parser to use, but the file at " + f"{file_path} does not have an extension." + ) + + return self.__find_parser_by_file_type( + file_type=file_type, file_contents=self.__contents, file_path=self.__path + ) + + @staticmethod + def __find_parser_by_file_type( + file_type: str, file_path: Union[str, Path], file_contents: str + ) -> Type[AbstractParser]: if file_type == "ini": - return IniParser(self.__contents) + return IniParser(file_contents) elif file_type == "json": - return JsonParser(self.__contents) + return JsonParser(file_contents) + elif file_type == "yaml" or file_type == "yml": + return YamlParser(file_contents) + elif file_type == "toml": + return TomlParser(file_contents) else: - raise ValueError( - f"File path contains an unrecognized file type: {file_path}" + raise UnrecognizedFileError( + f"File path at `{file_path}` contains an unrecognized file type." ) def get( @@ -57,8 +81,7 @@ class ConfigFile: key: str, parse_types: bool = False, return_type: Any = None, - default: Any = None, - all: bool = False, + default: Any = Default(None), ) -> Any: """ Retrieve the value of a key. @@ -76,54 +99,83 @@ class ConfigFile: :raises 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 ParsingError as error: + if default is not type(Default): + key_value = default + else: + raise ParsingError(error) + + 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: Any) -> bool: - """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) -> bool: - """Deletes a key/value pair or entire sections.""" - return self.__parser.delete(section_key) + """ + self.__parser.set(key, value) + + def delete(self, section_key: str) -> None: + """Deletes a section or key. + + Args: + section_key: + """ + self.__parser.delete(section_key) def stringify(self) -> str: - """Convert the parsed internal representation of the file back into a string.""" + """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, section_key: str) -> bool: """ - Check if a section, sub-section, or key exists in the file - using a section.key format. + 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. - Some formats, like JSON, do not have sections and therefore, - it would only be checking if the key exists. + Args: + section_key: + + Returns: + True if """ return self.__parser.has(section_key) def restore_original( - self, original_file_path: Union[str, Type[PurePath]] = None - ) -> 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. - - :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. - - :return: True if the reset succeeded - :raises FileNotFoundError: If the original configuration file does not exist - :raises OSError: If self.path is not writable - :raises SameFileError: If self.path and original_file_path are the same file. + self, original_file_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. + + Args: + original_file_path: The original file to reset to. + + If this is not provided, it is calculated for you. + e.g. if your configuration file is named config.json, + this will look for aa config.original.json in the same + directory. + + Raises: + FileNotFoundError: If the original configuration file (whether + calculated or passed in) does not exist. + + OSError: If the current configuration path is not writable. + + SameFileError: If current configuration path and the passed in + original_file_path are the same file. """ if original_file_path is None: original_file_path = create_config_path(self.__path, original=True) @@ -135,17 +187,17 @@ class ConfigFile: self.__path.expanduser().unlink() copyfile(original_file_path, self.__path) - self.__contents = read_file(self.__path) - self.__parser.parsed_content = self.__parser.parse(self.__contents) - return True + self.__contents = read_file(self.__path) + self.__parser.reset_internal_contents(self.__contents) - def save(self) -> bool: + def save(self) -> None: """ - Save the configuration changes by writing the file out to the specified path - :return: True if the save succeeded. + 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. """ - with open(self.__path, "w") as config_file: + with open(str(self.__path), "w") as config_file: config_file.write(self.__parser.stringify()) - - return True -- cgit v1.2.3 From 3fa782f5b5e3bd4445a5db63c0fce5aaac2d4ac3 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 22:55:40 -0500 Subject: Add work in progress tests --- tests/conftest.py | 23 +++++--- tests/templates/toml/default.toml | 6 ++ tests/templates/yaml/default.yaml | 6 ++ tests/test_abstract_parser.py | 0 tests/test_base_parser.py | 114 +++++++++++++++++++------------------- tests/test_config_file.py | 70 +++++++++++------------ tests/test_ini_parser.py | 26 +++------ tests/test_json_parser.py | 68 +---------------------- tests/test_lookup_api.py | 13 +++-- tests/test_nested_lookup.py | 44 ++++++++------- tests/test_parse_value.py | 2 +- tests/test_toml_parser.py | 0 tests/test_yaml_parser.py | 0 13 files changed, 161 insertions(+), 211 deletions(-) create mode 100644 tests/templates/toml/default.toml create mode 100644 tests/templates/yaml/default.yaml create mode 100644 tests/test_abstract_parser.py create mode 100644 tests/test_toml_parser.py create mode 100644 tests/test_yaml_parser.py diff --git a/tests/conftest.py b/tests/conftest.py index 5fdf5a9..70e88c7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,9 +1,9 @@ from pathlib import Path -from typing import Callable, Type +from typing import Callable, Tuple, Type, Union import pytest -from config_file.parsers import AbstractParser, IniParser, JsonParser +from config_file import AbstractParser, ConfigFile from config_file.utils import create_config_path @@ -33,7 +33,10 @@ def template_file(tmp_file: Callable[[str, str], Path]) -> Callable[[str, str], @pytest.fixture def template_original_file() -> Callable[[Path], Path]: - def func(template_path: str): + def func(template_path: Union[str, Path]): + if isinstance(template_path, str): + template_path = Path(template_path) + original_path = create_config_path(template_path, original=True) original_path.touch() original_path.write_text(template_path.read_text(encoding="utf-8")) @@ -44,17 +47,19 @@ def template_original_file() -> Callable[[Path], Path]: @pytest.fixture -def template_and_parser(template_file: Callable[[str, str], Path]) -> tuple: +def template_and_parser( + template_file: Callable[[str, str], Path] +) -> Callable[[str, str], Tuple[Path, Type[AbstractParser]]]: def func( file_type: str, template_name: str = "default" - ) -> Callable[[str, str], Type[AbstractParser]]: + ) -> Tuple[Path, Type[AbstractParser]]: test_file = template_file(file_type, template_name) text = test_file.read_text(encoding="utf-8") - if file_type == "ini": - return test_file, IniParser(text) - elif file_type == "json": - return test_file, JsonParser(text) + parser = ConfigFile._ConfigFile__find_parser_by_file_type( + file_contents=text, file_type=file_type, file_path=test_file + ) + return test_file, parser return func diff --git a/tests/templates/toml/default.toml b/tests/templates/toml/default.toml new file mode 100644 index 0000000..326de33 --- /dev/null +++ b/tests/templates/toml/default.toml @@ -0,0 +1,6 @@ +[header_one] +number_key = 0 + +[header_two] +str_key = "string" +list_key = [1, 2, 3] diff --git a/tests/templates/yaml/default.yaml b/tests/templates/yaml/default.yaml new file mode 100644 index 0000000..095ada8 --- /dev/null +++ b/tests/templates/yaml/default.yaml @@ -0,0 +1,6 @@ +header_one: + number_key: 0 + +header_two: + str_key: "string" + list_key: [1, 2, 3] \ No newline at end of file diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_base_parser.py b/tests/test_base_parser.py index 4eaa92f..90d45f4 100644 --- a/tests/test_base_parser.py +++ b/tests/test_base_parser.py @@ -1,57 +1,57 @@ -import pytest - -from config_file.parsers import AbstractParser - - -class ConcreteParser(AbstractParser): - def __init__(self, file_contents): - super().__init__(file_contents) - - def get(self, key, parse_types=True): - super().get(key, parse_types=parse_types) - - def set(self, key, value): - super().set(key, value) - - def delete(self, section_key): - super().delete(section_key) - - def has(self, section_key: str) -> bool: - super().has(section_key) - - def stringify(self) -> str: - super().stringify() - - def parse(self, file_contents: str, call_super=False): - if call_super: - super().parse(file_contents) - - return file_contents - - -def test_that_base_parser_can_not_be_instantiated(): - with pytest.raises(TypeError): - AbstractParser("") - - -@pytest.mark.parametrize( - "function, args", - [ - (ConcreteParser("").get, ""), - (ConcreteParser("").set, ("", "")), - (ConcreteParser("").delete, ""), - (ConcreteParser("").stringify, None), - (ConcreteParser("").has, ""), - (ConcreteParser("").parse, "call super"), - ], -) -def test_that_base_parser_raises_not_implemented_errors(function, args): - with pytest.raises(NotImplementedError): - if args is None: - function() - elif type(args) is tuple: - function(*args) - elif args == "call super": - function(args, call_super=True) - else: - function(args) +# import pytest +# +# from config_file.parsers import BaseParser +# + +# class ConcreteParser(AbstractParser): +# def __init__(self, file_contents): +# super().__init__(file_contents) +# +# def get(self, key, parse_types=True): +# super().get(key, parse_types=parse_types) +# +# def set(self, key, value): +# super().set(key, value) +# +# def delete(self, section_key): +# super().delete(section_key) +# +# def has(self, section_key: str) -> bool: +# super().has(section_key) +# +# def stringify(self) -> str: +# super().stringify() +# +# def parse(self, file_contents: str, call_super=False): +# if call_super: +# super().parse(file_contents) +# +# return file_contents +# +# +# def test_that_base_parser_can_not_be_instantiated(): +# with pytest.raises(TypeError): +# AbstractParser("") +# +# +# @pytest.mark.parametrize( +# "function, args", +# [ +# (ConcreteParser("").get, ""), +# (ConcreteParser("").set, ("", "")), +# (ConcreteParser("").delete, ""), +# (ConcreteParser("").stringify, None), +# (ConcreteParser("").has, ""), +# (ConcreteParser("").parse, "call super"), +# ], +# ) +# def test_that_base_parser_raises_not_implemented_errors(function, args): +# with pytest.raises(NotImplementedError): +# if args is None: +# function() +# elif type(args) is tuple: +# function(*args) +# elif args == "call super": +# function(args, call_super=True) +# else: +# function(args) diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 5430fb8..13d7271 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -4,21 +4,21 @@ from pathlib import Path import pytest from config_file.config_file import ConfigFile -from config_file.parsers import AbstractParser +from config_file.parsers.abstract_parser import AbstractParser -SUPPORTED_FILE_TYPES = ["ini", "json"] +SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"] @pytest.fixture(params=SUPPORTED_FILE_TYPES) def template_and_config_file(request, template_file): def func(template_name: str = "default", parser: AbstractParser = None): template = template_file(request.param, template_name) - return (template, ConfigFile(template, parser=parser)) + return template, ConfigFile(template, parser=parser) return func -@pytest.fixture +@pytest.fixture(params=SUPPORTED_FILE_TYPES) def templated_config_file(template_and_config_file): def func(template_name: str = "default", parser: AbstractParser = None): return template_and_config_file(template_name, parser)[1] @@ -179,34 +179,34 @@ def test_that_config_file_can_delete(templated_config_file): assert config.has("header_one.number_key") is False -def test_that_custom_parser_can_be_used(template_and_config_file): - class CustomParser(AbstractParser): - def __init__(self, file_contents): - super().__init__(file_contents) - - def parse(self, file_contents: str): - return file_contents - - def get(self, key, parse_types=True): - return key - - def set(self, key, value): - return key, value - - def delete(self, section_key): - return section_key - - def has(self, section_key: str) -> bool: - return True - - def stringify(self) -> str: - return str(self.parsed_content) - - template_file, config = template_and_config_file(parser=CustomParser) - - assert isinstance(config._ConfigFile__parser, CustomParser) - assert config.get("key") == "key" - assert config.set("key", "value") == ("key", "value") - assert config.delete("section_key") == "section_key" - assert config.has("blah") - assert config.stringify() == template_file.read_text() +# def test_that_custom_parser_can_be_used(template_and_config_file): +# class CustomParser(AbstractParser): +# def __init__(self, file_contents): +# super().__init__(file_contents) +# +# def parse(self, file_contents: str): +# return file_contents +# +# def get(self, key, parse_types=True): +# return key +# +# def set(self, key, value): +# return key, value +# +# def delete(self, section_key): +# return section_key +# +# def has(self, section_key: str) -> bool: +# return True +# +# def stringify(self) -> str: +# return str(self.parsed_content) +# +# template_file, config = template_and_config_file(parser=CustomParser) +# +# assert isinstance(config._ConfigFile__parser, CustomParser) +# assert config.get("key") == "key" +# assert config.set("key", "value") == ("key", "value") +# assert config.delete("section_key") == "section_key" +# assert config.has("blah") +# assert config.stringify() == template_file.read_text() diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index d39758e..6577291 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,7 +1,7 @@ import pytest from config_file.exceptions import ParsingError -from config_file.parsers import IniParser +from config_file.parsers.ini_parser import IniParser @pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"]) @@ -11,29 +11,19 @@ def test_incorrect_ini_formats(ini_file): @pytest.mark.parametrize( - "section_key,expected_value,parse_type", + "section_key,expected_value", [ - ("test_section.intkey", 5, True), - ("test_section.strkey", "blah", True), - ("test_section.boolkey", False, True), - ("test_section.floatkey", 5.3, True), - ("test_section.intkey", "5", False), - ("test_section.strkey", "blah", False), - ("test_section.boolkey", "false", False), - ("test_section.floatkey", "5.3", False), + ("test_section.intkey", "5"), + ("test_section.strkey", "blah"), + ("test_section.boolkey", "false"), + ("test_section.floatkey", "5.3"), ( "test_section", {"intkey": "5", "strkey": "blah", "boolkey": "false", "floatkey": "5.3"}, - False, - ), - ( - "test_section", - {"intkey": 5, "strkey": "blah", "boolkey": False, "floatkey": 5.3}, - True, ), ], ) -def test_that_ini_parser_can_get_values(section_key, expected_value, parse_type): +def test_that_ini_parser_can_get_values(section_key, expected_value): ini_file = """[test_section] intkey = 5 strkey = blah @@ -42,7 +32,7 @@ floatkey = 5.3 """ parser = IniParser(ini_file) - assert parser.get(section_key, parse_types=parse_type) == expected_value + assert parser.get(section_key) == expected_value @pytest.mark.parametrize( diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index b622f01..aa29ec6 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -1,9 +1,6 @@ -import json - import pytest from config_file.exceptions import ParsingError -from config_file.parsers import JsonParser def test_invalid_json(templated_parser): @@ -72,25 +69,6 @@ def test_that_json_parser_can_get_keys_without_parsing_types( assert parser.get(key) == value -@pytest.mark.parametrize( - "key, value", - [ - ("glossary.title", "str_value"), - ("glossary.dict.str_number", 5), - ( - "glossary.dict_within_dict", - { - "str_number": 16, - "another_dict": {"str_bool": False, "list_of_values": [5, False, True]}, - }, - ), - ], -) -def test_that_json_parser_can_get_keys_with_parsing_types(templated_parser, key, value): - parser = templated_parser("json", "glossary") - assert parser.get(key, parse_types=True) == value - - @pytest.mark.parametrize( "key, value", [("zip", "zipzip"), ("foo.bar.foobar", 5), ("foo.baz", "bar")] ) @@ -116,53 +94,9 @@ def test_that_json_parser_can_set_keys(templated_parser, key, value): "key", ["glossary", "dict_within_dict", "glossary.GlossDiv.GlossList.GlossEntry.Abbrev"], ) -def test_that_json_parser_can_delete_keys( - template_and_parser, key, test_json, expected_json -): +def test_that_json_parser_can_delete_keys(template_and_parser, key): template, parser = template_and_parser("json", "glossary") parser.delete(key) assert parser.stringify() == template.read_text(encoding="utf-8") - - -@pytest.mark.parametrize( - "test_json, key, expected_output", - [({"foo": {"bar": 5, "baz": {"bar": 10, "bam": {"bar": 15}}}}, "bar", [5, 10, 15])], -) -def test_that_json_parser_can_retrieve_all_keys(test_json, key, expected_output): - parser = JsonParser(json.dumps(test_json)) - assert parser.get(key, all=True) == expected_output - - -@pytest.mark.parametrize( - "test_json, key, value, expected_output", - [ - ( - {"foo": {"bar": 5, "baz": {"bar": 10, "bam": {"bar": 15}}}}, - "bar", - "new_value", - ["new_value", "new_value", "new_value"], - ) - ], -) -def test_that_json_parser_can_set_all_keys(test_json, key, value, expected_output): - parser = JsonParser(json.dumps(test_json)) - parser.set(key, value, all=True) - assert parser.get(key, all=True) == expected_output - - -@pytest.mark.parametrize( - "test_json, key, expected_output", - [ - ( - {"foo": {"bar": 5, "baz": {"bar": 10, "bam": {"bar": 15}}}}, - "bar", - {"foo": {"baz": {"bam": {}}}}, - ) - ], -) -def test_that_json_parser_can_delete_all_keys(test_json, key, expected_output): - parser = JsonParser(json.dumps(test_json)) - parser.delete(key, all=True) - assert parser.stringify() == json.dumps(expected_output) diff --git a/tests/test_lookup_api.py b/tests/test_lookup_api.py index f7b7cc3..8b8ca9e 100644 --- a/tests/test_lookup_api.py +++ b/tests/test_lookup_api.py @@ -1,7 +1,11 @@ from unittest import TestCase -from config_file.nested_lookup import nested_lookup, nested_update -from config_file.nested_lookup import nested_delete, nested_alter +from config_file.nested_lookup import ( + nested_alter, + nested_delete, + nested_lookup, + nested_update, +) class BaseLookUpApi(TestCase): @@ -553,17 +557,16 @@ class TestNestedAlter(BaseLookUpApi): self.assertEqual(out[1]["salsa"][0]["burrito"]["taco"], 79) def test_nested_alter_work_with_right_order(self): - document = {"taco": 42, "salsa": [{"burrito":{"key":20}}], "key":50} + document = {"taco": 42, "salsa": [{"burrito": {"key": 20}}], "key": 50} def callback(data): return data + 100 altered_document = nested_alter(document, "key", callback, in_place=True) - + self.assertEqual(altered_document["salsa"][0]["burrito"]["key"], 120) self.assertEqual(altered_document["key"], 150) - def test_sample_data4(self): result = { diff --git a/tests/test_nested_lookup.py b/tests/test_nested_lookup.py index 6c1cc83..591edda 100644 --- a/tests/test_nested_lookup.py +++ b/tests/test_nested_lookup.py @@ -1,11 +1,11 @@ from unittest import TestCase from config_file.nested_lookup import ( - nested_lookup, get_all_keys, get_occurrence_of_key, get_occurrence_of_value, - get_occurrences_and_values + get_occurrences_and_values, + nested_lookup, ) @@ -39,7 +39,13 @@ class TestNestedLookup(TestCase): "name": "Test", "date": "YYYY-MM-DD HH:MM:SS", } - self.subject_dict4 = {1: "a", 2: {"b": 44, "C": 55}, 3: "d", 4: "e", "6776": "works"} + self.subject_dict4 = { + 1: "a", + 2: {"b": 44, "C": 55}, + 3: "d", + 4: "e", + "6776": "works", + } def test_nested_lookup(self): results = nested_lookup("d", self.subject_dict) @@ -357,7 +363,7 @@ class TestGetOccurrence(TestCase): "core_details": { "total_numberof_cores": "4", "l2_cache(per_core)": "256 KB", - } + }, } ] @@ -368,7 +374,7 @@ class TestGetOccurrence(TestCase): "core_details": { "total_numberof_cores": "4", "l2_cache(per_core)": "256 KB", - } + }, }, { "processor_name": "4", @@ -376,8 +382,8 @@ class TestGetOccurrence(TestCase): "core_details": { "total_numberof_cores": "4", "l2_cache(per_core)": "256 KB", - } - } + }, + }, ] def test_sample_data1(self): @@ -417,28 +423,28 @@ class TestGetOccurrence(TestCase): self.assertEqual(2, get_occurrence_of_key(self.sample5, "memory")) def test_sample_data6(self): - value = '4' + value = "4" result = get_occurrences_and_values(self.sample6, value) - self.assertEqual(2, result[value]['occurrences']) - self.assertEqual(2, len(result[value]['values'])) + self.assertEqual(2, result[value]["occurrences"]) + self.assertEqual(2, len(result[value]["values"])) def test_sample_data7(self): - value = '2.7 GHz' + value = "2.7 GHz" result = get_occurrences_and_values(self.sample6, value) - self.assertEqual(1, result[value]['occurrences']) - self.assertEqual(1, len(result[value]['values'])) + self.assertEqual(1, result[value]["occurrences"]) + self.assertEqual(1, len(result[value]["values"])) def test_sample_data8(self): - value = '4' + value = "4" result = get_occurrences_and_values(self.sample7, value) - self.assertEqual(4, result[value]['occurrences']) - self.assertEqual(4, len(result[value]['values'])) + self.assertEqual(4, result[value]["occurrences"]) + self.assertEqual(4, len(result[value]["values"])) def test_sample_data9(self): - value = '5' + value = "5" result = get_occurrences_and_values(self.sample7, value) - self.assertEqual(0, result[value]['occurrences']) - self.assertEqual(0, len(result[value]['values'])) + self.assertEqual(0, result[value]["occurrences"]) + self.assertEqual(0, len(result[value]["values"])) if __name__ == "__main__": diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py index 37c68b5..81db472 100644 --- a/tests/test_parse_value.py +++ b/tests/test_parse_value.py @@ -1,6 +1,6 @@ import pytest -from config_file.parsers import can_be_parsed_as_bool, parse_value +from config_file.parsers.parse_value import can_be_parsed_as_bool, parse_value @pytest.mark.parametrize( diff --git a/tests/test_toml_parser.py b/tests/test_toml_parser.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_yaml_parser.py b/tests/test_yaml_parser.py new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3 From 18306b975e7d53ad58a6039e51741d2f75a7edbd Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 22:56:14 -0500 Subject: Lint nested_lookup --- config_file/nested_lookup/__init__.py | 21 ++++++++++++++------- config_file/nested_lookup/lookup_api.py | 12 +++--------- config_file/nested_lookup/nested_lookup.py | 10 ++++------ 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/config_file/nested_lookup/__init__.py b/config_file/nested_lookup/__init__.py index 768facc..4493bdf 100644 --- a/config_file/nested_lookup/__init__.py +++ b/config_file/nested_lookup/__init__.py @@ -1,12 +1,19 @@ +from .lookup_api import nested_alter, nested_delete, nested_update from .nested_lookup import ( - nested_lookup, get_all_keys, get_occurrence_of_key, get_occurrence_of_value, - get_occurrences_and_values -) -from .lookup_api import ( - nested_update, - nested_delete, - nested_alter + get_occurrences_and_values, + nested_lookup, ) + +__all__ = [ + "get_all_keys", + "get_occurrences_and_values", + "get_occurrence_of_value", + "get_occurrence_of_key", + "nested_update", + "nested_delete", + "nested_alter", + "nested_lookup", +] diff --git a/config_file/nested_lookup/lookup_api.py b/config_file/nested_lookup/lookup_api.py index e103534..886e8bf 100644 --- a/config_file/nested_lookup/lookup_api.py +++ b/config_file/nested_lookup/lookup_api.py @@ -65,9 +65,7 @@ def nested_update(document, key, value, in_place=False, treat_as_element=True): # 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." - ) + raise Exception("The value must be a list when treat_as_element is False.") elif treat_as_element: value = [value] @@ -91,18 +89,14 @@ def _nested_update(document, key, value): """ if isinstance(document, list): for list_items in document: - _nested_update( - document=list_items, key=key, value=value - ) + _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 - ) + _nested_update(document=dict_value, key=key, value=value) return document diff --git a/config_file/nested_lookup/nested_lookup.py b/config_file/nested_lookup/nested_lookup.py index 9454a44..6f2e7f2 100644 --- a/config_file/nested_lookup/nested_lookup.py +++ b/config_file/nested_lookup/nested_lookup.py @@ -1,6 +1,5 @@ from collections import defaultdict - values_list = [] @@ -98,15 +97,14 @@ def get_occurrences_and_values(items, value): value_list = [] for item in items: - occurrence_result, values = _get_occurrence_with_values(dictionary=item, item="value", keyword=value) + 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 - } + occurrences[value] = {"occurrences": occurrence, "values": value_list} return occurrences -- cgit v1.2.3 From 8d9283fad3c10190c4c9aa3d35994cb1177bf656 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 22:57:30 -0500 Subject: Add name to security workflow --- .github/workflows/python-security.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/python-security.yml b/.github/workflows/python-security.yml index c5339e4..a9f4a0f 100644 --- a/.github/workflows/python-security.yml +++ b/.github/workflows/python-security.yml @@ -1,3 +1,5 @@ +name: Python Security + on: [push] jobs: -- cgit v1.2.3 From d9232d03e6b91bc3c83e9b0b4484c27b2143ab9f Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 17:02:17 -0500 Subject: Include .venv/ in gitignore --- .gitignore | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 31ebf72..b659da2 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,7 @@ # Python *.egg-info/ __pycache__/ -venv/ +.venv/ # Pytest .pytest_cache/ @@ -22,4 +22,4 @@ dist/ .python-version # Sphinx -build/ \ No newline at end of file +build/ -- cgit v1.2.3 From 390586af721d65bfef452370ec77ed8be3003b77 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 17:02:36 -0500 Subject: Omit abstract parser from test coverage There's nothing really to test here so it doesn't make much sense to make a dummy test file just for code coverage. --- .coveragerc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.coveragerc b/.coveragerc index e272605..11aaf17 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,2 +1,4 @@ [run] -source=config_file \ No newline at end of file +source=config_file/ +branch = True +omit = config_file/parsers/abstract_parser.py \ No newline at end of file -- cgit v1.2.3 From d369373e8edb4f150d227d26563bb61ca48747b9 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 17:03:48 -0500 Subject: Use ruamel.yaml instead of PyYAML for YamlParser --- poetry.lock | 149 ++++++++++++++++++++++++++++++++++++++------------------- pyproject.toml | 4 +- 2 files changed, 102 insertions(+), 51 deletions(-) diff --git a/poetry.lock b/poetry.lock index 35b1683..d8e7f36 100644 --- a/poetry.lock +++ b/poetry.lock @@ -166,7 +166,7 @@ description = "Code coverage measurement for Python" name = "coverage" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4" -version = "5.2" +version = "5.2.1" [package.extras] toml = ["toml"] @@ -218,7 +218,7 @@ description = "File identification library for Python" name = "identify" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" -version = "1.4.24" +version = "1.4.25" [package.extras] license = ["editdistance"] @@ -508,7 +508,7 @@ python-versions = "*" version = "2020.1" [[package]] -category = "main" +category = "dev" description = "YAML parser and emitter for Python" name = "pyyaml" optional = false @@ -541,6 +541,32 @@ urllib3 = ">=1.21.1,<1.25.0 || >1.25.0,<1.25.1 || >1.25.1,<1.26" security = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)"] socks = ["PySocks (>=1.5.6,<1.5.7 || >1.5.7)", "win-inet-pton"] +[[package]] +category = "main" +description = "ruamel.yaml is a YAML parser/emitter that supports roundtrip preservation of comments, seq/map flow style, and map key order" +name = "ruamel.yaml" +optional = true +python-versions = "*" +version = "0.16.10" + +[package.dependencies] +[package.dependencies."ruamel.yaml.clib"] +python = "<3.9" +version = ">=0.1.2" + +[package.extras] +docs = ["ryd"] +jinja2 = ["ruamel.yaml.jinja2 (>=0.2)"] + +[[package]] +category = "main" +description = "C version of reader, parser and emitter for ruamel.yaml derived from libyaml" +marker = "platform_python_implementation == \"CPython\" and python_version < \"3.9\"" +name = "ruamel.yaml.clib" +optional = true +python-versions = "*" +version = "0.2.0" + [[package]] category = "dev" description = "Statically populate the `known_third_party` `isort` setting." @@ -743,7 +769,7 @@ description = "HTTP library with thread-safe connection pooling, file post, and name = "urllib3" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4" -version = "1.25.9" +version = "1.25.10" [package.extras] brotli = ["brotlipy (>=0.6.0)"] @@ -756,7 +782,7 @@ description = "Virtual Python Environment builder" name = "virtualenv" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" -version = "20.0.27" +version = "20.0.28" [package.dependencies] appdirs = ">=1.4.3,<2" @@ -773,8 +799,8 @@ python = "<3.7" version = ">=1.0" [package.extras] -docs = ["sphinx (>=3)", "sphinx-argparse (>=0.2.5)", "sphinx-rtd-theme (>=0.4.3)", "towncrier (>=19.9.0rc1)", "proselint (>=0.10.2)"] -testing = ["pytest (>=4)", "coverage (>=5)", "coverage-enable-subprocess (>=1)", "pytest-xdist (>=1.31.0)", "pytest-mock (>=2)", "pytest-env (>=0.6.2)", "pytest-randomly (>=1)", "pytest-timeout (>=1)", "pytest-freezegun (>=0.4.1)", "flaky (>=3)", "packaging (>=20.0)", "xonsh (>=0.9.16)"] +docs = ["proselint (>=0.10.2)", "sphinx (>=3)", "sphinx-argparse (>=0.2.5)", "sphinx-rtd-theme (>=0.4.3)", "towncrier (>=19.9.0rc1)"] +testing = ["coverage (>=5)", "coverage-enable-subprocess (>=1)", "flaky (>=3)", "pytest (>=4)", "pytest-env (>=0.6.2)", "pytest-freezegun (>=0.4.1)", "pytest-mock (>=2)", "pytest-randomly (>=1)", "pytest-timeout (>=1)", "pytest-xdist (>=1.31.0)", "packaging (>=20.0)", "xonsh (>=0.9.16)"] [[package]] category = "dev" @@ -799,10 +825,10 @@ testing = ["jaraco.itertools", "func-timeout"] [extras] toml = ["tomlkit"] -yaml = ["pyyaml"] +yaml = ["ruamel.yaml"] [metadata] -content-hash = "c3a4ce6bced99835c134bb9950d5c54b7b4bcd76e40fe95f279f918e879d0772" +content-hash = "04cae2b80bbae148abd0049b077febcfa63984c6d289560146fcc3e584f4f227" python-versions = "^3.6" [metadata.files] @@ -870,40 +896,40 @@ colorama = [ {file = "colorama-0.4.3.tar.gz", hash = "sha256:e96da0d330793e2cb9485e9ddfd918d456036c7149416295932478192f4436a1"}, ] coverage = [ - {file = "coverage-5.2-cp27-cp27m-macosx_10_13_intel.whl", hash = "sha256:d9ad0a988ae20face62520785ec3595a5e64f35a21762a57d115dae0b8fb894a"}, - {file = "coverage-5.2-cp27-cp27m-macosx_10_9_x86_64.whl", hash = "sha256:4bb385a747e6ae8a65290b3df60d6c8a692a5599dc66c9fa3520e667886f2e10"}, - {file = "coverage-5.2-cp27-cp27m-manylinux1_i686.whl", hash = "sha256:9702e2cb1c6dec01fb8e1a64c015817c0800a6eca287552c47a5ee0ebddccf62"}, - {file = "coverage-5.2-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:42fa45a29f1059eda4d3c7b509589cc0343cd6bbf083d6118216830cd1a51613"}, - {file = "coverage-5.2-cp27-cp27m-win32.whl", hash = "sha256:41d88736c42f4a22c494c32cc48a05828236e37c991bd9760f8923415e3169e4"}, - {file = "coverage-5.2-cp27-cp27m-win_amd64.whl", hash = "sha256:bbb387811f7a18bdc61a2ea3d102be0c7e239b0db9c83be7bfa50f095db5b92a"}, - {file = "coverage-5.2-cp27-cp27mu-manylinux1_i686.whl", hash = "sha256:3740b796015b889e46c260ff18b84683fa2e30f0f75a171fb10d2bf9fb91fc70"}, - {file = "coverage-5.2-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:ebf2431b2d457ae5217f3a1179533c456f3272ded16f8ed0b32961a6d90e38ee"}, - {file = "coverage-5.2-cp35-cp35m-macosx_10_13_x86_64.whl", hash = "sha256:d54d7ea74cc00482a2410d63bf10aa34ebe1c49ac50779652106c867f9986d6b"}, - {file = "coverage-5.2-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:87bdc8135b8ee739840eee19b184804e5d57f518578ffc797f5afa2c3c297913"}, - {file = "coverage-5.2-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:ed9a21502e9223f563e071759f769c3d6a2e1ba5328c31e86830368e8d78bc9c"}, - {file = "coverage-5.2-cp35-cp35m-win32.whl", hash = "sha256:509294f3e76d3f26b35083973fbc952e01e1727656d979b11182f273f08aa80b"}, - {file = "coverage-5.2-cp35-cp35m-win_amd64.whl", hash = "sha256:ca63dae130a2e788f2b249200f01d7fa240f24da0596501d387a50e57aa7075e"}, - {file = "coverage-5.2-cp36-cp36m-macosx_10_13_x86_64.whl", hash = "sha256:5c74c5b6045969b07c9fb36b665c9cac84d6c174a809fc1b21bdc06c7836d9a0"}, - {file = "coverage-5.2-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:c32aa13cc3fe86b0f744dfe35a7f879ee33ac0a560684fef0f3e1580352b818f"}, - {file = "coverage-5.2-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:1e58fca3d9ec1a423f1b7f2aa34af4f733cbfa9020c8fe39ca451b6071237405"}, - {file = "coverage-5.2-cp36-cp36m-win32.whl", hash = "sha256:3b2c34690f613525672697910894b60d15800ac7e779fbd0fccf532486c1ba40"}, - {file = "coverage-5.2-cp36-cp36m-win_amd64.whl", hash = "sha256:a4d511012beb967a39580ba7d2549edf1e6865a33e5fe51e4dce550522b3ac0e"}, - {file = "coverage-5.2-cp37-cp37m-macosx_10_13_x86_64.whl", hash = "sha256:32ecee61a43be509b91a526819717d5e5650e009a8d5eda8631a59c721d5f3b6"}, - {file = "coverage-5.2-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:6f91b4492c5cde83bfe462f5b2b997cdf96a138f7c58b1140f05de5751623cf1"}, - {file = "coverage-5.2-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:bfcc811883699ed49afc58b1ed9f80428a18eb9166422bce3c31a53dba00fd1d"}, - {file = "coverage-5.2-cp37-cp37m-win32.whl", hash = "sha256:60a3d36297b65c7f78329b80120f72947140f45b5c7a017ea730f9112b40f2ec"}, - {file = "coverage-5.2-cp37-cp37m-win_amd64.whl", hash = "sha256:12eaccd86d9a373aea59869bc9cfa0ab6ba8b1477752110cb4c10d165474f703"}, - {file = "coverage-5.2-cp38-cp38-macosx_10_13_x86_64.whl", hash = "sha256:d82db1b9a92cb5c67661ca6616bdca6ff931deceebb98eecbd328812dab52032"}, - {file = "coverage-5.2-cp38-cp38-manylinux1_i686.whl", hash = "sha256:214eb2110217f2636a9329bc766507ab71a3a06a8ea30cdeebb47c24dce5972d"}, - {file = "coverage-5.2-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:8a3decd12e7934d0254939e2bf434bf04a5890c5bf91a982685021786a08087e"}, - {file = "coverage-5.2-cp38-cp38-win32.whl", hash = "sha256:1dcebae667b73fd4aa69237e6afb39abc2f27520f2358590c1b13dd90e32abe7"}, - {file = "coverage-5.2-cp38-cp38-win_amd64.whl", hash = "sha256:f50632ef2d749f541ca8e6c07c9928a37f87505ce3a9f20c8446ad310f1aa87b"}, - {file = "coverage-5.2-cp39-cp39-macosx_10_13_x86_64.whl", hash = "sha256:7403675df5e27745571aba1c957c7da2dacb537c21e14007ec3a417bf31f7f3d"}, - {file = "coverage-5.2-cp39-cp39-manylinux1_i686.whl", hash = "sha256:0fc4e0d91350d6f43ef6a61f64a48e917637e1dcfcba4b4b7d543c628ef82c2d"}, - {file = "coverage-5.2-cp39-cp39-manylinux1_x86_64.whl", hash = "sha256:25fe74b5b2f1b4abb11e103bb7984daca8f8292683957d0738cd692f6a7cc64c"}, - {file = "coverage-5.2-cp39-cp39-win32.whl", hash = "sha256:d67599521dff98ec8c34cd9652cbcfe16ed076a2209625fca9dc7419b6370e5c"}, - {file = "coverage-5.2-cp39-cp39-win_amd64.whl", hash = "sha256:10f2a618a6e75adf64329f828a6a5b40244c1c50f5ef4ce4109e904e69c71bd2"}, - {file = "coverage-5.2.tar.gz", hash = "sha256:1874bdc943654ba46d28f179c1846f5710eda3aeb265ff029e0ac2b52daae404"}, + {file = "coverage-5.2.1-cp27-cp27m-macosx_10_13_intel.whl", hash = "sha256:40f70f81be4d34f8d491e55936904db5c527b0711b2a46513641a5729783c2e4"}, + {file = "coverage-5.2.1-cp27-cp27m-macosx_10_9_x86_64.whl", hash = "sha256:675192fca634f0df69af3493a48224f211f8db4e84452b08d5fcebb9167adb01"}, + {file = "coverage-5.2.1-cp27-cp27m-manylinux1_i686.whl", hash = "sha256:2fcc8b58953d74d199a1a4d633df8146f0ac36c4e720b4a1997e9b6327af43a8"}, + {file = "coverage-5.2.1-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:64c4f340338c68c463f1b56e3f2f0423f7b17ba6c3febae80b81f0e093077f59"}, + {file = "coverage-5.2.1-cp27-cp27m-win32.whl", hash = "sha256:52f185ffd3291196dc1aae506b42e178a592b0b60a8610b108e6ad892cfc1bb3"}, + {file = "coverage-5.2.1-cp27-cp27m-win_amd64.whl", hash = "sha256:30bc103587e0d3df9e52cd9da1dd915265a22fad0b72afe54daf840c984b564f"}, + {file = "coverage-5.2.1-cp27-cp27mu-manylinux1_i686.whl", hash = "sha256:9ea749fd447ce7fb1ac71f7616371f04054d969d412d37611716721931e36efd"}, + {file = "coverage-5.2.1-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:ce7866f29d3025b5b34c2e944e66ebef0d92e4a4f2463f7266daa03a1332a651"}, + {file = "coverage-5.2.1-cp35-cp35m-macosx_10_13_x86_64.whl", hash = "sha256:4869ab1c1ed33953bb2433ce7b894a28d724b7aa76c19b11e2878034a4e4680b"}, + {file = "coverage-5.2.1-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:a3ee9c793ffefe2944d3a2bd928a0e436cd0ac2d9e3723152d6fd5398838ce7d"}, + {file = "coverage-5.2.1-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:28f42dc5172ebdc32622a2c3f7ead1b836cdbf253569ae5673f499e35db0bac3"}, + {file = "coverage-5.2.1-cp35-cp35m-win32.whl", hash = "sha256:e26c993bd4b220429d4ec8c1468eca445a4064a61c74ca08da7429af9bc53bb0"}, + {file = "coverage-5.2.1-cp35-cp35m-win_amd64.whl", hash = "sha256:4186fc95c9febeab5681bc3248553d5ec8c2999b8424d4fc3a39c9cba5796962"}, + {file = "coverage-5.2.1-cp36-cp36m-macosx_10_13_x86_64.whl", hash = "sha256:b360d8fd88d2bad01cb953d81fd2edd4be539df7bfec41e8753fe9f4456a5082"}, + {file = "coverage-5.2.1-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:1adb6be0dcef0cf9434619d3b892772fdb48e793300f9d762e480e043bd8e716"}, + {file = "coverage-5.2.1-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:098a703d913be6fbd146a8c50cc76513d726b022d170e5e98dc56d958fd592fb"}, + {file = "coverage-5.2.1-cp36-cp36m-win32.whl", hash = "sha256:962c44070c281d86398aeb8f64e1bf37816a4dfc6f4c0f114756b14fc575621d"}, + {file = "coverage-5.2.1-cp36-cp36m-win_amd64.whl", hash = "sha256:b1ed2bdb27b4c9fc87058a1cb751c4df8752002143ed393899edb82b131e0546"}, + {file = "coverage-5.2.1-cp37-cp37m-macosx_10_13_x86_64.whl", hash = "sha256:c890728a93fffd0407d7d37c1e6083ff3f9f211c83b4316fae3778417eab9811"}, + {file = "coverage-5.2.1-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:538f2fd5eb64366f37c97fdb3077d665fa946d2b6d95447622292f38407f9258"}, + {file = "coverage-5.2.1-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:27ca5a2bc04d68f0776f2cdcb8bbd508bbe430a7bf9c02315cd05fb1d86d0034"}, + {file = "coverage-5.2.1-cp37-cp37m-win32.whl", hash = "sha256:aab75d99f3f2874733946a7648ce87a50019eb90baef931698f96b76b6769a46"}, + {file = "coverage-5.2.1-cp37-cp37m-win_amd64.whl", hash = "sha256:c2ff24df02a125b7b346c4c9078c8936da06964cc2d276292c357d64378158f8"}, + {file = "coverage-5.2.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:304fbe451698373dc6653772c72c5d5e883a4aadaf20343592a7abb2e643dae0"}, + {file = "coverage-5.2.1-cp38-cp38-manylinux1_i686.whl", hash = "sha256:c96472b8ca5dc135fb0aa62f79b033f02aa434fb03a8b190600a5ae4102df1fd"}, + {file = "coverage-5.2.1-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:8505e614c983834239f865da2dd336dcf9d72776b951d5dfa5ac36b987726e1b"}, + {file = "coverage-5.2.1-cp38-cp38-win32.whl", hash = "sha256:700997b77cfab016533b3e7dbc03b71d33ee4df1d79f2463a318ca0263fc29dd"}, + {file = "coverage-5.2.1-cp38-cp38-win_amd64.whl", hash = "sha256:46794c815e56f1431c66d81943fa90721bb858375fb36e5903697d5eef88627d"}, + {file = "coverage-5.2.1-cp39-cp39-macosx_10_13_x86_64.whl", hash = "sha256:16042dc7f8e632e0dcd5206a5095ebd18cb1d005f4c89694f7f8aafd96dd43a3"}, + {file = "coverage-5.2.1-cp39-cp39-manylinux1_i686.whl", hash = "sha256:c1bbb628ed5192124889b51204de27c575b3ffc05a5a91307e7640eff1d48da4"}, + {file = "coverage-5.2.1-cp39-cp39-manylinux1_x86_64.whl", hash = "sha256:4f6428b55d2916a69f8d6453e48a505c07b2245653b0aa9f0dee38785939f5e4"}, + {file = "coverage-5.2.1-cp39-cp39-win32.whl", hash = "sha256:9e536783a5acee79a9b308be97d3952b662748c4037b6a24cbb339dc7ed8eb89"}, + {file = "coverage-5.2.1-cp39-cp39-win_amd64.whl", hash = "sha256:b8f58c7db64d8f27078cbf2a4391af6aa4e4767cc08b37555c4ae064b8558d9b"}, + {file = "coverage-5.2.1.tar.gz", hash = "sha256:a34cb28e0747ea15e82d13e14de606747e9e484fb28d63c999483f5d5188e89b"}, ] distlib = [ {file = "distlib-0.3.1-py2.py3-none-any.whl", hash = "sha256:8c09de2c67b3e7deef7184574fc060ab8a793e7adbb183d942c389c8b13c52fb"}, @@ -922,8 +948,8 @@ flake8 = [ {file = "flake8-3.8.3.tar.gz", hash = "sha256:f04b9fcbac03b0a3e58c0ab3a0ecc462e023a9faf046d57794184028123aa208"}, ] identify = [ - {file = "identify-1.4.24-py2.py3-none-any.whl", hash = "sha256:5519601b70c831011fb425ffd214101df7639ba3980f24dc283f7675b19127b3"}, - {file = "identify-1.4.24.tar.gz", hash = "sha256:06b4373546ae55eaaefdac54f006951dbd968fe2912846c00e565b09cfaed101"}, + {file = "identify-1.4.25-py2.py3-none-any.whl", hash = "sha256:ccd88716b890ecbe10920659450a635d2d25de499b9a638525a48b48261d989b"}, + {file = "identify-1.4.25.tar.gz", hash = "sha256:110ed090fec6bce1aabe3c72d9258a9de82207adeaa5a05cd75c635880312f9a"}, ] idna = [ {file = "idna-2.10-py2.py3-none-any.whl", hash = "sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0"}, @@ -1090,6 +1116,31 @@ requests = [ {file = "requests-2.24.0-py2.py3-none-any.whl", hash = "sha256:fe75cc94a9443b9246fc7049224f75604b113c36acb93f87b80ed42c44cbb898"}, {file = "requests-2.24.0.tar.gz", hash = "sha256:b3559a131db72c33ee969480840fff4bb6dd111de7dd27c8ee1f820f4f00231b"}, ] +"ruamel.yaml" = [ + {file = "ruamel.yaml-0.16.10-py2.py3-none-any.whl", hash = "sha256:0962fd7999e064c4865f96fb1e23079075f4a2a14849bcdc5cdba53a24f9759b"}, + {file = "ruamel.yaml-0.16.10.tar.gz", hash = "sha256:099c644a778bf72ffa00524f78dd0b6476bca94a1da344130f4bf3381ce5b954"}, +] +"ruamel.yaml.clib" = [ + {file = "ruamel.yaml.clib-0.2.0-cp27-cp27m-macosx_10_9_x86_64.whl", hash = "sha256:9c6d040d0396c28d3eaaa6cb20152cb3b2f15adf35a0304f4f40a3cf9f1d2448"}, + {file = "ruamel.yaml.clib-0.2.0-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:4d55386129291b96483edcb93b381470f7cd69f97585829b048a3d758d31210a"}, + {file = "ruamel.yaml.clib-0.2.0-cp27-cp27m-win32.whl", hash = "sha256:8073c8b92b06b572e4057b583c3d01674ceaf32167801fe545a087d7a1e8bf52"}, + {file = "ruamel.yaml.clib-0.2.0-cp27-cp27m-win_amd64.whl", hash = "sha256:615b0396a7fad02d1f9a0dcf9f01202bf9caefee6265198f252c865f4227fcc6"}, + {file = "ruamel.yaml.clib-0.2.0-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:a0ff786d2a7dbe55f9544b3f6ebbcc495d7e730df92a08434604f6f470b899c5"}, + {file = "ruamel.yaml.clib-0.2.0-cp35-cp35m-macosx_10_6_intel.whl", hash = "sha256:ea4362548ee0cbc266949d8a441238d9ad3600ca9910c3fe4e82ee3a50706973"}, + {file = "ruamel.yaml.clib-0.2.0-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:77556a7aa190be9a2bd83b7ee075d3df5f3c5016d395613671487e79b082d784"}, + {file = "ruamel.yaml.clib-0.2.0-cp35-cp35m-win32.whl", hash = "sha256:392b7c371312abf27fb549ec2d5e0092f7ef6e6c9f767bfb13e83cb903aca0fd"}, + {file = "ruamel.yaml.clib-0.2.0-cp35-cp35m-win_amd64.whl", hash = "sha256:ed5b3698a2bb241b7f5cbbe277eaa7fe48b07a58784fba4f75224fd066d253ad"}, + {file = "ruamel.yaml.clib-0.2.0-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:7aee724e1ff424757b5bd8f6c5bbdb033a570b2b4683b17ace4dbe61a99a657b"}, + {file = "ruamel.yaml.clib-0.2.0-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:d0d3ac228c9bbab08134b4004d748cf9f8743504875b3603b3afbb97e3472947"}, + {file = "ruamel.yaml.clib-0.2.0-cp36-cp36m-win32.whl", hash = "sha256:f9dcc1ae73f36e8059589b601e8e4776b9976effd76c21ad6a855a74318efd6e"}, + {file = "ruamel.yaml.clib-0.2.0-cp36-cp36m-win_amd64.whl", hash = "sha256:1e77424825caba5553bbade750cec2277ef130647d685c2b38f68bc03453bac6"}, + {file = "ruamel.yaml.clib-0.2.0-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:d10e9dd744cf85c219bf747c75194b624cc7a94f0c80ead624b06bfa9f61d3bc"}, + {file = "ruamel.yaml.clib-0.2.0-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:550168c02d8de52ee58c3d8a8193d5a8a9491a5e7b2462d27ac5bf63717574c9"}, + {file = "ruamel.yaml.clib-0.2.0-cp37-cp37m-win32.whl", hash = "sha256:57933a6986a3036257ad7bf283529e7c19c2810ff24c86f4a0cfeb49d2099919"}, + {file = "ruamel.yaml.clib-0.2.0-cp37-cp37m-win_amd64.whl", hash = "sha256:b1b7fcee6aedcdc7e62c3a73f238b3d080c7ba6650cd808bce8d7761ec484070"}, + {file = "ruamel.yaml.clib-0.2.0-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:be018933c2f4ee7de55e7bd7d0d801b3dfb09d21dad0cce8a97995fd3e44be30"}, + {file = "ruamel.yaml.clib-0.2.0.tar.gz", hash = "sha256:b66832ea8077d9b3f6e311c4a53d06273db5dc2db6e8a908550f3c14d67e718c"}, +] seed-isort-config = [ {file = "seed_isort_config-2.2.0-py2.py3-none-any.whl", hash = "sha256:8601fb715a5a4aac39256bbf73c2da6a81f964da9c9d9897ab9074db3663526f"}, {file = "seed_isort_config-2.2.0.tar.gz", hash = "sha256:be4cfef8f9a3fe8ea1817069c6b624538ac0b429636ec746edeb27e98ed628c8"}, @@ -1177,12 +1228,12 @@ typed-ast = [ {file = "typed_ast-1.4.1.tar.gz", hash = "sha256:8c8aaad94455178e3187ab22c8b01a3837f8ee50e09cf31f1ba129eb293ec30b"}, ] urllib3 = [ - {file = "urllib3-1.25.9-py2.py3-none-any.whl", hash = "sha256:88206b0eb87e6d677d424843ac5209e3fb9d0190d0ee169599165ec25e9d9115"}, - {file = "urllib3-1.25.9.tar.gz", hash = "sha256:3018294ebefce6572a474f0604c2021e33b3fd8006ecd11d62107a5d2a963527"}, + {file = "urllib3-1.25.10-py2.py3-none-any.whl", hash = "sha256:e7983572181f5e1522d9c98453462384ee92a0be7fac5f1413a1e35c56cc0461"}, + {file = "urllib3-1.25.10.tar.gz", hash = "sha256:91056c15fa70756691db97756772bb1eb9678fa585d9184f24534b100dc60f4a"}, ] virtualenv = [ - {file = "virtualenv-20.0.27-py2.py3-none-any.whl", hash = "sha256:c51f1ba727d1614ce8fd62457748b469fbedfdab2c7e5dd480c9ae3fbe1233f1"}, - {file = "virtualenv-20.0.27.tar.gz", hash = "sha256:26cdd725a57fef4c7c22060dba4647ebd8ca377e30d1c1cf547b30a0b79c43b4"}, + {file = "virtualenv-20.0.28-py2.py3-none-any.whl", hash = "sha256:8f582a030156282a9ee9d319984b759a232b07f86048c1d6a9e394afa44e78c8"}, + {file = "virtualenv-20.0.28.tar.gz", hash = "sha256:688a61d7976d82b92f7906c367e83bb4b3f0af96f8f75bfcd3da95608fe8ac6c"}, ] wcwidth = [ {file = "wcwidth-0.2.5-py2.py3-none-any.whl", hash = "sha256:beb4802a9cebb9144e99086eff703a642a13d6a0052920003a230f3294bbe784"}, diff --git a/pyproject.toml b/pyproject.toml index 0e883c7..a6d098b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,11 +34,11 @@ pytest = "poetry run pytest tests/" [tool.poetry.dependencies] python = "^3.6" tomlkit = { version = "^0.6.0", optional = true } -pyyaml = { version = "^5.3.1", optional = true } +"ruamel.yaml" = { version = "^0.16.10", optional = true } [tool.poetry.extras] toml = ["tomlkit"] -yaml = ["pyyaml"] +yaml = ["ruamel.yaml"] [tool.poetry.dev-dependencies] pre-commit = "^1.20" -- cgit v1.2.3 From 7bc0188e011bb6ebe193e17d39fcd81e360230cd Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 20:53:26 -0500 Subject: Run ci and security checks on PRs --- .github/workflows/python-package-ci.yml | 2 +- .github/workflows/python-security.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-package-ci.yml b/.github/workflows/python-package-ci.yml index 5f7d348..5cdceca 100644 --- a/.github/workflows/python-package-ci.yml +++ b/.github/workflows/python-package-ci.yml @@ -1,6 +1,6 @@ name: python package CI -on: [push] +on: [push, pull_request] jobs: build: diff --git a/.github/workflows/python-security.yml b/.github/workflows/python-security.yml index c5339e4..cbdf0ff 100644 --- a/.github/workflows/python-security.yml +++ b/.github/workflows/python-security.yml @@ -1,4 +1,4 @@ -on: [push] +on: [push, pull_request] jobs: security_checks: -- cgit v1.2.3 From d8ee572672c6d5429257664c5339b23e0c84c55a Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 20:54:42 -0500 Subject: Move IniParserCompatibility to new file --- config_file/parsers/ini_parser.py | 44 ++------------------------------ config_file/parsers/ini_parser_compat.py | 42 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 42 deletions(-) create mode 100644 config_file/parsers/ini_parser_compat.py 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 -- cgit v1.2.3 From df90fd036d004c79f340e4c1d01b2171add80047 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 21:27:18 -0500 Subject: Fix tests for recent changes --- tests/templates/json/glossary.json | 4 +--- tests/templates/toml/default.toml | 1 + tests/templates/yaml/default.yaml | 5 +++-- tests/test_abstract_parser.py | 0 tests/test_config_file.py | 41 +++++++++++++++++++++++++------------- tests/test_ini_parser.py | 6 +++--- tests/test_json_parser.py | 36 +++++++++++++++++++-------------- 7 files changed, 56 insertions(+), 37 deletions(-) delete mode 100644 tests/test_abstract_parser.py diff --git a/tests/templates/json/glossary.json b/tests/templates/json/glossary.json index 189d8a4..d27eb8d 100644 --- a/tests/templates/json/glossary.json +++ b/tests/templates/json/glossary.json @@ -26,9 +26,7 @@ "str_number": "16", "another_dict": { "str_bool": "false", - "list_of_values": [ - "5", "false", "trUe" - ] + "list_of_values": ["5", "false", "trUe"] } } } diff --git a/tests/templates/toml/default.toml b/tests/templates/toml/default.toml index 326de33..e0437cf 100644 --- a/tests/templates/toml/default.toml +++ b/tests/templates/toml/default.toml @@ -2,5 +2,6 @@ number_key = 0 [header_two] +# preserves comments str_key = "string" list_key = [1, 2, 3] diff --git a/tests/templates/yaml/default.yaml b/tests/templates/yaml/default.yaml index 095ada8..8c7c63e 100644 --- a/tests/templates/yaml/default.yaml +++ b/tests/templates/yaml/default.yaml @@ -2,5 +2,6 @@ header_one: number_key: 0 header_two: - str_key: "string" - list_key: [1, 2, 3] \ No newline at end of file + # preserves comments + list_key: [1, 2, 3] + str_key: string diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 13d7271..6718a94 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -4,6 +4,7 @@ from pathlib import Path import pytest from config_file.config_file import ConfigFile +from config_file.exceptions import MissingKeyError from config_file.parsers.abstract_parser import AbstractParser SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"] @@ -47,7 +48,7 @@ def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): @pytest.mark.skip(msg="needs fixing up") @pytest.mark.parametrize("path", ["", "invalid", "invalid.conf"]) -def test_invalid_config_paths(path): +def test_invalid_path_raises_value_error(path): with pytest.raises(ValueError): ConfigFile(path) @@ -83,21 +84,18 @@ def test_missing_config_file_during_restore(templated_config_file): @pytest.mark.parametrize( - "file_extension, file_contents, section_key, expected_result", + "key, expected_result", [ - ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar.sunday_index", True), - ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar.blah", False), - ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar", True), - ("json", json.dumps({"calendar": {"sunday_index": 0}}), "calendar", True), - ("json", json.dumps({"calendar": {"sunday_index": 0}}), "blah", False), - ("json", json.dumps({"calendar": {"sunday_index": 0}}), "sunday_index", True), + ("header_one", True), + ("header_one.number_key", True), + ("header_one.blah", False), ], ) def test_that_config_file_can_find_sections_and_keys( - tmp_file, file_extension, file_contents, section_key, expected_result + templated_config_file, key, expected_result ): - config = ConfigFile(tmp_file(f"file.{file_extension}", file_contents)) - assert config.has(section_key) == expected_result + config = templated_config_file() + assert config.has(key) == expected_result @pytest.mark.parametrize( @@ -171,12 +169,27 @@ def test_that_config_file_can_get( assert ret_val == value -def test_that_config_file_can_delete(templated_config_file): +@pytest.mark.parametrize( + "key", ["header_one.number_key", "header_one"] +) +def test_that_config_file_can_delete(templated_config_file, key): config = templated_config_file() - config.delete("header_one.number_key") + config.delete(key) + + assert config.has(key) is False + + +@pytest.mark.parametrize( + "key", ["", 0, False, {}, "header_one.does_not_exist"] +) +def test_that_config_file_raises_missing_key_error_on_invalid_input_or_missing_key( + templated_config_file, key +): + config = templated_config_file() - assert config.has("header_one.number_key") is False + with pytest.raises(MissingKeyError): + config.delete(key) # def test_that_custom_parser_can_be_used(template_and_config_file): diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index 6577291..45e82c3 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,6 +1,6 @@ import pytest -from config_file.exceptions import ParsingError +from config_file.exceptions import ParsingError, MissingKeyError from config_file.parsers.ini_parser import IniParser @@ -81,10 +81,10 @@ def test_that_ini_parser_can_delete(section_key, expected_result): @pytest.mark.parametrize( "section_key", ["section_is_not_present", "test_section.but_key_is_not_present"], ) -def test_that_ini_parser_delete_raises_value_errors(section_key): +def test_that_ini_parser_delete_raises_missing_key_errors(section_key): ini_file = """[test_section]\nkey1 = value\n\n""" parser = IniParser(ini_file) - with pytest.raises(ValueError): + with pytest.raises(MissingKeyError): parser.delete(section_key) diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index aa29ec6..514a640 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -1,6 +1,9 @@ +import json + import pytest from config_file.exceptions import ParsingError +from config_file.parsers.json_parser import JsonParser def test_invalid_json(templated_parser): @@ -10,6 +13,7 @@ def test_invalid_json(templated_parser): def test_that_json_parser_can_stringify(template_and_parser): template, parser = template_and_parser("json") + assert parser.stringify() == template.read_text() @@ -31,7 +35,20 @@ def test_that_json_parser_can_stringify(template_and_parser): "GlossSee", ], ) -def test_that_json_parser_can_find_keys(key, templated_parser): +def test_that_json_parser_can_find_wild_keys(key, templated_parser): + parser = templated_parser("json", "glossary") + assert parser.has(key, wild=True) + + +@pytest.mark.parametrize( + "key", + [ + "glossary", + "glossary.title", + "glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para" + ] +) +def test_that_json_parser_can_find_dot_keys(key, templated_parser): parser = templated_parser("json", "glossary") assert parser.has(key) @@ -50,7 +67,7 @@ def test_that_json_parser_can_find_keys(key, templated_parser): "5.5", ], ) -def test_that_json_parser_does_not_find_value_with_has(value, templated_parser): +def test_that_json_parser_does_not_find_values(value, templated_parser): parser = templated_parser("json", "glossary") assert not parser.has(value) @@ -75,28 +92,17 @@ def test_that_json_parser_can_get_keys_without_parsing_types( def test_that_json_parser_can_set_keys(templated_parser, key, value): parser = templated_parser("json") - # test_json = { - # "foo": { - # "baz": 5, - # "boo": "4.4", - # "bar": {"test_key": "foobar", "foobar": {"test": True}}, - # }, - # "bar": {"baz": 10}, - # "zip": "piz", - # } - # parser = JsonParser(json.dumps(test_json)) - parser.set(key, value) assert parser.get(key) == value @pytest.mark.parametrize( "key", - ["glossary", "dict_within_dict", "glossary.GlossDiv.GlossList.GlossEntry.Abbrev"], + ["glossary", "glossary.dict_within_dict", "glossary.GlossDiv.GlossList.GlossEntry.Abbrev"], ) def test_that_json_parser_can_delete_keys(template_and_parser, key): template, parser = template_and_parser("json", "glossary") parser.delete(key) - assert parser.stringify() == template.read_text(encoding="utf-8") + assert parser.has(key) is False -- cgit v1.2.3 From 4f26a57b16d9b87891c36a3b5d23643f2c46c90e Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 21:27:37 -0500 Subject: Remove -v from `task test` --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index a6d098b..7f05d3e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ packages = [ ] [tool.taskipy.tasks] -test = "task test_cov -- -v --cov-report term-missing" +test = "task test_cov -- --cov-report term-missing" test_cov = "task pytest -- --cov=config_file/" test_ci = "task test_cov -- --cov-report=xml" lint = "poetry run pre-commit run --all" -- cgit v1.2.3 From 4cff66b069bebe222cf5332ec785c7ec71b536c5 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 21:28:28 -0500 Subject: Add name for security checks action --- .github/workflows/python-security.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/python-security.yml b/.github/workflows/python-security.yml index cbdf0ff..5597151 100644 --- a/.github/workflows/python-security.yml +++ b/.github/workflows/python-security.yml @@ -1,3 +1,4 @@ +name: Python Security Checks on: [push, pull_request] jobs: -- cgit v1.2.3 From 1b4f0a6ed40006fd45c85b5245e5d0b949728497 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 21:29:15 -0500 Subject: Fix CI dependency installs --- .github/workflows/python-package-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-package-ci.yml b/.github/workflows/python-package-ci.yml index 5cdceca..0b82937 100644 --- a/.github/workflows/python-package-ci.yml +++ b/.github/workflows/python-package-ci.yml @@ -20,8 +20,8 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install poetry codecov requests pytest pytest-cov taskipy - poetry install --no-dev -vvv -E toml -E yaml + pip install poetry + poetry install -E toml -E yaml - name: Run tests run: poetry run task test_ci -- cgit v1.2.3 From 5d78f7e5e8f24476ec21cf2b01c88a62e3acc9ba Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 21:31:35 -0500 Subject: Revise docstrings, add `wild` to `has` method --- config_file/config_file.py | 96 ++++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 33 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()) -- cgit v1.2.3 From 5b9cbecb01b3ac11f553a62d3ee6135a789943a7 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 21:32:00 -0500 Subject: Remove duplication from `can_be_parsed_as_float` --- config_file/parsers/parse_value.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config_file/parsers/parse_value.py b/config_file/parsers/parse_value.py index 26bdf2e..5f6dfe3 100644 --- a/config_file/parsers/parse_value.py +++ b/config_file/parsers/parse_value.py @@ -75,7 +75,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)) -- cgit v1.2.3 From 47a58715546d780a5df8d362ab5eeedd3813ecdb Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 21:32:57 -0500 Subject: Use ruamel.yaml for yaml parser --- config_file/parsers/yaml_parser.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/config_file/parsers/yaml_parser.py b/config_file/parsers/yaml_parser.py index 77b59fe..1937e17 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 @@ -23,7 +18,11 @@ class YamlParser(BaseParser): "file was attempted to be used. Install the `toml` " "extra first with `pip install config-file[toml]`." ) + + 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 ) -- 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 ++++++++++++++++++++++++++------------ config_file/parsers/toml_parser.py | 1 - 2 files changed, 27 insertions(+), 13 deletions(-) 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/toml_parser.py b/config_file/parsers/toml_parser.py index d369003..58e20b9 100644 --- a/config_file/parsers/toml_parser.py +++ b/config_file/parsers/toml_parser.py @@ -9,7 +9,6 @@ from config_file.exceptions import MissingDependencyError from .base_parser import BaseParser - class TomlParser(BaseParser): def __init__(self, file_contents: str): if not TOML_AVAILABLE: -- cgit v1.2.3 From f90048609b7a7f9182b8a9bfe73eaef5f096f8ee Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 23:52:12 -0500 Subject: Revise `get` section for readme docs --- README.md | 139 +++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 111 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index c0fc8a3..e0e3fe0 100644 --- a/README.md +++ b/README.md @@ -37,49 +37,132 @@ You can also use [Poetry](https://python-poetry.org). $ poetry install config-file -E yaml -E toml ``` -## Usage +## Usage Overview -Say you have an INI file you want to manipulate. It must have an .ini -extension in order for the package to recognize it. +For this overview, let's say you have the following `ini` file +you want to manipulate. ```ini [section] -first_key = 5 -second_key = blah -third_key = true +num_key = 5 +str_key = blah +bool_key = true +list_key = [1, 2] + +[second_section] +dict_key = { "another_num": 5 } ``` -Then we can manipulate it as follows. +It must have a `.ini` extension in order +for the package to recognize it and use the correct parser for it. -```python -from pathlib import Path +### Setting up ConfigFile + +To use the package, we import in the `ConfigFile` object. +We can set it up by giving it a string or `pathlib.Path` as the argument. +Any home tildes `~` in the string or `Path` are recognzied and converted +to the full path for us. + +```python from config_file import ConfigFile -ORIGINAL_CONFIG_PATH = Path("~/some-project/some-other-config-file.ini") -CONFIG_PATH = Path("~/some-project/config.ini") +config = ConfigFile("~/some-project/config.ini") +``` + +### Using `get()` + +A recurring pattern you'll see here is that all methods that +need to specify something inside your configuration file will +do so using a dot syntax. + +#### Retrieving keys and sections + +So to retrieve our `num_key`, we'd specify the heading and the +key separated by a dot. All values will then be retrieved as +strings. + +```python +config.get('section.num_key') +>>> '5' +``` + +While we can retrieves keys, we can also retrieve the entire +section, which will be returned back to us as a dictionary. + +```python +config.get('section') +>>> {'num_key': '5', 'str_key': 'blah', 'bool_key': 'true', 'list_key': '[1, 2]'} +``` + +#### Coercing the return types -# Our path can be a string or a Path object. -# The "~" will be automatically expanded to the full path for us. -config = ConfigFile(CONFIG_PATH) +However, some of these keys are obviously not strings natively. +If we are retrieving a particular value of a key, we may want to +coerce it right away without doing clunky type conversions after +each time we retrieve a value. To do this, we can utilize the +`return_type` keyword argument. -# All the types will be retrieved as strings unless specified otherwise. -print(config.get("section.first_key")) ->>> "5" -print(config.get("section.first_key", return_type=int)) +```python +config.get('section.num_key', return_type=int) >>> 5 +``` + +Sometimes we don't have structures quite that simple though. What +if we wanted all the values in `section` coerced? For that, we can +utilize a `parse_types` keyword argument. + +```python +config.get('section', parse_types=True) +>>> {'num_key': 5, 'str_key': 'blah', 'bool_key': True, 'list_key': [1, 2]} +``` + +It also works for regular keys. + +```python +config.get('section.num_key', parse_types=True) +>>> 5 +``` + +#### Handling non-existant keys + +Sometimes we want to retrieve a key but are unsure of if it will exist. +There are two ways we could handle that. + +The first is the one we're used to seeing: catch the error. + +```python +from config_file import MissingKeyError + +try: + important_value = config.get('section.i_do_not_exist') +except MissingKeyError: + important_value = 42 +``` + +However, the `get` method comes with a `default` keyword argument that we +can utilze for this purpose. + +```python +config.get('section.i_do_not_exist', default=42) +>>> 42 +``` + +This can be handy if you already know what you want to be the fallback case +if you have a default for a particular configuration value. + +### Using `set()` + +We can use `set()` to set a existing key's value. + +```python +config.set('section.num_key', 6) +``` + +The method does not return anything, since there is nothing +useful to return. -# This holds true when we retrieve entire sections as well. However, we can -# also recursively parse the entire section if desired. -print(config.get("section")) ->>> {'first_key': '5', 'second_key': 'blah', 'third_key': 'true'} -print(config.get("section", parse_types=True)) ->>> {'first_key': 5, 'second_key': 'blah', 'third_key': True} -# Sometimes we want to retrieve a key but don't know whether or not -# it will be set. In that case, we can set a default. -print(config.get("section.unknown", default=False)) ->>> False # Setting, deleting, and checking if a key exists is just as easy. config.set("section.first_key", 10) -- cgit v1.2.3 From 3c1ebfaf0db4a29cc8ee9ba742c6d8925dc79cff Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 23:53:15 -0500 Subject: Allow string lists and dicts to be auto parsed --- config_file/parsers/parse_value.py | 61 ++++++++++++++++++++++++++++++++------ config_file/utils.py | 8 +++-- tests/test_parse_value.py | 5 +++- 3 files changed, 61 insertions(+), 13 deletions(-) diff --git a/config_file/parsers/parse_value.py b/config_file/parsers/parse_value.py index 5f6dfe3..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 @@ -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/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) diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py index 81db472..63ce6bb 100644 --- a/tests/test_parse_value.py +++ b/tests/test_parse_value.py @@ -19,13 +19,16 @@ from config_file.parsers.parse_value import can_be_parsed_as_bool, parse_value (True, True), ({"blah": "5"}, {"blah": 5}), ( - {"blah": {"blah2": {"blah3": "false"}, "abc": "5.25", "boo": "tRuE"}}, + {"blah": {"blah2": {"blah3": "fAlSe"}, "abc": "5.25", "boo": "tRuE"}}, {"blah": {"blah2": {"blah3": False}, "abc": 5.25, "boo": True}}, ), ( ["5", "4.4", "faLSe", "trUE", "foo", "-5", "-5.2"], [5, 4.4, False, True, "foo", -5, -5.2], ), + ("[1, 2, 3]", [1, 2, 3]), + ("[True, [1, 5], {}, 5.5]", [True, [1, 5], {}, 5.5]), + ("{ 'key': '5', 'key2': 'fAlSE' }", {"key": 5, "key2": False}), ], ) def test_parse_value(test_input, expected): -- cgit v1.2.3 From 5c1164535aa8eafca4725549efa63719836cd6bd Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 30 Jul 2020 00:04:39 -0500 Subject: Add package creation rationale --- README.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e0e3fe0..414d69f 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,12 @@ The Config File project is designed to allow you to easily manipulate your configuration files with the same simple API whether they are in INI, JSON, YAML, or TOML. +The code for this package grew origically out of developing command +line tools in Python. I had felt like I was writing similar code over and over +again to abstract away Python's configparser so it was a little nicer to work with +for my uses. Therefore, I had ended up making a package out of it and adding a lot more +niceties, polish, and flexibility to it so others may find it useful as well. + ## Installation Config File is available to download through PyPI. @@ -98,8 +104,8 @@ config.get('section') #### Coercing the return types However, some of these keys are obviously not strings natively. -If we are retrieving a particular value of a key, we may want to -coerce it right away without doing clunky type conversions after +If we are retrieving a particular value of a key, we may want to +coerce it right away without doing clunky type conversions after each time we retrieve a value. To do this, we can utilize the `return_type` keyword argument. @@ -124,7 +130,7 @@ config.get('section.num_key', parse_types=True) >>> 5 ``` -#### Handling non-existant keys +#### Handling non-existent keys Sometimes we want to retrieve a key but are unsure of if it will exist. There are two ways we could handle that. @@ -140,7 +146,7 @@ except MissingKeyError: important_value = 42 ``` -However, the `get` method comes with a `default` keyword argument that we +However, the `get` method comes with a `default` keyword argument that we can utilze for this purpose. ```python @@ -148,8 +154,7 @@ config.get('section.i_do_not_exist', default=42) >>> 42 ``` -This can be handy if you already know what you want to be the fallback case -if you have a default for a particular configuration value. +This can be handy if you have a default for a particular configuration value. ### Using `set()` -- cgit v1.2.3 From 82bfa4299f51a47f7ebc629f93fffc1b4eaf9d84 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 30 Jul 2020 00:08:21 -0500 Subject: Add rest of sections (without content) to readme --- README.md | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 414d69f..45c0dd7 100644 --- a/README.md +++ b/README.md @@ -167,22 +167,19 @@ config.set('section.num_key', 6) The method does not return anything, since there is nothing useful to return. +### Using `delete()` -# Setting, deleting, and checking if a key exists is just as easy. -config.set("section.first_key", 10) -config.delete("section.third_key") +### Using `has()` -print(config.has("section.third_key")) ->>> False -# We can also convert the entire configuration file to a string. -print(config.stringify()) ->>> '[section]\nfirst_key = 10\nsecond_key = blah\n\n' +### Using `stringify()` -# Lastly, we need to make sure we save our changes. -# Nothing is written out until we do so. -config.save() + +### Using `save()` + + +### Using `restore_original()` # If we have, say, a default config file and a user config file, we can easily # restore default one. We can specify the file path to it. @@ -192,7 +189,7 @@ config.restore_original(original_file_path=ORIGINAL_CONFIG_PATH) # current directory (because our configuration file we passed in was # named config.ini). config.restore_original() -``` + ## Documentation -- cgit v1.2.3 From b84d8c1562ec362f30c6868ce5d52c515456bc7c Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 30 Jul 2020 00:09:08 -0500 Subject: Update code to be inside code block --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 45c0dd7..04d4090 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,7 @@ useful to return. ### Using `restore_original()` +```python # If we have, say, a default config file and a user config file, we can easily # restore default one. We can specify the file path to it. config.restore_original(original_file_path=ORIGINAL_CONFIG_PATH) @@ -189,6 +190,7 @@ config.restore_original(original_file_path=ORIGINAL_CONFIG_PATH) # current directory (because our configuration file we passed in was # named config.ini). config.restore_original() +``` ## Documentation -- cgit v1.2.3 From 633facbbd9db0be86be88ffc95996038a68db4fa Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 30 Jul 2020 00:20:20 -0500 Subject: Add `has`, `delete`, and `set` usage --- README.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 04d4090..f7067e4 100644 --- a/README.md +++ b/README.md @@ -165,13 +165,63 @@ config.set('section.num_key', 6) ``` The method does not return anything, since there is nothing -useful to return. +useful to return. If someone goes wrong where it is unable to set +the value, an exception will be raised instead. + +With `set()`, we can also create and set keys that don't exist yet. + +```python +config.set('new_section.new_key', 'New key value!') +``` + +Would then result in the following section being added to our original file: + +```ini +[new_section] +new_key = New key value! +``` + +The exact behavior of how these new keys or sections are added are a bit +dependent on the file format we're using. See the [full documentation](#Documentation) for +more information there. ### Using `delete()` +`delete()` allows us to delete entire sections or specific keys. + +```python +config.delete('section') +``` + +Would result in the entire section being removed from our configuration file. +However, we can also just delete a single key. + +```python +config.delete('section.num_key') +``` ### Using `has()` +`has()` allows us to check whether a given key exists in our file. There +are two ways to use `has()`. + +The first is using the dot syntax. + +```python +config.has('section.str_key') +>>> True +config.has('does_not_exist') +>>> False +``` + +This will check if our specific key or section exists. However, we can +also just check in general if a given key exists anywhere in our file +with the `wild` keyword argument. + +```python +config.has('str_key', wild=True) +>>> True +``` ### Using `stringify()` -- cgit v1.2.3 From 5dd82bef4cb56bc788f94688e6daaf825c89d313 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 30 Jul 2020 09:38:07 -0500 Subject: Finish `save()`, `stringify()`, and `restore_original()` docs in readme --- README.md | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f7067e4..312adfd 100644 --- a/README.md +++ b/README.md @@ -166,7 +166,8 @@ config.set('section.num_key', 6) The method does not return anything, since there is nothing useful to return. If someone goes wrong where it is unable to set -the value, an exception will be raised instead. +the value, an exception will be raised instead. This is the case +for most methods on `ConfigFile`. With `set()`, we can also create and set keys that don't exist yet. @@ -182,8 +183,9 @@ new_key = New key value! ``` The exact behavior of how these new keys or sections are added are a bit -dependent on the file format we're using. See the [full documentation](#Documentation) for -more information there. +dependent on the file format we're using, since every format is a little +different in it's structure and in what it supports. See the +[full documentation](#Documentation) for more information there. ### Using `delete()` @@ -223,25 +225,47 @@ config.has('str_key', wild=True) >>> True ``` +### Using `save()` + +For any changes we make to our configuration file, they are not written out +to the file on our filesystem until we call `save()`. This is to avoid +unnecessary write calls to our filesystem until we need to. + +```python +config.delete('section.list_key') +config.save() +``` + ### Using `stringify()` +`stringify()` allows us to show us our internal configuration file, with +any changes we've made, as a string. -### Using `save()` +```python +config.stringify() +>>> '[section]\nnum_key = 5\nstr_key = blah\nbool_key = true\nlist_key = [1, 2]\n\n[second_section]\ndict_key = { "another_num": 5 }\n\n' +``` ### Using `restore_original()` -```python -# If we have, say, a default config file and a user config file, we can easily -# restore default one. We can specify the file path to it. -config.restore_original(original_file_path=ORIGINAL_CONFIG_PATH) +If we have a initial configuration file state, we could keep a copy of that +initial file and restore back to it whenever needed using `restore_original()`. -# Otherwise, a config.original.ini file will automatically be looked for in the -# current directory (because our configuration file we passed in was -# named config.ini). +By default, if we created our `ConfigFile` with the path of "~/some-project/config.ini", +`restore_original()` will look for our original file at "~/some-project/config.original.ini". + +```python config.restore_original() ``` +However, if we have a specific path elsewhere that it is or it is named differently, we can +utilize the `original_file_path` keyword argument. + +```python +config.restore_original(original_file_path="~/some-project/original-configs/config.ini") +``` + ## Documentation -- cgit v1.2.3 From fbbb1636a7998cad6ec4e060e833c7e8e2efd2be Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 30 Jul 2020 09:40:23 -0500 Subject: Fix strike throughed paths in `restore_original()` --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 312adfd..511e2a0 100644 --- a/README.md +++ b/README.md @@ -252,8 +252,8 @@ config.stringify() If we have a initial configuration file state, we could keep a copy of that initial file and restore back to it whenever needed using `restore_original()`. -By default, if we created our `ConfigFile` with the path of "~/some-project/config.ini", -`restore_original()` will look for our original file at "~/some-project/config.original.ini". +By default, if we created our `ConfigFile` with the path of `~/some-project/config.ini`, +`restore_original()` will look for our original file at `~/some-project/config.original.ini`. ```python config.restore_original() -- cgit v1.2.3 From 8c10767e474dde5617b63abdddf5f3c903db6216 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 30 Jul 2020 09:46:38 -0500 Subject: Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 511e2a0..6d2e276 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ The Config File project is designed to allow you to easily manipulate your configuration files with the same simple API whether they are in INI, JSON, YAML, or TOML. -The code for this package grew origically out of developing command +The code for this package grew organically out of developing command line tools in Python. I had felt like I was writing similar code over and over again to abstract away Python's configparser so it was a little nicer to work with for my uses. Therefore, I had ended up making a package out of it and adding a lot more -- cgit v1.2.3 From 5f00e1b77214b542fddbac61619d4d8afeeb81d8 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 30 Jul 2020 12:53:14 -0500 Subject: Fix typos and reword some paragraphs --- README.md | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 6d2e276..3900971 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ for the package to recognize it and use the correct parser for it. To use the package, we import in the `ConfigFile` object. We can set it up by giving it a string or `pathlib.Path` as the argument. -Any home tildes `~` in the string or `Path` are recognzied and converted +Any home tildes `~` in the string or `Path` are recognized and converted to the full path for us. ```python @@ -165,9 +165,10 @@ config.set('section.num_key', 6) ``` The method does not return anything, since there is nothing -useful to return. If someone goes wrong where it is unable to set +useful to return. If something goes wrong where it is unable to set the value, an exception will be raised instead. This is the case -for most methods on `ConfigFile`. +for most methods on `ConfigFile`, such as `delete()` or `save()`, +where there would be no useful return value to utilize. With `set()`, we can also create and set keys that don't exist yet. @@ -217,8 +218,8 @@ config.has('does_not_exist') ``` This will check if our specific key or section exists. However, we can -also just check in general if a given key exists anywhere in our file -with the `wild` keyword argument. +also check in general if a given key or sections exists anywhere in our +file with the `wild` keyword argument. ```python config.has('str_key', wild=True) @@ -228,8 +229,8 @@ config.has('str_key', wild=True) ### Using `save()` For any changes we make to our configuration file, they are not written out -to the file on our filesystem until we call `save()`. This is to avoid -unnecessary write calls to our filesystem until we need to. +to the filesystem until we call `save()`. This is to avoid unnecessary write +calls after each operation until we actually need to save. ```python config.delete('section.list_key') @@ -238,8 +239,10 @@ config.save() ### Using `stringify()` -`stringify()` allows us to show us our internal configuration file, with -any changes we've made, as a string. +`stringify()` shows us our configuration file, with any changes we've made, +as a string. `stringify()` will always show us our latest changes since it +is stringify-ing our internal representation of the configuration file, not +just the file we've read in. ```python config.stringify() @@ -252,15 +255,16 @@ config.stringify() If we have a initial configuration file state, we could keep a copy of that initial file and restore back to it whenever needed using `restore_original()`. -By default, if we created our `ConfigFile` with the path of `~/some-project/config.ini`, +By default, if we created our `ConfigFile` object with the path of `~/some-project/config.ini`, `restore_original()` will look for our original file at `~/some-project/config.original.ini`. ```python config.restore_original() ``` -However, if we have a specific path elsewhere that it is or it is named differently, we can -utilize the `original_file_path` keyword argument. +However, if we have a specific path elsewhere that this original configuration file is or it +is named differently than what the default expects, we can utilize the `original_file_path` +keyword argument. ```python config.restore_original(original_file_path="~/some-project/original-configs/config.ini") -- cgit v1.2.3 From e40a3a23fb6649758f40e9b67e3f3100ec23f116 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 30 Jul 2020 12:57:56 -0500 Subject: Fix missing dependency error messages for yaml and toml --- config_file/parsers/toml_parser.py | 3 ++- config_file/parsers/yaml_parser.py | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py index 58e20b9..1c7f8a7 100644 --- a/config_file/parsers/toml_parser.py +++ b/config_file/parsers/toml_parser.py @@ -9,11 +9,12 @@ from config_file.exceptions import MissingDependencyError from .base_parser import BaseParser + 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 1937e17..babe873 100644 --- a/config_file/parsers/yaml_parser.py +++ b/config_file/parsers/yaml_parser.py @@ -14,12 +14,12 @@ 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 = YAML(typ="safe") yaml.loads = round_trip_load yaml.dumps = round_trip_dump -- cgit v1.2.3 From 3700e247d5b72f21c20c554c7f2a72d6c63b8eea Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 30 Jul 2020 20:34:27 -0500 Subject: Fix wording of docstring in missing dependency error --- config_file/exceptions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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. """ -- cgit v1.2.3 From 5ac52d231e56f60a22ba3f755415c4aa43a14d83 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 1 Aug 2020 11:20:39 -0500 Subject: Add tests to parse_value and fix bugs in it --- config_file/parsers/parse_value.py | 78 ++++++++++----- tests/test_parse_value.py | 191 ++++++++++++++++++++++++++++++++++--- 2 files changed, 230 insertions(+), 39 deletions(-) diff --git a/config_file/parsers/parse_value.py b/config_file/parsers/parse_value.py index aa8d1fe..28f1ccb 100644 --- a/config_file/parsers/parse_value.py +++ b/config_file/parsers/parse_value.py @@ -22,10 +22,22 @@ parse_value("-5") -> -5 import ast import re from distutils.util import strtobool +from typing import Any -def parse_value(value): - if type(value) is dict: +def parse_value(value: Any) -> Any: + """Parse a value into native type. + + 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: @@ -33,7 +45,7 @@ def parse_value(value): return parsed - if type(value) is list: + if isinstance(value, list): parsed = [] for item in value: @@ -42,92 +54,108 @@ def parse_value(value): return parsed if can_be_parsed_as_int(value): - return int(value) + return int(value.strip()) if isinstance(value, str) else int(value) if can_be_parsed_as_float(value): - return float(value) + return float(value.strip()) if isinstance(value, str) else float(value) if can_be_parsed_as_bool(value): - return value if type(value) is bool else bool(strtobool(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)) + return parse_value(ast.literal_eval(value.strip())) if can_be_parsed_as_list(value): - return parse_value(ast.literal_eval(value)) + return parse_value(ast.literal_eval(value.strip())) return value -def can_be_parsed_as_int(value) -> bool: - if type(value) is int: +def can_be_parsed_as_int(value: Any) -> bool: + """Check whether a value can be parsed as a integer. + + 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 type(value) is bool: return True - if type(value) is not str: + 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) -> bool: +def can_be_parsed_as_float(value: Any) -> bool: FLOAT_REGEX = r"^\d*\.\d+$" - if type(value) is float: + if isinstance(value, float): return True - if type(value) is not str: + 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) -> bool: - if type(value) is bool: +def can_be_parsed_as_bool(value: Any) -> bool: + if isinstance(value, bool): return True - if type(value) is not str: + if not isinstance(value, str): return False - return value.lower() == "true" or value.lower() == "false" + value = value.lower().strip() + + return value == "true" or value == "false" -def can_be_parsed_as_dict(value) -> bool: - if type(value) is dict: +def can_be_parsed_as_dict(value: Any) -> bool: + if isinstance(value, dict): return True - if type(value) is not str: + if not isinstance(value, str): return False value = value.strip() + if value[0] == "{" and value[-1] == "}": try: ast.literal_eval(value) return True - except SyntaxError: + except Exception: return False return False def can_be_parsed_as_list(value) -> bool: - if type(value) is dict: + if isinstance(value, list): return True - if type(value) is not str: + if not isinstance(value, str): return False value = value.strip() + if value[0] == "[" and value[-1] == "]": try: ast.literal_eval(value) return True - except SyntaxError: + except Exception: return False return False diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py index 63ce6bb..735abc3 100644 --- a/tests/test_parse_value.py +++ b/tests/test_parse_value.py @@ -1,6 +1,15 @@ +from datetime import datetime + import pytest -from config_file.parsers.parse_value import can_be_parsed_as_bool, parse_value +from config_file.parsers.parse_value import ( + can_be_parsed_as_bool, + can_be_parsed_as_dict, + can_be_parsed_as_float, + can_be_parsed_as_int, + can_be_parsed_as_list, + parse_value, +) @pytest.mark.parametrize( @@ -8,37 +17,191 @@ from config_file.parsers.parse_value import can_be_parsed_as_bool, parse_value [ ("5", 5), ("-5", -5), + ("0", 0), ("-234.5", -234.5), ("35.5", 35.5), ("TRUE", True), ("true", True), ("false", False), - ("blah", "blah"), - (343, 343), - (3.2, 3.2), - (True, True), + ], +) +def test_parse_value_parses_ints_floats_and_bools(test_input, expected): + """ + config_file.parsers.parse_value + + Parses string values that contain ints, floats, and booleans into + their native representation. + """ + assert parse_value(test_input) == expected + + +@pytest.mark.parametrize( + "test_input,expected", + [ ({"blah": "5"}, {"blah": 5}), ( {"blah": {"blah2": {"blah3": "fAlSe"}, "abc": "5.25", "boo": "tRuE"}}, {"blah": {"blah2": {"blah3": False}, "abc": 5.25, "boo": True}}, ), + ("{ 'key': '5', 'key2': 'fAlSE' }", {"key": 5, "key2": False}), ( ["5", "4.4", "faLSe", "trUE", "foo", "-5", "-5.2"], [5, 4.4, False, True, "foo", -5, -5.2], ), - ("[1, 2, 3]", [1, 2, 3]), - ("[True, [1, 5], {}, 5.5]", [True, [1, 5], {}, 5.5]), - ("{ 'key': '5', 'key2': 'fAlSE' }", {"key": 5, "key2": False}), + (" [1, 2, 3] ", [1, 2, 3]), + (" [True, [1, 5], {}, 5.5]", [True, [1, 5], {}, 5.5]), ], ) -def test_parse_value(test_input, expected): +def test_parse_value_parses_dicts_and_lists(test_input, expected): + """ + config_file.parsers.parse_value + + Parses string values that contain dictionaries or lists into + their native representation. + """ assert parse_value(test_input) == expected -def test_can_be_parsed_as_bool(): +@pytest.mark.parametrize("test_input", ["blah", datetime(2020, 1, 1)]) +def test_parse_value_returns_values_that_cannot_be_parsed(test_input): + """ + config_file.parsers.parse_value + + If a value is given that cannot be parsed by parse_value, + it should simply return that value back. + """ + assert parse_value(test_input) == test_input + + +@pytest.mark.parametrize( + "test_input", + [(343, 343), (3.2, 3.2), (True, True), (False, False), ({}, {}), ([], [])], +) +def test_parse_value_returns_values_that_are_already_in_their_native_type(test_input): + """ + config_file.parsers.parse_value + + Returns back the inputted value if it is already in its native type. + """ + assert parse_value(test_input) == test_input + + +@pytest.mark.parametrize("value", [-1, 0, 1, "1", "-1", " 0 "]) +def test_values_that_can_be_parsed_as_int(value): + """ + config_file.parsers.can_be_parsed_as_int + + Returns True for values that can be parsed as an int. + """ + assert can_be_parsed_as_int(value) + + +@pytest.mark.parametrize( + "value", [True, False, 5.5, "5.5", [], {}, "ruff", datetime.now()] +) +def test_values_that_cannot_be_parsed_as_int(value): + """ + config_file.parsers.can_be_parsed_as_int + + Returns False for values that cannot be parsed as an int. + """ + assert not can_be_parsed_as_int(value) + + +@pytest.mark.parametrize("value", [5.5, -1.2, ".1", "5.5", " 1.1 ", "-0.0", "0.0"]) +def test_values_that_can_be_parsed_as_float(value): """ - This is the section not being covered in the parse_value.py - file from test_parse_value, but I don't think it's reachable - from using it. + config_file.parsers.can_be_parsed_as_float + + Returns True for values that can be parsed as an float. + """ + assert can_be_parsed_as_float(value) + + +@pytest.mark.parametrize( + "value", + [ + 545, + 0, + -3, + "1", + "-1", + True, + False, + " 0 ", + "0..0", + [], + {}, + "ruff", + datetime.now(), + ], +) +def test_values_that_cannot_be_parsed_as_float(value): + """ + config_file.parsers.can_be_parsed_as_float + + Returns False for values that cannot be parsed as an float. + """ + assert not can_be_parsed_as_float(value) + + +@pytest.mark.parametrize( + "value", [True, False, "false", "true", "TRUE", "FALSE", " FALSE ", " TRUE "] +) +def test_values_that_can_be_parsed_as_bool(value): + """ + config_file.parsers.can_be_parsed_as_bool + + Returns True for values that can be parsed as a bool. + """ + assert can_be_parsed_as_bool(value) + + +@pytest.mark.parametrize("value", [0, 1, -1, 5.3, "ruff", {}, [], datetime.now()]) +def test_values_that_cannot_be_parsed_as_bool(value): + """ + config_file.parsers.can_be_parsed_as_bool + + Returns False for values that cannot be parsed as a bool. + """ + assert not can_be_parsed_as_bool(value) + + +@pytest.mark.parametrize("value", [{}, {"hello": 5}, "{}", " { } ", "{'test': 5}"]) +def test_values_that_can_be_parsed_as_dict(value): + """ + config_file.parsers.can_be_parsed_as_dict + + Returns True for values that can be parsed as a dict. + """ + assert can_be_parsed_as_dict(value) + + +@pytest.mark.parametrize("value", [0, 5, -1, 5.5, [], "ruff", " { invalid } "]) +def test_values_that_cannot_be_parsed_as_dict(value): + """ + config_file.parsers.can_be_parsed_as_dict + + Returns False for values that cannot be parsed as a dict. + """ + assert not can_be_parsed_as_dict(value) + + +@pytest.mark.parametrize("value", [[], [1, 2, True, 5.5], "[]", " [] ", " [1, 2, 3]"]) +def test_values_that_can_be_parsed_as_list(value): + """ + config_file.parsers.can_be_parsed_as_list + + Returns True for values that can be parsed as a list. + """ + assert can_be_parsed_as_list(value) + + +@pytest.mark.parametrize("value", [0, 5, -1, 5.5, {}, "ruff", " [ invalid ] "]) +def test_values_that_cannot_be_parsed_as_list(value): + """ + config_file.parsers.can_be_parsed_as_list + + Returns False for values that cannot be parsed as a list. """ - assert can_be_parsed_as_bool(5) is False + assert not can_be_parsed_as_list(value) -- cgit v1.2.3 From 64538efa75db78983db4b764a2731a4154fdcad6 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 1 Aug 2020 11:50:52 -0500 Subject: Remove origin --- README.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/README.md b/README.md index 3900971..2b51ac0 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,6 @@ The Config File project is designed to allow you to easily manipulate your configuration files with the same simple API whether they are in INI, JSON, YAML, or TOML. -The code for this package grew organically out of developing command -line tools in Python. I had felt like I was writing similar code over and over -again to abstract away Python's configparser so it was a little nicer to work with -for my uses. Therefore, I had ended up making a package out of it and adding a lot more -niceties, polish, and flexibility to it so others may find it useful as well. - ## Installation Config File is available to download through PyPI. -- cgit v1.2.3 From 3f7c98084b9717ea34ce4963e149b420ceaa5d6c Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 1 Aug 2020 11:51:09 -0500 Subject: Add '-E yaml -E toml' to install instructions --- CONTRIBUTING.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 902c669..b4e1b26 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -55,5 +55,4 @@ Here are some guidelines to keep in mind when submitting a pull request: - All it does is ensure that all the tests pass. -To get started, make sure you have poetry installed and run `poetry install` and -`poetry shell` to enter the virtual environment. +To get started, make sure you have poetry installed and run `poetry install -E yaml -E toml` and `poetry shell` to enter the virtual environment. -- 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/config_file.py | 32 ++++++++++------------ config_file/parsers/base_parser.py | 9 ++---- config_file/parsers/ini_parser.py | 2 +- tests/test_config_file.py | 41 ++-------------------------- tests/test_custom_parser_with_config_file.py | 31 +++++++++++++++++++++ tests/test_ini_parser.py | 2 +- tests/test_json_parser.py | 13 +++++---- 7 files changed, 60 insertions(+), 70 deletions(-) create mode 100644 tests/test_custom_parser_with_config_file.py diff --git a/config_file/config_file.py b/config_file/config_file.py index 9b14072..adcfaf1 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -15,9 +15,7 @@ from config_file.utils import Default, create_config_path, read_file, split_on_d class ConfigFile: def __init__( - self, - file_path: Union[str, Path], - parser: Optional[Type[AbstractParser]] = None + self, file_path: Union[str, Path], parser: Optional[Type[AbstractParser]] = None ) -> None: """ Stores the config file path and expands it if needed, reads in @@ -93,10 +91,10 @@ class ConfigFile: 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. + retrieving, not just a single type. + + e.g. If you are retrieving a section, all values in that + section will be parsed. return_type: The type to coerce the return value to. default: The default value to return if the value of the key is empty. @@ -123,14 +121,14 @@ 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 + 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` + 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: @@ -144,7 +142,7 @@ class ConfigFile: Args: key: The section, sub-section, or key to delete. - + Raises: MissingKeyError: If a key is attempted to be deleted that does not exist. @@ -170,9 +168,9 @@ class ConfigFile: Args: key: The section, sub-section, or key to find. - wild: Whether or not to search everywhere. - - Without `wild`, a single word `key` without a `.` + 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 @@ -226,7 +224,7 @@ class ConfigFile: Save your configuration changes. This writes the file back out, including any changes - you've made, to the specified path given from this + you've made, to the specified path given from this object's constructor. """ with open(str(self.__path), "w") as config_file: 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 - diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 8dc7f47..72974f1 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -10,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/tests/test_config_file.py b/tests/test_config_file.py index 6718a94..d655397 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -169,9 +169,7 @@ def test_that_config_file_can_get( assert ret_val == value -@pytest.mark.parametrize( - "key", ["header_one.number_key", "header_one"] -) +@pytest.mark.parametrize("key", ["header_one.number_key", "header_one"]) def test_that_config_file_can_delete(templated_config_file, key): config = templated_config_file() @@ -180,9 +178,7 @@ def test_that_config_file_can_delete(templated_config_file, key): assert config.has(key) is False -@pytest.mark.parametrize( - "key", ["", 0, False, {}, "header_one.does_not_exist"] -) +@pytest.mark.parametrize("key", ["", 0, False, {}, "header_one.does_not_exist"]) def test_that_config_file_raises_missing_key_error_on_invalid_input_or_missing_key( templated_config_file, key ): @@ -190,36 +186,3 @@ def test_that_config_file_raises_missing_key_error_on_invalid_input_or_missing_k with pytest.raises(MissingKeyError): config.delete(key) - - -# def test_that_custom_parser_can_be_used(template_and_config_file): -# class CustomParser(AbstractParser): -# def __init__(self, file_contents): -# super().__init__(file_contents) -# -# def parse(self, file_contents: str): -# return file_contents -# -# def get(self, key, parse_types=True): -# return key -# -# def set(self, key, value): -# return key, value -# -# def delete(self, section_key): -# return section_key -# -# def has(self, section_key: str) -> bool: -# return True -# -# def stringify(self) -> str: -# return str(self.parsed_content) -# -# template_file, config = template_and_config_file(parser=CustomParser) -# -# assert isinstance(config._ConfigFile__parser, CustomParser) -# assert config.get("key") == "key" -# assert config.set("key", "value") == ("key", "value") -# assert config.delete("section_key") == "section_key" -# assert config.has("blah") -# assert config.stringify() == template_file.read_text() diff --git a/tests/test_custom_parser_with_config_file.py b/tests/test_custom_parser_with_config_file.py new file mode 100644 index 0000000..3c8bf98 --- /dev/null +++ b/tests/test_custom_parser_with_config_file.py @@ -0,0 +1,31 @@ +# def test_that_custom_parser_can_be_used(template_and_config_file): +# class CustomParser(AbstractParser): +# def __init__(self, file_contents): +# super().__init__(file_contents) +# +# def parse(self, file_contents: str): +# return file_contents +# +# def get(self, key, parse_types=True): +# return key +# +# def set(self, key, value): +# return key, value +# +# def delete(self, section_key): +# return section_key +# +# def has(self, section_key: str) -> bool: +# return True +# +# def stringify(self) -> str: +# return str(self.parsed_content) +# +# template_file, config = template_and_config_file(parser=CustomParser) +# +# assert isinstance(config._ConfigFile__parser, CustomParser) +# assert config.get("key") == "key" +# assert config.set("key", "value") == ("key", "value") +# assert config.delete("section_key") == "section_key" +# assert config.has("blah") +# assert config.stringify() == template_file.read_text() diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index 45e82c3..46966c9 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,6 +1,6 @@ import pytest -from config_file.exceptions import ParsingError, MissingKeyError +from config_file.exceptions import MissingKeyError, ParsingError from config_file.parsers.ini_parser import IniParser diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index 514a640..f8200b0 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -1,9 +1,6 @@ -import json - import pytest from config_file.exceptions import ParsingError -from config_file.parsers.json_parser import JsonParser def test_invalid_json(templated_parser): @@ -45,8 +42,8 @@ def test_that_json_parser_can_find_wild_keys(key, templated_parser): [ "glossary", "glossary.title", - "glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para" - ] + "glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para", + ], ) def test_that_json_parser_can_find_dot_keys(key, templated_parser): parser = templated_parser("json", "glossary") @@ -98,7 +95,11 @@ def test_that_json_parser_can_set_keys(templated_parser, key, value): @pytest.mark.parametrize( "key", - ["glossary", "glossary.dict_within_dict", "glossary.GlossDiv.GlossList.GlossEntry.Abbrev"], + [ + "glossary", + "glossary.dict_within_dict", + "glossary.GlossDiv.GlossList.GlossEntry.Abbrev", + ], ) def test_that_json_parser_can_delete_keys(template_and_parser, key): template, parser = template_and_parser("json", "glossary") -- cgit v1.2.3 From c7922b08bf010375416138b2ca85fe88fd8edcef Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 1 Aug 2020 11:54:22 -0500 Subject: Expand on docstrings for parse_value file --- config_file/parsers/parse_value.py | 95 ++++++++++++++++++++++++++++++-------- tests/test_parse_value.py | 1 + 2 files changed, 77 insertions(+), 19 deletions(-) diff --git a/config_file/parsers/parse_value.py b/config_file/parsers/parse_value.py index 28f1ccb..12032d6 100644 --- a/config_file/parsers/parse_value.py +++ b/config_file/parsers/parse_value.py @@ -1,7 +1,13 @@ -""" -This module allows you to parse a string into its native type. +"""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. -Example: parse_value('5') -> 5 (int) parse_value('-5') -> -5 (int) @@ -13,20 +19,7 @@ Example: parse_value('true') -> True (bool) parse_value('FALSE') -> False (bool) - -"5".isdigit() -> True -"-5".isdigit() -> False -can_be_parsed_as_int("-5") -> True -parse_value("-5") -> -5 -""" -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(" [ [ [ [] ]] ] ") -> [[[[]]]] (list) Args: value: The value to parse. @@ -74,13 +67,18 @@ def parse_value(value: Any) -> Any: 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 type(value) is bool: + if isinstance(value, int) and not isinstance(value, bool): return True if not isinstance(value, str): @@ -95,6 +93,20 @@ def can_be_parsed_as_int(value: Any) -> bool: 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): @@ -112,6 +124,20 @@ def can_be_parsed_as_float(value: Any) -> bool: 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 @@ -124,6 +150,21 @@ def can_be_parsed_as_bool(value: Any) -> bool: 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 @@ -142,7 +183,23 @@ def can_be_parsed_as_dict(value: Any) -> bool: return False -def can_be_parsed_as_list(value) -> bool: +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 diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py index 735abc3..853537c 100644 --- a/tests/test_parse_value.py +++ b/tests/test_parse_value.py @@ -50,6 +50,7 @@ def test_parse_value_parses_ints_floats_and_bools(test_input, expected): ), (" [1, 2, 3] ", [1, 2, 3]), (" [True, [1, 5], {}, 5.5]", [True, [1, 5], {}, 5.5]), + (" [ [ [ [] ]] ] ", [[[[]]]]), ], ) def test_parse_value_parses_dicts_and_lists(test_input, expected): -- cgit v1.2.3 From 3fa2561f16f684bb83aa9b3dcacb44171b04ddc5 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 1 Aug 2020 11:56:44 -0500 Subject: Raise FileNotFoundError if the config file does not exist --- config_file/utils.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config_file/utils.py b/config_file/utils.py index a75968c..4d8bb36 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -39,6 +39,11 @@ def create_config_path(file_path: Path, original: bool = False) -> Path: if file_path.is_dir(): raise ValueError(f"The specified config file ({file_path}) is a directory.") + if not file_path.exists(): + raise FileNotFoundError( + f"The specified config file ({file_path}) does not exist." + ) + return file_path -- cgit v1.2.3 From 032196104a59761bd75b5d594440d1e8ef16965a Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 1 Aug 2020 17:58:32 -0500 Subject: Add ConfigFilePath to refactor out file specific code --- config_file/config_file.py | 74 +++++++------------------ config_file/config_file_path.py | 117 ++++++++++++++++++++++++++++++++++++++++ config_file/default.py | 14 +++++ config_file/utils.py | 33 ------------ tests/conftest.py | 11 ++-- tests/test_config_file.py | 9 ++-- tests/test_config_file_path.py | 0 7 files changed, 159 insertions(+), 99 deletions(-) create mode 100644 config_file/config_file_path.py create mode 100644 config_file/default.py create mode 100644 tests/test_config_file_path.py diff --git a/config_file/config_file.py b/config_file/config_file.py index adcfaf1..3e8a806 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -3,14 +3,11 @@ from pathlib import Path from shutil import copyfile from typing import Any, Optional, Type, Union -from config_file.exceptions import ParsingError, UnrecognizedFileError +from config_file.config_file_path import ConfigFilePath +from config_file.default import Default +from config_file.exceptions import ParsingError from config_file.parsers.abstract_parser import AbstractParser -from config_file.parsers.ini_parser import IniParser -from config_file.parsers.json_parser import JsonParser from config_file.parsers.parse_value import parse_value -from config_file.parsers.toml_parser import TomlParser -from config_file.parsers.yaml_parser import YamlParser -from config_file.utils import Default, create_config_path, read_file, split_on_dot class ConfigFile: @@ -29,53 +26,22 @@ class ConfigFile: 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. """ - if not isinstance(file_path, Path): - file_path = Path(file_path) - - self.__path = create_config_path(file_path) - self.__parser = self.__determine_parser(self.__path, parser) - - @property - def path(self) -> Path: - return self.__path - - def __determine_parser( - self, file_path: Path, parser: Optional[Type[AbstractParser]] = None - ) -> Type[AbstractParser]: - file_contents = read_file(file_path) + self.__path = ConfigFilePath(file_path).validate() if parser is not None and not inspect.isabstract(parser): - return parser(file_contents) + self.__parser = parser(self.__path.contents) + else: + self.__parser = self.__path.parser - try: - file_type = split_on_dot(file_path, only_last_dot=True)[-1] - except ValueError: - raise UnrecognizedFileError( - "Tried to determine a parser to use, but the file at " - f"{file_path} does not have an extension." - ) + @property + def file_path(self) -> Path: + return Path(self.__path) - return self.__find_parser_by_file_type( - file_type=file_type, file_contents=file_contents, file_path=file_path - ) - - @staticmethod - def __find_parser_by_file_type( - file_type: str, file_path: Union[str, Path], file_contents: str - ) -> Type[AbstractParser]: - if file_type == "ini": - return IniParser(file_contents) - elif file_type == "json": - return JsonParser(file_contents) - elif file_type == "yaml" or file_type == "yml": - return YamlParser(file_contents) - elif file_type == "toml": - return TomlParser(file_contents) - else: - raise UnrecognizedFileError( - f"File path at `{file_path}` contains an unrecognized file type." - ) + @property + def original_file_path(self) -> Path: + return Path(self.__path.original_path) def get( self, @@ -207,17 +173,17 @@ class ConfigFile: SameFileError: If current configuration path and the passed in original_file_path are the same file. """ - if original_file_path is None: - original_file_path = create_config_path(self.__path, original=True) - - if not Path(original_file_path).exists(): + if original_file_path and not ConfigFilePath(original_file_path).exists(): raise FileNotFoundError( f"The {original_file_path} file to restore to does not exist." ) + else: + original_file_path = self.__path.original_path + original_file_path = original_file_path.validate() - self.__path.expanduser().unlink() + self.__path.unlink() copyfile(original_file_path, self.__path) - self.__parser.reset_internal_contents(read_file(self.__path)) + self.__parser.reset_internal_contents(self.__path.contents) def save(self) -> None: """ diff --git a/config_file/config_file_path.py b/config_file/config_file_path.py new file mode 100644 index 0000000..a40cc40 --- /dev/null +++ b/config_file/config_file_path.py @@ -0,0 +1,117 @@ +import os +from pathlib import Path as _Path_ +from pathlib import _posix_flavour, _windows_flavour +from typing import Type + +from .exceptions import UnrecognizedFileError +from .parsers.abstract_parser import AbstractParser +from .parsers.ini_parser import IniParser +from .parsers.json_parser import JsonParser +from .parsers.toml_parser import TomlParser +from .parsers.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 most 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: + UnrecognizedFileError: If the extension of the file + is not recognized. + + Returns: + The instantiated parser that should be used for this file. + """ + if self.extension == "": + raise UnrecognizedFileError( + "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 UnrecognizedFileError( + 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: + This ConfigFilePath object + """ + 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/default.py b/config_file/default.py new file mode 100644 index 0000000..529021c --- /dev/null +++ b/config_file/default.py @@ -0,0 +1,14 @@ +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/utils.py b/config_file/utils.py index 4d8bb36..c6c3d24 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -20,36 +20,3 @@ def split_on_dot(line: Union[str, Path], only_last_dot=False) -> List[str]: 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(".") - - -def read_file(path: Union[str, Path]) -> str: - with open(path, "r") as file: - return file.read() - - -def create_config_path(file_path: Path, original: bool = False) -> Path: - if len(file_path.parts) >= 1 and file_path.parts[0] == "~": - return file_path.expanduser() - - if original: - file_parts = split_on_dot(file_path, only_last_dot=True) - file_parts.insert(-1, "original") - file_path = Path(".".join(file_parts)) - - if file_path.is_dir(): - raise ValueError(f"The specified config file ({file_path}) is a directory.") - - if not file_path.exists(): - raise FileNotFoundError( - f"The specified config file ({file_path}) does not exist." - ) - - return file_path - - -class Default: - def __init__(self, value): - self.value = value - - def __repr__(self): - return "Default Value: {} ({})".format(self.value, type(self.value)) diff --git a/tests/conftest.py b/tests/conftest.py index 70e88c7..faf1630 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,7 +4,7 @@ from typing import Callable, Tuple, Type, Union import pytest from config_file import AbstractParser, ConfigFile -from config_file.utils import create_config_path +from config_file.config_file_path import ConfigFilePath @pytest.fixture @@ -37,7 +37,7 @@ def template_original_file() -> Callable[[Path], Path]: if isinstance(template_path, str): template_path = Path(template_path) - original_path = create_config_path(template_path, original=True) + original_path = ConfigFilePath(template_path).original_path original_path.touch() original_path.write_text(template_path.read_text(encoding="utf-8")) @@ -54,12 +54,7 @@ def template_and_parser( file_type: str, template_name: str = "default" ) -> Tuple[Path, Type[AbstractParser]]: test_file = template_file(file_type, template_name) - text = test_file.read_text(encoding="utf-8") - - parser = ConfigFile._ConfigFile__find_parser_by_file_type( - file_contents=text, file_type=file_type, file_path=test_file - ) - return test_file, parser + return test_file, ConfigFile(test_file)._ConfigFile__parser return func diff --git a/tests/test_config_file.py b/tests/test_config_file.py index d655397..548951b 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -30,7 +30,7 @@ def templated_config_file(template_and_config_file): def test_that_config_file_is_initialized_correctly(template_and_config_file): template_file, config = template_and_config_file() - assert config.path == template_file + assert config.file_path == template_file assert config.stringify() == template_file.read_text() assert isinstance(config._ConfigFile__parser, AbstractParser) @@ -41,7 +41,7 @@ def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): try: Path(config_path).expanduser().touch() - assert ConfigFile(config_path).path == Path.home() / config_name + assert ConfigFile(config_path).file_path == Path.home() / config_name finally: Path(config_path).expanduser().unlink() @@ -66,7 +66,7 @@ def test_that_config_file_can_restore_the_original( templated_config_file, template_original_file ): config = templated_config_file() - original_file = template_original_file(config.path) + original_file = template_original_file(config.file_path) config.set("header_one.number_key", 5) config.restore_original() @@ -80,7 +80,8 @@ def test_missing_config_file_during_restore(templated_config_file): with pytest.raises(FileNotFoundError) as error: config.restore_original() - assert "file to restore to does not exist" in str(error) + assert "The specified config file" in str(error) + assert "does not exist" in str(error) @pytest.mark.parametrize( diff --git a/tests/test_config_file_path.py b/tests/test_config_file_path.py new file mode 100644 index 0000000..e69de29 -- 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/__init__.py | 16 +- config_file/abstract_parser.py | 117 ++++++ config_file/base_parser.py | 139 +++++++ config_file/config_file.py | 6 +- config_file/config_file_path.py | 21 +- config_file/default.py | 14 - config_file/exceptions.py | 2 +- config_file/ini_parser.py | 13 + config_file/ini_parser_compat.py | 42 ++ config_file/json_parser.py | 15 + config_file/nested_lookup.py | 402 +++++++++++++++++++ config_file/nested_lookup/__init__.py | 19 - config_file/nested_lookup/lookup_api.py | 214 ---------- config_file/nested_lookup/nested_lookup.py | 178 --------- config_file/parse_value.py | 218 ++++++++++ config_file/parsers/__init__.py | 0 config_file/parsers/abstract_parser.py | 117 ------ config_file/parsers/base_parser.py | 98 ----- config_file/parsers/ini_parser.py | 13 - config_file/parsers/ini_parser_compat.py | 42 -- config_file/parsers/json_parser.py | 15 - config_file/parsers/parse_value.py | 218 ---------- config_file/parsers/toml_parser.py | 28 -- config_file/parsers/yaml_parser.py | 28 -- config_file/toml_parser.py | 28 ++ config_file/utils.py | 16 + config_file/yaml_parser.py | 27 ++ tests/test_config_file.py | 98 ++--- tests/test_ini_parser.py | 4 +- tests/test_json_parser.py | 2 +- tests/test_lookup_api.py | 622 ----------------------------- tests/test_nested_lookup.py | 617 +++++++++++++++++++++++++++- tests/test_parse_value.py | 2 +- 33 files changed, 1677 insertions(+), 1714 deletions(-) create mode 100644 config_file/abstract_parser.py create mode 100644 config_file/base_parser.py delete mode 100644 config_file/default.py create mode 100644 config_file/ini_parser.py create mode 100644 config_file/ini_parser_compat.py create mode 100644 config_file/json_parser.py create mode 100644 config_file/nested_lookup.py delete mode 100644 config_file/nested_lookup/__init__.py delete mode 100644 config_file/nested_lookup/lookup_api.py delete mode 100644 config_file/nested_lookup/nested_lookup.py create mode 100644 config_file/parse_value.py delete mode 100644 config_file/parsers/__init__.py delete mode 100644 config_file/parsers/abstract_parser.py delete mode 100644 config_file/parsers/base_parser.py delete mode 100644 config_file/parsers/ini_parser.py delete mode 100644 config_file/parsers/ini_parser_compat.py delete mode 100644 config_file/parsers/json_parser.py delete mode 100644 config_file/parsers/parse_value.py delete mode 100644 config_file/parsers/toml_parser.py delete mode 100644 config_file/parsers/yaml_parser.py create mode 100644 config_file/toml_parser.py create mode 100644 config_file/yaml_parser.py delete mode 100644 tests/test_lookup_api.py diff --git a/config_file/__init__.py b/config_file/__init__.py index 3825140..d78baaf 100644 --- a/config_file/__init__.py +++ b/config_file/__init__.py @@ -1,21 +1,9 @@ -from config_file.config_file import ConfigFile -from config_file.exceptions import ( - ConfigFileError, - MissingDependencyError, - MissingKeyError, - ParsingError, - UnrecognizedFileError, -) -from config_file.parsers.abstract_parser import AbstractParser +from .abstract_parser import AbstractParser +from .config_file import ConfigFile __version__ = "0.10.0" __all__ = [ "ConfigFile", "AbstractParser", - "ConfigFileError", - "ParsingError", - "UnrecognizedFileError", - "MissingDependencyError", - "MissingKeyError", ] diff --git a/config_file/abstract_parser.py b/config_file/abstract_parser.py new file mode 100644 index 0000000..ed9430b --- /dev/null +++ b/config_file/abstract_parser.py @@ -0,0 +1,117 @@ +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: + MissingKeyError: 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: + MissingKeyError: 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) -> 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. + + 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..a987a55 --- /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 MissingKeyError, 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 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 diff --git a/config_file/config_file.py b/config_file/config_file.py index 3e8a806..d047560 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -3,11 +3,11 @@ from pathlib import Path from shutil import copyfile from typing import Any, Optional, Type, Union +from config_file.abstract_parser import AbstractParser from config_file.config_file_path import ConfigFilePath -from config_file.default import Default from config_file.exceptions import ParsingError -from config_file.parsers.abstract_parser import AbstractParser -from config_file.parsers.parse_value import parse_value +from config_file.parse_value import parse_value +from config_file.utils import Default class ConfigFile: diff --git a/config_file/config_file_path.py b/config_file/config_file_path.py index a40cc40..9f2b87d 100644 --- a/config_file/config_file_path.py +++ b/config_file/config_file_path.py @@ -3,12 +3,13 @@ from pathlib import Path as _Path_ from pathlib import _posix_flavour, _windows_flavour from typing import Type -from .exceptions import UnrecognizedFileError -from .parsers.abstract_parser import AbstractParser -from .parsers.ini_parser import IniParser -from .parsers.json_parser import JsonParser -from .parsers.toml_parser import TomlParser -from .parsers.yaml_parser import YamlParser +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 .exceptions import UnsupportedFileTypeError from .utils import split_on_dot @@ -20,7 +21,7 @@ class ConfigFilePath(_Path_): that should be use for the given file type. See https://codereview.stackexchange.com/questions/162426/subclassing-pathlib-path - for most details on subclassing the pathlib.Path object. + for more details on subclassing the pathlib.Path object. """ _flavour = _windows_flavour if os.name == "nt" else _posix_flavour @@ -57,14 +58,14 @@ class ConfigFilePath(_Path_): """Determine what parser should be used for this file. Raises: - UnrecognizedFileError: If the extension of the file + UnsupportedFileTypeError: If the extension of the file is not recognized. Returns: The instantiated parser that should be used for this file. """ if self.extension == "": - raise UnrecognizedFileError( + raise UnsupportedFileTypeError( "Tried to determine a parser to use, but the file at " f"{self} does not have an extension." ) @@ -78,7 +79,7 @@ class ConfigFilePath(_Path_): elif self.extension == "toml": return TomlParser(self.contents) else: - raise UnrecognizedFileError( + raise UnsupportedFileTypeError( f"File path at `{self}` contains an unrecognized file type." ) diff --git a/config_file/default.py b/config_file/default.py deleted file mode 100644 index 529021c..0000000 --- a/config_file/default.py +++ /dev/null @@ -1,14 +0,0 @@ -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/exceptions.py b/config_file/exceptions.py index d1b466b..22ee70c 100644 --- a/config_file/exceptions.py +++ b/config_file/exceptions.py @@ -9,7 +9,7 @@ class MissingDependencyError(ConfigFileError): """ -class UnrecognizedFileError(ConfigFileError): +class UnsupportedFileTypeError(ConfigFileError): """ A missing file extension or a file type not supported. """ 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..40da123 --- /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/nested_lookup/__init__.py b/config_file/nested_lookup/__init__.py deleted file mode 100644 index 4493bdf..0000000 --- a/config_file/nested_lookup/__init__.py +++ /dev/null @@ -1,19 +0,0 @@ -from .lookup_api import nested_alter, nested_delete, nested_update -from .nested_lookup import ( - get_all_keys, - get_occurrence_of_key, - get_occurrence_of_value, - get_occurrences_and_values, - nested_lookup, -) - -__all__ = [ - "get_all_keys", - "get_occurrences_and_values", - "get_occurrence_of_value", - "get_occurrence_of_key", - "nested_update", - "nested_delete", - "nested_alter", - "nested_lookup", -] diff --git a/config_file/nested_lookup/lookup_api.py b/config_file/nested_lookup/lookup_api.py deleted file mode 100644 index 886e8bf..0000000 --- a/config_file/nested_lookup/lookup_api.py +++ /dev/null @@ -1,214 +0,0 @@ -import copy -import warnings - -from .nested_lookup import nested_lookup - - -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/nested_lookup/nested_lookup.py b/config_file/nested_lookup/nested_lookup.py deleted file mode 100644 index 6f2e7f2..0000000 --- a/config_file/nested_lookup/nested_lookup.py +++ /dev/null @@ -1,178 +0,0 @@ -from collections import defaultdict - -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] 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 diff --git a/config_file/parsers/abstract_parser.py b/config_file/parsers/abstract_parser.py deleted file mode 100644 index ed9430b..0000000 --- a/config_file/parsers/abstract_parser.py +++ /dev/null @@ -1,117 +0,0 @@ -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: - MissingKeyError: 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: - MissingKeyError: 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) -> 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. - - Returns: - True if the working file has the key. False otherwise. - """ - raise NotImplementedError 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 diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py deleted file mode 100644 index 72974f1..0000000 --- a/config_file/parsers/ini_parser.py +++ /dev/null @@ -1,13 +0,0 @@ -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/parsers/ini_parser_compat.py b/config_file/parsers/ini_parser_compat.py deleted file mode 100644 index 9dfea2e..0000000 --- a/config_file/parsers/ini_parser_compat.py +++ /dev/null @@ -1,42 +0,0 @@ -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/json_parser.py b/config_file/parsers/json_parser.py deleted file mode 100644 index 5314cd7..0000000 --- a/config_file/parsers/json_parser.py +++ /dev/null @@ -1,15 +0,0 @@ -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/parsers/parse_value.py b/config_file/parsers/parse_value.py deleted file mode 100644 index 12032d6..0000000 --- a/config_file/parsers/parse_value.py +++ /dev/null @@ -1,218 +0,0 @@ -"""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/toml_parser.py b/config_file/parsers/toml_parser.py deleted file mode 100644 index 1c7f8a7..0000000 --- a/config_file/parsers/toml_parser.py +++ /dev/null @@ -1,28 +0,0 @@ -try: - import tomlkit - - TOML_AVAILABLE = True -except ImportError: - TOML_AVAILABLE = False - -from config_file.exceptions import MissingDependencyError - -from .base_parser import BaseParser - - -class TomlParser(BaseParser): - def __init__(self, file_contents: str): - if not TOML_AVAILABLE: - raise MissingDependencyError( - "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]`." - ) - - super().__init__( - file_contents=file_contents, - module=tomlkit, - # tomlkit doesn't expose it's exceptions, but - # it's parsing error is a subclass of ValueError - decode_error=ValueError, - ) diff --git a/config_file/parsers/yaml_parser.py b/config_file/parsers/yaml_parser.py deleted file mode 100644 index babe873..0000000 --- a/config_file/parsers/yaml_parser.py +++ /dev/null @@ -1,28 +0,0 @@ -try: - from ruamel.yaml import YAML, YAMLError, round_trip_dump, round_trip_load - - YAML_AVAILABLE = True -except ImportError: - YAML_AVAILABLE = False - -from config_file.exceptions import MissingDependencyError - -from .base_parser import BaseParser - - -class YamlParser(BaseParser): - def __init__(self, file_contents: str): - if not YAML_AVAILABLE: - raise MissingDependencyError( - "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=YAMLError - ) diff --git a/config_file/toml_parser.py b/config_file/toml_parser.py new file mode 100644 index 0000000..1c7f8a7 --- /dev/null +++ b/config_file/toml_parser.py @@ -0,0 +1,28 @@ +try: + import tomlkit + + TOML_AVAILABLE = True +except ImportError: + TOML_AVAILABLE = False + +from config_file.exceptions import MissingDependencyError + +from .base_parser import BaseParser + + +class TomlParser(BaseParser): + def __init__(self, file_contents: str): + if not TOML_AVAILABLE: + raise MissingDependencyError( + "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]`." + ) + + super().__init__( + file_contents=file_contents, + module=tomlkit, + # tomlkit doesn't expose it's exceptions, but + # it's parsing error is a subclass of ValueError + decode_error=ValueError, + ) diff --git a/config_file/utils.py b/config_file/utils.py index c6c3d24..3554c0e 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -20,3 +20,19 @@ def split_on_dot(line: Union[str, Path], only_last_dot=False) -> List[str]: 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..fb42e08 --- /dev/null +++ b/config_file/yaml_parser.py @@ -0,0 +1,27 @@ +try: + from ruamel.yaml import YAML, YAMLError, round_trip_dump, round_trip_load + + YAML_AVAILABLE = True +except ImportError: + YAML_AVAILABLE = False + +from .base_parser import BaseParser +from .exceptions import MissingDependencyError + + +class YamlParser(BaseParser): + def __init__(self, file_contents: str): + if not YAML_AVAILABLE: + raise MissingDependencyError( + "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=YAMLError + ) diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 548951b..91cd0e6 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -1,11 +1,10 @@ -import json from pathlib import Path import pytest +from config_file.abstract_parser import AbstractParser from config_file.config_file import ConfigFile from config_file.exceptions import MissingKeyError -from config_file.parsers.abstract_parser import AbstractParser SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"] @@ -46,9 +45,14 @@ def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): Path(config_path).expanduser().unlink() -@pytest.mark.skip(msg="needs fixing up") -@pytest.mark.parametrize("path", ["", "invalid", "invalid.conf"]) -def test_invalid_path_raises_value_error(path): +@pytest.mark.parametrize("path", ["invalid", "invalid.conf"]) +def test_invalid_path_raises_file_not_found_error(path): + with pytest.raises(FileNotFoundError): + ConfigFile(path) + + +@pytest.mark.parametrize("path", [""]) +def test_directory_raises_value_error(path): with pytest.raises(ValueError): ConfigFile(path) @@ -99,75 +103,21 @@ def test_that_config_file_can_find_sections_and_keys( assert config.has(key) == expected_result -@pytest.mark.parametrize( - "section_key, value, parse_type, return_type, default, file_name, file_contents", - [ - ( - "calendar.sunday_index", - 0, - True, - None, - False, - "config.ini", - "[calendar]\nsunday_index = 0\n\n", - ), - ( - "calendar.sunday_index", - "0", - False, - None, - False, - "config.ini", - "[calendar]\nsunday_index = 0\n\n", - ), - ( - "calendar.sunday_index", - 0, - False, - None, - False, - "config.json", - json.dumps({"calendar": {"sunday_index": 0}}), - ), - ( - "calendar.missing", - "my default value", - False, - None, - "my default value", - "config.json", - json.dumps({"calendar": 5}), - ), - ( - "calendar.sunday_index", - 0, - False, - int, - False, - "config.ini", - "[calendar]\nsunday_index = 0\n\n", - ), - ], -) -def test_that_config_file_can_get( - tmpdir, - section_key, - value, - return_type, - parse_type, - file_name, - file_contents, - default, -): - config_path = tmpdir / file_name - config_path.write_text(file_contents, encoding="utf-8") - config = ConfigFile(config_path) - - ret_val = config.get(section_key, parse_types=parse_type, default=default) - if return_type is not None: - assert return_type(ret_val) == value - else: - assert ret_val == value +# @pytest.mark.parametrize( +# "key, value", +# [ +# ("header_one.number_key", 0), +# ("header_two.list_key", [1, 2, 3]), +# ], +# ) +# def test_that_config_file_can_get_existent_keys(templated_config_file, key, value): +# config = templated_config_file() +# +# print(config.get(key)) +# print(type(config.get(key))) +# print(config._ConfigFile__parser) +# +# assert config.get(key) == value @pytest.mark.parametrize("key", ["header_one.number_key", "header_one"]) diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index 46966c9..24507b7 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,7 +1,7 @@ import pytest from config_file.exceptions import MissingKeyError, ParsingError -from config_file.parsers.ini_parser import IniParser +from config_file.ini_parser import IniParser @pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"]) @@ -39,7 +39,7 @@ floatkey = 5.3 "test_input", ["invalid.invalid_key"], ) def test_that_getting_unknown_sections_or_keys_throws_errors(test_input): - with pytest.raises(ParsingError): + with pytest.raises(KeyError): IniParser("").get(test_input) diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index f8200b0..54ea646 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -84,7 +84,7 @@ def test_that_json_parser_can_get_keys_without_parsing_types( @pytest.mark.parametrize( - "key, value", [("zip", "zipzip"), ("foo.bar.foobar", 5), ("foo.baz", "bar")] + "key, value", [("zip", "zipzip"), ("foo.bar.foobar", "5"), ("foo.baz", "bar")] ) def test_that_json_parser_can_set_keys(templated_parser, key, value): parser = templated_parser("json") diff --git a/tests/test_lookup_api.py b/tests/test_lookup_api.py deleted file mode 100644 index 8b8ca9e..0000000 --- a/tests/test_lookup_api.py +++ /dev/null @@ -1,622 +0,0 @@ -from unittest import TestCase - -from config_file.nested_lookup import ( - nested_alter, - nested_delete, - nested_lookup, - nested_update, -) - - -class BaseLookUpApi(TestCase): - def setUp(self): - self.sample_data1 = { - "build_version": { - "model_name": "MacBook Pro", - "build_version": { - "processor_name": "Intel Core i7", - "processor_speed": "2.7 GHz", - "core_details": { - "build_version": "4", - "l2_cache(per_core)": "256 KB", - }, - }, - "number_of_cores": "4", - "memory": "256 KB", - }, - "os_details": {"product_version": "10.13.6", "build_version": "17G65"}, - "name": "Test", - "date": "YYYY-MM-DD HH:MM:SS", - } - - self.sample_data2 = { - "hardware_details": { - "model_name": "MacBook Pro", - "processor_details": [ - {"processor_name": "Intel Core i7", "processor_speed": "2.7 GHz"}, - {"total_number_of_cores": "4", "l2_cache(per_core)": "256 KB"}, - ], - "total_number_of_cores": "5", - "memory": "16 GB", - } - } - - self.sample_data3 = { - "values": [ - { - "checks": [ - { - "monitoring_zones": [ - "mzdfw", - "mzfra", - "mzhkg", - "mziad", - "mzlon", - "mzord", - "mzsyd", - ] - } - ] - } - ] - } - - self.sample_data4 = { - "modelversion": "1.1.0", - "vorgangsID": "1", - "versorgungsvorschlagDatum": 1510558834978, - "eingangsdatum": 1510558834978, - "plz": 82269, - "vertragsteile": [ - { - "typ": "1", - "beitragsDaten": { - "endalter": 85, - "brutto": 58.76, - "netto": 58.76, - "zahlungsrhythmus": "MONATLICH", - "plz": 86899, - }, - "beginn": 1512082800000, - "lebenslang": "True", - "ueberschussverwendung": { - "ueberschussverwendung": "2", - "indexoption": "3", - }, - "deckung": [ - { - "typ": "2", - "art": "1", - "leistung": {"value": 7500242424.0, "einheit": "2"}, - "leistungsRhythmus": "1", - } - ], - "zuschlagNachlass": [], - }, - { - "typ": "1", - "beitragsDaten": { - "endalter": 85, - "brutto": 0.6, - "netto": 0.6, - "zahlungsrhythmus": "1", - }, - "zuschlagNachlass": [], - }, - ], - } - - -class TestNestedDelete(BaseLookUpApi): - def test_sample_data1(self): - result = { - "os_details": {"product_version": "10.13.6"}, - "name": "Test", - "date": "YYYY-MM-DD HH:MM:SS", - } - self.assertEqual(result, nested_delete(self.sample_data1, "build_version")) - - def test_sample_data2(self): - result = { - "hardware_details": { - "model_name": "MacBook Pro", - "total_number_of_cores": "5", - "memory": "16 GB", - } - } - self.assertEqual(result, nested_delete(self.sample_data2, "processor_details")) - - def test_sample_data3(self): - result = {"values": [{"checks": [{}]}]} - self.assertEqual(result, nested_delete(self.sample_data3, "monitoring_zones")) - - -class TestNestedUpdate(BaseLookUpApi): - def test_sample_data1(self): - result = { - "build_version": "Test1", - "os_details": {"product_version": "10.13.6", "build_version": "Test1"}, - "name": "Test", - "date": "YYYY-MM-DD HH:MM:SS", - } - self.assertEqual( - result, nested_update(self.sample_data1, "build_version", "Test1") - ) - - def test_sample_data1_list_input_treat_list_as_element_true(self): - result = { - "build_version": ["Test5", "Test6", "Test7"], - "os_details": { - "product_version": "10.13.6", - "build_version": ["Test5", "Test6", "Test7"], - }, - "name": "Test", - "date": "YYYY-MM-DD HH:MM:SS", - } - - self.assertEqual( - result, - nested_update( - self.sample_data1, - "build_version", - ["Test5", "Test6", "Test7"], - treat_as_element=True, - ), - ) - - def test_nested_update_in_place_false(self): - """ - ested_update should mutate and return a copy of the original document - """ - before_id = id(self.sample_data1) - result = nested_update( - self.sample_data1, "build_version", "Test2", in_place=False - ) - after_id = id(result) - # the object ids should _not_ match. - self.assertNotEqual(before_id, after_id) - - def test_nested_update_in_place_true(self): - """ - nested_update should mutate and return the original document - """ - before_id = id(self.sample_data1) - result = nested_update( - self.sample_data1, "build_version", "Test2", in_place=True - ) - after_id = id(result) - # the object ids should match. - self.assertEqual(before_id, after_id) - - def test_nested_update_in_place_true_list_input(self): - doc = self.sample_data4 - # get all instances of the given element - findings = nested_lookup("plz", doc, False, True) - # alter those instances - updated_findings = list() - for key, val in findings.items(): - for elem in val: - updated_findings.append(elem + 300) - # update those instances with the altered results - doc_updated = nested_update( - doc, "plz", updated_findings, treat_as_element=False - ) - elem1 = doc_updated["plz"] # 85269 - # 87199 - elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] - self.assertEqual(elem1, 82569) - self.assertEqual(elem2, 87199) - - def test_nested_update_in_place_false_list_input(self): - doc = self.sample_data4 - # get all instances of the given element - findings = nested_lookup("plz", doc, False, True) - # alter those instances - updated_findings = list() - for key, val in findings.items(): - for elem in val: - updated_findings.append(elem + 300) - # update those instances with the altered results - doc_updated = nested_update( - doc, "plz", updated_findings, in_place=False, treat_as_element=False - ) - elem1 = doc_updated["plz"] - elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] - self.assertEqual(elem1, 82569) - self.assertEqual(elem2, 87199) - - def test_nested_update_in_place_false_list_input_as_element_false(self): - doc = self.sample_data4 - # get all instances of the given element - list_input = [1, 2, 3, 4, 5] - # update those instances with the altered results - doc_updated = nested_update( - doc, "plz", list_input, in_place=False, treat_as_element=False - ) - elem1 = doc_updated["plz"] - elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] - # should not work without specifying "treat_list_as_element = True" - # in nested_update - self.assertNotEqual(elem1, list_input) - self.assertNotEqual(elem2, list_input) - - def test_nested_update_in_place_false_list_input_as_element_true(self): - doc = self.sample_data4 - # get all instances of the given element - list_input = [1, 2, 3, 4, 5] - # update those instances with the altered results - doc_updated = nested_update( - doc, "plz", list_input, in_place=False, treat_as_element=True - ) - elem1 = doc_updated["plz"] - elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] - # should not work without specifying "treat_list_as_element = True" - # in nested_update - self.assertEqual(elem1, list_input) - self.assertEqual(elem2, list_input) - - def test_nested_update_in_place_true_list_input_as_element_false(self): - doc = self.sample_data4 - # get all instances of the given element - list_input = [1, 2, 3, 4, 5] - # update those instances with the altered results - doc_updated = nested_update( - doc, "plz", list_input, in_place=True, treat_as_element=False - ) - elem1 = doc_updated["plz"] - elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] - # should not work without specifying "treat_list_as_element = True" - # in nested_update - self.assertNotEqual(elem1, list_input) - self.assertNotEqual(elem2, list_input) - - def test_nested_update_in_place_true_list_input_as_element_true(self): - doc = self.sample_data4 - # get all instances of the given element - list_input = [1, 2, 3, 4, 5] - # update those instances with the altered results - doc_updated = nested_update( - doc, "plz", list_input, in_place=True, treat_as_element=True - ) - elem1 = doc_updated["plz"] - elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] - # should not work without specifying "treat_list_as_element = True" - # in nested_update - self.assertEqual(elem1, list_input) - self.assertEqual(elem2, list_input) - - def test_nested_delete_in_place_false(self): - """ - nested_delete should mutate and return a copy of the original document - """ - before_id = id(self.sample_data1) - result = nested_delete(self.sample_data1, "build_version", in_place=False) - after_id = id(result) - # the object ids should _not_ match. - self.assertNotEqual(before_id, after_id) - - def test_nested_delete_in_place_true(self): - """nested_delete should mutate and return the original document""" - before_id = id(self.sample_data1) - result = nested_delete(self.sample_data1, "build_version", in_place=True) - after_id = id(result) - # the object ids should match. - self.assertEqual(before_id, after_id) - - def test_nested_update_taco_for_example(self): - document = [{"taco": 42}, {"salsa": [{"burrito": {"taco": 69}}]}] - - updated_document = nested_update( - document, "taco", [100, 200], treat_as_element=False - ) - - self.assertEqual(updated_document[0]["taco"], 100) - # The multi-update version only works for scalar input, - # if you need to adress a list of dicts, you have to - # manually iterate over those and pass them to nested_update - # one by one - self.assertEqual(updated_document[1]["salsa"][0]["burrito"]["taco"], 200) - - def test_nested_update_raise_error(self): - doc = self.sample_data4 - # get all instances of the given element - list_input = 1 - # update those instances with the altered results - self.assertRaises( - Exception, - nested_update, - doc, - "plz", - list_input, - in_place=True, - treat_as_element=False, - ) - - def test_sample_data2(self): - result = { - "hardware_details": { - "model_name": "MacBook Pro", - "processor_details": {"test_key1": "test_value1"}, - "total_number_of_cores": "5", - "memory": "16 GB", - } - } - self.assertEqual( - result, - nested_update( - self.sample_data2, "processor_details", {"test_key1": "test_value1"} - ), - ) - - def test_sample_data3(self): - result = {"values": [{"checks": {"key1": ["value1"], "key2": "value2"}}]} - self.assertEqual( - result, - nested_update( - self.sample_data3, "checks", {"key1": ["value1"], "key2": "value2"} - ), - ) - - def test_sample_data4(self): - result = { - "modelversion": {"key1": ["value1"], "key2": "value2"}, - "vorgangsID": "1", - "versorgungsvorschlagDatum": 1510558834978, - "eingangsdatum": 1510558834978, - "plz": 82269, - "vertragsteile": [ - { - "typ": "1", - "beitragsDaten": { - "endalter": 85, - "brutto": 58.76, - "netto": 58.76, - "zahlungsrhythmus": "MONATLICH", - "plz": 86899, - }, - "beginn": 1512082800000, - "lebenslang": "True", - "ueberschussverwendung": { - "ueberschussverwendung": "2", - "indexoption": "3", - }, - "deckung": [ - { - "typ": "2", - "art": "1", - "leistung": {"value": 7500242424.0, "einheit": "2"}, - "leistungsRhythmus": "1", - } - ], - "zuschlagNachlass": [], - }, - { - "typ": "1", - "beitragsDaten": { - "endalter": 85, - "brutto": 0.6, - "netto": 0.6, - "zahlungsrhythmus": "1", - }, - "zuschlagNachlass": [], - }, - ], - } - self.assertEqual( - result, - nested_update( - self.sample_data4, - "modelversion", - {"key1": ["value1"], "key2": "value2"}, - ), - ) - - -class TestNestedAlter(BaseLookUpApi): - def test_nested_alter_in_place_true(self): - - # callback functions - def callback(data): - return str(data) + "###" - - doc_updated = nested_alter( - self.sample_data4, "vorgangsID", callback, in_place=True - ) - - vorgangsid = doc_updated["vorgangsID"] - - self.assertEqual(vorgangsid, "1###") - - def test_nested_alter_in_place_false(self): - - # callback functions - def callback(data): - return str(data) + "###" - - doc_updated = nested_alter( - self.sample_data4, "vorgangsID", callback, in_place=False - ) - - vorgangsid = doc_updated["vorgangsID"] - - # should not work without specifying - # "treat_list_as_element = True" in nested_update - self.assertEqual(vorgangsid, "1###") - - def test_nested_alter_list_input_in_place_true(self): - - # callback functions - def callback(data): - return str(data) + "###" - - doc_updated = nested_alter( - self.sample_data4, ["plz", "vorgangsID"], callback, in_place=True - ) - - plz1 = doc_updated["plz"] - plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] - vorgangsid = doc_updated["vorgangsID"] - - # should not work without specifying - # "treat_list_as_element = True" in nested_update - self.assertEqual(plz1, "82269###") - self.assertEqual(plz2, "86899###") - self.assertEqual(vorgangsid, "1###") - - def test_nested_alter_list_input_with_args_in_place_true(self): - - # callback functions - def callback(data, str1, str2): - return str(data) + str1 + str2 - - doc_updated = nested_alter( - self.sample_data4, - ["plz", "vorgangsID"], - callback, - function_parameters=["abc", "def"], - in_place=True, - ) - - plz1 = doc_updated["plz"] - plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] - vorgangsid = doc_updated["vorgangsID"] - - # should not work without specifying - # "treat_list_as_element = True" in nested_update - self.assertEqual(plz1, "82269abcdef") - self.assertEqual(plz2, "86899abcdef") - self.assertEqual(vorgangsid, "1abcdef") - - def test_nested_alter_list_input_with_args_in_place_false(self): - - # callback functions - def callback(data, str1, str2): - return str(data) + str1 + str2 - - doc_updated = nested_alter( - self.sample_data4, - ["plz", "vorgangsID"], - callback, - function_parameters=["abc", "def"], - in_place=False, - ) - - plz1 = doc_updated["plz"] - plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] - vorgangsid = doc_updated["vorgangsID"] - - # should not work without specifying - # "treat_list_as_element = True" in nested_update - self.assertEqual(plz1, "82269abcdef") - self.assertEqual(plz2, "86899abcdef") - self.assertEqual(vorgangsid, "1abcdef") - - def test_nested_alter_list_input_in_place_false(self): - - # callback functions - def callback(data): - return str(data) + "###" - - doc_updated = nested_alter( - self.sample_data4, ["plz", "vorgangsID"], callback, in_place=False - ) - - plz1 = doc_updated["plz"] - plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] - vorgangsid = doc_updated["vorgangsID"] - - # should not work without specifying - # "treat_list_as_element = True" in nested_update - self.assertEqual(plz1, "82269###") - self.assertEqual(plz2, "86899###") - self.assertEqual(vorgangsid, "1###") - - def test_nested_alter_taco_for_example(self): - documents = [{"taco": 42}, {"salsa": [{"burrito": {"taco": 69}}]}] - - # write a callback function which processes a scalar value. - # Be aware about the possible types which can be passed to - # the callback functions. - # In this example we can be sure that only int will be passed, - # in production you should check the type because it could be - # anything. - def callback(data): - return data + 10 # (data/100*10) - - # The alter-version only works for scalar input (one dict), - # if you need to adress a list of dicts, you have to - # manually iterate over those and pass them to - # nested_update one by one - out = [] - for elem in documents: - altered_document = nested_alter(elem, "taco", callback) - out.append(altered_document) - - self.maxDiff = None - self.assertEqual(out[0]["taco"], 52) - self.assertEqual(out[1]["salsa"][0]["burrito"]["taco"], 79) - - def test_nested_alter_work_with_right_order(self): - document = {"taco": 42, "salsa": [{"burrito": {"key": 20}}], "key": 50} - - def callback(data): - return data + 100 - - altered_document = nested_alter(document, "key", callback, in_place=True) - - self.assertEqual(altered_document["salsa"][0]["burrito"]["key"], 120) - self.assertEqual(altered_document["key"], 150) - - def test_sample_data4(self): - - result = { - "modelversion": "1.1.0", - "vorgangsID": "1", - "versorgungsvorschlagDatum": 1510558834978, - "eingangsdatum": 1510558834978, - "plz": 82270, - "vertragsteile": [ - { - "typ": "1", - "beitragsDaten": { - "endalter": 85, - "brutto": 58.76, - "netto": 58.76, - "zahlungsrhythmus": "MONATLICH", - "plz": 86900, - }, - "beginn": 1512082800000, - "lebenslang": "True", - "ueberschussverwendung": { - "ueberschussverwendung": "2", - "indexoption": "3", - }, - "deckung": [ - { - "typ": "2", - "art": "1", - "leistung": {"value": 7500242424.0, "einheit": "2"}, - "leistungsRhythmus": "1", - } - ], - "zuschlagNachlass": [], - }, - { - "typ": "1", - "beitragsDaten": { - "endalter": 85, - "brutto": 0.6, - "netto": 0.6, - "zahlungsrhythmus": "1", - }, - "zuschlagNachlass": [], - }, - ], - } - - # add +1 to all plz - def callback(data): - return data + 1 - - self.maxDiff = None - self.assertEqual(result, nested_alter(self.sample_data4, "plz", callback)) diff --git a/tests/test_nested_lookup.py b/tests/test_nested_lookup.py index 591edda..1239fad 100644 --- a/tests/test_nested_lookup.py +++ b/tests/test_nested_lookup.py @@ -5,7 +5,10 @@ from config_file.nested_lookup import ( get_occurrence_of_key, get_occurrence_of_value, get_occurrences_and_values, + nested_alter, + nested_delete, nested_lookup, + nested_update, ) @@ -447,5 +450,615 @@ class TestGetOccurrence(TestCase): self.assertEqual(0, len(result[value]["values"])) -if __name__ == "__main__": - pass +class BaseLookUpApi(TestCase): + def setUp(self): + self.sample_data1 = { + "build_version": { + "model_name": "MacBook Pro", + "build_version": { + "processor_name": "Intel Core i7", + "processor_speed": "2.7 GHz", + "core_details": { + "build_version": "4", + "l2_cache(per_core)": "256 KB", + }, + }, + "number_of_cores": "4", + "memory": "256 KB", + }, + "os_details": {"product_version": "10.13.6", "build_version": "17G65"}, + "name": "Test", + "date": "YYYY-MM-DD HH:MM:SS", + } + + self.sample_data2 = { + "hardware_details": { + "model_name": "MacBook Pro", + "processor_details": [ + {"processor_name": "Intel Core i7", "processor_speed": "2.7 GHz"}, + {"total_number_of_cores": "4", "l2_cache(per_core)": "256 KB"}, + ], + "total_number_of_cores": "5", + "memory": "16 GB", + } + } + + self.sample_data3 = { + "values": [ + { + "checks": [ + { + "monitoring_zones": [ + "mzdfw", + "mzfra", + "mzhkg", + "mziad", + "mzlon", + "mzord", + "mzsyd", + ] + } + ] + } + ] + } + + self.sample_data4 = { + "modelversion": "1.1.0", + "vorgangsID": "1", + "versorgungsvorschlagDatum": 1510558834978, + "eingangsdatum": 1510558834978, + "plz": 82269, + "vertragsteile": [ + { + "typ": "1", + "beitragsDaten": { + "endalter": 85, + "brutto": 58.76, + "netto": 58.76, + "zahlungsrhythmus": "MONATLICH", + "plz": 86899, + }, + "beginn": 1512082800000, + "lebenslang": "True", + "ueberschussverwendung": { + "ueberschussverwendung": "2", + "indexoption": "3", + }, + "deckung": [ + { + "typ": "2", + "art": "1", + "leistung": {"value": 7500242424.0, "einheit": "2"}, + "leistungsRhythmus": "1", + } + ], + "zuschlagNachlass": [], + }, + { + "typ": "1", + "beitragsDaten": { + "endalter": 85, + "brutto": 0.6, + "netto": 0.6, + "zahlungsrhythmus": "1", + }, + "zuschlagNachlass": [], + }, + ], + } + + +class TestNestedDelete(BaseLookUpApi): + def test_sample_data1(self): + result = { + "os_details": {"product_version": "10.13.6"}, + "name": "Test", + "date": "YYYY-MM-DD HH:MM:SS", + } + self.assertEqual(result, nested_delete(self.sample_data1, "build_version")) + + def test_sample_data2(self): + result = { + "hardware_details": { + "model_name": "MacBook Pro", + "total_number_of_cores": "5", + "memory": "16 GB", + } + } + self.assertEqual(result, nested_delete(self.sample_data2, "processor_details")) + + def test_sample_data3(self): + result = {"values": [{"checks": [{}]}]} + self.assertEqual(result, nested_delete(self.sample_data3, "monitoring_zones")) + + +class TestNestedUpdate(BaseLookUpApi): + def test_sample_data1(self): + result = { + "build_version": "Test1", + "os_details": {"product_version": "10.13.6", "build_version": "Test1"}, + "name": "Test", + "date": "YYYY-MM-DD HH:MM:SS", + } + self.assertEqual( + result, nested_update(self.sample_data1, "build_version", "Test1") + ) + + def test_sample_data1_list_input_treat_list_as_element_true(self): + result = { + "build_version": ["Test5", "Test6", "Test7"], + "os_details": { + "product_version": "10.13.6", + "build_version": ["Test5", "Test6", "Test7"], + }, + "name": "Test", + "date": "YYYY-MM-DD HH:MM:SS", + } + + self.assertEqual( + result, + nested_update( + self.sample_data1, + "build_version", + ["Test5", "Test6", "Test7"], + treat_as_element=True, + ), + ) + + def test_nested_update_in_place_false(self): + """ + ested_update should mutate and return a copy of the original document + """ + before_id = id(self.sample_data1) + result = nested_update( + self.sample_data1, "build_version", "Test2", in_place=False + ) + after_id = id(result) + # the object ids should _not_ match. + self.assertNotEqual(before_id, after_id) + + def test_nested_update_in_place_true(self): + """ + nested_update should mutate and return the original document + """ + before_id = id(self.sample_data1) + result = nested_update( + self.sample_data1, "build_version", "Test2", in_place=True + ) + after_id = id(result) + # the object ids should match. + self.assertEqual(before_id, after_id) + + def test_nested_update_in_place_true_list_input(self): + doc = self.sample_data4 + # get all instances of the given element + findings = nested_lookup("plz", doc, False, True) + # alter those instances + updated_findings = list() + for key, val in findings.items(): + for elem in val: + updated_findings.append(elem + 300) + # update those instances with the altered results + doc_updated = nested_update( + doc, "plz", updated_findings, treat_as_element=False + ) + elem1 = doc_updated["plz"] # 85269 + # 87199 + elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + self.assertEqual(elem1, 82569) + self.assertEqual(elem2, 87199) + + def test_nested_update_in_place_false_list_input(self): + doc = self.sample_data4 + # get all instances of the given element + findings = nested_lookup("plz", doc, False, True) + # alter those instances + updated_findings = list() + for key, val in findings.items(): + for elem in val: + updated_findings.append(elem + 300) + # update those instances with the altered results + doc_updated = nested_update( + doc, "plz", updated_findings, in_place=False, treat_as_element=False + ) + elem1 = doc_updated["plz"] + elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + self.assertEqual(elem1, 82569) + self.assertEqual(elem2, 87199) + + def test_nested_update_in_place_false_list_input_as_element_false(self): + doc = self.sample_data4 + # get all instances of the given element + list_input = [1, 2, 3, 4, 5] + # update those instances with the altered results + doc_updated = nested_update( + doc, "plz", list_input, in_place=False, treat_as_element=False + ) + elem1 = doc_updated["plz"] + elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + # should not work without specifying "treat_list_as_element = True" + # in nested_update + self.assertNotEqual(elem1, list_input) + self.assertNotEqual(elem2, list_input) + + def test_nested_update_in_place_false_list_input_as_element_true(self): + doc = self.sample_data4 + # get all instances of the given element + list_input = [1, 2, 3, 4, 5] + # update those instances with the altered results + doc_updated = nested_update( + doc, "plz", list_input, in_place=False, treat_as_element=True + ) + elem1 = doc_updated["plz"] + elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + # should not work without specifying "treat_list_as_element = True" + # in nested_update + self.assertEqual(elem1, list_input) + self.assertEqual(elem2, list_input) + + def test_nested_update_in_place_true_list_input_as_element_false(self): + doc = self.sample_data4 + # get all instances of the given element + list_input = [1, 2, 3, 4, 5] + # update those instances with the altered results + doc_updated = nested_update( + doc, "plz", list_input, in_place=True, treat_as_element=False + ) + elem1 = doc_updated["plz"] + elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + # should not work without specifying "treat_list_as_element = True" + # in nested_update + self.assertNotEqual(elem1, list_input) + self.assertNotEqual(elem2, list_input) + + def test_nested_update_in_place_true_list_input_as_element_true(self): + doc = self.sample_data4 + # get all instances of the given element + list_input = [1, 2, 3, 4, 5] + # update those instances with the altered results + doc_updated = nested_update( + doc, "plz", list_input, in_place=True, treat_as_element=True + ) + elem1 = doc_updated["plz"] + elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + # should not work without specifying "treat_list_as_element = True" + # in nested_update + self.assertEqual(elem1, list_input) + self.assertEqual(elem2, list_input) + + def test_nested_delete_in_place_false(self): + """ + nested_delete should mutate and return a copy of the original document + """ + before_id = id(self.sample_data1) + result = nested_delete(self.sample_data1, "build_version", in_place=False) + after_id = id(result) + # the object ids should _not_ match. + self.assertNotEqual(before_id, after_id) + + def test_nested_delete_in_place_true(self): + """nested_delete should mutate and return the original document""" + before_id = id(self.sample_data1) + result = nested_delete(self.sample_data1, "build_version", in_place=True) + after_id = id(result) + # the object ids should match. + self.assertEqual(before_id, after_id) + + def test_nested_update_taco_for_example(self): + document = [{"taco": 42}, {"salsa": [{"burrito": {"taco": 69}}]}] + + updated_document = nested_update( + document, "taco", [100, 200], treat_as_element=False + ) + + self.assertEqual(updated_document[0]["taco"], 100) + # The multi-update version only works for scalar input, + # if you need to adress a list of dicts, you have to + # manually iterate over those and pass them to nested_update + # one by one + self.assertEqual(updated_document[1]["salsa"][0]["burrito"]["taco"], 200) + + def test_nested_update_raise_error(self): + doc = self.sample_data4 + # get all instances of the given element + list_input = 1 + # update those instances with the altered results + self.assertRaises( + Exception, + nested_update, + doc, + "plz", + list_input, + in_place=True, + treat_as_element=False, + ) + + def test_sample_data2(self): + result = { + "hardware_details": { + "model_name": "MacBook Pro", + "processor_details": {"test_key1": "test_value1"}, + "total_number_of_cores": "5", + "memory": "16 GB", + } + } + self.assertEqual( + result, + nested_update( + self.sample_data2, "processor_details", {"test_key1": "test_value1"} + ), + ) + + def test_sample_data3(self): + result = {"values": [{"checks": {"key1": ["value1"], "key2": "value2"}}]} + self.assertEqual( + result, + nested_update( + self.sample_data3, "checks", {"key1": ["value1"], "key2": "value2"} + ), + ) + + def test_sample_data4(self): + result = { + "modelversion": {"key1": ["value1"], "key2": "value2"}, + "vorgangsID": "1", + "versorgungsvorschlagDatum": 1510558834978, + "eingangsdatum": 1510558834978, + "plz": 82269, + "vertragsteile": [ + { + "typ": "1", + "beitragsDaten": { + "endalter": 85, + "brutto": 58.76, + "netto": 58.76, + "zahlungsrhythmus": "MONATLICH", + "plz": 86899, + }, + "beginn": 1512082800000, + "lebenslang": "True", + "ueberschussverwendung": { + "ueberschussverwendung": "2", + "indexoption": "3", + }, + "deckung": [ + { + "typ": "2", + "art": "1", + "leistung": {"value": 7500242424.0, "einheit": "2"}, + "leistungsRhythmus": "1", + } + ], + "zuschlagNachlass": [], + }, + { + "typ": "1", + "beitragsDaten": { + "endalter": 85, + "brutto": 0.6, + "netto": 0.6, + "zahlungsrhythmus": "1", + }, + "zuschlagNachlass": [], + }, + ], + } + self.assertEqual( + result, + nested_update( + self.sample_data4, + "modelversion", + {"key1": ["value1"], "key2": "value2"}, + ), + ) + + +class TestNestedAlter(BaseLookUpApi): + def test_nested_alter_in_place_true(self): + + # callback functions + def callback(data): + return str(data) + "###" + + doc_updated = nested_alter( + self.sample_data4, "vorgangsID", callback, in_place=True + ) + + vorgangsid = doc_updated["vorgangsID"] + + self.assertEqual(vorgangsid, "1###") + + def test_nested_alter_in_place_false(self): + + # callback functions + def callback(data): + return str(data) + "###" + + doc_updated = nested_alter( + self.sample_data4, "vorgangsID", callback, in_place=False + ) + + vorgangsid = doc_updated["vorgangsID"] + + # should not work without specifying + # "treat_list_as_element = True" in nested_update + self.assertEqual(vorgangsid, "1###") + + def test_nested_alter_list_input_in_place_true(self): + + # callback functions + def callback(data): + return str(data) + "###" + + doc_updated = nested_alter( + self.sample_data4, ["plz", "vorgangsID"], callback, in_place=True + ) + + plz1 = doc_updated["plz"] + plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + vorgangsid = doc_updated["vorgangsID"] + + # should not work without specifying + # "treat_list_as_element = True" in nested_update + self.assertEqual(plz1, "82269###") + self.assertEqual(plz2, "86899###") + self.assertEqual(vorgangsid, "1###") + + def test_nested_alter_list_input_with_args_in_place_true(self): + + # callback functions + def callback(data, str1, str2): + return str(data) + str1 + str2 + + doc_updated = nested_alter( + self.sample_data4, + ["plz", "vorgangsID"], + callback, + function_parameters=["abc", "def"], + in_place=True, + ) + + plz1 = doc_updated["plz"] + plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + vorgangsid = doc_updated["vorgangsID"] + + # should not work without specifying + # "treat_list_as_element = True" in nested_update + self.assertEqual(plz1, "82269abcdef") + self.assertEqual(plz2, "86899abcdef") + self.assertEqual(vorgangsid, "1abcdef") + + def test_nested_alter_list_input_with_args_in_place_false(self): + + # callback functions + def callback(data, str1, str2): + return str(data) + str1 + str2 + + doc_updated = nested_alter( + self.sample_data4, + ["plz", "vorgangsID"], + callback, + function_parameters=["abc", "def"], + in_place=False, + ) + + plz1 = doc_updated["plz"] + plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + vorgangsid = doc_updated["vorgangsID"] + + # should not work without specifying + # "treat_list_as_element = True" in nested_update + self.assertEqual(plz1, "82269abcdef") + self.assertEqual(plz2, "86899abcdef") + self.assertEqual(vorgangsid, "1abcdef") + + def test_nested_alter_list_input_in_place_false(self): + + # callback functions + def callback(data): + return str(data) + "###" + + doc_updated = nested_alter( + self.sample_data4, ["plz", "vorgangsID"], callback, in_place=False + ) + + plz1 = doc_updated["plz"] + plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] + vorgangsid = doc_updated["vorgangsID"] + + # should not work without specifying + # "treat_list_as_element = True" in nested_update + self.assertEqual(plz1, "82269###") + self.assertEqual(plz2, "86899###") + self.assertEqual(vorgangsid, "1###") + + def test_nested_alter_taco_for_example(self): + documents = [{"taco": 42}, {"salsa": [{"burrito": {"taco": 69}}]}] + + # write a callback function which processes a scalar value. + # Be aware about the possible types which can be passed to + # the callback functions. + # In this example we can be sure that only int will be passed, + # in production you should check the type because it could be + # anything. + def callback(data): + return data + 10 # (data/100*10) + + # The alter-version only works for scalar input (one dict), + # if you need to adress a list of dicts, you have to + # manually iterate over those and pass them to + # nested_update one by one + out = [] + for elem in documents: + altered_document = nested_alter(elem, "taco", callback) + out.append(altered_document) + + self.maxDiff = None + self.assertEqual(out[0]["taco"], 52) + self.assertEqual(out[1]["salsa"][0]["burrito"]["taco"], 79) + + def test_nested_alter_work_with_right_order(self): + document = {"taco": 42, "salsa": [{"burrito": {"key": 20}}], "key": 50} + + def callback(data): + return data + 100 + + altered_document = nested_alter(document, "key", callback, in_place=True) + + self.assertEqual(altered_document["salsa"][0]["burrito"]["key"], 120) + self.assertEqual(altered_document["key"], 150) + + def test_sample_data4(self): + + result = { + "modelversion": "1.1.0", + "vorgangsID": "1", + "versorgungsvorschlagDatum": 1510558834978, + "eingangsdatum": 1510558834978, + "plz": 82270, + "vertragsteile": [ + { + "typ": "1", + "beitragsDaten": { + "endalter": 85, + "brutto": 58.76, + "netto": 58.76, + "zahlungsrhythmus": "MONATLICH", + "plz": 86900, + }, + "beginn": 1512082800000, + "lebenslang": "True", + "ueberschussverwendung": { + "ueberschussverwendung": "2", + "indexoption": "3", + }, + "deckung": [ + { + "typ": "2", + "art": "1", + "leistung": {"value": 7500242424.0, "einheit": "2"}, + "leistungsRhythmus": "1", + } + ], + "zuschlagNachlass": [], + }, + { + "typ": "1", + "beitragsDaten": { + "endalter": 85, + "brutto": 0.6, + "netto": 0.6, + "zahlungsrhythmus": "1", + }, + "zuschlagNachlass": [], + }, + ], + } + + # add +1 to all plz + def callback(data): + return data + 1 + + self.maxDiff = None + self.assertEqual(result, nested_alter(self.sample_data4, "plz", callback)) diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py index 853537c..976c613 100644 --- a/tests/test_parse_value.py +++ b/tests/test_parse_value.py @@ -2,7 +2,7 @@ from datetime import datetime import pytest -from config_file.parsers.parse_value import ( +from config_file.parse_value import ( can_be_parsed_as_bool, can_be_parsed_as_dict, can_be_parsed_as_float, -- cgit v1.2.3 From ffaad214ad4e5e1e966b0f32675a1cdd93e3b728 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 20:42:35 -0500 Subject: Update virtualenv dependency in lockfile --- poetry.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/poetry.lock b/poetry.lock index d8e7f36..1d2ab7b 100644 --- a/poetry.lock +++ b/poetry.lock @@ -782,7 +782,7 @@ description = "Virtual Python Environment builder" name = "virtualenv" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" -version = "20.0.28" +version = "20.0.30" [package.dependencies] appdirs = ">=1.4.3,<2" @@ -1232,8 +1232,8 @@ urllib3 = [ {file = "urllib3-1.25.10.tar.gz", hash = "sha256:91056c15fa70756691db97756772bb1eb9678fa585d9184f24534b100dc60f4a"}, ] virtualenv = [ - {file = "virtualenv-20.0.28-py2.py3-none-any.whl", hash = "sha256:8f582a030156282a9ee9d319984b759a232b07f86048c1d6a9e394afa44e78c8"}, - {file = "virtualenv-20.0.28.tar.gz", hash = "sha256:688a61d7976d82b92f7906c367e83bb4b3f0af96f8f75bfcd3da95608fe8ac6c"}, + {file = "virtualenv-20.0.30-py2.py3-none-any.whl", hash = "sha256:8cd7b2a4850b003a11be2fc213e206419efab41115cc14bca20e69654f2ac08e"}, + {file = "virtualenv-20.0.30.tar.gz", hash = "sha256:7b54fd606a1b85f83de49ad8d80dbec08e983a2d2f96685045b262ebc7481ee5"}, ] wcwidth = [ {file = "wcwidth-0.2.5-py2.py3-none-any.whl", hash = "sha256:beb4802a9cebb9144e99086eff703a642a13d6a0052920003a230f3294bbe784"}, -- cgit v1.2.3 From 040359d1bba7de17033d932024c36e1f5437f516 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 20:45:14 -0500 Subject: Update changelog to remove old info --- CHANGELOG.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 037dcc8..0b288a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,24 +9,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- A generic `ConfigFileError`. -- More type hints from `typings` have been added to `ConfigFile` for better type information. -- `YamlParser` and `TomlParser`. This means `ConfigFile` can now accept yaml and toml files, - as long as `PyYaml` is also installed if you're trying to use a yaml file and `toml` is - installed if you're trying to use a toml file. +- `ConfigFile` can now accept yaml and toml files as optional extra file types. + As long as the extras (`yaml` and `toml`) are installed, those file types can also + be used. ### Changed -- `ParsingError` now inherits from `ConfigFileError` rather than `Exception`. -- Errors have been moved to `exceptions.py`. -- The `get_all`, `set_all`, and `delete_all` optional parameter options in `JsonParser` have - been renamed to just `all`. This shouldn't have any effect since `JsonParser` is not intended - to be used directly, and that option was not in `ConfigFile`. - `path` on the `ConfigFile` is now a property that can only be retrieved. `contents` is now private but you can use `.stringify()` instead to get the contents. `parser` is now also private. + - `restore_original` in `ConfigFile` now raises a `FileNotFoundError` instead of an `OSError` if the original file path does not exist. +### Fixed + +- `default` in `ConfigFile`'s `get` method can now be `None`. Previously, it was defaulted to the + value `None` so there was no way of distinguishing between the default value and a user inputted + value of `None`. + ## 0.10.0 - 2020-05-10 ### Changed -- cgit v1.2.3 From d9a2ade40e72176449433782957bf503d69cc1d8 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 20:59:42 -0500 Subject: Remove custom parser and add ability to index ConfigFile --- config_file/config_file.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/config_file/config_file.py b/config_file/config_file.py index d047560..0e67794 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -1,9 +1,7 @@ -import inspect from pathlib import Path from shutil import copyfile -from typing import Any, Optional, Type, Union +from typing import Any, Union -from config_file.abstract_parser import AbstractParser from config_file.config_file_path import ConfigFilePath from config_file.exceptions import ParsingError from config_file.parse_value import parse_value @@ -11,9 +9,7 @@ from config_file.utils import Default class ConfigFile: - def __init__( - self, file_path: Union[str, Path], parser: Optional[Type[AbstractParser]] = None - ) -> None: + def __init__(self, file_path: Union[str, Path]) -> None: """ Stores the config file path and expands it if needed, reads in the file contents, and determines what parser should be used for @@ -21,7 +17,6 @@ class ConfigFile: 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 @@ -29,11 +24,7 @@ class ConfigFile: FileNotFoundError: If the specified file path does not exist. """ self.__path = ConfigFilePath(file_path).validate() - - if parser is not None and not inspect.isabstract(parser): - self.__parser = parser(self.__path.contents) - else: - self.__parser = self.__path.parser + self.__parser = self.__path.parser @property def file_path(self) -> Path: @@ -43,6 +34,15 @@ class ConfigFile: def original_file_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, -- cgit v1.2.3 From 9b612846ecc89fc1bb54400be892055cfb291659 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:01:42 -0500 Subject: Fix test that was using `parser` option --- tests/test_config_file.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 91cd0e6..8b03e47 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -11,17 +11,17 @@ SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"] @pytest.fixture(params=SUPPORTED_FILE_TYPES) def template_and_config_file(request, template_file): - def func(template_name: str = "default", parser: AbstractParser = None): + def func(template_name: str = "default"): template = template_file(request.param, template_name) - return template, ConfigFile(template, parser=parser) + return template, ConfigFile(template) return func @pytest.fixture(params=SUPPORTED_FILE_TYPES) def templated_config_file(template_and_config_file): - def func(template_name: str = "default", parser: AbstractParser = None): - return template_and_config_file(template_name, parser)[1] + def func(template_name: str = "default"): + return template_and_config_file(template_name)[1] return func -- cgit v1.2.3 From 36b8e0f288883284e2677f7873088c5a2d779d79 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:12:38 -0500 Subject: Add tests for abstract parser --- tests/test_abstract_parser.py | 50 +++++++++++++++++++++++++++++++++++++ tests/test_base_parser.py | 57 ------------------------------------------- 2 files changed, 50 insertions(+), 57 deletions(-) create mode 100644 tests/test_abstract_parser.py diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py new file mode 100644 index 0000000..d1cc4ef --- /dev/null +++ b/tests/test_abstract_parser.py @@ -0,0 +1,50 @@ +import pytest + +from config_file import AbstractParser + + +class ConcreteParser(AbstractParser): + def __init__(self, file_contents): + super().__init__(file_contents) + + def get(self, key): + super().get(key) + + def set(self, key, value): + super().set(key, value) + + def delete(self, section_key): + super().delete(section_key) + + def has(self, section_key: str): + super().has(section_key) + + def stringify(self): + super().stringify() + + def reset_internal_contents(self, file_contents: str) -> None: + super().reset_internal_contents(file_contents) + + +def test_that_abstract_parser_can_not_be_instantiated(): + with pytest.raises(TypeError): + AbstractParser("") + + +@pytest.mark.parametrize( + "function, args", + [ + (ConcreteParser("").get, ("",)), + (ConcreteParser("").set, ("", "")), + (ConcreteParser("").delete, ("",)), + (ConcreteParser("").stringify, None), + (ConcreteParser("").has, ("",)), + (ConcreteParser("").reset_internal_contents, ("",)), + ], +) +def test_that_abstract_parser_raises_not_implemented_errors(function, args): + with pytest.raises(NotImplementedError): + if args is None: + function() + else: + function(*args) diff --git a/tests/test_base_parser.py b/tests/test_base_parser.py index 90d45f4..e69de29 100644 --- a/tests/test_base_parser.py +++ b/tests/test_base_parser.py @@ -1,57 +0,0 @@ -# import pytest -# -# from config_file.parsers import BaseParser -# - -# class ConcreteParser(AbstractParser): -# def __init__(self, file_contents): -# super().__init__(file_contents) -# -# def get(self, key, parse_types=True): -# super().get(key, parse_types=parse_types) -# -# def set(self, key, value): -# super().set(key, value) -# -# def delete(self, section_key): -# super().delete(section_key) -# -# def has(self, section_key: str) -> bool: -# super().has(section_key) -# -# def stringify(self) -> str: -# super().stringify() -# -# def parse(self, file_contents: str, call_super=False): -# if call_super: -# super().parse(file_contents) -# -# return file_contents -# -# -# def test_that_base_parser_can_not_be_instantiated(): -# with pytest.raises(TypeError): -# AbstractParser("") -# -# -# @pytest.mark.parametrize( -# "function, args", -# [ -# (ConcreteParser("").get, ""), -# (ConcreteParser("").set, ("", "")), -# (ConcreteParser("").delete, ""), -# (ConcreteParser("").stringify, None), -# (ConcreteParser("").has, ""), -# (ConcreteParser("").parse, "call super"), -# ], -# ) -# def test_that_base_parser_raises_not_implemented_errors(function, args): -# with pytest.raises(NotImplementedError): -# if args is None: -# function() -# elif type(args) is tuple: -# function(*args) -# elif args == "call super": -# function(args, call_super=True) -# else: -# function(args) -- cgit v1.2.3 From 5ce47e47da9b6b5c55afa732ffbed787493d779d Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:14:07 -0500 Subject: Remove custom parser test --- tests/test_custom_parser_with_config_file.py | 31 ---------------------------- 1 file changed, 31 deletions(-) delete mode 100644 tests/test_custom_parser_with_config_file.py diff --git a/tests/test_custom_parser_with_config_file.py b/tests/test_custom_parser_with_config_file.py deleted file mode 100644 index 3c8bf98..0000000 --- a/tests/test_custom_parser_with_config_file.py +++ /dev/null @@ -1,31 +0,0 @@ -# def test_that_custom_parser_can_be_used(template_and_config_file): -# class CustomParser(AbstractParser): -# def __init__(self, file_contents): -# super().__init__(file_contents) -# -# def parse(self, file_contents: str): -# return file_contents -# -# def get(self, key, parse_types=True): -# return key -# -# def set(self, key, value): -# return key, value -# -# def delete(self, section_key): -# return section_key -# -# def has(self, section_key: str) -> bool: -# return True -# -# def stringify(self) -> str: -# return str(self.parsed_content) -# -# template_file, config = template_and_config_file(parser=CustomParser) -# -# assert isinstance(config._ConfigFile__parser, CustomParser) -# assert config.get("key") == "key" -# assert config.set("key", "value") == ("key", "value") -# assert config.delete("section_key") == "section_key" -# assert config.has("blah") -# assert config.stringify() == template_file.read_text() -- cgit v1.2.3 From fb34cb2b000e425c152bc7d4de5c5227bed138e4 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:15:47 -0500 Subject: Remove AbstractParser from __init__ --- config_file/__init__.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/config_file/__init__.py b/config_file/__init__.py index d78baaf..85d07ad 100644 --- a/config_file/__init__.py +++ b/config_file/__init__.py @@ -1,9 +1,5 @@ -from .abstract_parser import AbstractParser from .config_file import ConfigFile __version__ = "0.10.0" -__all__ = [ - "ConfigFile", - "AbstractParser", -] +__all__ = ["ConfigFile"] -- cgit v1.2.3 From 1fdde7b696674546220b85bb3ffd2994e93fec3a Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:27:26 -0500 Subject: Add `wild` to AbstractParser --- config_file/abstract_parser.py | 3 ++- config_file/config_file.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/config_file/abstract_parser.py b/config_file/abstract_parser.py index ed9430b..ab5d205 100644 --- a/config_file/abstract_parser.py +++ b/config_file/abstract_parser.py @@ -100,7 +100,7 @@ class AbstractParser(ABC): raise NotImplementedError @abstractmethod - def has(self, key: str) -> bool: + 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 @@ -110,6 +110,7 @@ class AbstractParser(ABC): 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. diff --git a/config_file/config_file.py b/config_file/config_file.py index 0e67794..d66cc48 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -137,7 +137,7 @@ class ConfigFile: 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. + will look at the outer most hierarchy of the file for it. With `wild`, that single word `key` will be searched for throughout the entire file. -- cgit v1.2.3 From a933dc9c424e68b1773d8af3140c7d60422b428b Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:28:44 -0500 Subject: Fix AbstractParser imports for tests --- tests/conftest.py | 3 ++- tests/test_abstract_parser.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index faf1630..3ec303e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,7 +3,8 @@ from typing import Callable, Tuple, Type, Union import pytest -from config_file import AbstractParser, ConfigFile +from config_file import ConfigFile +from config_file.abstract_parser import AbstractParser from config_file.config_file_path import ConfigFilePath diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py index d1cc4ef..890b173 100644 --- a/tests/test_abstract_parser.py +++ b/tests/test_abstract_parser.py @@ -1,6 +1,6 @@ import pytest -from config_file import AbstractParser +from config_file.abstract_parser import AbstractParser class ConcreteParser(AbstractParser): -- cgit v1.2.3 From e8a7c789e80df6c1ee72a5c4e4d028a57ab52079 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:36:41 -0500 Subject: Remove BaseParser test It is a big redundant. The ConfigFile is a superset of it, which should test everything in it. --- tests/test_base_parser.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 tests/test_base_parser.py diff --git a/tests/test_base_parser.py b/tests/test_base_parser.py deleted file mode 100644 index e69de29..0000000 -- cgit v1.2.3 From 924896167d69d753e0150f6089a0ca3d931f0169 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:37:13 -0500 Subject: Fix parse_value test docstring paths --- tests/test_parse_value.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py index 976c613..7956a06 100644 --- a/tests/test_parse_value.py +++ b/tests/test_parse_value.py @@ -27,7 +27,7 @@ from config_file.parse_value import ( ) def test_parse_value_parses_ints_floats_and_bools(test_input, expected): """ - config_file.parsers.parse_value + config_file.parse_value.parse_value Parses string values that contain ints, floats, and booleans into their native representation. @@ -55,7 +55,7 @@ def test_parse_value_parses_ints_floats_and_bools(test_input, expected): ) def test_parse_value_parses_dicts_and_lists(test_input, expected): """ - config_file.parsers.parse_value + config_file.parse_value.parse_value Parses string values that contain dictionaries or lists into their native representation. @@ -66,7 +66,7 @@ def test_parse_value_parses_dicts_and_lists(test_input, expected): @pytest.mark.parametrize("test_input", ["blah", datetime(2020, 1, 1)]) def test_parse_value_returns_values_that_cannot_be_parsed(test_input): """ - config_file.parsers.parse_value + config_file.parse_value.parse_value If a value is given that cannot be parsed by parse_value, it should simply return that value back. @@ -80,7 +80,7 @@ def test_parse_value_returns_values_that_cannot_be_parsed(test_input): ) def test_parse_value_returns_values_that_are_already_in_their_native_type(test_input): """ - config_file.parsers.parse_value + config_file.parse_value.parse_value Returns back the inputted value if it is already in its native type. """ @@ -90,7 +90,7 @@ def test_parse_value_returns_values_that_are_already_in_their_native_type(test_i @pytest.mark.parametrize("value", [-1, 0, 1, "1", "-1", " 0 "]) def test_values_that_can_be_parsed_as_int(value): """ - config_file.parsers.can_be_parsed_as_int + config_file.parse_value.can_be_parsed_as_int Returns True for values that can be parsed as an int. """ @@ -102,7 +102,7 @@ def test_values_that_can_be_parsed_as_int(value): ) def test_values_that_cannot_be_parsed_as_int(value): """ - config_file.parsers.can_be_parsed_as_int + config_file.parse_value.can_be_parsed_as_int Returns False for values that cannot be parsed as an int. """ @@ -112,7 +112,7 @@ def test_values_that_cannot_be_parsed_as_int(value): @pytest.mark.parametrize("value", [5.5, -1.2, ".1", "5.5", " 1.1 ", "-0.0", "0.0"]) def test_values_that_can_be_parsed_as_float(value): """ - config_file.parsers.can_be_parsed_as_float + config_file.parse_value.can_be_parsed_as_float Returns True for values that can be parsed as an float. """ @@ -139,7 +139,7 @@ def test_values_that_can_be_parsed_as_float(value): ) def test_values_that_cannot_be_parsed_as_float(value): """ - config_file.parsers.can_be_parsed_as_float + config_file.parse_value.can_be_parsed_as_float Returns False for values that cannot be parsed as an float. """ @@ -151,7 +151,7 @@ def test_values_that_cannot_be_parsed_as_float(value): ) def test_values_that_can_be_parsed_as_bool(value): """ - config_file.parsers.can_be_parsed_as_bool + config_file.parse_value.can_be_parsed_as_bool Returns True for values that can be parsed as a bool. """ @@ -161,7 +161,7 @@ def test_values_that_can_be_parsed_as_bool(value): @pytest.mark.parametrize("value", [0, 1, -1, 5.3, "ruff", {}, [], datetime.now()]) def test_values_that_cannot_be_parsed_as_bool(value): """ - config_file.parsers.can_be_parsed_as_bool + config_file.parse_value.can_be_parsed_as_bool Returns False for values that cannot be parsed as a bool. """ @@ -171,7 +171,7 @@ def test_values_that_cannot_be_parsed_as_bool(value): @pytest.mark.parametrize("value", [{}, {"hello": 5}, "{}", " { } ", "{'test': 5}"]) def test_values_that_can_be_parsed_as_dict(value): """ - config_file.parsers.can_be_parsed_as_dict + config_file.parse_value.can_be_parsed_as_dict Returns True for values that can be parsed as a dict. """ @@ -181,7 +181,7 @@ def test_values_that_can_be_parsed_as_dict(value): @pytest.mark.parametrize("value", [0, 5, -1, 5.5, [], "ruff", " { invalid } "]) def test_values_that_cannot_be_parsed_as_dict(value): """ - config_file.parsers.can_be_parsed_as_dict + config_file.parse_value.can_be_parsed_as_dict Returns False for values that cannot be parsed as a dict. """ @@ -191,7 +191,7 @@ def test_values_that_cannot_be_parsed_as_dict(value): @pytest.mark.parametrize("value", [[], [1, 2, True, 5.5], "[]", " [] ", " [1, 2, 3]"]) def test_values_that_can_be_parsed_as_list(value): """ - config_file.parsers.can_be_parsed_as_list + config_file.parse_value.can_be_parsed_as_list Returns True for values that can be parsed as a list. """ @@ -201,7 +201,7 @@ def test_values_that_can_be_parsed_as_list(value): @pytest.mark.parametrize("value", [0, 5, -1, 5.5, {}, "ruff", " [ invalid ] "]) def test_values_that_cannot_be_parsed_as_list(value): """ - config_file.parsers.can_be_parsed_as_list + config_file.parse_value.can_be_parsed_as_list Returns False for values that cannot be parsed as a list. """ -- cgit v1.2.3 From 3708a6e91c73858e7e5a176956e51711c4e530f3 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:38:08 -0500 Subject: Fix `get_existent_keys` test with a IniParser check --- tests/test_config_file.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 8b03e47..7e21e0b 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -5,6 +5,7 @@ import pytest from config_file.abstract_parser import AbstractParser from config_file.config_file import ConfigFile from config_file.exceptions import MissingKeyError +from config_file.ini_parser import IniParser SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"] @@ -103,21 +104,16 @@ def test_that_config_file_can_find_sections_and_keys( assert config.has(key) == expected_result -# @pytest.mark.parametrize( -# "key, value", -# [ -# ("header_one.number_key", 0), -# ("header_two.list_key", [1, 2, 3]), -# ], -# ) -# def test_that_config_file_can_get_existent_keys(templated_config_file, key, value): -# config = templated_config_file() -# -# print(config.get(key)) -# print(type(config.get(key))) -# print(config._ConfigFile__parser) -# -# assert config.get(key) == value +@pytest.mark.parametrize( + "key, value", [("header_one.number_key", 0), ("header_two.list_key", [1, 2, 3])], +) +def test_that_config_file_can_get_existent_keys(templated_config_file, key, value): + config = templated_config_file() + + if isinstance(config._ConfigFile__parser, IniParser): + value = str(value) + + assert config.get(key) == value @pytest.mark.parametrize("key", ["header_one.number_key", "header_one"]) -- cgit v1.2.3 From 4baf3f13f6bd1a4a21b249969b3b311948568cbb Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:38:37 -0500 Subject: Add docstrings to AbstractParser tests --- tests/test_abstract_parser.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py index 890b173..a2d9dbe 100644 --- a/tests/test_abstract_parser.py +++ b/tests/test_abstract_parser.py @@ -4,6 +4,12 @@ from config_file.abstract_parser import AbstractParser class ConcreteParser(AbstractParser): + """ + ConcreteParser is a concrete implementation of the + AbstractParser that just calls the AbstractParser's + methods so we can ensure they raise a NotImplementedError. + """ + def __init__(self, file_contents): super().__init__(file_contents) @@ -27,6 +33,12 @@ class ConcreteParser(AbstractParser): def test_that_abstract_parser_can_not_be_instantiated(): + """ + config_file.abstract_parser.abstract_parser + + Ensure the AbstractParser raises a TypeError when it is + instantiated. + """ with pytest.raises(TypeError): AbstractParser("") @@ -43,6 +55,12 @@ def test_that_abstract_parser_can_not_be_instantiated(): ], ) def test_that_abstract_parser_raises_not_implemented_errors(function, args): + """ + config_file.abstract_parser.abstract_parser + + Ensure the AbstractParser raises a NotImplementedError + for every method in it. + """ with pytest.raises(NotImplementedError): if args is None: function() -- cgit v1.2.3 From 19d23dc13636b30f6282e8b3f02b659bcdb2bb11 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:48:08 -0500 Subject: Remove MissingDependencyError in favor of ImportError --- config_file/config_file_path.py | 2 +- config_file/exceptions.py | 7 ------- config_file/toml_parser.py | 3 +-- config_file/yaml_parser.py | 3 +-- 4 files changed, 3 insertions(+), 12 deletions(-) diff --git a/config_file/config_file_path.py b/config_file/config_file_path.py index 9f2b87d..42466d9 100644 --- a/config_file/config_file_path.py +++ b/config_file/config_file_path.py @@ -102,7 +102,7 @@ class ConfigFilePath(_Path_): ValueError: If the path leads to a directory. Returns: - This ConfigFilePath object + A ConfigFilePath object with any ~'s expanded. """ if len(self.parts) >= 1 and self.parts[0] == "~": self = ConfigFilePath(self.expanduser()) diff --git a/config_file/exceptions.py b/config_file/exceptions.py index 22ee70c..a8b8e5c 100644 --- a/config_file/exceptions.py +++ b/config_file/exceptions.py @@ -2,13 +2,6 @@ class ConfigFileError(Exception): """A generic error from the package.""" -class MissingDependencyError(ConfigFileError): - """ - yaml or toml dependencies were not installed, - but the parsers for them were attempted to be used. - """ - - class UnsupportedFileTypeError(ConfigFileError): """ A missing file extension or a file type not supported. diff --git a/config_file/toml_parser.py b/config_file/toml_parser.py index 1c7f8a7..fc12ab3 100644 --- a/config_file/toml_parser.py +++ b/config_file/toml_parser.py @@ -5,7 +5,6 @@ try: except ImportError: TOML_AVAILABLE = False -from config_file.exceptions import MissingDependencyError from .base_parser import BaseParser @@ -13,7 +12,7 @@ from .base_parser import BaseParser class TomlParser(BaseParser): def __init__(self, file_contents: str): if not TOML_AVAILABLE: - raise MissingDependencyError( + 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/yaml_parser.py b/config_file/yaml_parser.py index fb42e08..938b0d7 100644 --- a/config_file/yaml_parser.py +++ b/config_file/yaml_parser.py @@ -6,13 +6,12 @@ except ImportError: YAML_AVAILABLE = False from .base_parser import BaseParser -from .exceptions import MissingDependencyError class YamlParser(BaseParser): def __init__(self, file_contents: str): if not YAML_AVAILABLE: - raise MissingDependencyError( + 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]`." -- cgit v1.2.3 From d2f3161dac2313e4109ba2159fdc0d43e50506f8 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 6 Aug 2020 09:43:44 -0500 Subject: Test extras parsers & move imports inside those parsers --- config_file/toml_parser.py | 25 ++++++++----------------- config_file/yaml_parser.py | 27 +++++++++++---------------- tests/test_toml_parser.py | 24 ++++++++++++++++++++++++ tests/test_yaml_parser.py | 24 ++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 33 deletions(-) diff --git a/config_file/toml_parser.py b/config_file/toml_parser.py index fc12ab3..d3cf226 100644 --- a/config_file/toml_parser.py +++ b/config_file/toml_parser.py @@ -1,27 +1,18 @@ -try: - import tomlkit - - TOML_AVAILABLE = True -except ImportError: - TOML_AVAILABLE = False - - from .base_parser import BaseParser class TomlParser(BaseParser): def __init__(self, file_contents: str): - if not TOML_AVAILABLE: + 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]`." ) - - super().__init__( - file_contents=file_contents, - module=tomlkit, - # tomlkit doesn't expose it's exceptions, but - # it's parsing error is a subclass of ValueError - decode_error=ValueError, - ) diff --git a/config_file/yaml_parser.py b/config_file/yaml_parser.py index 938b0d7..8d88881 100644 --- a/config_file/yaml_parser.py +++ b/config_file/yaml_parser.py @@ -1,26 +1,21 @@ -try: - from ruamel.yaml import YAML, YAMLError, round_trip_dump, round_trip_load - - YAML_AVAILABLE = True -except ImportError: - YAML_AVAILABLE = False - from .base_parser import BaseParser class YamlParser(BaseParser): def __init__(self, file_contents: str): - if not YAML_AVAILABLE: + 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]`." ) - - 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 - ) diff --git a/tests/test_toml_parser.py b/tests/test_toml_parser.py index e69de29..845e78e 100644 --- a/tests/test_toml_parser.py +++ b/tests/test_toml_parser.py @@ -0,0 +1,24 @@ +from unittest.mock import patch + +import pytest + +from config_file.toml_parser import TomlParser + + +def test_that_import_error_is_raised_if_no_tomlkit(): + """ + config_file.toml_parser.TomlParser + + Ensure an import error is raised if tomlkit is not + installed. + """ + with pytest.raises(ImportError) as error: + + with patch("builtins.__import__") as mock_import: + mock_import.side_effect = ImportError + TomlParser("") + + assert ( + "Install the `toml` extra first with `pip install config-file[toml]`." + in str(error) + ) diff --git a/tests/test_yaml_parser.py b/tests/test_yaml_parser.py index e69de29..1f46692 100644 --- a/tests/test_yaml_parser.py +++ b/tests/test_yaml_parser.py @@ -0,0 +1,24 @@ +from unittest.mock import patch + +import pytest + +from config_file.yaml_parser import YamlParser + + +def test_that_import_error_is_raised_if_no_ruamelyaml(): + """ + config_file.yaml_parser.YamlParser + + Ensure an import error is raised if ruamel.yaml is not + installed. + """ + with pytest.raises(ImportError) as error: + + with patch("builtins.__import__") as mock_import: + mock_import.side_effect = ImportError + YamlParser("") + + assert ( + "Install the `yaml` extra first with `pip install config-file[yaml]`." + in str(error) + ) -- cgit v1.2.3 From 3a6a03805abeb8aa5ab080f79a94d65d02421c7f Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 6 Aug 2020 09:46:04 -0500 Subject: Update README example to remove MissingKeyError --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2b51ac0..96a27af 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,8 @@ for the package to recognize it and use the correct parser for it. ### Setting up ConfigFile -To use the package, we import in the `ConfigFile` object. +To use the package, we import in the `ConfigFile` object. This object +is the only thing considered to be part of the public API. We can set it up by giving it a string or `pathlib.Path` as the argument. Any home tildes `~` in the string or `Path` are recognized and converted @@ -132,11 +133,9 @@ There are two ways we could handle that. The first is the one we're used to seeing: catch the error. ```python -from config_file import MissingKeyError - try: important_value = config.get('section.i_do_not_exist') -except MissingKeyError: +except KeyError: important_value = 42 ``` -- cgit v1.2.3 From 836cefe45633331d0e5692295174bf3181a5e365 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 6 Aug 2020 09:47:35 -0500 Subject: Change MissingKeyErrors to KeyError --- config_file/abstract_parser.py | 4 ++-- config_file/base_parser.py | 4 ++-- tests/test_abstract_parser.py | 4 ++-- tests/test_config_file.py | 5 ++--- tests/test_ini_parser.py | 6 +++--- 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/config_file/abstract_parser.py b/config_file/abstract_parser.py index ab5d205..689fece 100644 --- a/config_file/abstract_parser.py +++ b/config_file/abstract_parser.py @@ -57,7 +57,7 @@ class AbstractParser(ABC): Whatever value the given key happens to contain. Raises: - MissingKeyError: If the key to retrieve does not exist. + KeyError: If the key to retrieve does not exist. """ raise NotImplementedError @@ -81,7 +81,7 @@ class AbstractParser(ABC): key: The key we would like to remove. Raises: - MissingKeyError: If the key to remove does not exist. + KeyError: If the key to remove does not exist. """ raise NotImplementedError diff --git a/config_file/base_parser.py b/config_file/base_parser.py index a987a55..a440dcb 100644 --- a/config_file/base_parser.py +++ b/config_file/base_parser.py @@ -2,7 +2,7 @@ from types import ModuleType from typing import Any, Type from config_file.abstract_parser import AbstractParser -from config_file.exceptions import MissingKeyError, ParsingError +from config_file.exceptions import ParsingError from config_file.nested_lookup import get_occurrence_of_key from config_file.utils import split_on_dot @@ -123,7 +123,7 @@ class BaseParser(AbstractParser): content_reference = content_reference[key] except (KeyError, TypeError): - raise MissingKeyError(f"The specified key '{key}' to delete was not found.") + raise KeyError(f"The specified key '{key}' to delete was not found.") def stringify(self) -> str: return self.__module.dumps(self.parsed_content) diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py index a2d9dbe..1816f6b 100644 --- a/tests/test_abstract_parser.py +++ b/tests/test_abstract_parser.py @@ -34,7 +34,7 @@ class ConcreteParser(AbstractParser): def test_that_abstract_parser_can_not_be_instantiated(): """ - config_file.abstract_parser.abstract_parser + config_file.abstract_parser.AbstractParser Ensure the AbstractParser raises a TypeError when it is instantiated. @@ -56,7 +56,7 @@ def test_that_abstract_parser_can_not_be_instantiated(): ) def test_that_abstract_parser_raises_not_implemented_errors(function, args): """ - config_file.abstract_parser.abstract_parser + config_file.abstract_parser.AbstractParser Ensure the AbstractParser raises a NotImplementedError for every method in it. diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 7e21e0b..023b6ae 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -4,7 +4,6 @@ import pytest from config_file.abstract_parser import AbstractParser from config_file.config_file import ConfigFile -from config_file.exceptions import MissingKeyError from config_file.ini_parser import IniParser SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"] @@ -126,10 +125,10 @@ def test_that_config_file_can_delete(templated_config_file, key): @pytest.mark.parametrize("key", ["", 0, False, {}, "header_one.does_not_exist"]) -def test_that_config_file_raises_missing_key_error_on_invalid_input_or_missing_key( +def test_that_config_file_raises_key_error_on_invalid_input_or_missing_key( templated_config_file, key ): config = templated_config_file() - with pytest.raises(MissingKeyError): + with pytest.raises(KeyError): config.delete(key) diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index 24507b7..2accc8f 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,6 +1,6 @@ import pytest -from config_file.exceptions import MissingKeyError, ParsingError +from config_file.exceptions import ParsingError from config_file.ini_parser import IniParser @@ -81,10 +81,10 @@ def test_that_ini_parser_can_delete(section_key, expected_result): @pytest.mark.parametrize( "section_key", ["section_is_not_present", "test_section.but_key_is_not_present"], ) -def test_that_ini_parser_delete_raises_missing_key_errors(section_key): +def test_that_ini_parser_delete_raises_key_error(section_key): ini_file = """[test_section]\nkey1 = value\n\n""" parser = IniParser(ini_file) - with pytest.raises(MissingKeyError): + with pytest.raises(KeyError): parser.delete(section_key) -- cgit v1.2.3 From c57bc17e43561c745a9913cb21a90c84db684ef0 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 6 Aug 2020 12:29:28 -0500 Subject: Add test for Default util --- tests/test_utils.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 6779d18..1f22e0b 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,6 +1,6 @@ import pytest -from config_file.utils import split_on_dot +from config_file.utils import Default, split_on_dot @pytest.mark.parametrize( @@ -17,3 +17,13 @@ def test_that_split_on_dot_splits_the_line_correctly( def test_that_split_on_dot_raises_error_when_there_is_no_dot(test_input): with pytest.raises(ValueError): split_on_dot(test_input) + + +def test_that_default_is_initialized_correctly(): + default = Default(5) + assert default.value == 5 + + +def test_that_default_has_the_correct_repr_format(): + default = Default("test") + assert str(default) == "Default Value: test ()" -- cgit v1.2.3 From 286cec0d1c05b6b85f49714ec4635d2e8dcacfc7 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 6 Aug 2020 19:03:53 -0500 Subject: Remove ini and json parser tests They heavily overlap with the config_file test case now and are unnecessary. --- tests/test_ini_parser.py | 97 ----------------------------------------- tests/test_json_parser.py | 109 ---------------------------------------------- 2 files changed, 206 deletions(-) delete mode 100644 tests/test_ini_parser.py delete mode 100644 tests/test_json_parser.py diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py deleted file mode 100644 index 2accc8f..0000000 --- a/tests/test_ini_parser.py +++ /dev/null @@ -1,97 +0,0 @@ -import pytest - -from config_file.exceptions import ParsingError -from config_file.ini_parser import IniParser - - -@pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"]) -def test_incorrect_ini_formats(ini_file): - with pytest.raises(ParsingError): - IniParser(ini_file) - - -@pytest.mark.parametrize( - "section_key,expected_value", - [ - ("test_section.intkey", "5"), - ("test_section.strkey", "blah"), - ("test_section.boolkey", "false"), - ("test_section.floatkey", "5.3"), - ( - "test_section", - {"intkey": "5", "strkey": "blah", "boolkey": "false", "floatkey": "5.3"}, - ), - ], -) -def test_that_ini_parser_can_get_values(section_key, expected_value): - ini_file = """[test_section] -intkey = 5 -strkey = blah -boolkey = false -floatkey = 5.3 - -""" - parser = IniParser(ini_file) - assert parser.get(section_key) == expected_value - - -@pytest.mark.parametrize( - "test_input", ["invalid.invalid_key"], -) -def test_that_getting_unknown_sections_or_keys_throws_errors(test_input): - with pytest.raises(KeyError): - IniParser("").get(test_input) - - -@pytest.mark.parametrize( - "section_key,value,expected_value", - [ - ("test.key1", "different value", "[test]\nkey1 = different value\n\n"), - ("test.key2", 5, "[test]\nkey1 = value1\nkey2 = 5\n\n"), - ("test2.key", False, "[test]\nkey1 = value1\n\n[test2]\nkey = False\n\n"), - ], -) -def test_that_ini_parser_can_set_values(section_key, value, expected_value): - ini_file = """[test]\nkey1 = value1\n\n""" - parser = IniParser(ini_file) - parser.set(section_key, value) - assert parser.stringify() == expected_value - - -@pytest.mark.parametrize( - "ini_file", - ["[test]\nkey1 = value1\n\n", "[test]\nblah = 5\n\n[section2]\nkey = 2\n\n", ""], -) -def test_that_ini_parser_can_be_stringified(ini_file): - parser = IniParser(ini_file) - assert parser.stringify() == ini_file - - -@pytest.mark.parametrize( - "section_key,expected_result", - [("test_section", ""), ("test_section.key1", "[test_section]\n\n")], -) -def test_that_ini_parser_can_delete(section_key, expected_result): - ini_file = """[test_section]\nkey1 = value\n\n""" - parser = IniParser(ini_file) - parser.delete(section_key) - assert parser.stringify() == expected_result - - -@pytest.mark.parametrize( - "section_key", ["section_is_not_present", "test_section.but_key_is_not_present"], -) -def test_that_ini_parser_delete_raises_key_error(section_key): - ini_file = """[test_section]\nkey1 = value\n\n""" - parser = IniParser(ini_file) - with pytest.raises(KeyError): - parser.delete(section_key) - - -@pytest.mark.parametrize( - "section_key", ["test_section", "test_section.key1"], -) -def test_that_ini_parser_can_check_its_section_and_keys(section_key): - ini_file = """[test_section]\nkey1 = value\n\n""" - parser = IniParser(ini_file) - assert parser.has(section_key) diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py deleted file mode 100644 index 54ea646..0000000 --- a/tests/test_json_parser.py +++ /dev/null @@ -1,109 +0,0 @@ -import pytest - -from config_file.exceptions import ParsingError - - -def test_invalid_json(templated_parser): - with pytest.raises(ParsingError): - templated_parser("json", "invalid") - - -def test_that_json_parser_can_stringify(template_and_parser): - template, parser = template_and_parser("json") - - assert parser.stringify() == template.read_text() - - -@pytest.mark.parametrize( - "key", - [ - "glossary", - "title", - "GlossDiv", - "title", - "GlossList", - "GlossEntry", - "ID", - "GlossTerm", - "Acronym", - "GlossDef", - "para", - "GlossSeeAlso", - "GlossSee", - ], -) -def test_that_json_parser_can_find_wild_keys(key, templated_parser): - parser = templated_parser("json", "glossary") - assert parser.has(key, wild=True) - - -@pytest.mark.parametrize( - "key", - [ - "glossary", - "glossary.title", - "glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para", - ], -) -def test_that_json_parser_can_find_dot_keys(key, templated_parser): - parser = templated_parser("json", "glossary") - assert parser.has(key) - - -@pytest.mark.parametrize( - "value", - [ - "str_value", - "5", - "false", - "Standard Generalized Markup Language", - "ISO 8879:1986", - "A meta-markup language, used to create markup languages such as DocBook.", - "GML", - "XML", - "5.5", - ], -) -def test_that_json_parser_does_not_find_values(value, templated_parser): - parser = templated_parser("json", "glossary") - assert not parser.has(value) - - -@pytest.mark.parametrize( - "key, value", - [ - ("glossary.title", "str_value"), - ("glossary.dict", {"key": "str_value", "str_number": "5"}), - ], -) -def test_that_json_parser_can_get_keys_without_parsing_types( - templated_parser, key, value -): - parser = templated_parser("json", "glossary") - assert parser.get(key) == value - - -@pytest.mark.parametrize( - "key, value", [("zip", "zipzip"), ("foo.bar.foobar", "5"), ("foo.baz", "bar")] -) -def test_that_json_parser_can_set_keys(templated_parser, key, value): - parser = templated_parser("json") - - parser.set(key, value) - assert parser.get(key) == value - - -@pytest.mark.parametrize( - "key", - [ - "glossary", - "glossary.dict_within_dict", - "glossary.GlossDiv.GlossList.GlossEntry.Abbrev", - ], -) -def test_that_json_parser_can_delete_keys(template_and_parser, key): - template, parser = template_and_parser("json", "glossary") - - parser.delete(key) - - assert parser.has(key) is False -- cgit v1.2.3 From 05876de8dbb429de461832fc9106fd6514a5e97a Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 08:47:57 -0500 Subject: Add ParsingError to __init__ --- config_file/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config_file/__init__.py b/config_file/__init__.py index 85d07ad..853da2a 100644 --- a/config_file/__init__.py +++ b/config_file/__init__.py @@ -1,5 +1,6 @@ from .config_file import ConfigFile +from .exceptions import ParsingError __version__ = "0.10.0" -__all__ = ["ConfigFile"] +__all__ = ["ConfigFile", "ParsingError"] -- cgit v1.2.3 From c24f1741a91950480fd169a6583e6c2b675ee08e Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 08:49:03 -0500 Subject: Change UnsupportedFileTypeError to ValueError --- config_file/config_file_path.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/config_file/config_file_path.py b/config_file/config_file_path.py index 42466d9..11a5daa 100644 --- a/config_file/config_file_path.py +++ b/config_file/config_file_path.py @@ -9,7 +9,6 @@ from config_file.json_parser import JsonParser from config_file.toml_parser import TomlParser from config_file.yaml_parser import YamlParser -from .exceptions import UnsupportedFileTypeError from .utils import split_on_dot @@ -58,14 +57,13 @@ class ConfigFilePath(_Path_): """Determine what parser should be used for this file. Raises: - UnsupportedFileTypeError: If the extension of the file - is not recognized. + 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 UnsupportedFileTypeError( + raise ValueError( "Tried to determine a parser to use, but the file at " f"{self} does not have an extension." ) @@ -79,7 +77,7 @@ class ConfigFilePath(_Path_): elif self.extension == "toml": return TomlParser(self.contents) else: - raise UnsupportedFileTypeError( + raise ValueError( f"File path at `{self}` contains an unrecognized file type." ) -- cgit v1.2.3 From 32826f7d510ff4c469b2fa67d232325224a368eb Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 08:49:14 -0500 Subject: Add type hint to split_on_dot --- config_file/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config_file/utils.py b/config_file/utils.py index 3554c0e..cbbf7a7 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -2,7 +2,7 @@ from pathlib import Path from typing import List, Union -def split_on_dot(line: Union[str, Path], only_last_dot=False) -> List[str]: +def split_on_dot(line: Union[str, Path], only_last_dot: bool = False) -> List[str]: """ Split a string on the dot character (.). -- cgit v1.2.3 From b3740e39a71c4b92dfcf47653b3e6a6b0c536e57 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 08:49:47 -0500 Subject: Add utils tests --- tests/test_utils.py | 66 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 1f22e0b..663aaba 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,29 +1,73 @@ +from pathlib import Path + import pytest from config_file.utils import Default, split_on_dot -@pytest.mark.parametrize( - "test_input, only_last_dot, expected_result", - [("section.key.blah", False, ["section", "key", "blah"])], -) -def test_that_split_on_dot_splits_the_line_correctly( - test_input, only_last_dot, expected_result -): - assert split_on_dot(test_input, only_last_dot=only_last_dot) == expected_result +def test_that_split_on_dot_splits_the_line_correctly(): + """ + config_file.utils.split_on_dot + + Ensure splitting a string with dots in it returns an + array of strings split on those dots. + """ + assert split_on_dot("section.key.blah") == ["section", "key", "blah"] + + +def test_that_split_on_dot_can_only_split_on_the_last_one(): + """ + config_file.utils.split_on_dot + + Ensure that the only_last_dot parameter only splits the input + on the last dot. + """ + assert split_on_dot("test.input.should.only.split.on.last", only_last_dot=True) == [ + "test.input.should.only.split.on", + "last", + ] + +def test_that_split_on_dot_raises_error_when_there_is_no_dot(): + """ + config_file.utils.split_on_dot -@pytest.mark.parametrize("test_input", ["this line has no dots in it"]) -def test_that_split_on_dot_raises_error_when_there_is_no_dot(test_input): + Ensure that a ValueError is raised if the input to split_on_dot + has no dot (.) in it. + """ with pytest.raises(ValueError): - split_on_dot(test_input) + split_on_dot("this line has no dots in it") + + +def test_that_split_on_dot_can_accept_path_objects(): + """ + config_file.utils.split_on_dot + + Ensure that a pathlib.Path object is able to be + given to split_on_dot. + """ + split = split_on_dot(Path("some.file.path.with.dots")) + + assert split == ["some", "file", "path", "with", "dots"] def test_that_default_is_initialized_correctly(): + """ + config_file.utils.Default + + Ensure that the Default class's value attribute + is the same value that it was constructed with. + """ default = Default(5) assert default.value == 5 def test_that_default_has_the_correct_repr_format(): + """ + config_file.utils.Default + + Ensure that when the Default class is coerced to + a string, it has the correct format and output. + """ default = Default("test") assert str(default) == "Default Value: test ()" -- cgit v1.2.3 From 08a0725f7d4c34ef073110c30117ae520d081a01 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 08:50:06 -0500 Subject: Remove all custom exceptions except ParsingError --- config_file/exceptions.py | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/config_file/exceptions.py b/config_file/exceptions.py index a8b8e5c..f68b7e8 100644 --- a/config_file/exceptions.py +++ b/config_file/exceptions.py @@ -1,19 +1,9 @@ -class ConfigFileError(Exception): - """A generic error from the package.""" - - -class UnsupportedFileTypeError(ConfigFileError): +class ParsingError(ValueError): """ - A missing file extension or a file type not supported. - """ - + Unable to parse the configuration file. -class ParsingError(ConfigFileError): - """Unable to parse the configuration file.""" - - -class MissingKeyError(ParsingError): - """ - A key that was specified (to retrieve, delete, etc.) - is not in the 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. """ -- cgit v1.2.3 From c7a873ba71d1103b66c1cdf93451cb03ddf19832 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 08:58:26 -0500 Subject: Add information on indexing into ConfigFile --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 96a27af..c1d4e9a 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,14 @@ config.get('section') >>> {'num_key': '5', 'str_key': 'blah', 'bool_key': 'true', 'list_key': '[1, 2]'} ``` +Furthermore, you can also index into the `ConfigFile` object +to retrieve keys if that is preferred. + +```python +config['section']['num_key'] +>>> '5' +``` + #### Coercing the return types However, some of these keys are obviously not strings natively. @@ -181,6 +189,20 @@ dependent on the file format we're using, since every format is a little different in it's structure and in what it supports. See the [full documentation](#Documentation) for more information there. +Lastly, we can set values using an array notation as well. The underlying +content is all manipulated as a dictionary for every file type. If we wanted +to create a new section, we'd simply set it to be an empty dictionary. + +```python +config['new_section'] = {} +``` + +Which would result to be an empty section: + +```ini +[new_section] +``` + ### Using `delete()` `delete()` allows us to delete entire sections or specific keys. @@ -196,6 +218,12 @@ However, we can also just delete a single key. config.delete('section.num_key') ``` +We can also use the array notation here as well. + +```python +del config['section']['num_key'] +``` + ### Using `has()` `has()` allows us to check whether a given key exists in our file. There -- cgit v1.2.3 From 9cc7eab40cb0f922b4981a863a2a0c67a261d77c Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 09:10:28 -0500 Subject: Add ConfigFilePath test --- tests/test_config_file_path.py | 112 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/tests/test_config_file_path.py b/tests/test_config_file_path.py index e69de29..3bb7215 100644 --- a/tests/test_config_file_path.py +++ b/tests/test_config_file_path.py @@ -0,0 +1,112 @@ +import os +from pathlib import Path, _posix_flavour, _windows_flavour + +import pytest + +from config_file.config_file_path import ConfigFilePath +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 + + +def test_config_file_path_is_a_path(): + """ + config_file.config_file_path.ConfigFilePath + + Ensure that the ConfigFilePath object is an instance + of Path from pathlib. + """ + assert isinstance(ConfigFilePath("."), Path) + + +def test_config_file_path_has_the_correct_flavour(): + """ + config_file.config_file_path.ConfigFilePath + + Ensure that the ConfigFilePath object has the + correct Path flavor (since Path is actually + swapped out, depending on the operating system, + to a Posix flavour or Windows flavour) + """ + path = ConfigFilePath(".") + + assert path._flavour == _windows_flavour if os.name == "nt" else _posix_flavour + + +def test_config_file_path_is_able_to_read_the_contents(template_file): + """ + config_file.config_file_path.ConfigFilePath + + Ensure that the contents retrieved are the same + as the contents of the file. + """ + template = template_file("ini", "default") + path = ConfigFilePath(template) + + assert path.contents == template.read_text() + + +def test_config_file_path_is_able_to_calculate_the_original_path(): + """ + config_file.config_file_path.ConfigFilePath + + Ensure that the calculated original configuration + path includes a "original" between the filename and + extension. + """ + path = ConfigFilePath("path-existing-does-not-matter.txt") + + assert str(path.original_path) == "path-existing-does-not-matter.original.txt" + + +def test_config_file_path_is_able_to_retrieve_the_extension(): + """ + config_file.config_file_path.ConfigFilePath + + Ensure that the retrieved extension is correct + for the given filepath and that it does not retrieve + the dot (.) from the extension. + """ + path = ConfigFilePath("some-file.txt") + + assert path.extension == "txt" + + +@pytest.mark.parametrize( + "template_file_type, expected_parser_type", + [ + ("ini", IniParser), + ("json", JsonParser), + ("yaml", YamlParser), + ("toml", TomlParser), + ], +) +def test_config_file_path_decides_on_the_correct_parser( + template_file, template_file_type, expected_parser_type +): + """ + config_file.config_file_path.ConfigFilePath + + Ensure that a filepath with an extension that is supported + retrieves the correct parser for it. + """ + template = template_file(template_file_type, "default") + path = ConfigFilePath(template) + + assert isinstance(path.parser, expected_parser_type) + + +@pytest.mark.parametrize("filename", ["some-file", "some-file.unsupported-extension"]) +def test_config_file_path_raises_value_error_on_parser_if_invalid_extension(filename): + """ + config_file.config_file_path.ConfigFilePath + + Ensure that a ValueError is raised if we have a + filepath with no extension or a filepath with an + unsupported extension. + """ + path = ConfigFilePath(filename) + + with pytest.raises(ValueError): + path.parser -- cgit v1.2.3 From 08d7c88cae495ac8904be2367eb0d223e16ac8e8 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 09:52:11 -0500 Subject: Fix invalid reference to MissingKeyError --- config_file/config_file.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config_file/config_file.py b/config_file/config_file.py index d66cc48..2ce61ef 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -69,6 +69,7 @@ class ConfigFile: 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. """ try: @@ -110,7 +111,7 @@ class ConfigFile: key: The section, sub-section, or key to delete. Raises: - MissingKeyError: If a key is attempted to be deleted that + KeyError: If a key is attempted to be deleted that does not exist. """ self.__parser.delete(key) -- cgit v1.2.3 From 577193b1c025e18599ef1cdb91841a30b631b103 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 10:25:55 -0500 Subject: Add docstrings to config file test --- tests/test_config_file.py | 153 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 134 insertions(+), 19 deletions(-) diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 023b6ae..33f1945 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -1,4 +1,6 @@ +import json from pathlib import Path +from typing import Callable, Tuple import pytest @@ -10,7 +12,21 @@ SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"] @pytest.fixture(params=SUPPORTED_FILE_TYPES) -def template_and_config_file(request, template_file): +def template_and_config_file( + request, template_file +) -> Callable[[str], Tuple[Path, ConfigFile]]: + """ + Fixture that is a wrapper around the template_file + fixture. Using the function returned, we can retrieve + a temporary file that has the contents of the specified + template file as well as a ConfigFile with the template + already passed in. + + This fixture will make the test function run once for every + supported file type, each with the corresponding template + in the different file type. + """ + def func(template_name: str = "default"): template = template_file(request.param, template_name) return template, ConfigFile(template) @@ -19,7 +35,12 @@ def template_and_config_file(request, template_file): @pytest.fixture(params=SUPPORTED_FILE_TYPES) -def templated_config_file(template_and_config_file): +def templated_config_file(template_and_config_file) -> Callable[[str], ConfigFile]: + """ + Wrapper around template_and_config_file for when we + only need the ConfigFile and not the template. + """ + def func(template_name: str = "default"): return template_and_config_file(template_name)[1] @@ -27,14 +48,25 @@ def templated_config_file(template_and_config_file): def test_that_config_file_is_initialized_correctly(template_and_config_file): + """ + config_file.config_file.ConfigFile.__init__ + + Ensure that an initialized ConfigFile has the + correct file path and has a parser that is a AbstractParser. + """ template_file, config = template_and_config_file() assert config.file_path == template_file - assert config.stringify() == template_file.read_text() assert isinstance(config._ConfigFile__parser, AbstractParser) def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): + """ + config_file.config_file.ConfigFile.__init__ + + Ensure that a home tilde (~) in a file path passed + to ConfigFile is expanded to the full path. + """ config_name = "temp.ini" config_path = "~/" + config_name @@ -47,17 +79,34 @@ def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): @pytest.mark.parametrize("path", ["invalid", "invalid.conf"]) def test_invalid_path_raises_file_not_found_error(path): + """ + config_file.config_file.ConfigFile.__init__ + + Ensure that when a path to a file that does not exist + that is passed into the constructor, it raises a FileNotFoundError. + """ with pytest.raises(FileNotFoundError): ConfigFile(path) -@pytest.mark.parametrize("path", [""]) -def test_directory_raises_value_error(path): +def test_directory_raises_value_error(): + """ + config_file.config_file.ConfigFile.__init__ + + Ensure when a path to a directory is passed into + the constructor, it raises a ValueError. + """ with pytest.raises(ValueError): - ConfigFile(path) + ConfigFile(".") def test_that_config_file_can_save(template_and_config_file): + """ + config_file.config_file.ConfigFile.save + + Ensure that altering the configuration file and saving it + writes those changes out to the filesystem. + """ template_file, config = template_and_config_file() config.set("header_one.number_key", 25) @@ -69,6 +118,14 @@ def test_that_config_file_can_save(template_and_config_file): def test_that_config_file_can_restore_the_original( templated_config_file, template_original_file ): + """ + config_file.config_file.ConfigFile.restore_original + + Ensure that we can restore the "original" configuration + file, using the calculated path and after altering the + internal config contents. Those internal contents and file + should be reset to the original file. + """ config = templated_config_file() original_file = template_original_file(config.file_path) @@ -76,9 +133,18 @@ def test_that_config_file_can_restore_the_original( config.restore_original() assert config.stringify() == original_file.read_text() + assert config.file_path.read_text() == original_file.read_text() def test_missing_config_file_during_restore(templated_config_file): + """ + config_file.config_file.ConfigFile.restore_original + + Ensure that if we try to restore_original without using + the optional original_file_path argument but we do not + have a file at the calculated default path, a FileNotFoundError + is raised. + """ config = templated_config_file() with pytest.raises(FileNotFoundError) as error: @@ -88,35 +154,76 @@ def test_missing_config_file_during_restore(templated_config_file): assert "does not exist" in str(error) -@pytest.mark.parametrize( - "key, expected_result", - [ - ("header_one", True), - ("header_one.number_key", True), - ("header_one.blah", False), - ], -) -def test_that_config_file_can_find_sections_and_keys( - templated_config_file, key, expected_result +@pytest.mark.parametrize("key", ["header_one", "header_one.number_key"]) +def test_that_config_file_has_can_find_sections_and_keys_that_exist( + templated_config_file, key ): + """ + config_file.config_file.ConfigFile.has + + Ensure that when a section or key that exists + is passed to has(), it can find it and returns True. + """ config = templated_config_file() - assert config.has(key) == expected_result + assert config.has(key) + + +def test_that_config_file_has_returns_false_for_sections_and_keys_that_do_not_exist( + templated_config_file, +): + """ + config_file.config_file.ConfigFile.has + + Ensure that when a section or key that does not exist + is passed to has(), it can't find it and returns False. + """ + config = templated_config_file() + assert not config.has("header_one.blah") @pytest.mark.parametrize( - "key, value", [("header_one.number_key", 0), ("header_two.list_key", [1, 2, 3])], + "key, value", + [ + ("header_one.number_key", 0), + ("header_two.list_key", [1, 2, 3]), + ("header_one", {"number_key": 0}), + ], ) def test_that_config_file_can_get_existent_keys(templated_config_file, key, value): + """ + config_file.config_file.ConfigFile.get + + Ensure that we can retrieve a section or key + and that it is in its native type (the type that + it was set to). + + The one caveat here is the IniParser. The IniParser + uses configparser under the hood which simply always + returns back a string. However, that is not the case + for JsonParser, YamlParser, or TomlParser. + """ config = templated_config_file() if isinstance(config._ConfigFile__parser, IniParser): - value = str(value) + if type(value) is not dict: + # not a section being retrieved + value = str(value) + else: + # section is being retrieved so it should be a dict + # but the values inside should be strings. + value = json.loads(json.dumps(value), parse_int=str) assert config.get(key) == value @pytest.mark.parametrize("key", ["header_one.number_key", "header_one"]) def test_that_config_file_can_delete(templated_config_file, key): + """ + config_file.config_file.ConfigFile.delete + + Ensure that we can delete keys and that they are removed + from the internal contents of the ConfigFile. + """ config = templated_config_file() config.delete(key) @@ -128,6 +235,14 @@ def test_that_config_file_can_delete(templated_config_file, key): def test_that_config_file_raises_key_error_on_invalid_input_or_missing_key( templated_config_file, key ): + """ + config_file.config_file.ConfigFile.delete + + Ensure that a KeyError is raised if we + tried to provide a value that is not valid + (like the wrong type so it is an invalid key) + or one that does not exist. + """ config = templated_config_file() with pytest.raises(KeyError): -- cgit v1.2.3 From 16620abbbc92c8fa3999fb55e5d45d6397c328d6 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 10:26:09 -0500 Subject: Fix indentation of docstring --- config_file/nested_lookup.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/config_file/nested_lookup.py b/config_file/nested_lookup.py index 40da123..7b13d83 100644 --- a/config_file/nested_lookup.py +++ b/config_file/nested_lookup.py @@ -59,11 +59,11 @@ def _nested_lookup(key, document, wild=False, with_keys=False): 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 + 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 = [] -- cgit v1.2.3 From 4f44d7a2063c8dfdffe9d19f2d5bb6979c718bd1 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 19:30:37 -0500 Subject: Remove docs/ and update readme with more info --- README.md | 25 +++++-- docs/Makefile | 20 ------ docs/make.bat | 35 ---------- docs/source/api-reference.rst | 2 - docs/source/conf.py | 62 ------------------ docs/source/getting-started.rst | 141 ---------------------------------------- docs/source/index.rst | 22 ------- docs/source/license.rst | 4 -- 8 files changed, 20 insertions(+), 291 deletions(-) delete mode 100644 docs/Makefile delete mode 100644 docs/make.bat delete mode 100644 docs/source/api-reference.rst delete mode 100644 docs/source/conf.py delete mode 100644 docs/source/getting-started.rst delete mode 100644 docs/source/index.rst delete mode 100644 docs/source/license.rst diff --git a/README.md b/README.md index c1d4e9a..86c0f3d 100644 --- a/README.md +++ b/README.md @@ -37,11 +37,17 @@ You can also use [Poetry](https://python-poetry.org). $ poetry install config-file -E yaml -E toml ``` -## Usage Overview +## Usage For this overview, let's say you have the following `ini` file you want to manipulate. +Do note, however, that the `ini` format is the oddest format that +`ConfigFile` supports in that it has no formal specification and is +not type aware. When retrieving items from the file, it will return +them as strings by default. Others are more type aware and do not +require as much type coercion. + ```ini [section] num_key = 5 @@ -284,17 +290,26 @@ config.restore_original() ``` However, if we have a specific path elsewhere that this original configuration file is or it -is named differently than what the default expects, we can utilize the `original_file_path` +is named differently than what the default expects, we can utilize the `original_path` keyword argument. ```python -config.restore_original(original_file_path="~/some-project/original-configs/config.ini") +config.restore_original(original_path="~/some-project/original-configs/config.ini") ``` -## Documentation +## Format Versions Supported + +| Format | Specification version supported | +| ------------- | ------------- | +| INI | No official specification. | +| JSON | [RFC 7159](https://tools.ietf.org/html/rfc7159.html) | +| YAML | [v1.2](https://yaml.org/spec/1.2/spec.html) | +| TOML | [v1.0.0-rc.1](https://github.com/toml-lang/toml/releases/tag/v1.0.0-rc.1) | -Full documentation and API reference is available at https://config-file.readthedocs.io +For `ini` and `json`, Python's standard library modules are used. +Regarding `ini`, there is no formal specification so the syntax that configparser +supports is what is supported here. ## License diff --git a/docs/Makefile b/docs/Makefile deleted file mode 100644 index d0c3cbf..0000000 --- a/docs/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -# Minimal makefile for Sphinx documentation -# - -# You can set these variables from the command line, and also -# from the environment for the first two. -SPHINXOPTS ?= -SPHINXBUILD ?= sphinx-build -SOURCEDIR = source -BUILDDIR = build - -# Put it first so that "make" without argument is like "make help". -help: - @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) - -.PHONY: help Makefile - -# Catch-all target: route all unknown targets to Sphinx using the new -# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). -%: Makefile - @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/make.bat b/docs/make.bat deleted file mode 100644 index 6247f7e..0000000 --- a/docs/make.bat +++ /dev/null @@ -1,35 +0,0 @@ -@ECHO OFF - -pushd %~dp0 - -REM Command file for Sphinx documentation - -if "%SPHINXBUILD%" == "" ( - set SPHINXBUILD=sphinx-build -) -set SOURCEDIR=source -set BUILDDIR=build - -if "%1" == "" goto help - -%SPHINXBUILD% >NUL 2>NUL -if errorlevel 9009 ( - echo. - echo.The 'sphinx-build' command was not found. Make sure you have Sphinx - echo.installed, then set the SPHINXBUILD environment variable to point - echo.to the full path of the 'sphinx-build' executable. Alternatively you - echo.may add the Sphinx directory to PATH. - echo. - echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ - exit /b 1 -) - -%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% -goto end - -:help -%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% - -:end -popd diff --git a/docs/source/api-reference.rst b/docs/source/api-reference.rst deleted file mode 100644 index 4ea8c91..0000000 --- a/docs/source/api-reference.rst +++ /dev/null @@ -1,2 +0,0 @@ -API Reference -============= \ No newline at end of file diff --git a/docs/source/conf.py b/docs/source/conf.py deleted file mode 100644 index 69cfce0..0000000 --- a/docs/source/conf.py +++ /dev/null @@ -1,62 +0,0 @@ -# Configuration file for the Sphinx documentation builder. -# -# This file only contains a selection of the most common options. For a full -# list see the documentation: -# https://www.sphinx-doc.org/en/master/usage/configuration.html - -# -- Path setup -------------------------------------------------------------- - -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. - -import os -import sys - -sys.path.insert(0, os.path.abspath("../..")) - - -# -- Project information ----------------------------------------------------- - -project = "Config File" -copyright = "2020, Eugene Triguba" -author = "Eugene Triguba" - -# The full version, including alpha/beta/rc tags -release = "0.10.0" - - -# -- General configuration --------------------------------------------------- - -# Add any Sphinx extension module names here, as strings. They can be -# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom -# ones. -extensions = [ - "sphinx.ext.autodoc", - "sphinx.ext.autosectionlabel", - "sphinx.ext.napoleon", -] - -# Add any paths that contain templates here, relative to this directory. -templates_path = ["_templates"] - -# List of patterns, relative to source directory, that match files and -# directories to ignore when looking for source files. -# This pattern also affects html_static_path and html_extra_path. -exclude_patterns = [] - -# Specify the master doc for read the docs -master_doc = "index" - - -# -- Options for HTML output ------------------------------------------------- - -# The theme to use for HTML and HTML Help pages. See the documentation for -# a list of builtin themes. -# -html_theme = "sphinx_rtd_theme" - -# Add any paths that contain custom static files (such as style sheets) here, -# relative to this directory. They are copied after the builtin static files, -# so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ["_static"] diff --git a/docs/source/getting-started.rst b/docs/source/getting-started.rst deleted file mode 100644 index 92264a3..0000000 --- a/docs/source/getting-started.rst +++ /dev/null @@ -1,141 +0,0 @@ -Getting Started -=============== - -Example -------- - -The ``config_file`` package exposes ``ConfigFile``, ``ParsingError``, -and ``BaseParser`` to the public API. - -Sample Configuration File -~~~~~~~~~~~~~~~~~~~~~~~~~ - -``config.ini`` - -.. code:: ini - - [calendar] - today = monday - start_week_on_sunday = false - today_index = 0 - quarter_hours_passed = 0.25 - -Create a ConfigFile -~~~~~~~~~~~~~~~~~~~ - -.. code:: python - - from config_file import ConfigFile - - config = ConfigFile("~/.config/test/config.ini") - -Output your config file as a string -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code:: python - - config.stringify() - >>> '[calendar]\ntoday = monday\nstart_week_on_sunday = false\ntoday_index = 0\nquarter_hours_passed = 0.25\n\n' - -Retrieve values or sections -~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -A section.key format is used for retrieving and setting values. - -.. code:: python - - # Values from the config file are automatically parsed - config.get("calendar.start_week_on_sunday") - >>> False - - # Unless you don't want them to be parsed - config.get("calendar.start_week_on_sunday", parse_types=False) - >>> 'false' - - config.get("calendar") - >>> {'today': 'monday', 'start_week_on_sunday': False, 'today_index': 0, 'quarter_hours_passed': 0.25} - -Set values -~~~~~~~~~~ - -.. code:: python - - config.set("calendar.today_index", 20) - >>> True - config.stringify() - >>> '[calendar]\ntoday = monday\nstart_week_on_sunday = false\ntoday_index = 20\nquarter_hours_passed = 0.25\n\n' - - # If you specify a section that isn't in your config file, the section and the key are added for you. - config.set("week.tuesday_index", 2) - >>> True - config.stringify() - >>> '[calendar]\ntoday = monday\nstart_week_on_sunday = false\ntoday_index = 20\nquarter_hours_passed = 0.25\n\n[week]\ntuesday_index = 2\n\n' - -.. _delete-sections-or-keyvalue-pairs: - -Delete sections or key/value pairs. -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code:: python - - config.delete('week') - >>> True - config.stringify() - >>> '[calendar]\ntoday = monday\nstart_week_on_sunday = false\ntoday_index = 20\nquarter_hours_passed = 0.25\n\n' - - config.delete('calendar.today') - >>> True - config.stringify() - >>> '[calendar]\nstart_week_on_sunday = false\ntoday_index = 20\nquarter_hours_passed = 0.25\n\n' - -Check whether you have a particular section or key -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code:: python - - config.has('calendar') - >>> True - - config.has('week') - >>> False - - config.has('calendar.start_week_on_sunday') - >>> True - -Save when you're done -~~~~~~~~~~~~~~~~~~~~~ - -Write the contents of the file back out. ``set()`` and ``delete()`` both -modify the contents of the file and require a call to ``save()`` to -write those changes back out. - -.. code:: python - - config.save() - >>> True - -Restore the file back to its original -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -The current configuration file would be deleted and replaced by a copy -of the original. By default, since our passed in config file was at path -``~/.config/test/config.ini``, ``restore_original()`` will look for -``~/.config/test/config.original.ini``. - -.. code:: python - - config.restore_original() - >>> True - - # But you can also specify the original config file explicitly. - config.restore_original(original_file_path="~/some_other_directory/this_is_actually_the_original.ini") - >>> True - -Using your own parser ---------------------- - -You can still use config file, even if you don't use one of our supported -configuration formats. The ``ConfigFile`` object swaps in the parser it needs -based on the file format. However, the constructor takes in an optional -parser argument that you can use to supply your own custom parser. The only -requirement is that the parser must be a concrete implementation of ``BaseParser``. \ No newline at end of file diff --git a/docs/source/index.rst b/docs/source/index.rst deleted file mode 100644 index 6b59715..0000000 --- a/docs/source/index.rst +++ /dev/null @@ -1,22 +0,0 @@ -Config File Documentation -========================= - -Config File is a package that allows you to easily manipulate your configuration -files whether they are in INI, JSON, YAML, or TOML. - -**Table of Contents:** - -.. toctree:: - :maxdepth: 2 - - getting-started - api-reference - license - - -Indices and tables -================== - -* :ref:`genindex` -* :ref:`modindex` -* :ref:`search` diff --git a/docs/source/license.rst b/docs/source/license.rst deleted file mode 100644 index 01f3963..0000000 --- a/docs/source/license.rst +++ /dev/null @@ -1,4 +0,0 @@ -License -======= - -.. include:: ../../LICENSE \ No newline at end of file -- cgit v1.2.3 From c73f41ccdc52dd7bfe91342960b2388e6939bb80 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 19:37:58 -0500 Subject: Ignore mypy cache --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b659da2..9b8c400 100644 --- a/.gitignore +++ b/.gitignore @@ -10,10 +10,11 @@ __pycache__/ .venv/ -# Pytest +# Testing / Linting .pytest_cache/ .coverage .benchmarks/ +.mypy_cache/ # Package Publishing dist/ -- cgit v1.2.3 From e1475e2b00bdc62599b6947914e8b389a0307f19 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 19:38:16 -0500 Subject: Add info on setting with ini --- README.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 86c0f3d..8cbf360 100644 --- a/README.md +++ b/README.md @@ -192,8 +192,20 @@ new_key = New key value! The exact behavior of how these new keys or sections are added are a bit dependent on the file format we're using, since every format is a little -different in it's structure and in what it supports. See the -[full documentation](#Documentation) for more information there. +different in it's structure and in what it supports. Mostly though, `ini` +is just the odd one. + +If we try the following in `ini`, which does not support subsections or +nested keys, we simply get a single section. + +```python +config.set("section.sub_section.sub_sub_section.key", 5) +``` + +```ini +[section.sub_section.sub_sub_section] +key = 5 +``` Lastly, we can set values using an array notation as well. The underlying content is all manipulated as a dictionary for every file type. If we wanted -- cgit v1.2.3 From 70851cf40e33c08c0b2728a0ef3cac20482905d5 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 19:38:38 -0500 Subject: Add mypy, update pytest, and add lint tasks --- poetry.lock | 127 +++++++++++++++++++++++++++++++++++++++++++++++---------- pyproject.toml | 7 +++- 2 files changed, 110 insertions(+), 24 deletions(-) diff --git a/poetry.lock b/poetry.lock index 1d2ab7b..af82db2 100644 --- a/poetry.lock +++ b/poetry.lock @@ -272,6 +272,14 @@ version = ">=0.4" [package.extras] docs = ["sphinx", "rst.linker", "jaraco.packaging"] +[[package]] +category = "dev" +description = "iniconfig: brain-dead simple config-ini parsing" +name = "iniconfig" +optional = false +python-versions = "*" +version = "1.0.1" + [[package]] category = "dev" description = "A Python utility / library to sort Python imports." @@ -324,6 +332,30 @@ optional = false python-versions = ">=3.5" version = "8.4.0" +[[package]] +category = "dev" +description = "Optional static typing for Python" +name = "mypy" +optional = false +python-versions = ">=3.5" +version = "0.782" + +[package.dependencies] +mypy-extensions = ">=0.4.3,<0.5.0" +typed-ast = ">=1.4.0,<1.5.0" +typing-extensions = ">=3.7.4" + +[package.extras] +dmypy = ["psutil (>=4.0)"] + +[[package]] +category = "dev" +description = "Experimental type system extensions for programs checked with the mypy typechecker." +name = "mypy-extensions" +optional = false +python-versions = "*" +version = "0.4.3" + [[package]] category = "dev" description = "Node.js virtual environment builder" @@ -405,6 +437,17 @@ version = "*" python = "<3.7" version = "*" +[[package]] +category = "dev" +description = "Cross-platform lib for process and system monitoring in Python." +name = "psutil" +optional = false +python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" +version = "5.7.2" + +[package.extras] +test = ["ipaddress", "mock", "unittest2", "enum34", "pywin32", "wmi"] + [[package]] category = "dev" description = "library with cross-python path, ini-parsing, io, code, log facilities" @@ -451,24 +494,25 @@ description = "pytest: simple powerful testing with Python" name = "pytest" optional = false python-versions = ">=3.5" -version = "5.4.3" +version = "6.0.1" [package.dependencies] atomicwrites = ">=1.0" attrs = ">=17.4.0" colorama = "*" +iniconfig = "*" more-itertools = ">=4.0.0" packaging = "*" pluggy = ">=0.12,<1.0" -py = ">=1.5.0" -wcwidth = "*" +py = ">=1.8.2" +toml = "*" [package.dependencies.importlib-metadata] python = "<3.8" version = ">=0.12" [package.extras] -checkqa-mypy = ["mypy (v0.761)"] +checkqa_mypy = ["mypy (0.780)"] testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "requests", "xmlschema"] [[package]] @@ -726,9 +770,10 @@ description = "tasks runner for python projects" name = "taskipy" optional = false python-versions = ">=3.6,<4.0" -version = "1.2.1" +version = "1.3.0" [package.dependencies] +psutil = ">=5.7.2,<6.0.0" toml = ">=0.10.0,<0.11.0" [[package]] @@ -763,6 +808,14 @@ optional = false python-versions = "*" version = "1.4.1" +[[package]] +category = "dev" +description = "Backported and Experimental Type Hints for Python 3.5+" +name = "typing-extensions" +optional = false +python-versions = "*" +version = "3.7.4.2" + [[package]] category = "dev" description = "HTTP library with thread-safe connection pooling, file post, and more." @@ -802,14 +855,6 @@ version = ">=1.0" docs = ["proselint (>=0.10.2)", "sphinx (>=3)", "sphinx-argparse (>=0.2.5)", "sphinx-rtd-theme (>=0.4.3)", "towncrier (>=19.9.0rc1)"] testing = ["coverage (>=5)", "coverage-enable-subprocess (>=1)", "flaky (>=3)", "pytest (>=4)", "pytest-env (>=0.6.2)", "pytest-freezegun (>=0.4.1)", "pytest-mock (>=2)", "pytest-randomly (>=1)", "pytest-timeout (>=1)", "pytest-xdist (>=1.31.0)", "packaging (>=20.0)", "xonsh (>=0.9.16)"] -[[package]] -category = "dev" -description = "Measures the displayed width of unicode strings in a terminal" -name = "wcwidth" -optional = false -python-versions = "*" -version = "0.2.5" - [[package]] category = "dev" description = "Backport of pathlib-compatible object wrapper for zip files" @@ -828,7 +873,7 @@ toml = ["tomlkit"] yaml = ["ruamel.yaml"] [metadata] -content-hash = "04cae2b80bbae148abd0049b077febcfa63984c6d289560146fcc3e584f4f227" +content-hash = "77ccb2f3b39aa3e40a6efccf08acd2065c796a615940f8c3bbc5a05666c1f813" python-versions = "^3.6" [metadata.files] @@ -967,6 +1012,10 @@ importlib-resources = [ {file = "importlib_resources-3.0.0-py2.py3-none-any.whl", hash = "sha256:d028f66b66c0d5732dae86ba4276999855e162a749c92620a38c1d779ed138a7"}, {file = "importlib_resources-3.0.0.tar.gz", hash = "sha256:19f745a6eca188b490b1428c8d1d4a0d2368759f32370ea8fb89cad2ab1106c3"}, ] +iniconfig = [ + {file = "iniconfig-1.0.1-py3-none-any.whl", hash = "sha256:80cf40c597eb564e86346103f609d74efce0f6b4d4f30ec8ce9e2c26411ba437"}, + {file = "iniconfig-1.0.1.tar.gz", hash = "sha256:e5f92f89355a67de0595932a6c6c02ab4afddc6fcdc0bfc5becd0d60884d3f69"}, +] isort = [ {file = "isort-4.3.21-py2.py3-none-any.whl", hash = "sha256:6e811fcb295968434526407adb8796944f1988c5b65e8139058f2014cbe100fd"}, {file = "isort-4.3.21.tar.gz", hash = "sha256:54da7e92468955c4fceacd0c86bd0ec997b0e1ee80d97f67c35a78b719dccab1"}, @@ -1018,6 +1067,26 @@ more-itertools = [ {file = "more-itertools-8.4.0.tar.gz", hash = "sha256:68c70cc7167bdf5c7c9d8f6954a7837089c6a36bf565383919bb595efb8a17e5"}, {file = "more_itertools-8.4.0-py3-none-any.whl", hash = "sha256:b78134b2063dd214000685165d81c154522c3ee0a1c0d4d113c80361c234c5a2"}, ] +mypy = [ + {file = "mypy-0.782-cp35-cp35m-macosx_10_6_x86_64.whl", hash = "sha256:2c6cde8aa3426c1682d35190b59b71f661237d74b053822ea3d748e2c9578a7c"}, + {file = "mypy-0.782-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:9c7a9a7ceb2871ba4bac1cf7217a7dd9ccd44c27c2950edbc6dc08530f32ad4e"}, + {file = "mypy-0.782-cp35-cp35m-win_amd64.whl", hash = "sha256:c05b9e4fb1d8a41d41dec8786c94f3b95d3c5f528298d769eb8e73d293abc48d"}, + {file = "mypy-0.782-cp36-cp36m-macosx_10_6_x86_64.whl", hash = "sha256:6731603dfe0ce4352c555c6284c6db0dc935b685e9ce2e4cf220abe1e14386fd"}, + {file = "mypy-0.782-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:f05644db6779387ccdb468cc47a44b4356fc2ffa9287135d05b70a98dc83b89a"}, + {file = "mypy-0.782-cp36-cp36m-win_amd64.whl", hash = "sha256:b7fbfabdbcc78c4f6fc4712544b9b0d6bf171069c6e0e3cb82440dd10ced3406"}, + {file = "mypy-0.782-cp37-cp37m-macosx_10_6_x86_64.whl", hash = "sha256:3fdda71c067d3ddfb21da4b80e2686b71e9e5c72cca65fa216d207a358827f86"}, + {file = "mypy-0.782-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:d7df6eddb6054d21ca4d3c6249cae5578cb4602951fd2b6ee2f5510ffb098707"}, + {file = "mypy-0.782-cp37-cp37m-win_amd64.whl", hash = "sha256:a4a2cbcfc4cbf45cd126f531dedda8485671545b43107ded25ce952aac6fb308"}, + {file = "mypy-0.782-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:6bb93479caa6619d21d6e7160c552c1193f6952f0668cdda2f851156e85186fc"}, + {file = "mypy-0.782-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:81c7908b94239c4010e16642c9102bfc958ab14e36048fa77d0be3289dda76ea"}, + {file = "mypy-0.782-cp38-cp38-win_amd64.whl", hash = "sha256:5dd13ff1f2a97f94540fd37a49e5d255950ebcdf446fb597463a40d0df3fac8b"}, + {file = "mypy-0.782-py3-none-any.whl", hash = "sha256:e0b61738ab504e656d1fe4ff0c0601387a5489ca122d55390ade31f9ca0e252d"}, + {file = "mypy-0.782.tar.gz", hash = "sha256:eff7d4a85e9eea55afa34888dfeaccde99e7520b51f867ac28a48492c0b1130c"}, +] +mypy-extensions = [ + {file = "mypy_extensions-0.4.3-py2.py3-none-any.whl", hash = "sha256:090fedd75945a69ae91ce1303b5824f428daf5a028d2f6ab8a299250a846f15d"}, + {file = "mypy_extensions-0.4.3.tar.gz", hash = "sha256:2d82818f5bb3e369420cb3c4060a7970edba416647068eb4c5343488a6c604a8"}, +] nodeenv = [ {file = "nodeenv-1.4.0-py2.py3-none-any.whl", hash = "sha256:4b0b77afa3ba9b54f4b6396e60b0c83f59eaeb2d63dc3cc7a70f7f4af96c82bc"}, ] @@ -1041,6 +1110,19 @@ pre-commit = [ {file = "pre_commit-1.21.0-py2.py3-none-any.whl", hash = "sha256:f92a359477f3252452ae2e8d3029de77aec59415c16ae4189bcfba40b757e029"}, {file = "pre_commit-1.21.0.tar.gz", hash = "sha256:8f48d8637bdae6fa70cc97db9c1dd5aa7c5c8bf71968932a380628c25978b850"}, ] +psutil = [ + {file = "psutil-5.7.2-cp27-none-win32.whl", hash = "sha256:f2018461733b23f308c298653c8903d32aaad7873d25e1d228765e91ae42c3f2"}, + {file = "psutil-5.7.2-cp27-none-win_amd64.whl", hash = "sha256:66c18ca7680a31bf16ee22b1d21b6397869dda8059dbdb57d9f27efa6615f195"}, + {file = "psutil-5.7.2-cp35-cp35m-win32.whl", hash = "sha256:5e9d0f26d4194479a13d5f4b3798260c20cecf9ac9a461e718eb59ea520a360c"}, + {file = "psutil-5.7.2-cp35-cp35m-win_amd64.whl", hash = "sha256:4080869ed93cce662905b029a1770fe89c98787e543fa7347f075ade761b19d6"}, + {file = "psutil-5.7.2-cp36-cp36m-win32.whl", hash = "sha256:d8a82162f23c53b8525cf5f14a355f5d1eea86fa8edde27287dd3a98399e4fdf"}, + {file = "psutil-5.7.2-cp36-cp36m-win_amd64.whl", hash = "sha256:0ee3c36428f160d2d8fce3c583a0353e848abb7de9732c50cf3356dd49ad63f8"}, + {file = "psutil-5.7.2-cp37-cp37m-win32.whl", hash = "sha256:ff1977ba1a5f71f89166d5145c3da1cea89a0fdb044075a12c720ee9123ec818"}, + {file = "psutil-5.7.2-cp37-cp37m-win_amd64.whl", hash = "sha256:a5b120bb3c0c71dfe27551f9da2f3209a8257a178ed6c628a819037a8df487f1"}, + {file = "psutil-5.7.2-cp38-cp38-win32.whl", hash = "sha256:10512b46c95b02842c225f58fa00385c08fa00c68bac7da2d9a58ebe2c517498"}, + {file = "psutil-5.7.2-cp38-cp38-win_amd64.whl", hash = "sha256:68d36986ded5dac7c2dcd42f2682af1db80d4bce3faa126a6145c1637e1b559f"}, + {file = "psutil-5.7.2.tar.gz", hash = "sha256:90990af1c3c67195c44c9a889184f84f5b2320dce3ee3acbd054e3ba0b4a7beb"}, +] py = [ {file = "py-1.9.0-py2.py3-none-any.whl", hash = "sha256:366389d1db726cd2fcfc79732e75410e5fe4d31db13692115529d34069a043c2"}, {file = "py-1.9.0.tar.gz", hash = "sha256:9ca6883ce56b4e8da7e79ac18787889fa5206c79dcc67fb065376cd2fe03f342"}, @@ -1062,8 +1144,8 @@ pyparsing = [ {file = "pyparsing-2.4.7.tar.gz", hash = "sha256:c203ec8783bf771a155b207279b9bccb8dea02d8f0c9e5f8ead507bc3246ecc1"}, ] pytest = [ - {file = "pytest-5.4.3-py3-none-any.whl", hash = "sha256:5c0db86b698e8f170ba4582a492248919255fcd4c79b1ee64ace34301fb589a1"}, - {file = "pytest-5.4.3.tar.gz", hash = "sha256:7979331bfcba207414f5e1263b5a0f8f521d0f457318836a7355531ed1a4c7d8"}, + {file = "pytest-6.0.1-py3-none-any.whl", hash = "sha256:8b6007800c53fdacd5a5c192203f4e531eb2a1540ad9c752e052ec0f7143dbad"}, + {file = "pytest-6.0.1.tar.gz", hash = "sha256:85228d75db9f45e06e57ef9bf4429267f81ac7c0d742cc9ed63d09886a9fe6f4"}, ] pytest-cov = [ {file = "pytest-cov-2.10.0.tar.gz", hash = "sha256:1a629dc9f48e53512fcbfda6b07de490c374b0c83c55ff7a1720b3fccff0ac87"}, @@ -1190,8 +1272,8 @@ sphinxcontrib-serializinghtml = [ {file = "sphinxcontrib_serializinghtml-1.1.4-py2.py3-none-any.whl", hash = "sha256:f242a81d423f59617a8e5cf16f5d4d74e28ee9a66f9e5b637a18082991db5a9a"}, ] taskipy = [ - {file = "taskipy-1.2.1-py3-none-any.whl", hash = "sha256:99bdaf5b19791c2345806847147e0fc2d28e1ac9446058def5a8b6b3fc9f23e2"}, - {file = "taskipy-1.2.1.tar.gz", hash = "sha256:5eb2c3b1606c896c7fa799848e71e8883b880759224958d07ba760e5db263175"}, + {file = "taskipy-1.3.0-py3-none-any.whl", hash = "sha256:f1c939009f464a93d87644011bcc36756be67478c1034921adeaeb34bfc0b5fd"}, + {file = "taskipy-1.3.0.tar.gz", hash = "sha256:9775108e6efcc8a8883492a7427f1a596e4528b2bcf273fcef2b3ba756173160"}, ] termcolor = [ {file = "termcolor-1.1.0.tar.gz", hash = "sha256:1d6d69ce66211143803fbc56652b41d73b4a400a2891d7bf7a1cdf4c02de613b"}, @@ -1227,6 +1309,11 @@ typed-ast = [ {file = "typed_ast-1.4.1-cp39-cp39-macosx_10_15_x86_64.whl", hash = "sha256:d43943ef777f9a1c42bf4e552ba23ac77a6351de620aa9acf64ad54933ad4d34"}, {file = "typed_ast-1.4.1.tar.gz", hash = "sha256:8c8aaad94455178e3187ab22c8b01a3837f8ee50e09cf31f1ba129eb293ec30b"}, ] +typing-extensions = [ + {file = "typing_extensions-3.7.4.2-py2-none-any.whl", hash = "sha256:f8d2bd89d25bc39dabe7d23df520442fa1d8969b82544370e03d88b5a591c392"}, + {file = "typing_extensions-3.7.4.2-py3-none-any.whl", hash = "sha256:6e95524d8a547a91e08f404ae485bbb71962de46967e1b71a0cb89af24e761c5"}, + {file = "typing_extensions-3.7.4.2.tar.gz", hash = "sha256:79ee589a3caca649a9bfd2a8de4709837400dfa00b6cc81962a1e6a1815969ae"}, +] urllib3 = [ {file = "urllib3-1.25.10-py2.py3-none-any.whl", hash = "sha256:e7983572181f5e1522d9c98453462384ee92a0be7fac5f1413a1e35c56cc0461"}, {file = "urllib3-1.25.10.tar.gz", hash = "sha256:91056c15fa70756691db97756772bb1eb9678fa585d9184f24534b100dc60f4a"}, @@ -1235,10 +1322,6 @@ virtualenv = [ {file = "virtualenv-20.0.30-py2.py3-none-any.whl", hash = "sha256:8cd7b2a4850b003a11be2fc213e206419efab41115cc14bca20e69654f2ac08e"}, {file = "virtualenv-20.0.30.tar.gz", hash = "sha256:7b54fd606a1b85f83de49ad8d80dbec08e983a2d2f96685045b262ebc7481ee5"}, ] -wcwidth = [ - {file = "wcwidth-0.2.5-py2.py3-none-any.whl", hash = "sha256:beb4802a9cebb9144e99086eff703a642a13d6a0052920003a230f3294bbe784"}, - {file = "wcwidth-0.2.5.tar.gz", hash = "sha256:c4d647b99872929fdb7bdcaa4fbe7f01413ed3d98077df798530e5b04f116c83"}, -] zipp = [ {file = "zipp-3.1.0-py3-none-any.whl", hash = "sha256:aa36550ff0c0b7ef7fa639055d797116ee891440eac1a56f378e2d3179e0320b"}, {file = "zipp-3.1.0.tar.gz", hash = "sha256:c599e4d75c98f6798c509911d08a22e6c021d074469042177c8c86fb92eefd96"}, diff --git a/pyproject.toml b/pyproject.toml index 7f05d3e..810e5c8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,9 @@ packages = [ test = "task test_cov -- --cov-report term-missing" test_cov = "task pytest -- --cov=config_file/" test_ci = "task test_cov -- --cov-report=xml" -lint = "poetry run pre-commit run --all" +lint = "task lint_types && task lint_precommit" +lint_types = "poetry run mypy config_file/ tests/" +lint_precommit = "poetry run pre-commit run --all" pytest = "poetry run pytest tests/" [tool.poetry.dependencies] @@ -45,7 +47,7 @@ pre-commit = "^1.20" autoflake = "^1.3.1" isort = "^4.3.21" seed-isort-config = { version = "^2.1.1", python = "^3.6.1" } -pytest = "^5.4.1" +pytest = "^6.0.0" pytest-cov = "^2.8.1" pytest-sugar = "^0.9.3" taskipy = "^1.2.1" @@ -55,6 +57,7 @@ black = "^19.10b0" sphinx = "^2.4.4" sphinx_rtd_theme = "^0.4.3" sphinxcontrib-napoleon = "^0.7" +mypy = "^0.782" [tool.black] line-length = 88 -- cgit v1.2.3 From c73710bd173702e9d0e1cf005333274c23418574 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 19:39:30 -0500 Subject: Add invalid templates --- tests/templates/ini/invalid.ini | 2 ++ tests/templates/toml/invalid.toml | 1 + tests/templates/yaml/invalid.yaml | 6 ++++++ 3 files changed, 9 insertions(+) create mode 100644 tests/templates/ini/invalid.ini create mode 100644 tests/templates/toml/invalid.toml create mode 100644 tests/templates/yaml/invalid.yaml diff --git a/tests/templates/ini/invalid.ini b/tests/templates/ini/invalid.ini new file mode 100644 index 0000000..c2a81db --- /dev/null +++ b/tests/templates/ini/invalid.ini @@ -0,0 +1,2 @@ +[[[[[]]]]] +i am a key \ No newline at end of file diff --git a/tests/templates/toml/invalid.toml b/tests/templates/toml/invalid.toml new file mode 100644 index 0000000..d1b18e3 --- /dev/null +++ b/tests/templates/toml/invalid.toml @@ -0,0 +1 @@ +[[[[[[notvalidtoml]]]]]] \ No newline at end of file diff --git a/tests/templates/yaml/invalid.yaml b/tests/templates/yaml/invalid.yaml new file mode 100644 index 0000000..cf2a4ff --- /dev/null +++ b/tests/templates/yaml/invalid.yaml @@ -0,0 +1,6 @@ +invalid-yaml: +yar: +i: +am: +invalid: +boo \ No newline at end of file -- cgit v1.2.3 From ee37bd79de456c4e55a2e68f2f9062b1d01ab035 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 19:40:02 -0500 Subject: Conform test abstract parser has syntax --- tests/test_abstract_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py index 1816f6b..0012127 100644 --- a/tests/test_abstract_parser.py +++ b/tests/test_abstract_parser.py @@ -22,7 +22,7 @@ class ConcreteParser(AbstractParser): def delete(self, section_key): super().delete(section_key) - def has(self, section_key: str): + def has(self, section_key: str, wild: bool = False): super().has(section_key) def stringify(self): -- cgit v1.2.3 From e529b2be912925e91be6466dfd926610012ba58c Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 19:40:38 -0500 Subject: Change file_path to path and original_file_path to original_path --- config_file/config_file.py | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/config_file/config_file.py b/config_file/config_file.py index 2ce61ef..8cde5cb 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -27,11 +27,11 @@ class ConfigFile: self.__parser = self.__path.parser @property - def file_path(self) -> Path: + def path(self) -> Path: return Path(self.__path) @property - def original_file_path(self) -> Path: + def original_path(self) -> Path: return Path(self.__path.original_path) def __getitem__(self, key: str): @@ -148,9 +148,7 @@ class ConfigFile: """ return self.__parser.has(key, wild=wild) - def restore_original( - self, original_file_path: Union[str, Path, None] = None - ) -> None: + 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 @@ -158,32 +156,25 @@ class ConfigFile: new file. Args: - original_file_path: The original file to reset to. + original_path: The original file to reset to. - If this is not provided, it is calculated for you. - e.g. if your configuration file is named config.json, - this will look for aa config.original.json in the same - directory. + Defaults to the original_path property if it is not + provided. Raises: FileNotFoundError: If the original configuration file (whether - calculated or passed in) does not exist. + 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). OSError: If the current configuration path is not writable. - - SameFileError: If current configuration path and the passed in - original_file_path are the same file. """ - if original_file_path and not ConfigFilePath(original_file_path).exists(): - raise FileNotFoundError( - f"The {original_file_path} file to restore to does not exist." - ) - else: - original_file_path = self.__path.original_path - original_file_path = original_file_path.validate() + original_path = ConfigFilePath( + original_path if original_path else self.original_path + ).validate() self.__path.unlink() - copyfile(original_file_path, self.__path) + copyfile(original_path, self.__path) self.__parser.reset_internal_contents(self.__path.contents) def save(self) -> None: -- cgit v1.2.3 From 2ef17a5a002b3c304076339619f208ee01e36f48 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 20:14:44 -0500 Subject: Add all_strings template files --- tests/templates/ini/all_strings.ini | 7 ++ tests/templates/json/all_strings.json | 9 ++ tests/templates/json/glossary.json | 33 -------- tests/templates/toml/all_strings.toml | 6 ++ tests/templates/yaml/all_strings.yaml | 6 ++ tests/test_config_file.py | 155 +++++++++++++++++++++++++++++----- 6 files changed, 161 insertions(+), 55 deletions(-) create mode 100644 tests/templates/ini/all_strings.ini create mode 100644 tests/templates/json/all_strings.json delete mode 100644 tests/templates/json/glossary.json create mode 100644 tests/templates/toml/all_strings.toml create mode 100644 tests/templates/yaml/all_strings.yaml diff --git a/tests/templates/ini/all_strings.ini b/tests/templates/ini/all_strings.ini new file mode 100644 index 0000000..9ac0ecf --- /dev/null +++ b/tests/templates/ini/all_strings.ini @@ -0,0 +1,7 @@ +[header] +list = [] +dict = {} +num = 5 +bool = false +float = 5.5 + diff --git a/tests/templates/json/all_strings.json b/tests/templates/json/all_strings.json new file mode 100644 index 0000000..18ada7b --- /dev/null +++ b/tests/templates/json/all_strings.json @@ -0,0 +1,9 @@ +{ + "header": { + "list": "[]", + "dict": "{}", + "num": "5", + "bool": "false", + "float": "5.5" + } +} \ No newline at end of file diff --git a/tests/templates/json/glossary.json b/tests/templates/json/glossary.json deleted file mode 100644 index d27eb8d..0000000 --- a/tests/templates/json/glossary.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "glossary": { - "title": "str_value", - "GlossDiv": { - "title": "5", - "GlossList": { - "GlossEntry": { - "ID": "false", - "SortAs": "SGML", - "GlossTerm": "Standard Generalized Markup Language", - "Acronym": "SGML", - "Abbrev": "ISO 8879:1986", - "GlossDef": { - "para": "A meta-markup language, used to create markup languages such as DocBook.", - "GlossSeeAlso": ["GML", "XML"] - }, - "GlossSee": "5.5" - } - } - }, - "dict": { - "key": "str_value", - "str_number": "5" - }, - "dict_within_dict": { - "str_number": "16", - "another_dict": { - "str_bool": "false", - "list_of_values": ["5", "false", "trUe"] - } - } - } -} \ No newline at end of file diff --git a/tests/templates/toml/all_strings.toml b/tests/templates/toml/all_strings.toml new file mode 100644 index 0000000..8fda0d8 --- /dev/null +++ b/tests/templates/toml/all_strings.toml @@ -0,0 +1,6 @@ +[header] +list = "[]" +dict = "{}" +num = "5" +bool = "false" +float = "5.5" \ No newline at end of file diff --git a/tests/templates/yaml/all_strings.yaml b/tests/templates/yaml/all_strings.yaml new file mode 100644 index 0000000..1c94b37 --- /dev/null +++ b/tests/templates/yaml/all_strings.yaml @@ -0,0 +1,6 @@ +header: + list: "[]" + dict: "{}" + num: "5" + bool: "false" + float: "5.5" \ No newline at end of file diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 33f1945..e120de2 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -6,6 +6,7 @@ import pytest from config_file.abstract_parser import AbstractParser from config_file.config_file import ConfigFile +from config_file.exceptions import ParsingError from config_file.ini_parser import IniParser SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"] @@ -47,7 +48,7 @@ def templated_config_file(template_and_config_file) -> Callable[[str], ConfigFil return func -def test_that_config_file_is_initialized_correctly(template_and_config_file): +def test_config_file_init_has_correct_path_and_parser(template_and_config_file): """ config_file.config_file.ConfigFile.__init__ @@ -56,11 +57,22 @@ def test_that_config_file_is_initialized_correctly(template_and_config_file): """ template_file, config = template_and_config_file() - assert config.file_path == template_file + assert config.path == template_file assert isinstance(config._ConfigFile__parser, AbstractParser) -def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): +@pytest.mark.parametrize("file_type", SUPPORTED_FILE_TYPES) +def test_config_file_raises_error_on_malformed_file(template_file, file_type): + """ + config_file.config_file.ConfigFile.__init__ + + Ensure that a ParsingError is raised on a malformed file. + """ + with pytest.raises(ParsingError): + ConfigFile(template_file(file_type, template_name="invalid")) + + +def test_config_file_expands_home_tildes_in_path(): """ config_file.config_file.ConfigFile.__init__ @@ -72,13 +84,13 @@ def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): try: Path(config_path).expanduser().touch() - assert ConfigFile(config_path).file_path == Path.home() / config_name + assert ConfigFile(config_path).path == Path.home() / config_name finally: Path(config_path).expanduser().unlink() @pytest.mark.parametrize("path", ["invalid", "invalid.conf"]) -def test_invalid_path_raises_file_not_found_error(path): +def test_config_file_raises_error_on_non_existent_path(path): """ config_file.config_file.ConfigFile.__init__ @@ -89,7 +101,7 @@ def test_invalid_path_raises_file_not_found_error(path): ConfigFile(path) -def test_directory_raises_value_error(): +def test_config_file_raises_error_if_path_is_directory(): """ config_file.config_file.ConfigFile.__init__ @@ -100,7 +112,17 @@ def test_directory_raises_value_error(): ConfigFile(".") -def test_that_config_file_can_save(template_and_config_file): +@pytest.mark.parametrize( + "key, value", [("header_one.list_key", "different value"), ("header_one", {})], +) +def test_set_can_add_to_or_alter_the_file(templated_config_file, key, value): + config = templated_config_file() + config.set(key, value) + + assert config.get(key) == value + + +def test_config_file_can_save(template_and_config_file): """ config_file.config_file.ConfigFile.save @@ -115,33 +137,60 @@ def test_that_config_file_can_save(template_and_config_file): assert config.stringify() == template_file.read_text() -def test_that_config_file_can_restore_the_original( +def test_restore_original_can_restore_with_calculated_in_path( templated_config_file, template_original_file ): """ config_file.config_file.ConfigFile.restore_original Ensure that we can restore the "original" configuration - file, using the calculated path and after altering the - internal config contents. Those internal contents and file - should be reset to the original file. + file, using the *calculated* path, after altering the + internal config contents. + + Those internal contents and file should be reset to the + original file. """ config = templated_config_file() - original_file = template_original_file(config.file_path) + original_file = template_original_file(config.path) config.set("header_one.number_key", 5) config.restore_original() assert config.stringify() == original_file.read_text() - assert config.file_path.read_text() == original_file.read_text() + assert config.path.read_text() == original_file.read_text() -def test_missing_config_file_during_restore(templated_config_file): +@pytest.mark.parametrize("file_type", SUPPORTED_FILE_TYPES) +def test_restore_original_can_restore_with_passed_in_path( + template_file, tmp_file, file_type +): + """ + config_file.config_file.ConfigFile.restore_original + + Ensure that we can restore the "original" configuration + file, using the *passed* in path, after altering the + internal config contents. + """ + config_file = template_file(file_type) + config = ConfigFile(config_file) + + original_file = tmp_file(f"file.{file_type}", config_file.read_text()) + original_config = ConfigFile(original_file) + original_config.set("some_new_section.some_new_key", 5) + original_config.save() + + config.restore_original(original_path=original_file) + + assert config.stringify() == original_config.stringify() + assert config.path.read_text() == original_file.read_text() + + +def test_restore_original_raises_error_on_missing_file(templated_config_file): """ config_file.config_file.ConfigFile.restore_original Ensure that if we try to restore_original without using - the optional original_file_path argument but we do not + the optional original_path argument but we do not have a file at the calculated default path, a FileNotFoundError is raised. """ @@ -155,9 +204,7 @@ def test_missing_config_file_during_restore(templated_config_file): @pytest.mark.parametrize("key", ["header_one", "header_one.number_key"]) -def test_that_config_file_has_can_find_sections_and_keys_that_exist( - templated_config_file, key -): +def test_has_can_find_sections_and_keys(templated_config_file, key): """ config_file.config_file.ConfigFile.has @@ -168,7 +215,7 @@ def test_that_config_file_has_can_find_sections_and_keys_that_exist( assert config.has(key) -def test_that_config_file_has_returns_false_for_sections_and_keys_that_do_not_exist( +def test_has_returns_false_for_sections_and_keys_that_do_not_exist( templated_config_file, ): """ @@ -181,6 +228,11 @@ def test_that_config_file_has_returns_false_for_sections_and_keys_that_do_not_ex assert not config.has("header_one.blah") +def test_has_can_search_for_a_key_anywhere(templated_config_file): + config = templated_config_file() + assert config.has("list_key", wild=True) + + @pytest.mark.parametrize( "key, value", [ @@ -189,7 +241,7 @@ def test_that_config_file_has_returns_false_for_sections_and_keys_that_do_not_ex ("header_one", {"number_key": 0}), ], ) -def test_that_config_file_can_get_existent_keys(templated_config_file, key, value): +def test_get_can_retrieve_existent_keys(templated_config_file, key, value): """ config_file.config_file.ConfigFile.get @@ -216,8 +268,27 @@ def test_that_config_file_can_get_existent_keys(templated_config_file, key, valu assert config.get(key) == value +def test_get_can_parse_all_types(templated_config_file): + config = templated_config_file(template_name="all_strings") + output = {"list": [], "dict": {}, "num": 5, "bool": False, "float": 5.5} + + assert config.get("header", parse_types=True) == output + + +def test_get_can_use_a_default(templated_config_file): + config = templated_config_file() + assert config.get("does_not_exist", default="default!") == "default!" + + +def test_get_raises_a_key_error_on_invalid_key(templated_config_file): + config = templated_config_file() + + with pytest.raises(KeyError): + config.get("does_not_exist") + + @pytest.mark.parametrize("key", ["header_one.number_key", "header_one"]) -def test_that_config_file_can_delete(templated_config_file, key): +def test_delete_can_remove_sections_and_keys(templated_config_file, key): """ config_file.config_file.ConfigFile.delete @@ -232,7 +303,7 @@ def test_that_config_file_can_delete(templated_config_file, key): @pytest.mark.parametrize("key", ["", 0, False, {}, "header_one.does_not_exist"]) -def test_that_config_file_raises_key_error_on_invalid_input_or_missing_key( +def test_delete_raises_key_error_on_invalid_input_or_missing_key( templated_config_file, key ): """ @@ -247,3 +318,43 @@ def test_that_config_file_raises_key_error_on_invalid_input_or_missing_key( with pytest.raises(KeyError): config.delete(key) + + +def test_keys_can_be_set_with_array_notation(template_and_config_file): + """ + config_file.config_file.ConfigFile.__setitem__ + + Ensure that the ConfigFile exposes its parsed contents + using an array notation so we can set keys that way. + """ + template_file, config = template_and_config_file() + + config["header_one"] = {} + config.save() + + assert config.stringify() == template_file.read_text() + + +def test_keys_can_be_retrieved_with_array_notation(templated_config_file): + """ + config_file.config_file.ConfigFile.__getitem__ + + Ensure that the ConfigFile exposes its parsed contents + using an array notation so we can retrieve keys that way. + """ + config = templated_config_file() + assert int(config["header_one"]["number_key"]) == 0 + + +def test_keys_can_be_deleted_with_array_notation(templated_config_file): + """ + config_file.config_file.ConfigFile.__delitem__ + + Ensure that the ConfigFile exposes its parsed contents + using an array notation so we can delete keys that way. + """ + config = templated_config_file() + del config["header_one"] + + with pytest.raises(KeyError): + config["header_one"] -- cgit v1.2.3 From a0cdb32e3f8ae69bd1163e2d44e3d731d9e8594e Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 20:15:05 -0500 Subject: Fix ConfigFile default --- config_file/config_file.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/config_file/config_file.py b/config_file/config_file.py index 8cde5cb..2a8d9ae 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -3,7 +3,6 @@ from shutil import copyfile from typing import Any, Union from config_file.config_file_path import ConfigFilePath -from config_file.exceptions import ParsingError from config_file.parse_value import parse_value from config_file.utils import Default @@ -74,11 +73,11 @@ class ConfigFile: """ try: key_value = self.__parser.get(key) - except ParsingError as error: - if default is not type(Default): - key_value = default + except KeyError as error: + if isinstance(default, Default) and default.value is None: + raise KeyError(error) else: - raise ParsingError(error) + key_value = default if parse_types: key_value = parse_value(key_value) -- cgit v1.2.3