aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md16
-rw-r--r--CONTRIBUTING.md3
-rw-r--r--config_file/config_file.py25
-rw-r--r--config_file/parsers/base_parser.py4
-rw-r--r--config_file/parsers/ini_parser.py14
-rw-r--r--config_file/parsers/json_parser.py8
-rw-r--r--docs/source/getting-started.rst2
-rw-r--r--poetry.lock67
-rw-r--r--pyproject.toml4
-rw-r--r--tests/test_base_parser.py4
-rw-r--r--tests/test_config_file.py33
-rw-r--r--tests/test_ini_parser.py2
-rw-r--r--tests/test_json_parser.py2
13 files changed, 125 insertions, 59 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7a8e5af..72cdaa6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,22 @@ 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.
+
+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()` mothod is now cakked
+ `parse_types`.
## 0.8.0 - 2020-02-02
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 6b307cc..8669fd0 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -25,8 +25,7 @@ Here are some guidelines to keep in mind when submitting a pull request:
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 pytest tests/`` to run all tests.
- - If you'd like to run with test coverage, pass ``--cov=config_file/`` to pytest.
+- 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/).
diff --git a/config_file/config_file.py b/config_file/config_file.py
index 524272c..734bb57 100644
--- a/config_file/config_file.py
+++ b/config_file/config_file.py
@@ -66,22 +66,29 @@ class ConfigFile:
with open(self.path, "r") as file:
return file.read()
- def get(self, key: str, parse_type: bool = True, default=None):
+ def get(self, key: str, parse_types: bool = False, return_type=None, default=None):
"""
- 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_type is set to False, all values will be returned
- back as strings.
+ Retrieve the value of a key.
+
+ :param key: The key to retrieve.
+ :param parse_type: 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.
+
+ :return: The value of the key.
"""
if default is not None:
try:
- return self.parser.get(key, parse_type=parse_type)
+ return self.parser.get(key, parse_types=parse_types)
except ParsingError:
return default
- return self.parser.get(key, parse_type=parse_type)
+ key_value = self.parser.get(key, parse_types=parse_types)
+ return return_type(key_value) if return_type else key_value
def set(self, key: str, value):
"""Sets the value of a key."""
diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py
index 54756cd..42f93b1 100644
--- a/config_file/parsers/base_parser.py
+++ b/config_file/parsers/base_parser.py
@@ -25,13 +25,13 @@ class BaseParser(ABC):
self.parsed_content = self.parse(self.content)
@abstractmethod
- def get(self, key, parse_type=True):
+ def get(self, key, parse_types=False):
"""
Retrieve the value of a key in its native type.
This means the string 'true' will be parsed back as the
boolean True.
- If parse_type is set to False, all values will be returned
+ If parse_types is set to False, all values will be returned
back as strings.
"""
raise NotImplementedError
diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py
index ba44c64..81f2d1b 100644
--- a/config_file/parsers/ini_parser.py
+++ b/config_file/parsers/ini_parser.py
@@ -19,14 +19,14 @@ class IniParser(BaseParser):
except configparser.Error as error:
raise ParsingError(error)
- def get(self, section_key: str, parse_type: bool = True):
+ 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_type: Coerces the return value to its native type.
- :return: The value of the key, parsed to its native type if parse_type is True.
+ :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'
@@ -34,19 +34,19 @@ class IniParser(BaseParser):
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_type)
+ 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_type else value
+ return parse_value(value) if parse_types else value
except configparser.Error as error:
raise ParsingError(error.message)
- def __retrieve_section(self, section, parse_type):
+ def __retrieve_section(self, section, parse_types):
items = dict(self.parsed_content.items(section))
- if not parse_type:
+ if not parse_types:
return items
for item in items:
diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py
index 9e9f52d..67ddc11 100644
--- a/config_file/parsers/json_parser.py
+++ b/config_file/parsers/json_parser.py
@@ -16,7 +16,7 @@ class JsonParser(BaseParser):
"""
The JsonParser allows use of json files with ConfigFile. The syntax it
supports is a bit more flexible than the IniParser.
-
+
By default, the dot syntax is used. However, if you'd like to get/set/delete all
of a particular key, you can specify so with an optional parameter.
@@ -48,13 +48,13 @@ class JsonParser(BaseParser):
except json.decoder.JSONDecodeError as error:
raise ParsingError(error)
- def get(self, key: str, parse_type: bool = True, retrieve_all: bool = False):
+ def get(self, key: str, parse_types: bool = False, retrieve_all: bool = False):
"""
Retrieve values using a dot syntax.
:param key: The key to retrieve. e.g. 'foo.bar.boo'
- :param parse_type: Whether you'd like the type parsed into its native one.
+ :param parse_types: Whether you'd like the type parsed into its native one.
:param retrieve_all: Specify whether you'd like to recursively receive
all values that match the given key. Returned as a list.
@@ -82,7 +82,7 @@ class JsonParser(BaseParser):
else:
result = self.parsed_content[key]
- return parse_value(result) if parse_type else result
+ return parse_value(result) if parse_types else result
def set(self, key: str, value, set_all: bool = False):
if set_all:
diff --git a/docs/source/getting-started.rst b/docs/source/getting-started.rst
index 3ceb17e..92264a3 100644
--- a/docs/source/getting-started.rst
+++ b/docs/source/getting-started.rst
@@ -49,7 +49,7 @@ A section.key format is used for retrieving and setting values.
>>> False
# Unless you don't want them to be parsed
- config.get("calendar.start_week_on_sunday", parse_type=False)
+ config.get("calendar.start_week_on_sunday", parse_types=False)
>>> 'false'
config.get("calendar")
diff --git a/poetry.lock b/poetry.lock
index cdc96fc..8415ff0 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -1,5 +1,5 @@
[[package]]
-category = "main"
+category = "dev"
description = "A configurable sidebar-enabled Sphinx theme"
name = "alabaster"
optional = false
@@ -70,7 +70,7 @@ version = "1.3.1"
pyflakes = ">=1.1.0"
[[package]]
-category = "main"
+category = "dev"
description = "Internationalization utilities"
name = "babel"
optional = false
@@ -114,7 +114,7 @@ python-versions = "*"
version = "1.5.1"
[[package]]
-category = "main"
+category = "dev"
description = "Python package for providing Mozilla's CA Bundle."
name = "certifi"
optional = false
@@ -133,7 +133,7 @@ version = "2.0.1"
six = "*"
[[package]]
-category = "main"
+category = "dev"
description = "Universal encoding detector for Python 2 and 3"
name = "chardet"
optional = false
@@ -149,7 +149,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
version = "7.0"
[[package]]
-category = "main"
+category = "dev"
description = "Cross-platform colored terminal text."
marker = "sys_platform == \"win32\""
name = "colorama"
@@ -169,7 +169,7 @@ version = "5.0.3"
toml = ["toml"]
[[package]]
-category = "main"
+category = "dev"
description = "Docutils -- Python Documentation Utilities"
name = "docutils"
optional = false
@@ -210,7 +210,7 @@ version = "1.4.11"
license = ["editdistance"]
[[package]]
-category = "main"
+category = "dev"
description = "Internationalized Domain Names in Applications (IDNA)"
name = "idna"
optional = false
@@ -218,7 +218,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
version = "2.8"
[[package]]
-category = "main"
+category = "dev"
description = "Getting image size from png/jpeg/jpeg2000/gif file"
name = "imagesize"
optional = false
@@ -265,7 +265,7 @@ requirements = ["pipreqs", "pip-api"]
xdg_home = ["appdirs (>=1.4.0)"]
[[package]]
-category = "main"
+category = "dev"
description = "A very fast and expressive template engine."
name = "jinja2"
optional = false
@@ -279,7 +279,7 @@ MarkupSafe = ">=0.23"
i18n = ["Babel (>=0.8)"]
[[package]]
-category = "main"
+category = "dev"
description = "Safely add untrusted strings to HTML/XML markup."
name = "markupsafe"
optional = false
@@ -322,7 +322,7 @@ python-versions = "*"
version = "1.3.4"
[[package]]
-category = "main"
+category = "dev"
description = "Core utilities for Python packages"
name = "packaging"
optional = false
@@ -400,7 +400,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
version = "2.1.1"
[[package]]
-category = "main"
+category = "dev"
description = "Pygments is a syntax highlighting package written in Python."
name = "pygments"
optional = false
@@ -408,7 +408,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
version = "2.5.2"
[[package]]
-category = "main"
+category = "dev"
description = "Python parsing module"
name = "pyparsing"
optional = false
@@ -449,7 +449,7 @@ pytest = ">=3.6"
testing = ["fields", "hunter", "process-tests (2.0.2)", "six", "virtualenv"]
[[package]]
-category = "main"
+category = "dev"
description = "World timezone definitions, modern and historical"
name = "pytz"
optional = false
@@ -465,7 +465,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
version = "5.3"
[[package]]
-category = "main"
+category = "dev"
description = "Python HTTP for Humans."
name = "requests"
optional = false
@@ -502,7 +502,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*"
version = "1.14.0"
[[package]]
-category = "main"
+category = "dev"
description = "This package provides 26 stemmers for 25 languages generated from Snowball algorithms."
name = "snowballstemmer"
optional = false
@@ -510,7 +510,7 @@ python-versions = "*"
version = "2.0.0"
[[package]]
-category = "main"
+category = "dev"
description = "Python documentation generator"
name = "sphinx"
optional = false
@@ -541,7 +541,7 @@ docs = ["sphinxcontrib-websupport"]
test = ["pytest", "pytest-cov", "html5lib", "flake8 (>=3.5.0)", "flake8-import-order", "mypy (>=0.761)", "docutils-stubs"]
[[package]]
-category = "main"
+category = "dev"
description = "Read the Docs theme for Sphinx"
name = "sphinx-rtd-theme"
optional = false
@@ -552,7 +552,7 @@ version = "0.4.3"
sphinx = "*"
[[package]]
-category = "main"
+category = "dev"
description = ""
name = "sphinxcontrib-applehelp"
optional = false
@@ -563,7 +563,7 @@ version = "1.0.1"
test = ["pytest", "flake8", "mypy"]
[[package]]
-category = "main"
+category = "dev"
description = ""
name = "sphinxcontrib-devhelp"
optional = false
@@ -574,7 +574,7 @@ version = "1.0.1"
test = ["pytest", "flake8", "mypy"]
[[package]]
-category = "main"
+category = "dev"
description = ""
name = "sphinxcontrib-htmlhelp"
optional = false
@@ -585,7 +585,7 @@ version = "1.0.2"
test = ["pytest", "flake8", "mypy", "html5lib"]
[[package]]
-category = "main"
+category = "dev"
description = "A sphinx extension which renders display math in HTML via JavaScript"
name = "sphinxcontrib-jsmath"
optional = false
@@ -596,7 +596,7 @@ version = "1.0.1"
test = ["pytest", "flake8", "mypy"]
[[package]]
-category = "main"
+category = "dev"
description = ""
name = "sphinxcontrib-qthelp"
optional = false
@@ -607,7 +607,7 @@ version = "1.0.2"
test = ["pytest", "flake8", "mypy"]
[[package]]
-category = "main"
+category = "dev"
description = ""
name = "sphinxcontrib-serializinghtml"
optional = false
@@ -618,6 +618,17 @@ version = "1.1.3"
test = ["pytest", "flake8", "mypy"]
[[package]]
+category = "dev"
+description = "tasks runner for python projects"
+name = "taskipy"
+optional = false
+python-versions = ">=3.6,<4.0"
+version = "1.1.3"
+
+[package.dependencies]
+toml = ">=0.10.0,<0.11.0"
+
+[[package]]
category = "main"
description = "Python Library for Tom's Obvious, Minimal Language"
name = "toml"
@@ -626,7 +637,7 @@ python-versions = "*"
version = "0.10.0"
[[package]]
-category = "main"
+category = "dev"
description = "HTTP library with thread-safe connection pooling, file post, and more."
name = "urllib3"
optional = false
@@ -666,7 +677,7 @@ more-itertools = "*"
docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"]
[metadata]
-content-hash = "41f7048a3693045fee50d3fbd0701ef5c0cec5b0577a42ea294193e5b1bbf834"
+content-hash = "51fd6453afa3536f7868615c4414c984116ffe3e771244797ac750982f514f59"
python-versions = "^3.6"
[metadata.files]
@@ -955,6 +966,10 @@ sphinxcontrib-serializinghtml = [
{file = "sphinxcontrib-serializinghtml-1.1.3.tar.gz", hash = "sha256:c0efb33f8052c04fd7a26c0a07f1678e8512e0faec19f4aa8f2473a8b81d5227"},
{file = "sphinxcontrib_serializinghtml-1.1.3-py2.py3-none-any.whl", hash = "sha256:db6615af393650bf1151a6cd39120c29abaf93cc60db8c48eb2dddbfdc3a9768"},
]
+taskipy = [
+ {file = "taskipy-1.1.3-py3-none-any.whl", hash = "sha256:b5c94e1d06d5f5d10b916c29e12615c34ab701e2132fcdb5024572c728c83595"},
+ {file = "taskipy-1.1.3.tar.gz", hash = "sha256:2ca48f072549ed44091a8b29a14c37d22d8b6dad8c6a065469f6b944951c7f07"},
+]
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"},
diff --git a/pyproject.toml b/pyproject.toml
index 7a2150a..8cbb491 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -16,6 +16,9 @@ classifiers = [
"Topic :: Software Development :: Libraries :: Python Modules",
]
+[tool.taskipy.tasks]
+test = "poetry run pytest -v --cov=config_file/ tests/"
+
[tool.poetry.dependencies]
python = "^3.6"
toml = "^0.10.0"
@@ -34,6 +37,7 @@ pytest = "^3.0"
pytest-cov = "^2.8"
sphinx = "^2.3.1"
sphinx_rtd_theme = "^0.4.3"
+taskipy = "^1.1.3"
[tool.black]
line-length = 88
diff --git a/tests/test_base_parser.py b/tests/test_base_parser.py
index 64be16e..1127a61 100644
--- a/tests/test_base_parser.py
+++ b/tests/test_base_parser.py
@@ -7,8 +7,8 @@ class ConcreteParser(BaseParser):
def __init__(self, file_contents):
super().__init__(file_contents)
- def get(self, key, parse_type=True):
- super().get(key, parse_type=parse_type)
+ def get(self, key, parse_types=True):
+ super().get(key, parse_types=parse_types)
def set(self, key, value):
super().set(key, value)
diff --git a/tests/test_config_file.py b/tests/test_config_file.py
index 24b948b..f083538 100644
--- a/tests/test_config_file.py
+++ b/tests/test_config_file.py
@@ -76,12 +76,13 @@ def test_that_config_file_can_save(tmpdir, file_contents, file_name):
@pytest.mark.parametrize(
- "section_key, value, parse_type, default, file_name, file_contents",
+ "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",
@@ -90,6 +91,7 @@ def test_that_config_file_can_save(tmpdir, file_contents, file_name):
"calendar.sunday_index",
"0",
False,
+ None,
False,
"config.ini",
"[calendar]\nsunday_index = 0\n\n",
@@ -98,6 +100,7 @@ def test_that_config_file_can_save(tmpdir, file_contents, file_name):
"calendar.sunday_index",
0,
False,
+ None,
False,
"config.json",
json.dumps({"calendar": {"sunday_index": 0}}),
@@ -106,19 +109,41 @@ def test_that_config_file_can_save(tmpdir, file_contents, file_name):
"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, parse_type, file_name, file_contents, default
+ 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(str(config_path))
- assert config.get(section_key, parse_type=parse_type, default=default) == value
+
+ 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(
@@ -183,7 +208,7 @@ def test_that_custom_parser_can_be_used(tmpdir):
def __init__(self, file_contents):
super().__init__(file_contents)
- def get(self, key, parse_type=True):
+ def get(self, key, parse_types=True):
return key
def set(self, key, value):
diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py
index 7af3ea4..7cdeea8 100644
--- a/tests/test_ini_parser.py
+++ b/tests/test_ini_parser.py
@@ -41,7 +41,7 @@ floatkey = 5.3
"""
parser = IniParser(ini_file)
- assert parser.get(section_key, parse_type=parse_type) == expected_value
+ assert parser.get(section_key, parse_types=parse_type) == expected_value
@pytest.mark.parametrize(
diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py
index fd715a8..0e91395 100644
--- a/tests/test_json_parser.py
+++ b/tests/test_json_parser.py
@@ -121,7 +121,7 @@ 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_type=parse_type) == expected_result
+ assert parser.get(section_key, parse_types=parse_type) == expected_result
@pytest.mark.parametrize(