-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style(optional): apply basic ruff linting and formatting
- Loading branch information
1 parent
bc58d1c
commit 7d43518
Showing
11 changed files
with
3,110 additions
and
2,117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
@@ -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: | ||
|
@@ -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__": | ||
|
@@ -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") | ||
|
@@ -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) | ||
|
@@ -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()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
@@ -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) | ||
|
@@ -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() | ||
|
Oops, something went wrong.