Skip to content

Commit e2f92b8

Browse files
Cleanup and pin MystBin dependencies (#45)
* pin requirements and remove unneeded deps * create a util for natural_time to negate the humanize dep * resolve dt math * Update core/utils.py Co-authored-by: Lilly Rose Berner <[email protected]> --------- Co-authored-by: Lilly Rose Berner <[email protected]>
1 parent 5cb705a commit e2f92b8

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

core/utils.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import base64
2222
import binascii
23+
import datetime
2324
import json
2425
import re
2526
import secrets
@@ -98,3 +99,37 @@ def validate_discord_token(token: str) -> bool:
9899
return False
99100
else:
100101
return True
102+
103+
104+
def natural_time(
105+
td: datetime.timedelta,
106+
/,
107+
*,
108+
source: datetime.datetime | None = None,
109+
) -> str:
110+
now = source or datetime.datetime.now(datetime.UTC)
111+
112+
then = now - td
113+
future = then > now
114+
115+
ago = "{delta} from now" if future else "{delta} ago"
116+
117+
seconds = round(td.total_seconds())
118+
weeks, seconds = divmod(seconds, 60 * 60 * 24 * 7)
119+
days, seconds = divmod(seconds, 60 * 60 * 24)
120+
hours, seconds = divmod(seconds, 60 * 60)
121+
minutes, seconds = divmod(seconds, 60)
122+
123+
ret = ""
124+
125+
if weeks:
126+
ret += f"{weeks} weeks,"
127+
if days:
128+
ret += f"{days} days,"
129+
if hours:
130+
ret += f"{hours} hours,"
131+
if minutes:
132+
ret += f"{minutes} minutes and "
133+
ret += f"{seconds} seconds"
134+
135+
return ago.format(delta=ret)

requirements.txt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
git+https://github.com/PythonistaGuild/StarlettePlus.git
2-
uvicorn
3-
asyncpg
4-
asyncpg-stubs
5-
bleach
6-
humanize
7-
python-multipart
8-
pyyaml
9-
aiohttp
1+
starlette_plus @ git+https://github.com/PythonistaGuild/StarlettePlus.git@f21169a
2+
uvicorn==0.29.0
3+
asyncpg==0.29.0
4+
asyncpg-stubs==0.29.1
5+
bleach==6.1.0
6+
python-multipart==0.0.9
7+
aiohttp==3.9.5

views/htmx.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@
2626

2727
import asyncpg
2828
import bleach
29-
import humanize
3029
import starlette_plus
3130

3231
from core import CONFIG
33-
from core.utils import validate_paste
32+
from core.utils import natural_time, validate_paste
3433

3534

3635
if TYPE_CHECKING:
@@ -155,7 +154,7 @@ async def paste(self, request: starlette_plus.Request) -> starlette_plus.Respons
155154
<div class="identifierHeader">
156155
<div class="identifierHeaderLeft">
157156
<a href="{url}">/{identifier}</a>
158-
<span>Created {humanize.naturaltime(created_delta)}...</span>
157+
<span>Created {natural_time(created_delta)}...</span>
159158
</div>
160159
{security_html}
161160
<div class="identifierHeaderSection">

0 commit comments

Comments
 (0)