2
0
mirror of https://github.com/cmehay/pyentrypoint synced 2024-10-30 15:21:11 +00:00

Merge pull request #1 from cmehay/color

Remove Twiggy, using Colorlog instead
This commit is contained in:
Christophe Mehay 2016-03-14 23:48:03 +01:00
commit 4a96418aa4
7 changed files with 92 additions and 10 deletions

View File

@ -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/

View File

@ -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/

View File

@ -5,3 +5,6 @@ build:
test: build
@docker-compose up --force-recreate testpython2 testpython3
publish:
@python setup.py register && python setup.py sdist upload

View File

@ -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

81
pyentrypoint/logs.py Normal file
View File

@ -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)

View File

@ -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

View File

@ -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'],