[Python-modules-commits] [python-click-log] 01/05: Import python-click-log_0.2.0.orig.tar.gz
Filip Pytloun
fpytloun-guest at moszumanska.debian.org
Thu Sep 7 08:33:06 UTC 2017
This is an automated email from the git hooks/post-receive script.
fpytloun-guest pushed a commit to branch master
in repository python-click-log.
commit 711202b36632465cd24993c4be18ec7eeb6c5ac7
Author: Filip Pytloun <filip at pytloun.cz>
Date: Wed Sep 6 13:18:24 2017 +0200
Import python-click-log_0.2.0.orig.tar.gz
---
click_log/__init__.py | 5 +----
click_log/core.py | 50 ++++++++------------------------------------------
click_log/options.py | 12 ++++++++----
docs/index.rst | 17 +++++++++--------
setup.py | 20 ++++++++++++++++++++
tests/test_basic.py | 25 +++++++++++++++++++++----
6 files changed, 67 insertions(+), 62 deletions(-)
diff --git a/click_log/__init__.py b/click_log/__init__.py
index fd96638..db884dc 100644
--- a/click_log/__init__.py
+++ b/click_log/__init__.py
@@ -3,7 +3,7 @@
import click
-__version__ = '0.1.8'
+__version__ = '0.2.0'
if not hasattr(click, 'get_current_context'):
@@ -13,9 +13,6 @@ from .core import (
ClickHandler,
ColorFormatter,
basic_config,
- get_level,
- init,
- set_level,
)
from .options import simple_verbosity_option
diff --git a/click_log/core.py b/click_log/core.py
index 5098296..ed80376 100644
--- a/click_log/core.py
+++ b/click_log/core.py
@@ -2,6 +2,7 @@
import sys
+import collections
import functools
import logging
@@ -68,52 +69,17 @@ _default_handler = ClickHandler()
_default_handler.formatter = ColorFormatter()
+def _normalize_logger(logger):
+ if not isinstance(logger, logging.Logger):
+ logger = logging.getLogger(logger)
+ return logger
+
+
def basic_config(logger=None):
'''Set up the default handler (:py:class:`ClickHandler`) and formatter
(:py:class:`ColorFormatter`) on the given logger.'''
- if not isinstance(logger, logging.Logger):
- logger = logging.getLogger(logger)
+ logger = _normalize_logger(logger)
logger.handlers = [_default_handler]
logger.propagate = False
return logger
-
-
-def init(logger=None):
- '''Set the application's logger and call :py:func:`basic_config` on it.'''
- def decorator(f):
- @functools.wraps(f)
- def wrapper(*args, **kwargs):
- m = _meta()
- l = basic_config(logger=logger)
- l.setLevel(m.get('level', DEFAULT_LEVEL))
-
- if m.setdefault('logger', l) is not l:
- raise RuntimeError('Only one main logger allowed.')
-
- return f(*args, **kwargs)
- return wrapper
- return decorator
-
-
-def get_logger():
- '''Get the application's logger.'''
- return _meta().get('logger')
-
-
-def set_level(level):
- '''Set the level for the application's logger.'''
-
- if not isinstance(level, int):
- raise ValueError('Must be constant from `logging`.')
-
- logger = get_logger()
- if logger is not None:
- logger.setLevel(level)
-
- _meta()['level'] = level
-
-
-def get_level():
- '''Get the level for the application's logger.'''
- return _meta().get('level', DEFAULT_LEVEL)
diff --git a/click_log/options.py b/click_log/options.py
index 5af92f3..01fcb3b 100644
--- a/click_log/options.py
+++ b/click_log/options.py
@@ -1,11 +1,10 @@
import logging
import click
+from .core import _normalize_logger
-from .core import set_level
-
-def simple_verbosity_option(*names, **kwargs):
+def simple_verbosity_option(logger, *names, **kwargs):
'''A decorator that adds a `--verbosity, -v` option to the decorated
command.
@@ -15,6 +14,9 @@ def simple_verbosity_option(*names, **kwargs):
if not names:
names = ['--verbosity', '-v']
+ if isinstance(logger, str) and logger.startswith('-'):
+ raise ValueError('Since click-log 0.2.0, the first argument must now '
+ 'be a logger.')
kwargs.setdefault('default', 'INFO')
kwargs.setdefault('metavar', 'LVL')
@@ -22,6 +24,8 @@ def simple_verbosity_option(*names, **kwargs):
kwargs.setdefault('help', 'Either CRITICAL, ERROR, WARNING, INFO or DEBUG')
kwargs.setdefault('is_eager', True)
+ logger = _normalize_logger(logger)
+
def decorator(f):
def _set_level(ctx, param, value):
x = getattr(logging, value.upper(), None)
@@ -29,7 +33,7 @@ def simple_verbosity_option(*names, **kwargs):
raise click.BadParameter(
'Must be CRITICAL, ERROR, WARNING, INFO or DEBUG, not {}'
)
- set_level(x)
+ logger.setLevel(x)
return click.option(*names, callback=_set_level, **kwargs)(f)
return decorator
diff --git a/docs/index.rst b/docs/index.rst
index a6a22da..75ef3e1 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -65,10 +65,10 @@ This is where click-log comes in::
import logging
logger = logging.getLogger(__name__)
+ click_log.basic_config(logger)
@click.command()
- @click_log.simple_verbosity_option()
- @click_log.init(__name__)
+ @click_log.simple_verbosity_option(logger)
def cli():
logger.info("Dividing by zero.")
@@ -95,18 +95,19 @@ The :py:func:`simple_verbosity_option` decorator adds a ``--verbosity`` option
that takes a (case-insensitive) value of ``DEBUG``, ``INFO``, ``WARNING``,
``ERROR``, or ``CRITICAL``, and calls :py:func:`set_level` accordingly.
+.. note::
+
+ Make sure to define the `simple_verbosity_option` as early as possible.
+ Otherwise logging setup will not be early enough for some of your other
+ eager options.
+
API
===
-.. autofunction:: init
-
-.. autofunction:: simple_verbosity_option
.. autofunction:: basic_config
-.. autofunction:: get_level
-
-.. autofunction:: set_level
+.. autofunction:: simple_verbosity_option
Classes
-------
diff --git a/setup.py b/setup.py
index 7617854..13528f1 100644
--- a/setup.py
+++ b/setup.py
@@ -20,6 +20,26 @@ setup(
url='https://github.com/click-contrib/click-log',
license='MIT',
long_description=open('README.rst').read(),
+ classifiers=[
+ # See: https://pypi.python.org/pypi?:action=list_classifiers
+ 'Development Status :: 4 - Beta',
+ 'Environment :: Console',
+ 'Environment :: Plugins',
+ 'Intended Audience :: Developers',
+ 'License :: OSI Approved :: MIT License',
+ # List of python versions and their support status:
+ # https://en.wikipedia.org/wiki/CPython#Version_history
+ 'Programming Language :: Python',
+ 'Programming Language :: Python :: 2',
+ 'Programming Language :: Python :: 2.7',
+ 'Programming Language :: Python :: 3',
+ 'Programming Language :: Python :: 3.3',
+ 'Programming Language :: Python :: 3.4',
+ 'Programming Language :: Python :: 3.5',
+ 'Programming Language :: Python :: 3.6',
+ 'Programming Language :: Python :: Implementation :: CPython',
+ 'Topic :: System :: Logging',
+ ],
packages=['click_log'],
install_requires=[
'click',
diff --git a/tests/test_basic.py b/tests/test_basic.py
index 0628ffd..58aaa46 100644
--- a/tests/test_basic.py
+++ b/tests/test_basic.py
@@ -11,6 +11,8 @@ import pytest
test_logger = logging.getLogger(__name__)
+click_log.basic_config(test_logger)
+test_logger.level = logging.INFO
@pytest.fixture
@@ -20,7 +22,6 @@ def runner():
def test_basic(runner):
@click.command()
- @click_log.init()
def cli():
test_logger.info('hey')
test_logger.error('damn')
@@ -32,7 +33,6 @@ def test_basic(runner):
def test_multilines(runner):
@click.command()
- @click_log.init()
def cli():
test_logger.warning("""
Lorem ipsum dolor sit amet,
@@ -50,7 +50,6 @@ def test_multilines(runner):
def test_unicode(runner):
@click.command()
- @click_log.init()
def cli():
test_logger.error(u"""
❤️ 💔 💌 💕 💞 💓 💗 💖 💘
@@ -66,7 +65,6 @@ def test_unicode(runner):
def test_weird_types_log(runner):
@click.command()
- @click_log.init()
def cli():
test_logger.error(42)
test_logger.error('42')
@@ -76,3 +74,22 @@ def test_weird_types_log(runner):
result = runner.invoke(cli, catch_exceptions=False)
assert not result.exception
assert result.output == 'error: 42\n' * 4
+
+
+def test_early_logging(runner):
+ i = None
+
+ def callback(context, param, value):
+ test_logger.debug('catch me {}!'.format(i))
+
+ @click.command()
+ @click_log.simple_verbosity_option(test_logger)
+ @click.option('--config', is_eager=True, default=None, expose_value=False,
+ callback=callback)
+ def cli():
+ test_logger.debug('hello')
+
+ for i in range(2):
+ result = runner.invoke(cli, ['-v', 'debug'], catch_exceptions=False)
+ assert 'debug: hello' in result.output
+ assert 'debug: catch me {}!'.format(i) in result.output
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-click-log.git
More information about the Python-modules-commits
mailing list