From 36484a6f90cc08925de4a0fd1a28be865b526637 Mon Sep 17 00:00:00 2001 From: Christophe Mehay Date: Mon, 14 Mar 2016 17:18:36 +0100 Subject: [PATCH] Remove Twiggy, using Colorlog instead --- Dockerfile.py2 | 2 +- Dockerfile.py3 | 2 +- Makefile | 3 ++ pyentrypoint/entrypoint.py | 9 ++--- pyentrypoint/logs.py | 81 ++++++++++++++++++++++++++++++++++++++ requirements-dev.txt | 1 + setup.py | 4 +- 7 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 pyentrypoint/logs.py diff --git a/Dockerfile.py2 b/Dockerfile.py2 index 09d8587..d03dc5d 100644 --- a/Dockerfile.py2 +++ b/Dockerfile.py2 @@ -2,7 +2,7 @@ FROM python:2 -RUN pip install pytest twiggy six pyyaml jinja2 +RUN pip install pytest six pyyaml jinja2 colorlog ENV PYTHONPATH /opt/ diff --git a/Dockerfile.py3 b/Dockerfile.py3 index 17a8a9a..76a6771 100644 --- a/Dockerfile.py3 +++ b/Dockerfile.py3 @@ -2,7 +2,7 @@ FROM python:3 -RUN pip3 install pytest twiggy six pyyaml jinja2 +RUN pip3 install pytest six pyyaml jinja2 colorlog ENV PYTHONPATH /opt/ diff --git a/Makefile b/Makefile index b6bb608..4b5de94 100644 --- a/Makefile +++ b/Makefile @@ -5,3 +5,6 @@ build: test: build @docker-compose up --force-recreate testpython2 testpython3 + +publish: + @python setup.py register && python setup.py sdist upload diff --git a/pyentrypoint/entrypoint.py b/pyentrypoint/entrypoint.py index 24fbee7..4cd4d19 100644 --- a/pyentrypoint/entrypoint.py +++ b/pyentrypoint/entrypoint.py @@ -13,13 +13,11 @@ from sys import stdout from jinja2 import Environment from jinja2 import FileSystemLoader -from twiggy import levels -from twiggy import log -from twiggy import quickSetup from .config import Config from .constants import ENTRYPOINT_FILE from .docker_links import DockerLinks +from .logs import Logs __all__ = ['Entrypoint', 'main'] @@ -29,8 +27,7 @@ class Entrypoint(object): """Entrypoint class.""" def _set_logguer(self): - quickSetup(min_level=levels.INFO) - self.log = log.name('entrypoint') + self.log = Logs().log def __init__(self, conf=ENTRYPOINT_FILE, args=[]): self._set_logguer() @@ -41,7 +38,7 @@ class Entrypoint(object): except Exception as err: self.log.error(err) if self.config.debug: - quickSetup(min_level=levels.DEBUG) + Logs.set_debug() self.args = args @property diff --git a/pyentrypoint/logs.py b/pyentrypoint/logs.py new file mode 100644 index 0000000..46581e0 --- /dev/null +++ b/pyentrypoint/logs.py @@ -0,0 +1,81 @@ +""" + Get log object +""" +from __future__ import absolute_import +from __future__ import unicode_literals + +import logging + +from colorlog import ColoredFormatter + + +class Switch(object): + """Just a mutable boolean to fool init Logs method""" + + def __init__(self, b=True): + self._v = b + + def __eq__(self, val): + return self._v == val + + def __bool__(self): + return self._v + + def __nonzero__(self): + "Python 2 bool" + return self._v + + def true(self): + self._v = True + + def false(self): + self._v = False + + +class Logs(object): + """Get a logguer""" + + lvl = logging.INFO + + # As log attribute is muttable, we can use method to change + # log level accross instances + log = logging.getLogger('Entrypoint') + + # Just a trick to avoid multiple formatter in logging instance + _switch = Switch(False) + + def __init__(self): + if bool(self._switch): + # Log is static, don't override it + return None + formatter = ColoredFormatter( + "%(name)s %(log_color)s%(levelname)-8s%(reset)s %(message)s", + datefmt=None, + reset=True, + log_colors={ + 'DEBUG': 'cyan', + 'INFO': 'green', + 'WARNING': 'yellow', + 'ERROR': 'red', + 'CRITICAL': 'red', + } + ) + + handler = logging.StreamHandler() + handler.setFormatter(formatter) + self.log.addHandler(handler) + self.log.setLevel(self.lvl) + + self._switch.true() + + @classmethod + def set_debug(cls): + """Set log level to debug""" + cls.lvl = logging.DEBUG + cls.log.setLevel(logging.DEBUG) + + @classmethod + def set_info(cls): + """Set log level to info""" + cls.lvl = logging.INFO + cls.log.setLevel(logging.INFO) diff --git a/requirements-dev.txt b/requirements-dev.txt index b2f614e..927c1c1 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -5,6 +5,7 @@ aspy.yaml==0.2.1 blessings==1.6 bpython==0.14.2 cached-property==1.3.0 +colorlog==2.6.1 curtsies==0.1.23 docker-compose==1.5.2 docker-py==1.6.0 diff --git a/setup.py b/setup.py index 550df91..4d98f2e 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ setup( name='pyentrypoint', - version='0.1.18', + version='0.2.1', packages=find_packages(), @@ -23,7 +23,7 @@ setup( install_requires=['Jinja2>=2.8', 'PyYAML>=3.11', - 'Twiggy>=0.4.7', + 'colorlog>=2.6.1', 'argparse>=1.4.0', 'six>=1.10.0'],