aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-08-01 08:57:17 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-08-01 08:57:17 -0500
commitba655e72f74bc488cb8e5988766fef8f5dd45bcc (patch)
treef967eb203b0a6e389022c5383a7eb23414059051
parentAdd name to security workflow (diff)
parentFix wording of docstring in missing dependency error (diff)
downloadconfig-file-ba655e72f74bc488cb8e5988766fef8f5dd45bcc.zip
Merge branch 'dev' into dev
-rw-r--r--.coveragerc4
-rw-r--r--.github/workflows/python-package-ci.yml6
-rw-r--r--.github/workflows/python-security.yml5
-rw-r--r--.gitignore4
-rw-r--r--README.md251
-rw-r--r--config_file/config_file.py96
-rw-r--r--config_file/exceptions.py4
-rw-r--r--config_file/parsers/base_parser.py39
-rw-r--r--config_file/parsers/ini_parser.py44
-rw-r--r--config_file/parsers/ini_parser_compat.py42
-rw-r--r--config_file/parsers/parse_value.py63
-rw-r--r--config_file/parsers/toml_parser.py2
-rw-r--r--config_file/parsers/yaml_parser.py19
-rw-r--r--config_file/utils.py8
-rw-r--r--poetry.lock149
-rw-r--r--pyproject.toml6
-rw-r--r--tests/templates/json/glossary.json4
-rw-r--r--tests/templates/toml/default.toml1
-rw-r--r--tests/templates/yaml/default.yaml5
-rw-r--r--tests/test_abstract_parser.py0
-rw-r--r--tests/test_config_file.py41
-rw-r--r--tests/test_ini_parser.py6
-rw-r--r--tests/test_json_parser.py36
-rw-r--r--tests/test_parse_value.py5
24 files changed, 585 insertions, 255 deletions
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
diff --git a/.github/workflows/python-package-ci.yml b/.github/workflows/python-package-ci.yml
index 5f7d348..0b82937 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:
@@ -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
diff --git a/.github/workflows/python-security.yml b/.github/workflows/python-security.yml
index a9f4a0f..5597151 100644
--- a/.github/workflows/python-security.yml
+++ b/.github/workflows/python-security.yml
@@ -1,6 +1,5 @@
-name: Python Security
-
-on: [push]
+name: Python Security Checks
+on: [push, pull_request]
jobs:
security_checks:
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/
diff --git a/README.md b/README.md
index c0fc8a3..3900971 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 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.
@@ -37,75 +43,234 @@ 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 recognized 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.
-# 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)
+#### Retrieving keys and sections
-# 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))
+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
+
+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.
+
+```python
+config.get('section.num_key', return_type=int)
>>> 5
+```
-# 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 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.
-# 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
+```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-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.
+
+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 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. 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`, 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.
+
+```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, 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()`
-# Setting, deleting, and checking if a key exists is just as easy.
-config.set("section.first_key", 10)
-config.delete("section.third_key")
+`delete()` allows us to delete entire sections or specific keys.
-print(config.has("section.third_key"))
+```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 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)
+>>> True
+```
+
+### Using `save()`
-# We can also convert the entire configuration file to a string.
-print(config.stringify())
->>> '[section]\nfirst_key = 10\nsecond_key = blah\n\n'
+For any changes we make to our configuration file, they are not written out
+to the filesystem until we call `save()`. This is to avoid unnecessary write
+calls after each operation until we actually need to save.
-# Lastly, we need to make sure we save our changes.
-# Nothing is written out until we do so.
+```python
+config.delete('section.list_key')
config.save()
+```
+
+### Using `stringify()`
+
+`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()
+>>> '[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'
+```
+
-# 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)
+### 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).
+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` 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 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")
+```
+
+
## 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 3ac4b91..9b14072 100644
--- a/config_file/config_file.py
+++ b/config_file/config_file.py
@@ -17,25 +17,26 @@ class ConfigFile:
def __init__(
self,
file_path: Union[str, Path],
- parser: Optional[Type[AbstractParser]] = None,
+ parser: Optional[Type[AbstractParser]] = None
) -> None:
"""
- Saves the config file path and expands it if needed, reads in
+ Stores the config file path and expands it if needed, reads in
the file contents, and determines what parser should be used for
the given file.
- :param file_path: The path to your configuration file.
- :param parser: A custom parser you'd like used for your config file.
+ Args:
+ file_path: The path to your configuration file.
+ parser: A custom parser you'd like used for your config file.
- :raises ValueError: If the specified file path does not have an extension
- that is supported or it is a directory.
+ Raises:
+ ValueError: If the specified file path does not have an extension
+ that is supported or it is a directory.
"""
if not isinstance(file_path, Path):
file_path = Path(file_path)
self.__path = create_config_path(file_path)
- self.__contents = read_file(self.__path)
- self.__parser = self.__determine_parser(file_path, parser)
+ self.__parser = self.__determine_parser(self.__path, parser)
@property
def path(self) -> Path:
@@ -44,8 +45,10 @@ class ConfigFile:
def __determine_parser(
self, file_path: Path, parser: Optional[Type[AbstractParser]] = None
) -> Type[AbstractParser]:
+ file_contents = read_file(file_path)
+
if parser is not None and not inspect.isabstract(parser):
- return parser(self.__contents)
+ return parser(file_contents)
try:
file_type = split_on_dot(file_path, only_last_dot=True)[-1]
@@ -56,7 +59,7 @@ class ConfigFile:
)
return self.__find_parser_by_file_type(
- file_type=file_type, file_contents=self.__contents, file_path=self.__path
+ file_type=file_type, file_contents=file_contents, file_path=file_path
)
@staticmethod
@@ -86,18 +89,23 @@ class ConfigFile:
"""
Retrieve the value of a key.
- :param key: The key to retrieve.
- :param parse_types: Automatically parse ints, floats, booleans, dicts, and
- lists. This recursively parses all types in whatever you're
- retrieving, not just a single type. e.g. If you are
- retrieving a section, all values in that section with be
- parsed.
- :param return_type: The type to coerce the return value to.
- :param default: The default value to return if the value of the key is empty.
+ Args:
+ key: The key to retrieve.
+ parse_types: Automatically parse ints, floats, booleans, dicts, and
+ lists. This recursively parses all types in whatever you're
+ retrieving, not just a single type.
+
+ e.g. If you are retrieving a section, all values in that section
+ will be parsed.
- :return: The value of the key.
+ return_type: The type to coerce the return value to.
+ default: The default value to return if the value of the key is empty.
- :raises ValueError: If the value is not able to be coerced into return_type
+ Returns:
+ The value of the key.
+
+ Raises:
+ ValueError: If the value is not able to be coerced into return_type.
"""
try:
key_value = self.__parser.get(key)
@@ -115,16 +123,33 @@ class ConfigFile:
def set(self, key: str, value: Any) -> None:
"""Sets the value of a key.
+ If the given key does not exist, it will be automatically
+ created for you. That includes if there are multiple keys
+ in a row that do not exist.
+ e.g. set('exists.does_not.does_not.also_does_not', 5)
+
+ The behavior of how this is done, however, depends on the
+ file used. For example, with INI, subsections are not supported.
+ So it would create a key in the section `exists` called `does_not`
+ and set it to the value {'does_not': {'also_does_not': 5}}.
+
+ Args:
+ key: The section, sub-section, or key to delete.
+ value: The value to set the key to.
"""
self.__parser.set(key, value)
- def delete(self, section_key: str) -> None:
+ def delete(self, key: str) -> None:
"""Deletes a section or key.
Args:
- section_key:
+ key: The section, sub-section, or key to delete.
+
+ Raises:
+ MissingKeyError: If a key is attempted to be deleted that
+ does not exist.
"""
- self.__parser.delete(section_key)
+ self.__parser.delete(key)
def stringify(self) -> str:
"""Retrieves file contents as a string.
@@ -135,7 +160,7 @@ class ConfigFile:
"""
return self.__parser.stringify()
- def has(self, section_key: str) -> bool:
+ def has(self, key: str, wild: bool = False) -> bool:
"""
Check if a section, sub-section, or key exists.
@@ -144,12 +169,19 @@ class ConfigFile:
key exists.
Args:
- section_key:
+ key: The section, sub-section, or key to find.
+ wild: Whether or not to search everywhere.
+
+ Without `wild`, a single word `key` without a `.`
+ will look at the outer most heirarchy of the file for it.
+
+ With `wild`, that single word `key` will be searched
+ for throughout the entire file.
Returns:
- True if
+ True if the key exists. False otherwise.
"""
- return self.__parser.has(section_key)
+ return self.__parser.has(key, wild=wild)
def restore_original(
self, original_file_path: Union[str, Path, None] = None
@@ -187,17 +219,15 @@ class ConfigFile:
self.__path.expanduser().unlink()
copyfile(original_file_path, self.__path)
-
- self.__contents = read_file(self.__path)
- self.__parser.reset_internal_contents(self.__contents)
+ self.__parser.reset_internal_contents(read_file(self.__path))
def save(self) -> None:
"""
Save your configuration changes.
This writes the file back out, including any changes
- you've made, to the specified path when this object
- was created.
+ you've made, to the specified path given from this
+ object's constructor.
"""
with open(str(self.__path), "w") as config_file:
- config_file.write(self.__parser.stringify())
+ config_file.write(self.stringify())
diff --git a/config_file/exceptions.py b/config_file/exceptions.py
index d11e373..d1b466b 100644
--- a/config_file/exceptions.py
+++ b/config_file/exceptions.py
@@ -4,8 +4,8 @@ class ConfigFileError(Exception):
class MissingDependencyError(ConfigFileError):
"""
- PyYaml or toml is not installed, but the parsers
- for them were attempted to be used.
+ yaml or toml dependencies were not installed,
+ but the parsers for them were attempted to be used.
"""
diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py
index 09fbbbf..546450c 100644
--- a/config_file/parsers/base_parser.py
+++ b/config_file/parsers/base_parser.py
@@ -1,4 +1,5 @@
from typing import Any
+from types import ModuleType
from config_file.exceptions import MissingKeyError, ParsingError
from config_file.nested_lookup import get_occurrence_of_key
@@ -8,7 +9,9 @@ from .abstract_parser import AbstractParser
class BaseParser(AbstractParser):
- def __init__(self, file_contents: str, module, decode_error):
+ def __init__(
+ self, file_contents: str, module: ModuleType, decode_error: Exception
+ ):
super().__init__(file_contents)
self.__module = module
self.__decode_error = decode_error
@@ -39,9 +42,9 @@ class BaseParser(AbstractParser):
for key in split_keys:
key_causing_error = key
result = result[key]
- except TypeError:
+ except (KeyError, TypeError):
raise ParsingError(
- f"Cannot get {search_key} because "
+ f"Cannot `get` {search_key} because "
f"{key_causing_error} is not subscriptable."
)
@@ -59,9 +62,13 @@ class BaseParser(AbstractParser):
content_reference[key] = value
return
- content_reference = content_reference[key]
+ try:
+ content_reference = content_reference[key]
+ except KeyError:
+ content_reference[key] = {}
+ content_reference = content_reference[key]
- def delete(self, key) -> None:
+ def delete(self, key: str) -> None:
try:
if "." not in key:
del self.parsed_content[key]
@@ -69,18 +76,26 @@ class BaseParser(AbstractParser):
indexes = split_on_dot(key)
indexes_length = len(indexes)
- ref = self.parsed_content
+ content_reference = self.parsed_content
for index, key in enumerate(indexes):
if index == indexes_length - 1:
- del ref[key]
+ del content_reference[key]
break
- ref = ref[key]
- except KeyError:
- raise MissingKeyError(f"The specified key '{key}' was not found.")
+ content_reference = content_reference[key]
+ except (KeyError, TypeError):
+ raise MissingKeyError(f"The specified key '{key}' to delete was not found.")
def stringify(self) -> str:
return self.__module.dumps(self.parsed_content)
- def has(self, search_key: str) -> bool:
- return get_occurrence_of_key(self.parsed_content, key=search_key) > 0
+ def has(self, search_key: str, wild: bool = False) -> bool:
+ if wild:
+ return get_occurrence_of_key(self.parsed_content, key=search_key) > 0
+
+ try:
+ self.get(search_key)
+ return True
+ except (KeyError, ParsingError):
+ return False
+
diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py
index 2a29ba7..8dc7f47 100644
--- a/config_file/parsers/ini_parser.py
+++ b/config_file/parsers/ini_parser.py
@@ -1,47 +1,7 @@
import configparser
-from io import StringIO
from .base_parser import BaseParser
-
-
-class IniParserCompatibility:
- """
- Provides compatibility with the json, yaml,
- and toml API so we can use the BaseParser
- easily, while still being able to leverage
- the built-in configparser to parse the file.
-
- It is used as the `module` parameter
- for BaseParser, which requires us to have a
- `dumps` and `loads` method.
- """
-
- @staticmethod
- def loads(file_contents: str) -> dict:
- parser = configparser.ConfigParser()
- parser.read_string(file_contents)
-
- return IniParserCompatibility.__create_configparser_dict(parser)
-
- @staticmethod
- def dumps(file_contents: dict) -> str:
- parser = configparser.ConfigParser()
- buffer = StringIO()
-
- parser.read_dict(file_contents)
- parser.write(buffer)
-
- return buffer.getvalue()
-
- @staticmethod
- def __create_configparser_dict(parser: configparser.ConfigParser) -> dict:
- result = {}
- items = dict(parser.items())
-
- for section in parser.sections():
- result.update({str(section): dict(items[section])})
-
- return result
+from .ini_parser_compat import IniParserCompatibility
class IniParser(BaseParser):
@@ -50,4 +10,4 @@ class IniParser(BaseParser):
file_contents=file_contents,
module=IniParserCompatibility,
decode_error=configparser.Error,
- )
+ ) \ No newline at end of file
diff --git a/config_file/parsers/ini_parser_compat.py b/config_file/parsers/ini_parser_compat.py
new file mode 100644
index 0000000..9dfea2e
--- /dev/null
+++ b/config_file/parsers/ini_parser_compat.py
@@ -0,0 +1,42 @@
+import configparser
+from io import StringIO
+
+
+class IniParserCompatibility:
+ """
+ Provides compatibility with the json, yaml,
+ and toml API so we can use the BaseParser
+ easily, while still being able to leverage
+ the built-in configparser to parse the file.
+
+ It is used as the `module` parameter
+ for BaseParser, which requires us to have a
+ `dumps` and `loads` method.
+ """
+
+ @staticmethod
+ def loads(file_contents: str) -> dict:
+ parser = configparser.ConfigParser()
+ parser.read_string(file_contents)
+
+ return IniParserCompatibility.__create_configparser_dict(parser)
+
+ @staticmethod
+ def dumps(file_contents: dict) -> str:
+ parser = configparser.ConfigParser()
+ buffer = StringIO()
+
+ parser.read_dict(file_contents)
+ parser.write(buffer)
+
+ return buffer.getvalue()
+
+ @staticmethod
+ def __create_configparser_dict(parser: configparser.ConfigParser) -> dict:
+ result = {}
+ items = dict(parser.items())
+
+ for section in parser.sections():
+ result.update({str(section): dict(items[section])})
+
+ return result
diff --git a/config_file/parsers/parse_value.py b/config_file/parsers/parse_value.py
index 26bdf2e..aa8d1fe 100644
--- a/config_file/parsers/parse_value.py
+++ b/config_file/parsers/parse_value.py
@@ -19,20 +19,12 @@ Example:
can_be_parsed_as_int("-5") -> True
parse_value("-5") -> -5
"""
+import ast
import re
from distutils.util import strtobool
def parse_value(value):
- if can_be_parsed_as_int(value):
- return int(value)
-
- if can_be_parsed_as_float(value):
- return float(value)
-
- if can_be_parsed_as_bool(value):
- return value if type(value) is bool else bool(strtobool(value))
-
if type(value) is dict:
parsed = {}
@@ -49,6 +41,21 @@ def parse_value(value):
return parsed
+ if can_be_parsed_as_int(value):
+ return int(value)
+
+ if can_be_parsed_as_float(value):
+ return float(value)
+
+ if can_be_parsed_as_bool(value):
+ return value if type(value) is bool else bool(strtobool(value))
+
+ if can_be_parsed_as_dict(value):
+ return parse_value(ast.literal_eval(value))
+
+ if can_be_parsed_as_list(value):
+ return parse_value(ast.literal_eval(value))
+
return value
@@ -75,7 +82,7 @@ def can_be_parsed_as_float(value) -> bool:
return False
if value.startswith("-"):
- return bool(re.match(FLOAT_REGEX, value[1:]))
+ value = value[1:]
return bool(re.match(FLOAT_REGEX, value))
@@ -88,3 +95,39 @@ def can_be_parsed_as_bool(value) -> bool:
return False
return value.lower() == "true" or value.lower() == "false"
+
+
+def can_be_parsed_as_dict(value) -> bool:
+ if type(value) is dict:
+ return True
+
+ if type(value) is not str:
+ return False
+
+ value = value.strip()
+ if value[0] == "{" and value[-1] == "}":
+ try:
+ ast.literal_eval(value)
+ return True
+ except SyntaxError:
+ return False
+
+ return False
+
+
+def can_be_parsed_as_list(value) -> bool:
+ if type(value) is dict:
+ return True
+
+ if type(value) is not str:
+ return False
+
+ value = value.strip()
+ if value[0] == "[" and value[-1] == "]":
+ try:
+ ast.literal_eval(value)
+ return True
+ except SyntaxError:
+ return False
+
+ return False
diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py
index d369003..1c7f8a7 100644
--- a/config_file/parsers/toml_parser.py
+++ b/config_file/parsers/toml_parser.py
@@ -14,7 +14,7 @@ class TomlParser(BaseParser):
def __init__(self, file_contents: str):
if not TOML_AVAILABLE:
raise MissingDependencyError(
- "It doesn't appear `toml` is installed, but a toml "
+ "It doesn't appear `tomlkit` is installed, but a toml "
"file was attempted to be used. Install the `toml` "
"extra first with `pip install config-file[toml]`."
)
diff --git a/config_file/parsers/yaml_parser.py b/config_file/parsers/yaml_parser.py
index 77b59fe..babe873 100644
--- a/config_file/parsers/yaml_parser.py
+++ b/config_file/parsers/yaml_parser.py
@@ -1,12 +1,7 @@
try:
- import yaml
+ from ruamel.yaml import YAML, YAMLError, round_trip_dump, round_trip_load
YAML_AVAILABLE = True
-
- # Conform yaml to json and
- # toml package syntax
- yaml.loads = yaml.load
- yaml.dumps = yaml.dump
except ImportError:
YAML_AVAILABLE = False
@@ -19,11 +14,15 @@ class YamlParser(BaseParser):
def __init__(self, file_contents: str):
if not YAML_AVAILABLE:
raise MissingDependencyError(
- "It doesn't appear `PyYaml` is installed, but a yaml "
- "file was attempted to be used. Install the `toml` "
- "extra first with `pip install config-file[toml]`."
+ "It doesn't appear `ruamel.yaml` is installed, but a yaml "
+ "file was attempted to be used. Install the `yaml` "
+ "extra first with `pip install config-file[yaml]`."
)
+ yaml = YAML(typ="safe")
+ yaml.loads = round_trip_load
+ yaml.dumps = round_trip_dump
+
super().__init__(
- file_contents=file_contents, module=yaml, decode_error=yaml.YAMLError
+ file_contents=file_contents, module=yaml, decode_error=YAMLError
)
diff --git a/config_file/utils.py b/config_file/utils.py
index de31c69..a75968c 100644
--- a/config_file/utils.py
+++ b/config_file/utils.py
@@ -6,10 +6,12 @@ def split_on_dot(line: Union[str, Path], only_last_dot=False) -> List[str]:
"""
Split a string on the dot character (.).
- :param line: The line ot split on.
- :param only_last_dot: Only split on the last occurrence of the dot.
+ Args:
+ line: The line ot split on.
+ only_last_dot: Only split on the last occurrence of the dot.
- :raises ValueError: if the line does not have a dot.
+ Raises:
+ ValueError: if the line does not have a dot.
"""
if isinstance(line, Path):
line = str(line)
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
@@ -542,6 +542,32 @@ 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."
marker = "python_version >= \"3.6.1\" and python_version < \"4.0.0\""
@@ -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..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"
@@ -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"
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
--- a/tests/test_abstract_parser.py
+++ /dev/null
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
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):