This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from lendingblock/master
1.3.1 - color logging
- Loading branch information
Showing
15 changed files
with
159 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
"RedirectOutput" | ||
], | ||
"args": [ | ||
"tests/test_db_cli.py" | ||
"tests/test_cli.py" | ||
] | ||
} | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,7 @@ pytest-mock | |
twine | ||
agile-toolkit | ||
asynctest | ||
# | ||
# additional features | ||
colorlog | ||
raven-aiohttp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
"""Minimal OpenAPI asynchronous server application""" | ||
|
||
__version__ = '1.3.0' | ||
__version__ = '1.3.1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import os | ||
import logging | ||
|
||
import click | ||
|
||
try: | ||
import colorlog | ||
except ImportError: # pragma: no cover | ||
colorlog = None | ||
|
||
|
||
LEVEL = (os.environ.get('LOG_LEVEL') or 'info').upper() | ||
LOGGER_NAME = os.environ.get('APP_NAME') or 'openapi' | ||
LOG_FORMAT = '%(levelname)s: %(name)s: %(message)s' | ||
|
||
logger = logging.getLogger(LOGGER_NAME) | ||
|
||
|
||
def getLogger(name=None): | ||
if not name: | ||
return logger | ||
return logging.getLogger(f'{LOGGER_NAME}.{name}') | ||
|
||
|
||
@click.pass_context | ||
def setup_logging(ctx, verbose, quiet): | ||
if verbose: | ||
level = 'DEBUG' | ||
elif quiet: | ||
level = 'ERROR' | ||
else: | ||
level = LEVEL | ||
level = getattr(logging, level) if level != 'NONE' else None | ||
ctx.obj['log_level'] = level | ||
if level: | ||
logger.setLevel(level) | ||
if not logger.hasHandlers(): | ||
fmt = LOG_FORMAT | ||
if colorlog: | ||
handler = colorlog.StreamHandler() | ||
fmt = colorlog.ColoredFormatter(f'%(log_color)s{LOG_FORMAT}') | ||
else: # pragma: no cover | ||
handler = logging.StreamHandler() | ||
handler.setFormatter(fmt) | ||
logger.addHandler(handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from datetime import datetime | ||
|
||
import pytz | ||
|
||
|
||
UTC = pytz.utc | ||
|
||
|
||
def utcnow(): | ||
return datetime.now(tz=UTC) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import os | ||
import dotenv | ||
|
||
dotenv.load_dotenv() | ||
|
||
if not os.environ.get('PYTHON_ENV'): | ||
os.environ['PYTHON_ENV'] = 'test' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,8 @@ | |
ValidationError, data_field, bool_field, uuid_field, number_field, | ||
decimal_field, email_field, enum_field, date_time_field, | ||
ListValidator, UUIDValidator, EnumValidator, Choice, DateTimeValidator, | ||
NumberValidator, DecimalValidator, email_validator, BoolValidator, | ||
Validator, IntegerValidator, | ||
NumberValidator, DecimalValidator, EmailValidator, BoolValidator, | ||
Validator, IntegerValidator, VALIDATOR | ||
) | ||
|
||
|
||
|
@@ -129,6 +129,14 @@ def test_DateTimeValidator_dump(): | |
assert validator.dump(value.isoformat()) == value.isoformat() | ||
|
||
|
||
def test_DateTimeValidator_timezone(): | ||
value = datetime.now() | ||
field = date_time_field(timezone=True) | ||
validator = field.metadata[VALIDATOR] | ||
with pytest.raises(ValidationError): | ||
validator(field, value) | ||
|
||
|
||
def test_NumberValidator_valid(): | ||
field = number_field() | ||
validator = NumberValidator(min_value=-10, max_value=10, precision=2) | ||
|
@@ -224,21 +232,21 @@ def test_DecimalValidator_dump(): | |
|
||
def test_email_validator_valid(): | ||
field = email_field() | ||
assert email_validator(field, '[email protected]') == '[email protected]' | ||
assert email_validator(field, '[email protected]') == '[email protected]' | ||
assert email_validator(field, '[email protected]') == '[email protected]' | ||
assert EmailValidator()(field, '[email protected]') == '[email protected]' | ||
assert EmailValidator()(field, '[email protected]') == '[email protected]' | ||
assert EmailValidator()(field, '[email protected]') == '[email protected]' | ||
|
||
|
||
def test_email_validator_invalid(): | ||
field = email_field() | ||
with pytest.raises(ValidationError): | ||
email_validator(field, 'a@email') | ||
EmailValidator()(field, 'a@email') | ||
with pytest.raises(ValidationError): | ||
email_validator(field, 'email.com') | ||
EmailValidator()(field, 'email.com') | ||
with pytest.raises(ValidationError): | ||
email_validator(field, '@email.com') | ||
EmailValidator()(field, '@email.com') | ||
with pytest.raises(ValidationError): | ||
email_validator(field, 1) | ||
EmailValidator()(field, 1) | ||
|
||
|
||
def test_BoolValidator_valid(): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from unittest.mock import patch | ||
|
||
from click.testing import CliRunner | ||
|
||
from openapi.rest import rest | ||
from openapi.logger import getLogger | ||
|
||
|
||
def test_logger(): | ||
logger = getLogger() | ||
assert logger.name == 'openapi' | ||
logger = getLogger('foo') | ||
assert logger.name == 'openapi.foo' | ||
|
||
|
||
def test_serve(): | ||
runner = CliRunner() | ||
cli = rest(base_path='/v1') | ||
with patch('aiohttp.web.run_app') as mock: | ||
with patch('openapi.logger.logger.hasHandlers') as hasHandlers: | ||
hasHandlers.return_value = False | ||
with patch('openapi.logger.logger.addHandler') as addHandler: | ||
result = runner.invoke(cli, ['serve']) | ||
assert result.exit_code == 0 | ||
assert mock.call_count == 1 | ||
assert addHandler.call_count == 1 |