Skip to content

Commit

Permalink
feat: initial py2 to 3 work
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobCoffee committed Jul 22, 2024
1 parent 2d349c0 commit 9979bc6
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 290 deletions.
93 changes: 45 additions & 48 deletions code/planet-cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#!/usr/bin/env python3
"""Planet cache tool.
"""
Expand All @@ -12,48 +11,48 @@
import os
import sys
import time
import dbhash
import ConfigParser
import dbm
import configparser

import planet


def usage():
print "Usage: planet-cache [options] CACHEFILE [ITEMID]..."
print
print "Examine and modify information in the Planet cache."
print
print "Channel Commands:"
print " -C, --channel Display known information on the channel"
print " -L, --list List items in the channel"
print " -K, --keys List all keys found in channel items"
print
print "Item Commands (need ITEMID):"
print " -I, --item Display known information about the item(s)"
print " -H, --hide Mark the item(s) as hidden"
print " -U, --unhide Mark the item(s) as not hidden"
print
print "Other Options:"
print " -h, --help Display this help message and exit"
print("Usage: planet-cache [options] CACHEFILE [ITEMID]...")
print()
print("Examine and modify information in the Planet cache.")
print()
print("Channel Commands:")
print(" -C, --channel Display known information on the channel")
print(" -L, --list List items in the channel")
print(" -K, --keys List all keys found in channel items")
print()
print("Item Commands (need ITEMID):")
print(" -I, --item Display known information about the item(s)")
print(" -H, --hide Mark the item(s) as hidden")
print(" -U, --unhide Mark the item(s) as not hidden")
print()
print("Other Options:")
print(" -h, --help Display this help message and exit")
sys.exit(0)

def usage_error(msg, *args):
print >>sys.stderr, msg, " ".join(args)
print >>sys.stderr, "Perhaps you need --help ?"
print(msg, " ".join(args), file=sys.stderr)
print("Perhaps you need --help ?", file=sys.stderr)
sys.exit(1)

def print_keys(item, title):
keys = item.keys()
keys.sort()
key_len = max([ len(k) for k in keys ])

print title + ":"
print(title + ":")
for key in keys:
if item.key_type(key) == item.DATE:
value = time.strftime(planet.TIMEFMT_ISO, item[key])
else:
value = str(item[key])
print " %-*s %s" % (key_len, key, fit_str(value, 74 - key_len))
print(" %-*s %s" % (key_len, key, fit_str(value, 74 - key_len)))

def fit_str(string, length):
if len(string) <= length:
Expand Down Expand Up @@ -116,24 +115,23 @@ def fit_str(string, length):

# Open the cache file directly to get the URL it represents
try:
db = dbhash.open(cache_file)
url = db["url"]
db.close()
except dbhash.bsddb._db.DBError, e:
print >>sys.stderr, cache_file + ":", e.args[1]
with dbm.open(cache_file, 'r') as db:
url = db[b"url"].decode('utf-8')
except dbm.error as e:
print(f"{cache_file}: {str(e)}", file=sys.stderr)
sys.exit(1)
except KeyError:
print >>sys.stderr, cache_file + ": Probably not a cache file"
print(f"{cache_file}: Probably not a cache file", file=sys.stderr)
sys.exit(1)

# Now do it the right way :-)
my_planet = planet.Planet(ConfigParser.ConfigParser())
my_planet = planet.Planet(configparser.ConfigParser())
my_planet.cache_directory = os.path.dirname(cache_file)
channel = planet.Channel(my_planet, url)

for item_id in ids:
if not channel.has_item(item_id):
print >>sys.stderr, item_id + ": Not in channel"
print(item_id + ": Not in channel", file=sys.stderr)
sys.exit(1)

# Do the user's bidding
Expand All @@ -146,49 +144,48 @@ def fit_str(string, length):
print_keys(item, "Item Keys for %s" % item_id)

