Skip to content

Commit d0a199e

Browse files
committed
feat: working-ish app
1 parent 7e5539e commit d0a199e

6 files changed

Lines changed: 202 additions & 231 deletions

File tree

code/planet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def main():
7272
config_file = arg
7373

7474
# Read the configuration file
75-
config = configparser()
75+
config = configparser.ConfigParser()
7676
config.read(config_file)
7777
if not config.has_section("Planet"):
7878
print("Configuration missing [Planet] section.", file=sys.stderr)

code/planet/__init__.py

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,32 @@
1010
__authors__ = ["Scott James Remnant <scott@netsplit.com>", "Jeff Waugh <jdub@perkypants.org>"]
1111
__license__ = "Python"
1212

13-
1413
# Modules available without separate import
15-
import cache
16-
import feedparser
17-
import htmltmpl
18-
import sanitize
19-
20-
try:
21-
import logging
22-
except:
23-
import compat_logging as logging
24-
25-
# Limit the effect of "from planet import *"
26-
__all__ = ("cache", "feedparser", "htmltmpl", "logging", "Planet", "Channel", "NewsItem")
27-
28-
2914
import dbm
3015
import os
3116
import re
17+
import sys
3218
import time
3319
from hashlib import md5
3420
from html.parser import HTMLParser
3521

22+
try:
23+
import logging
24+
except:
25+
import compat_logging as logging
26+
3627
try:
3728
from xml.sax.saxutils import escape
3829
except:
3930

4031
def escape(data):
4132
return data.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;")
4233

34+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
35+
from . import cache, feedparser, htmltmpl, sanitize
36+
37+
# Limit the effect of "from planet import *"
38+
__all__ = ("cache", "feedparser", "htmltmpl", "logging", "Planet", "Channel", "NewsItem")
4339

4440
# Version information (for generator headers)
4541
VERSION = "Planet/%s +http://www.planetplanet.org" % __version__
@@ -57,7 +53,6 @@ def escape(data):
5753
TIMEFMT_ISO = "%Y-%m-%dT%H:%M:%S+00:00"
5854
TIMEFMT_822 = "%a, %d %b %Y %H:%M:%S +0000"
5955

