Skip to content

Commit

Permalink
Merge pull request #347 from getlogbook/feature/remove-old-compatibil…
Browse files Browse the repository at this point in the history
…ity-stuff

Remove old compatibility stuff
  • Loading branch information
RazerM authored Jul 30, 2023
2 parents 81a1db9 + 6c54e0f commit 097e3a5
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 38 deletions.
6 changes: 2 additions & 4 deletions src/logbook/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,7 @@ def __init__(
#: where custom log processors can attach custom context sensitive
#: data.

# TODO: Replace the lambda with str when we remove support for python 2
self.extra = defaultdict(lambda: "", extra or ())
self.extra = defaultdict(str, extra or ())
#: If available, optionally the interpreter frame that pulled the
#: heavy init. This usually points to somewhere in the dispatcher.
#: Might not be available for all calls and is removed when the log
Expand Down Expand Up @@ -559,8 +558,7 @@ def update_from_dict(self, d):
if isinstance(self.time, str):
self.time = parse_iso8601(self.time)

# TODO: Replace the lambda with str when we remove support for python 2`
self.extra = defaultdict(lambda: "", self.extra)
self.extra = defaultdict(str, self.extra)
return self

def _format_message(self, msg, *args, **kwargs):
Expand Down
14 changes: 5 additions & 9 deletions src/logbook/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
import sys
import warnings
from collections.abc import Mapping
from datetime import date, datetime
from datetime import date, datetime, timezone

import logbook

_epoch_ord = date(1970, 1, 1).toordinal()


def redirect_logging(set_root_logger_level=True):
"""Permanently redirects logging to the stdlib. This also
Expand Down Expand Up @@ -213,12 +211,10 @@ def convert_level(self, level):

def convert_time(self, dt):
"""Converts a datetime object into a timestamp."""
year, month, day, hour, minute, second = dt.utctimetuple()[:6]
days = date(year, month, 1).toordinal() - _epoch_ord + day - 1
hours = days * 24 + hour
minutes = hours * 60 + minute
seconds = minutes * 60 + second
return seconds
if dt.tzinfo is None:
# Logbook uses naive datetimes to represent UTC (utcnow)
return dt.replace(tzinfo=timezone.utc).timestamp()
return dt.timestamp()

def convert_record(self, old_record):
"""Converts a record from logbook to logging."""
Expand Down
6 changes: 2 additions & 4 deletions tests/test_logging_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import logbook

from .utils import get_total_delta_seconds


def test_timedate_format(activation_strategy, logger):
"""
Expand All @@ -29,7 +27,7 @@ def test_timedate_format(activation_strategy, logger):
t1 = datetime.now()
t2 = datetime.utcnow()

tz_minutes_diff = get_total_delta_seconds(t1 - t2) / 60.0
tz_minutes_diff = (t1 - t2).total_seconds() / 60.0

if abs(tz_minutes_diff) < 1:
pytest.skip(
Expand All @@ -38,7 +36,7 @@ def test_timedate_format(activation_strategy, logger):
)

# get the difference between LogRecord local and utc times
logbook_minutes_diff = get_total_delta_seconds(time_local - time_utc) / 60.0
logbook_minutes_diff = (time_local - time_utc).total_seconds() / 60.0
assert abs(logbook_minutes_diff) > 1, (
"Localtime does not differ from UTC by more than 1 "
"minute (Local: %s, UTC: %s)" % (time_local, time_utc)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def test_default_format_bad_encoding(logger):


def test_custom_unicode_format_unicode(logger):
format_string = "[{record.level_name}] " "{record.channel}: {record.message}"
format_string = "[{record.level_name}] {record.channel}: {record.message}"
with capturing_stderr_context() as stream:
with logbook.StderrHandler(format_string=format_string):
logger.warn("\u2603")
assert "[WARNING] testlogger: \u2603" in stream.getvalue()


def test_custom_string_format_unicode(logger):
format_string = "[{record.level_name}] " "{record.channel}: {record.message}"
format_string = "[{record.level_name}] {record.channel}: {record.message}"
with capturing_stderr_context() as stream:
with logbook.StderrHandler(format_string=format_string):
logger.warn("\u2603")
Expand Down
21 changes: 2 additions & 19 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
:license: BSD, see LICENSE for more details.
"""
import functools
import os
import importlib
import sys
from contextlib import contextmanager
from io import StringIO
Expand All @@ -20,27 +20,10 @@
LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"


def get_total_delta_seconds(delta):
"""
Replacement for datetime.timedelta.total_seconds() for Python 2.5, 2.6
and 3.1
"""
return (
delta.microseconds + (delta.seconds + delta.days * 24 * 3600) * 10**6
) / 10**6


appveyor = pytest.mark.skipif(
os.environ.get("APPVEYOR") != "True", reason="AppVeyor CI test"
)

travis = pytest.mark.skipif(os.environ.get("TRAVIS") != "true", reason="Travis CI test")


def require_module(module_name):
found = True
try:
__import__(module_name)
importlib.import_module(module_name)
except ImportError:
found = False

Expand Down

0 comments on commit 097e3a5

Please sign in to comment.