Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove pendulum #492

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible we can use builtin timedelta?

from datetime import tiemdelta

dt = now + timedelta(**{f"{unit}s": current})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That what I went with first, but timedelta doesn't handle month or year units, so I went with python-dateutils.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, got it!

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
Loading