60-
6156
# Log instance to use here
6257
log = logging.getLogger("planet")
6358
try:
@@ -602,14 +597,18 @@ def update(self):
602597
updates the cached information about the feed and entries within it.
603598
"""
604599
info = feedparser.parse(self.url, etag=self.url_etag, modified=self.url_modified, agent=self._planet.user_agent)
605-
if info.has_key("status"):
600+
601+
if hasattr(info, "status"):
606602
self.url_status = str(info.status)
607-
elif info.has_key("entries") and len(info.entries) > 0:
608-
self.url_status = str(200)
609-
elif info.bozo and info.bozo_exception.__class__.__name__ == "Timeout":
610-
self.url_status = str(408)
603+
elif hasattr(info, "entries") and info.entries:
604+
self.url_status = "200"
605+
elif hasattr(info, "bozo") and info.bozo and hasattr(info, "bozo_exception"):
606+
if info.bozo_exception.__class__.__name__ == "Timeout":
607+
self.url_status = "408"
608+
else:
609+
self.url_status = "500"
611610
else:
612-
self.url_status = str(500)
611+
self.url_status = "500"
613612

614613
if self.url_status == "301" and (info.has_key("entries") and len(info.entries) > 0):
615614
log.warning("Feed has moved from <%s> to <%s>", self.url, info.url)
@@ -751,9 +750,9 @@ def update_entries(self, entries):
751750

752751
# Hide excess items the first time through
753752
if (
754-
self.last_updated is None
755-
and self._planet.new_feed_items
756-
and len(feed_items) > self._planet.new_feed_items
753+
self.last_updated is None
754+
and self._planet.new_feed_items
755+
and len(feed_items) > self._planet.new_feed_items
757756
):
758757
item.hidden = "yes"
759758
log.debug("Marked <%s> as hidden (new feed)", entry_id)
@@ -853,9 +852,9 @@ def update(self, entry):
853852
if entry[key].has_key("email") and entry[key].email:
854853
self.set_as_string(key.replace("_detail", "_email"), entry[key].email)
855854
if (
856-
entry[key].has_key("language")
857-
and entry[key].language
858-
and (not self._channel.has_key("language") or entry[key].language != self._channel.language)
855+
entry[key].has_key("language")
856+
and entry[key].language
857+
and (not self._channel.has_key("language") or entry[key].language != self._channel.language)
859858
):
860859
self.set_as_string(key.replace("_detail", "_language"), entry[key].language)
861860
elif key.endswith("_parsed"):
@@ -877,9 +876,9 @@ def update(self, entry):
877876
elif item.type == "text/plain":
878877
item.value = escape(item.value)
879878
if (
880-
item.has_key("language")
881-
and item.language
882-
and (not self._channel.has_key("language") or item.language != self._channel.language)
879+
item.has_key("language")
880+
and item.language
881+
and (not self._channel.has_key("language") or item.language != self._channel.language)
883882
):
884883
self.set_as_string(key + "_language", item.language)
885884
value += cache.utf8(item.value)

code/planet/cache.py

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,19 @@ def cache_key(self, key):
5555

5656
def cache_read(self):
5757
"""Read information from the cache."""
58-
if self._root:
59-
keys_key = " keys"
60-
else:
61-
keys_key = self._id
58+
keys_key = " keys" if self._root else self._id
6259

63-
if self._cache.has_key(keys_key):
60+
if keys_key in self._cache:
6461
keys = self._cache[keys_key].split(" ")
6562
else:
6663
return
6764

6865
for key in keys:
6966
cache_key = self.cache_key(key)
70-
if not self._cached.has_key(key) or self._cached[key]:
67+
if key not in self._cached or self._cached[key]:
7168
# Key either hasn't been loaded, or is one for the cache
7269
self._value[key] = self._cache[cache_key]
73-
self._type[key] = self._cache[cache_key + " type"]
70+
self._type[key] = self._cache[f"{cache_key} type"]
7471
self._cached[key] = 1
7572

7673
def cache_write(self, sync=1):
@@ -81,50 +78,42 @@ def cache_write(self, sync=1):
8178
for key in self.keys():
8279
cache_key = self.cache_key(key)
8380
if not self._cached[key]:
84-
if self._cache.has_key(cache_key):
81+
if cache_key in self._cache:
8582
# Non-cached keys need to be cleared
8683
del self._cache[cache_key]
87-
del self._cache[cache_key + " type"]
84+
del self._cache[f"{cache_key} type"]
8885
continue
8986

9087
keys.append(key)
9188
self._cache[cache_key] = self._value[key]
92-
self._cache[cache_key + " type"] = self._type[key]
93-
94-
if self._root:
95-
keys_key = " keys"
96-
else:
97-
keys_key = self._id
89+
self._cache[f"{cache_key} type"] = self._type[key]
9890

91+
keys_key = " keys" if self._root else self._id
9992
self._cache[keys_key] = " ".join(keys)
10093
if sync:
10194
self._cache.sync()
10295

10396
def cache_clear(self, sync=1):
10497
"""Remove information from the cache."""
105-
if self._root:
106-
keys_key = " keys"
107-
else:
108-
keys_key = self._id
98+
keys_key = " keys" if self._root else self._id
10999

110-
if self._cache.has_key(keys_key):
111-
keys = self._cache[keys_key].split(" ")
112-
del self._cache[keys_key]
113-
else:
100+
if keys_key not in self._cache:
114101
return
115102

103+
keys = self._cache[keys_key].split(" ")
104+
del self._cache[keys_key]
116105
for key in keys:
117106
cache_key = self.cache_key(key)
118107
del self._cache[cache_key]
119-
del self._cache[cache_key + " type"]
108+
del self._cache[f"{cache_key} type"]
120109

121110
if sync:
122111
self._cache.sync()
123112

124113
def has_key(self, key):
125114
"""Check whether the key exists."""
126115
key = key.replace(" ", "_")
127-
return self._value.has_key(key)
116+
return key in self._value
128117

129118
def key_type(self, key):
130119
"""Return the key type."""
@@ -196,9 +185,8 @@ def set_as_string(self, key, value, cached=1):
196185
def get_as_string(self, key):
197186
"""Return the key as a string value."""
198187
key = key.replace(" ", "_")
199-
if not self.has_key(key):
188+
if key not in self._value:
200189
raise KeyError(key)
201-
202190
return self._value[key]
203191

204192
def set_as_date(self, key, value, cached=1):
@@ -216,11 +204,10 @@ def set_as_date(self, key, value, cached=1):
216204
def get_as_date(self, key):
217205
"""Return the key as a date value."""
218206
key = key.replace(" ", "_")
219-
if not self.has_key(key):
207+
if key not in self._value:
220208
raise KeyError(key)
221-
222209
value = self._value[key]
223-
return tuple([int(i) for i in value.split(" ")])
210+
return tuple(int(i) for i in value.split(" "))
224211

225212
def set_as_null(self, key, value, cached=1):
226213
"""Set the key to the null value.
@@ -235,13 +222,13 @@ def set_as_null(self, key, value, cached=1):
235222
def get_as_null(self, key):
236223
"""Return the key as the null value."""
237224
key = key.replace(" ", "_")
238-
if not self.has_key(key):
225+
if key not in self._value:
239226
raise KeyError(key)
240227

241228
def del_key(self, key):
242229
"""Delete the given key."""
243230
key = key.replace(" ", "_")
244-
if not self.has_key(key):
231+
if key not in self._value:
245232
raise KeyError(key)
246233

247234
del self._value[key]
@@ -270,10 +257,9 @@ def __setattr__(self, key, value):
270257
self.set(key, value)
271258

272259
def __getattr__(self, key):
273-
if self.has_key(key):
260+
if key in self._value:
274261
return self.get(key)
275-
else:
276-
raise AttributeError(key)
262+
raise AttributeError(key)
277263

278264

279265
def filename(directory, filename):
@@ -292,13 +278,6 @@ def filename(directory, filename):
292278

293279
def utf8(value):
294280
"""Return the value as a UTF-8 string."""
295-
if type(value) == str:
296-
return value.encode("utf-8")
297-
else:
298-
try:
299-
return str(value, "utf-8").encode("utf-8")
300-
except UnicodeError:
301-
try:
302-
return str(value, "iso-8859-1").encode("utf-8")
303-
except UnicodeError:
304-
return str(value, "ascii", "replace").encode("utf-8")
281+
if isinstance(value, str):
282+
return value
283+
return value.decode("utf-8") if isinstance(value, bytes) else str(value)

0 commit comments

Comments
 (0)