Skip to content

Commit 1241a86

Browse files
committed
Various small fixes for 2 to 3
- use markupsafe instead of the stripHtml mini-class - fix sorting of news items - handle out of bound dates (terribly)
1 parent 4579636 commit 1241a86

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

code/planet/__init__.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import re
1919
import shelve
2020
import time
21+
from datetime import datetime
22+
from functools import total_ordering
2123
from hashlib import md5
2224
from html.parser import HTMLParser
2325
from typing import cast
@@ -95,7 +97,7 @@ def template_info(item, date_format):
9597
else:
9698
info[key] = item[key]
9799
if "title" in item.keys():
98-
info["title_plain"] = stripHtml(info["title"]).result
100+
info["title_plain"] = Markup(info["title"])
99101

100102
return info
101103

@@ -243,6 +245,7 @@ def run(self, planet_name, planet_link, template_files, offline=False):
243245

244246
# The other configuration blocks are channels to subscribe to
245247
for feed_url in self.config.sections():
248+
# The "Planet" config section is a special case. We also allow template-file specific configuration, apparently :D
246249
if feed_url == "Planet" or feed_url in template_files:
247250
continue
248251

@@ -457,7 +460,7 @@ def items(
457460

458461
if item.id not in seen_guids:
459462
seen_guids[item.id] = 1
460-
items.append((time.mktime(item.date), item.order, item))
463+
items.append((item.time_since_epoch, item.order, item))
461464

462465
# Sort the list
463466
if sorted:
@@ -597,7 +600,7 @@ def items(self, hidden=False, sorted=False):
597600
for item in self._items.values():
598601
if hidden or "hidden" not in item:
599602
try:
600-
items.append((time.mktime(item.date), item.order, item))
603+
items.append((item.time_since_epoch, item.order, item))
601604
except OverflowError:
602605
log.warning(f"Unable to parse date for {item.id}")
603606

@@ -855,12 +858,13 @@ def update_entries(self, entries):
855858
def get_name(self, key):
856859
"""Return the key containing the name."""
857860
for key in ("name", "title"):
858-
if key in self and self.key_type(key) != self.NULL:
861+
if self.has_key(key) and self.key_type(key) != self.NULL:
859862
return self.get_as_string(key)
860863

861864
return ""
862865

863866

867+
@total_ordering
864868
class NewsItem(cache.CachedInfo):
865869
"""An item of news.
866870
@@ -997,6 +1001,18 @@ def update(self, entry):
9971001
# Generate the date field if we need to
9981002
self.get_date("date")
9991003

1004+
def __eq__(self, other):
1005+
return self.id == other.id
1006+
1007+
def __lt__(self, other):
1008+
# compare on the date field, and then the order field
1009+
if self.date < other.date:
1010+
return True
1011+
elif self.date == other.date:
1012+
return self.order < other.order
1013+
else:
1014+
return False
1015+
10001016
def get_date(self, key):
10011017
"""Get (or update) the date key.
10021018
@@ -1030,6 +1046,13 @@ def get_date(self, key):
10301046
self.set_as_date(key, date)
10311047
return date
10321048

1049+
@property
1050+
def time_since_epoch(self) -> float:
1051+
try:
1052+
return time.mktime(self.date)
1053+
except OverflowError:
1054+
return 0.0
1055+
10331056
def get_content(self, key):
10341057
"""Return the key containing the content."""
10351058
for key in ("content", "tagline", "summary"):

code/planet/cache.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class CachedInfo:
4242
def __init__(self, cache: shelve.Shelf[Any], id_, root=False):
4343
self._type: dict[str, str] = {}
4444
self._value: dict[str, Any] = {}
45-
self._cached: dict[str, Any] = {}
45+
self._cached: dict[str, bool] = {}
4646

4747
self._cache = cache
4848
self._id = id_.replace(" ", "%20")
@@ -71,7 +71,7 @@ def cache_read(self) -> None:
7171
# Key either hasn't been loaded, or is one for the cache
7272
self._value[key] = self._cache[cache_key]
7373
self._type[key] = self._cache[f"{cache_key} type"]
74-
self._cached[key] = 1
74+
self._cached[key] = True
7575

7676
def cache_write(self, sync: bool = True):
7777
"""Write information to the cache."""
@@ -214,7 +214,7 @@ def get_as_date(self, key):
214214
value = self._value[key]
215215
return tuple(int(i) for i in value.split(" "))
216216

217-
def set_as_null(self, key, value, cached: bool = True):
217+
def set_as_null(self, key, _value, cached: bool = True):
218218
"""Set the key to the null value.
219219
220220
This only exists to make things less magic.

0 commit comments

Comments
 (0)