elif command == "list":
print "Items in Channel:"
print("Items in Channel:")
for item in channel.items(hidden=1, sorted=1):
print " " + item.id
print " " + time.strftime(planet.TIMEFMT_ISO, item.date)
print(" " + item.id)
print(" " + time.strftime(planet.TIMEFMT_ISO, item.date))
if hasattr(item, "title"):
print " " + fit_str(item.title, 70)
print(" " + fit_str(item.title, 70))
if hasattr(item, "hidden"):
print " (hidden)"
print(" (hidden)")

elif command == "keys":
keys = {}
for item in channel.items():
for key in item.keys():
keys[key] = 1

keys = keys.keys()
keys.sort()
keys = sorted(keys.keys())

print "Keys used in Channel:"
print("Keys used in Channel:")
for key in keys:
print " " + key
print
print(" " + key)
print()

print "Use --item to output values of particular items."
print("Use --item to output values of particular items.")

elif command == "hide":
for item_id in ids:
item = channel.get_item(item_id)
if hasattr(item, "hidden"):
print item_id + ": Already hidden."
print(item_id + ": Already hidden.")
else:
item.hidden = "yes"

channel.cache_write()
print "Done."
print("Done.")

elif command == "unhide":
for item_id in ids:
item = channel.get_item(item_id)
if hasattr(item, "hidden"):
del(item.hidden)
del item.hidden
else:
print item_id + ": Not hidden."
print(item_id + ": Not hidden.")

channel.cache_write()
print "Done."
print("Done.")
31 changes: 15 additions & 16 deletions code/planet.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""The Planet aggregator.
A flexible and easy-to-use aggregator for generating websites.
Expand All @@ -16,14 +16,13 @@

import os
import sys
import time
import locale
import socket
import urlparse
import configparser
from urllib.parse import urljoin

import planet

from ConfigParser import ConfigParser

# Default configuration file path
CONFIG_FILE = "config.ini"
Expand Down Expand Up @@ -56,29 +55,29 @@ def main():

for arg in sys.argv[1:]:
if arg == "-h" or arg == "--help":
print "Usage: planet [options] [CONFIGFILE]"
print
print "Options:"
print " -v, --verbose DEBUG level logging during update"
print " -o, --offline Update the Planet from the cache only"
print " -h, --help Display this help message and exit"
print
print("Usage: planet [options] [CONFIGFILE]")
print()
print("Options:")
print(" -v, --verbose DEBUG level logging during update")
print(" -o, --offline Update the Planet from the cache only")
print(" -h, --help Display this help message and exit")
print()
sys.exit(0)
elif arg == "-v" or arg == "--verbose":
verbose = 1
elif arg == "-o" or arg == "--offline":
offline = 1
elif arg.startswith("-"):
print >>sys.stderr, "Unknown option:", arg
print("Unknown option:", arg, file=sys.stderr)
sys.exit(1)
else:
config_file = arg

# Read the configuration file
config = ConfigParser()
config = configparser()
config.read(config_file)
if not config.has_section("Planet"):
print >>sys.stderr, "Configuration missing [Planet] section."
print("Configuration missing [Planet] section.", file=sys.stderr)
sys.exit(1)

# Read the [Planet] config section
Expand All @@ -100,7 +99,7 @@ def main():
for template_file in template_files:
name = os.path.splitext(os.path.basename(template_file))[0]
if name.find('atom')>=0 or name.find('rss')>=0:
planet_feed = urlparse.urljoin(planet_link, name)
planet_feed = urljoin(planet_link, name)
break

# Define locale
Expand All @@ -118,7 +117,7 @@ def main():
locale_ok = True
break
if not locale_ok:
print >>sys.stderr, "Unsupported locale setting."
print("Unsupported locale setting.", file=sys.stderr)
sys.exit(1)

