Skip to content

Commit

Permalink
Merge pull request #492 from deronnax/drop-pendulum
Browse files Browse the repository at this point in the history
remove pendulum
  • Loading branch information
laixintao committed Apr 15, 2024
2 parents c0992a0 + e98349f commit 499769a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 71 deletions.
17 changes: 9 additions & 8 deletions iredis/completers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging
from typing import Iterable
from datetime import datetime, timezone

import pendulum
from dateutil.relativedelta import relativedelta
from prompt_toolkit.completion import (
CompleteEvent,
Completer,
Expand Down Expand Up @@ -102,31 +103,31 @@ def _completion_humanize_time(self, document: Document) -> Iterable[Completion]:
if not text.isnumeric():
return
current = int(text)
now = pendulum.now()
now = datetime.now()
for unit, minimum in self.when_lower_than.items():
if current <= minimum:
if self.future_time:
dt = now.add(**{f"{unit}s": current})
dt = now + relativedelta(**{f"{unit}s": current})
offset_text = "later"
else:
dt = now.subtract(**{f"{unit}s": current})
dt = now - relativedelta(**{f"{unit}s": current})
offset_text = "ago"

meta = f"{text} {unit}{'s' if current > 1 else ''} {offset_text} ({dt.format('YYYY-MM-DD HH:mm:ss')})"
meta = f"{text} {unit}{'s' if current > 1 else ''} {offset_text} ({dt.strftime('%Y-%m-%d %H:%M:%S')})"
yield Completion(
str(dt.int_timestamp * self.factor),
str(int(dt.timestamp()) * self.factor),
start_position=-len(document.text_before_cursor),
display_meta=meta,
)

def _completion_formatted_time(self, document: Document) -> Iterable[Completion]:
text = document.text
try:
dt = pendulum.parse(text)
dt = datetime.fromisoformat(text).replace(tzinfo=timezone.utc)
except Exception:
return
yield Completion(
str(dt.int_timestamp * self.factor),
str(int(dt.timestamp()) * self.factor),
start_position=-len(document.text_before_cursor),
display_meta=str(dt),
)
Expand Down
69 changes: 19 additions & 50 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ Pygments = "^2"
mistune = "^3.0"
configobj = "^5.0"
click = "^8.0"
pendulum = "^2.1.0"
# wcwidth 0.2.x uses pkg_resources which is not supported by PyOxidizer
wcwidth = "0.1.9"
packaging = "^23.0"
redis = "^5.0.0"
python-dateutil = "^2.8.2"

[tool.poetry.dev-dependencies]
pytest = "^7.2"
Expand All @@ -49,6 +49,9 @@ pexpect = "^4.7"
[tool.poetry.scripts]
iredis = 'iredis.entry:main'

[tool.poetry.group.dev.dependencies]
freezegun = "^1.4.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
21 changes: 9 additions & 12 deletions tests/unittests/test_completers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest.mock import MagicMock, patch
from unittest.mock import MagicMock

import pendulum
from freezegun import freeze_time
from prompt_toolkit.formatted_text import FormattedText
from prompt_toolkit.completion import Completion

Expand Down Expand Up @@ -180,9 +180,8 @@ def test_group_completer():
]


@patch("iredis.completers.pendulum.now")
def test_timestamp_completer_humanize_time_completion(fake_now):
fake_now.return_value = pendulum.from_timestamp(1578487013)
@freeze_time("2020-01-08 12:36:53")
def test_timestamp_completer_humanize_time_completion():
c = TimestampCompleter(is_milliseconds=True, future_time=False)

fake_document = MagicMock()
Expand Down Expand Up @@ -260,9 +259,8 @@ def test_timestamp_completer_humanize_time_completion(fake_now):
]


@patch("iredis.completers.pendulum.now")
def test_timestamp_completer_humanize_time_completion_seconds(fake_now):
fake_now.return_value = pendulum.from_timestamp(1578487013)
@freeze_time("2020-01-08 12:36:53")
def test_timestamp_completer_humanize_time_completion_seconds():
c = TimestampCompleter(is_milliseconds=False, future_time=False)

fake_document = MagicMock()
Expand Down Expand Up @@ -297,9 +295,8 @@ def test_timestamp_completer_humanize_time_completion_seconds(fake_now):
]


@patch("iredis.completers.pendulum.now")
def test_timestamp_completer_humanize_time_completion_seconds_future_time(fake_now):
fake_now.return_value = pendulum.from_timestamp(1578487013)
@freeze_time("2020-01-08 12:36:53")
def test_timestamp_completer_humanize_time_completion_seconds_future_time():
c = TimestampCompleter(is_milliseconds=False, future_time=True)

fake_document = MagicMock()
Expand Down Expand Up @@ -350,7 +347,7 @@ def test_timestamp_completer_datetime_format_time_completion():
text="1581033600000",
start_position=-10,
display=FormattedText([("", "1581033600000")]),
display_meta="2020-02-07T00:00:00+00:00",
display_meta="2020-02-07 00:00:00+00:00",
)
]

Expand Down

0 comments on commit 499769a

Please sign in to comment.