Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use warnings pre log setup #10

Merged
merged 3 commits into from
Apr 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion climatecontrol/logtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
'root': {
'level': 'INFO',
'handlers': ["console"]
'handlers': ['console']
}
}

Expand Down
49 changes: 38 additions & 11 deletions climatecontrol/settings_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import json
import toml
import warnings
try:
import yaml
except ImportError:
Expand Down Expand Up @@ -55,8 +56,9 @@ class Settings(Mapping):
and output a nested dictionary.
preparsers: Sequence of preparsers (callables) to use. Note that
strings are assumed to be methods in the current class. Each
preprocessor must take a mapping as input and return a mapping
as output.
preprocessor must take a mapping as input and return a mapping as
output. Currently only :meth:`parse_from_file_vars` is available
and set by default.
parse_order: Order in which options are parsed. If no
``parse_order`` argument is given upon initialization, the
default order: ``("files", "env_file", "env", "external")`` is
Expand Down Expand Up @@ -243,8 +245,16 @@ def update(self, d: Optional[Union[Mapping, Dict]] = None, clear_external: bool
settings_map = self.subtree(settings_map)
self._data = self.parse(settings_map)

def parse(self, data) -> Dict:
"""Parse data into settings."""
def parse(self, data: Mapping) -> Dict:
"""Parse data into settings.

Args:
data: Raw mapping to be parsed

Returns:
Parsed data that has run through all preparsers and the `Settings`.

"""
for preparser in self.preparsers:
data = preparser(data)

Expand Down Expand Up @@ -285,7 +295,21 @@ def _parse_files(self) -> Dict[str, Any]:
return file_settings_map

def parse_from_file_vars(self, data: Any, postfix_trigger='_from_file') -> Any:
"""Parse settings from environment variables."""
"""Parse settings values from content local files.

Args:
data: Given subset of settings data (or entire settings mapping)
postfix_trigger: Optionally configurable string to trigger a local
file value. If a key is found which ends with this string, the
value is assumed to be a file path and the settings value will
be set to the content of the file.

Returns:
An updated copy of `data` with keys and values replaced accordingly.

Note: This method is typically used as a preparser method in order to in

"""
if not data:
return data
elif isinstance(data, Mapping):
Expand All @@ -304,7 +328,9 @@ def parse_from_file_vars(self, data: Any, postfix_trigger='_from_file') -> Any:
except FileNotFoundError as e:
logger.info('Error while trying to load variable from file: %s. Skipping...', e)
else:
new_data[k[:-len(postfix_trigger)]] = v_from_file
new_key = k[:-len(postfix_trigger)]
new_data[new_key] = v_from_file
logger.info('Settings key %s set to contents of file %s', new_key, v)
finally:
del new_data[k]
elif isinstance(v, (Mapping, Sequence)) and not isinstance(v, str):
Expand Down Expand Up @@ -368,8 +394,9 @@ def __init__(self,
"""Initialize object."""
self.settings_file_suffix = str(settings_file_suffix)
if max_depth is not None:
logger.warning('`max_depth` is deprecated and will be removed '
'in next release. Please use `implicit_depth` instead.')
# Use warnings module as logging probably isn't configured yet at this stage of the app.
warnings.warn('`max_depth` is deprecated and will be removed '
'in next release. Please use `implicit_depth` instead.')
self.implicit_depth = int(max_depth)
else:
self.implicit_depth = int(implicit_depth)
Expand Down Expand Up @@ -452,7 +479,7 @@ def parse(self, include_vars=True, include_file: bool = True) -> Dict[str, Any]:
"""
settings_map = {} # type: dict
for env_var, settings in self._iter_parse(include_vars=include_vars, include_file=include_file):
logger.info('Parsed setting from env var: {}.'.format(env_var))
logger.info('Parsed setting from env var: %s.', env_var)
update_nested(settings_map, settings)
return settings_map

Expand Down Expand Up @@ -652,10 +679,10 @@ def load(cls, path_or_content: str) -> Any:
if not isinstance(path_or_content, str):
raise TypeError('Expected "path_or_content" to be of type str, got {}'.format(type(path_or_content)))
if cls._is_path(path_or_content):
logger.debug('Loaded settings from file at path: {}'.format(path_or_content))
logger.info('Loaded settings from file at path: {}'.format(path_or_content))
return cls.from_path(path_or_content)
elif cls._is_content(path_or_content):
logger.debug('Loaded settings from string found in settings file env var')
logger.debug('Loaded settings from string.')
return cls.from_content(path_or_content)
else:
raise SettingsLoadError('path or content could not be loaded using {}'.format(cls.__name__))
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def read(fname):
setup(
name='climatecontrol',
use_scm_version=True,
description="Python library for loading app configurations from files and/or namespaced environment variables",
description='Python library for loading app configurations from files and/or namespaced environment variables',
long_description=read('README.rst'),
author="Davis Kirkendall",
author='Davis Kirkendall',
author_email='davis.e.kirkendall@gmail.com',
url='https://github.com/daviskirk/climatecontrol',
packages=[
Expand All @@ -45,7 +45,7 @@ def read(fname):
package_dir={'climatecontrol': 'climatecontrol'},
include_package_data=True,
install_requires=requirements,
license="MIT",
license='MIT',
zip_safe=False,
keywords='climatecontrol',
classifiers=[
Expand Down