Skip to content

Commit

Permalink
Fixes getpelican#2958. Replaces pytz dependency with zoneinfo.
Browse files Browse the repository at this point in the history
  • Loading branch information
willthong committed Jul 24, 2023
1 parent b8bf595 commit 1965b25
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Release type: minor

Replace `pytz` with native alternatives.
19 changes: 14 additions & 5 deletions pelican/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
from html import unescape
from urllib.parse import unquote, urljoin, urlparse, urlunparse

import pytz
from datetime import timezone

try:
import zoneinfo
except ModuleNotFoundError:
import backports.zoneinfo


from pelican.plugins import signals
from pelican.settings import DEFAULT_CONFIG
Expand Down Expand Up @@ -120,9 +126,12 @@ def __init__(self, content, metadata=None, settings=None,
self.date_format = self.date_format[1]

# manage timezone
default_timezone = settings.get('TIMEZONE', 'UTC')
timezone = getattr(self, 'timezone', default_timezone)
self.timezone = pytz.timezone(timezone)
default_timezone = settings.get("TIMEZONE", "UTC")
timezone = getattr(self, "timezone", default_timezone)
try:
self.timezone = zoneinfo.ZoneInfo(timezone)
except NameError:
self.timezone = backports.zoneinfo.ZoneInfo(timezone)

if hasattr(self, 'date'):
self.date = set_date_tzinfo(self.date, timezone)
Expand Down Expand Up @@ -525,7 +534,7 @@ def __init__(self, *args, **kwargs):
if self.date.tzinfo is None:
now = datetime.datetime.now()
else:
now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
now = datetime.datetime.utcnow().replace(tzinfo=timezone.utc)
if self.date > now:
self.status = 'draft'

Expand Down
23 changes: 14 additions & 9 deletions pelican/tools/pelican_quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

from jinja2 import Environment, FileSystemLoader

import pytz
try:
import zoneinfo
except ModuleNotFoundError:
import backports.zoneinfo

try:
import readline # NOQA
Expand All @@ -17,8 +20,8 @@
try:
import tzlocal
_DEFAULT_TIMEZONE = tzlocal.get_localzone().zone
except ImportError:
_DEFAULT_TIMEZONE = 'Europe/Rome'
except ModuleNotFoundError:
_DEFAULT_TIMEZONE = "Europe/Rome"

from pelican import __version__

Expand Down Expand Up @@ -158,16 +161,18 @@ def ask(question, answer=str, default=None, length=None):

def ask_timezone(question, default, tzurl):
"""Prompt for time zone and validate input"""
lower_tz = [tz.lower() for tz in pytz.all_timezones]
try:
tz_dict = {tz.lower(): tz for tz in zoneinfo.available_timezones()}
except NameError:
tz_dict = {tz.lower(): tz for tz in backports.zoneinfo.available_timezones()}
while True:
r = ask(question, str, default)
r = r.strip().replace(' ', '_').lower()
if r in lower_tz:
r = pytz.all_timezones[lower_tz.index(r)]
r = r.strip().replace(" ", "_").lower()
if r in tz_dict.keys():
r = tz_dict[r]
break
else:
print('Please enter a valid time zone:\n'
' (check [{}])'.format(tzurl))
print("Please enter a valid time zone:\n" " (check [{}])".format(tzurl))
return r


Expand Down
18 changes: 12 additions & 6 deletions pelican/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

import dateutil.parser

try:
import zoneinfo
except ModuleNotFoundError:
import backports.zoneinfo
from markupsafe import Markup

import pytz


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -919,10 +921,14 @@ def file_watcher(path):
def set_date_tzinfo(d, tz_name=None):
"""Set the timezone for dates that don't have tzinfo"""
if tz_name and not d.tzinfo:
tz = pytz.timezone(tz_name)
d = tz.localize(d)
return SafeDatetime(d.year, d.month, d.day, d.hour, d.minute, d.second,
d.microsecond, d.tzinfo)
try:
timezone = zoneinfo.ZoneInfo(tz_name)
except NameError:
timezone = backports.zoneinfo.ZoneInfo(tz_name)
d = d.replace(tzinfo=timezone)
return SafeDatetime(
d.year, d.month, d.day, d.hour, d.minute, d.second, d.microsecond, d.tzinfo
)
return d


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ feedgenerator = ">=1.9"
jinja2 = ">=2.7"
pygments = ">=2.6"
python-dateutil = ">=2.8"
pytz = ">=2020.1"
rich = ">=10.1"
unidecode = ">=1.1"
markdown = {version = ">=3.1", optional = true}
backports-zoneinfo = {version = "^0.2.1", python = "<3.9"}

[tool.poetry.dev-dependencies]
BeautifulSoup4 = "^4.9"
Expand Down

0 comments on commit 1965b25

Please sign in to comment.