# Activate logging
Expand Down
32 changes: 17 additions & 15 deletions code/planet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#!/usr/bin/env python3
"""Planet aggregator library.
This package is a library for developing web sites or software that
Expand All @@ -18,7 +17,6 @@
import feedparser
import sanitize
import htmltmpl
import sgmllib
try:
import logging
except:
Expand All @@ -29,10 +27,11 @@
"Planet", "Channel", "NewsItem")


from html.parser import HTMLParser
import os
import md5
from hashlib import md5
import time
import dbhash
import dbm
import re

try:
Expand Down Expand Up @@ -74,15 +73,18 @@ def escape(data):
NEW_DATE_FORMAT = "%B %d, %Y"
ACTIVITY_THRESHOLD = 0

class stripHtml(sgmllib.SGMLParser):

class stripHtml(HTMLParser):
"remove all tags from the data"
def __init__(self, data):
sgmllib.SGMLParser.__init__(self)
self.result=''
self.feed(data)
self.close()
def __init__(self):
super().__init__()
self.result = []

def handle_data(self, data):
if data: self.result+=data
self.result.append(data)

def get_data(self):
return "".join(self.result)

def template_info(item, date_format):
"""Produce a dictionary of template information."""
Expand Down Expand Up @@ -504,7 +506,7 @@ def __init__(self, planet, url):
if not os.path.isdir(planet.cache_directory):
os.makedirs(planet.cache_directory)
cache_filename = cache.filename(planet.cache_directory, url)
cache_file = dbhash.open(cache_filename, "c", 0666)
cache_file = dbm.open(cache_filename, "c", 0o666)

cache.CachedInfo.__init__(self, cache_file, url, root=1)

Expand Down Expand Up @@ -695,7 +697,7 @@ def update_info(self, feed):
self.set_as_string(key + "_width", str(feed[key].width))
if feed[key].has_key("height"):
self.set_as_string(key + "_height", str(feed[key].height))
elif isinstance(feed[key], (str, unicode)):
elif isinstance(feed[key], str):
# String fields
try:
detail = key + '_detail'
Expand Down Expand Up @@ -890,7 +892,7 @@ def update(self, entry):
self.set_as_string(key + "_language", item.language)
value += cache.utf8(item.value)
self.set_as_string(key, value)
elif isinstance(entry[key], (str, unicode)):
elif isinstance(entry[key], str):
# String fields
try:
detail = key + '_detail'
Expand Down
12 changes: 6 additions & 6 deletions code/planet/atomstyler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from xml.dom import minidom, Node
from urlparse import urlparse, urlunparse
from urllib.parse import urlparse, urlunparse
from xml.parsers.expat import ExpatError
from htmlentitydefs import name2codepoint
from html.entities import name2codepoint
import re

# select and apply an xml:base for this entry
Expand Down Expand Up @@ -75,20 +75,20 @@ def retype(parent):
elif len(node.childNodes)==1:

# replace html entity defs with utf-8
chunks=re.split('&(\w+);', node.childNodes[0].nodeValue)
chunks=re.split(r'&(\w+);', node.childNodes[0].nodeValue)
for i in range(1,len(chunks),2):
if chunks[i] in ['amp', 'lt', 'gt', 'apos', 'quot']:
chunks[i] ='&' + chunks[i] +';'
elif chunks[i] in name2codepoint:
chunks[i]=unichr(name2codepoint[chunks[i]])
chunks[i] = chr(name2codepoint[chunks[i]])
else:
chunks[i]='&' + chunks[i] + ';'
text = u"".join(chunks)
text = "".join(chunks)

try:
# see if the resulting text is a well-formed XML fragment
div = '<div xmlns="http://www.w3.org/1999/xhtml">%s</div>'
data = minidom.parseString((div % text.encode('utf-8')))
data = minidom.parseString(div % text.encode('utf-8'))

if text.find('<') < 0:
# plain text
Expand Down
Loading

0 comments on commit 9979bc6

Please sign in to comment.