Skip to content

Commit

Permalink
style(optional): apply basic ruff linting and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobCoffee committed Jul 22, 2024
1 parent bc58d1c commit 7d43518
Show file tree
Hide file tree
Showing 11 changed files with 3,110 additions and 2,117 deletions.
37 changes: 18 additions & 19 deletions code/planet-cache.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#!/usr/bin/env python3
"""Planet cache tool.
"""Planet cache tool."""

"""

__authors__ = [ "Scott James Remnant <[email protected]>",
"Jeff Waugh <[email protected]>" ]
__authors__ = ["Scott James Remnant <[email protected]>", "Jeff Waugh <[email protected]>"]
__license__ = "Python"


import configparser
import dbm
import os
import sys
import time
import dbm
import configparser

import planet

Expand All @@ -36,15 +33,17 @@ def usage():
print(" -h, --help Display this help message and exit")
sys.exit(0)


def usage_error(msg, *args):
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 ])
key_len = max([len(k) for k in keys])

print(title + ":")
for key in keys:
Expand All @@ -54,11 +53,12 @@ def print_keys(item, title):
value = str(item[key])
print(" %-*s %s" % (key_len, key, fit_str(value, 74 - key_len)))


def fit_str(string, length):
if len(string) <= length:
return string
else:
return string[:length-4] + " ..."
return string[: length - 4] + " ..."


if __name__ == "__main__":
Expand Down Expand Up @@ -100,13 +100,12 @@ def fit_str(string, length):
want_ids = 1
elif arg.startswith("-"):
usage_error("Unknown option:", arg)
elif cache_file is None:
cache_file = arg
elif want_ids:
ids.append(arg)
else:
if cache_file is None:
cache_file = arg
elif want_ids:
ids.append(arg)
else:
usage_error("Unexpected extra argument:", arg)
usage_error("Unexpected extra argument:", arg)

if cache_file is None:
usage_error("Missing expected cache filename")
Expand All @@ -115,10 +114,10 @@ def fit_str(string, length):

# Open the cache file directly to get the URL it represents
try:
with dbm.open(cache_file, 'r') as db:
url = db[b"url"].decode('utf-8')
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)
print(f"{cache_file}: {e!s}", file=sys.stderr)
sys.exit(1)
except KeyError:
print(f"{cache_file}: Probably not a cache file", file=sys.stderr)
Expand Down Expand Up @@ -156,7 +155,7 @@ def fit_str(string, length):
elif command == "keys":
keys = {}
for item in channel.items():
for key in item.keys():
for key in item:
keys[key] = 1

keys = sorted(keys.keys())
Expand Down
41 changes: 18 additions & 23 deletions code/planet.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,43 @@
Requires Python 2.1, recommends 2.3.
"""

__authors__ = [ "Scott James Remnant <[email protected]>",
"Jeff Waugh <[email protected]>" ]
__authors__ = ["Scott James Remnant <[email protected]>", "Jeff Waugh <[email protected]>"]
__license__ = "Python"


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

import planet


# Default configuration file path
CONFIG_FILE = "config.ini"

# Defaults for the [Planet] config section
PLANET_NAME = "Unconfigured Planet"
PLANET_LINK = "Unconfigured Planet"
PLANET_FEED = None
OWNER_NAME = "Anonymous Coward"
OWNER_NAME = "Anonymous Coward"
OWNER_EMAIL = ""
LOG_LEVEL = "WARNING"
FEED_TIMEOUT = 20 # seconds
LOG_LEVEL = "WARNING"
FEED_TIMEOUT = 20 # seconds

# Default template file list
TEMPLATE_FILES = "examples/basic/planet.html.tmpl"



def config_get(config, section, option, default=None, raw=0, vars=None):
"""Get a value from the configuration, with a default."""
if config.has_option(section, option):
return config.get(section, option, raw=raw, vars=None)
else:
return default


def main():
config_file = CONFIG_FILE
offline = 0
Expand Down Expand Up @@ -81,24 +79,23 @@ def main():
sys.exit(1)

# Read the [Planet] config section
planet_name = config_get(config, "Planet", "name", PLANET_NAME)
planet_link = config_get(config, "Planet", "link", PLANET_LINK)
planet_feed = config_get(config, "Planet", "feed", PLANET_FEED)
owner_name = config_get(config, "Planet", "owner_name", OWNER_NAME)
planet_name = config_get(config, "Planet", "name", PLANET_NAME)
planet_link = config_get(config, "Planet", "link", PLANET_LINK)
planet_feed = config_get(config, "Planet", "feed", PLANET_FEED)
owner_name = config_get(config, "Planet", "owner_name", OWNER_NAME)
owner_email = config_get(config, "Planet", "owner_email", OWNER_EMAIL)
if verbose:
log_level = "DEBUG"
else:
log_level = config_get(config, "Planet", "log_level", LOG_LEVEL)
feed_timeout = config_get(config, "Planet", "feed_timeout", FEED_TIMEOUT)
template_files = config_get(config, "Planet", "template_files",
TEMPLATE_FILES).split(" ")
log_level = config_get(config, "Planet", "log_level", LOG_LEVEL)
feed_timeout = config_get(config, "Planet", "feed_timeout", FEED_TIMEOUT)
template_files = config_get(config, "Planet", "template_files", TEMPLATE_FILES).split(" ")

# Default feed to the first feed for which there is a template
if not planet_feed:
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:
if name.find("atom") >= 0 or name.find("rss") >= 0:
planet_feed = urljoin(planet_link, name)
break

Expand All @@ -107,7 +104,7 @@ def main():
# The user can specify more than one locale (separated by ":") as
# fallbacks.
locale_ok = False
for user_locale in config.get("Planet", "locale").split(':'):
for user_locale in config.get("Planet", "locale").split(":"):
user_locale = user_locale.strip()
try:
locale.setlocale(locale.LC_ALL, user_locale)
Expand Down Expand Up @@ -144,10 +141,8 @@ def main():
my_planet = planet.Planet(config)
my_planet.run(planet_name, planet_link, template_files, offline)

my_planet.generate_all_files(template_files, planet_name,
planet_link, planet_feed, owner_name, owner_email)
my_planet.generate_all_files(template_files, planet_name, planet_link, planet_feed, owner_name, owner_email)


if __name__ == "__main__":
main()

Loading

0 comments on commit 7d43518

Please sign in to comment.