Skip to content

Commit

Permalink
Removed config.py, moved general config related code to bnmutils::Con…
Browse files Browse the repository at this point in the history
…figParser
  • Loading branch information
BNMetrics committed May 31, 2018
1 parent 6e08814 commit 5d71c84
Show file tree
Hide file tree
Showing 21 changed files with 293 additions and 623 deletions.
14 changes: 10 additions & 4 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
CHANGELOG
=========

dev
==================
1.2.1 (2018-06-1)
=================

**Bug Fixes**

- Error handling for color_provider.py was outputting invalid error message when invalid style was passed in.


**Misc**

- changed utils.py::dict_to_conf() to use ConfigParser.read_dict()
- Changed _cli_utils.py::validate_conf() to use ConfigParser.has_section()
- Removed config.py, and moved everything configuration file related to `bnmutils <https://github.com/BNMetrics/bnmetrics-utils>`_ repository.
Moved everything logme configuration related to utils.py
- Changed exception.py::InvalidConfig to InvalidLoggerConfig



Expand Down
2 changes: 1 addition & 1 deletion logme/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.2.0'
__version__ = '1.2.1'
10 changes: 5 additions & 5 deletions logme/cli/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from pathlib import Path

from ..config import read_config
from bnmutils import ConfigParser

from ..exceptions import LogmeError
from ..utils import dict_to_config
from ..__version__ import __version__

from ._cli_utils import ensure_conf_exist, validate_conf, get_tpl, get_color_tpl
Expand Down Expand Up @@ -94,7 +94,7 @@ def init(ctx, project_root, override, mkdir, level, formatter, log_path):
formatter=formatter, filename=log_path)
conf_content.update(master_logging_config)

config = dict_to_config(conf_content)
config = ConfigParser.from_dict(conf_content)

abs_path = Path(project_root).resolve()
conf_location = abs_path.joinpath('logme.ini')
Expand Down Expand Up @@ -127,7 +127,7 @@ def add(ctx, project_root, name, level, formatter, log_path):
validate_conf(name, logme_conf)

conf_content = get_tpl(name, level=level, formatter=formatter, filename=log_path)
config = dict_to_config(conf_content)
config = ConfigParser.from_dict(conf_content)

with logme_conf.open('a') as conf:
config.write(conf)
Expand All @@ -153,7 +153,7 @@ def remove(ctx, name, project_root):

with ensure_conf_exist(project_root) as logme_conf:

config = read_config(logme_conf)
config = ConfigParser.from_files(logme_conf)
config.remove_section(name)

with logme_conf.open('w+') as conf:
Expand Down
11 changes: 5 additions & 6 deletions logme/cli/_cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

from typing import Union

from ..config import read_config
from bnmutils import ConfigParser
from ..exceptions import LogmeError
from ..utils import flatten_config_dict, ensure_dir
from ..utils import ensure_dir


@contextmanager
Expand Down Expand Up @@ -37,7 +37,7 @@ def validate_conf(name: str, ini_file_path: Union[str, Path]):
:param name: name of the section to be added
:param ini_file_path: path of the logme.ini file
"""
config = read_config(ini_file_path)
config = ConfigParser.from_files(ini_file_path)

if config.has_section(name):
raise LogmeError(f"'{name}' logging config already exists in config file: {ini_file_path}")
Expand All @@ -57,7 +57,7 @@ def get_color_tpl() -> dict:
'DEBUG': 'GREEN',
}

return {'colors': flatten_config_dict(color_config)}
return {'colors': color_config}


def get_tpl(name: str, **kwargs: str) -> dict:
Expand Down Expand Up @@ -97,8 +97,7 @@ def get_tpl(name: str, **kwargs: str) -> dict:

map_template(logger_template, kwargs)

# flatten config dictionary
config[name] = flatten_config_dict(logger_template)
config[name] = logger_template

return dict(config)

Expand Down
28 changes: 13 additions & 15 deletions logme/cli/_upgrade_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
from pathlib import Path
from copy import deepcopy

from configparser import NoSectionError
from bnmutils import ConfigParser

from ._cli_utils import get_color_tpl
from ..config import read_config
from ..utils import conf_to_dict, dict_to_config, flatten_config_dict


NONLOGGER_CONFIGS = ['colors']
Expand All @@ -20,20 +18,20 @@ def upgrade_to_latest(config_path: Union[str, Path]):
:param config_path: logme.ini file
"""
config = read_config(config_path)
# config = read_config(config_path)
config_dict = ConfigParser.from_files(config_path).to_dict()

config_dict_updated = {}

_upgrade_with_color_config(config, config_dict_updated)
_upgrade_with_color_config(config_dict, config_dict_updated)

for i in config.sections():
if i not in NONLOGGER_CONFIGS:
config_dict = conf_to_dict(config.items(i))
updated = _upgrade_logging_config_section(config_dict)
for k, v in config_dict.items():
if k not in NONLOGGER_CONFIGS:
updated = _upgrade_logging_config_section(v)

config_dict_updated[i] = flatten_config_dict(updated)
config_dict_updated[k] = updated

new_conf = dict_to_config(config_dict_updated)
new_conf = ConfigParser.from_dict(config_dict_updated)

with open(config_path, 'w') as file:
new_conf.write(file)
Expand Down Expand Up @@ -65,19 +63,19 @@ def _upgrade_logging_config_section(config_dict: dict) -> dict:
return latest


def _upgrade_with_color_config(config, config_dict_updated: dict):
def _upgrade_with_color_config(config_dict, config_dict_updated: dict):
"""
-- v1.2.0 update --
Upgrade the new config dict with color config.
* This function updates the original 'config_dict_updated'*
:param config: the original config to be passed
:param config_dict: the original config dictionary to be passed
:param config_dict_updated: new config dict to be written to 'logme.ini' file
"""
try:
color_config = {'colors': dict(config.items('colors'))}
except NoSectionError:
color_config = {'colors': config_dict['colors']}
except KeyError:
color_config = get_color_tpl()

config_dict_updated.update(color_config)
2 changes: 1 addition & 1 deletion logme/color_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self, color: str=None, style: str=None, bg: str=None):
if e.args[0] == 'reset':
self.text_style = {}
else:
if color:
if not self.color_map.get(color):
message = f"{e} is not a valid color"
else:
message = f"{e} is not a valid style or background color"
Expand Down
98 changes: 0 additions & 98 deletions logme/config.py

This file was deleted.

4 changes: 2 additions & 2 deletions logme/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class MisMatchScope(Exception):


class InvalidOption(Exception):
"""Used when the option in config file is invalid"""
"""Used when the an option is invalid"""


class InvalidConfig(Exception):
class InvalidLoggerConfig(Exception):
"""Used when invalid configuration is passed"""


Expand Down
3 changes: 1 addition & 2 deletions logme/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from logging import handlers as logging_handlers

from .color_provider import ColorFormatter
from .config import get_logger_config, get_color_config
from .utils import ensure_dir
from .utils import ensure_dir, get_logger_config, get_color_config
from .exceptions import InvalidOption, DuplicatedHandler, LogmeError


Expand Down
Loading

0 comments on commit 5d71c84

Please sign in to comment.