Skip to content

Commit 7d43518

Browse files
committed
style(optional): apply basic ruff linting and formatting
1 parent bc58d1c commit 7d43518

File tree

11 files changed

+3110
-2117
lines changed

11 files changed

+3110
-2117
lines changed

code/planet-cache.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
#!/usr/bin/env python3
2-
"""Planet cache tool.
2+
"""Planet cache tool."""
33

4-
"""
5-
6-
__authors__ = [ "Scott James Remnant <[email protected]>",
7-
"Jeff Waugh <[email protected]>" ]
4+
__authors__ = ["Scott James Remnant <[email protected]>", "Jeff Waugh <[email protected]>"]
85
__license__ = "Python"
96

107

8+
import configparser
9+
import dbm
1110
import os
1211
import sys
1312
import time
14-
import dbm
15-
import configparser
1613

1714
import planet
1815

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

36+
3937
def usage_error(msg, *args):
4038
print(msg, " ".join(args), file=sys.stderr)
4139
print("Perhaps you need --help ?", file=sys.stderr)
4240
sys.exit(1)
4341

42+
4443
def print_keys(item, title):
4544
keys = item.keys()
4645
keys.sort()
47-
key_len = max([ len(k) for k in keys ])
46+
key_len = max([len(k) for k in keys])
4847

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

56+
5757
def fit_str(string, length):
5858
if len(string) <= length:
5959
return string
6060
else:
61-
return string[:length-4] + " ..."
61+
return string[: length - 4] + " ..."
6262

6363

6464
if __name__ == "__main__":
@@ -100,13 +100,12 @@ def fit_str(string, length):
100100
want_ids = 1
101101
elif arg.startswith("-"):
102102
usage_error("Unknown option:", arg)
103+
elif cache_file is None:
104+
cache_file = arg
105+
elif want_ids:
106+
ids.append(arg)
103107
else:
104-
if cache_file is None:
105-
cache_file = arg
106-
elif want_ids:
107-
ids.append(arg)
108-
else:
109-
usage_error("Unexpected extra argument:", arg)
108+
usage_error("Unexpected extra argument:", arg)
110109

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

116115
# Open the cache file directly to get the URL it represents
117116
try:
118-
with dbm.open(cache_file, 'r') as db:
119-
url = db[b"url"].decode('utf-8')
117+
with dbm.open(cache_file, "r") as db:
118+
url = db[b"url"].decode("utf-8")
120119
except dbm.error as e:
121-
print(f"{cache_file}: {str(e)}", file=sys.stderr)
120+
print(f"{cache_file}: {e!s}", file=sys.stderr)
122121
sys.exit(1)
123122
except KeyError:
124123
print(f"{cache_file}: Probably not a cache file", file=sys.stderr)
@@ -156,7 +155,7 @@ def fit_str(string, length):
156155
elif command == "keys":
157156
keys = {}
158157
for item in channel.items():
159-
for key in item.keys():
158+
for key in item:
160159
keys[key] = 1
161160

162161
keys = sorted(keys.keys())

code/planet.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,43 @@
99
Requires Python 2.1, recommends 2.3.
1010
"""
1111

12-
__authors__ = [ "Scott James Remnant <[email protected]>",
13-
"Jeff Waugh <[email protected]>" ]
12+
__authors__ = ["Scott James Remnant <[email protected]>", "Jeff Waugh <[email protected]>"]
1413
__license__ = "Python"
1514

1615

17-
import os
18-
import sys
16+
import configparser
1917
import locale
18+
import os
2019
import socket
21-
import configparser
20+
import sys
2221
from urllib.parse import urljoin
2322

2423
import planet
2524

26-
2725
# Default configuration file path
2826
CONFIG_FILE = "config.ini"
2927

3028
# Defaults for the [Planet] config section
3129
PLANET_NAME = "Unconfigured Planet"
3230
PLANET_LINK = "Unconfigured Planet"
3331
PLANET_FEED = None
34-
OWNER_NAME = "Anonymous Coward"
32+
OWNER_NAME = "Anonymous Coward"
3533
OWNER_EMAIL = ""
36-
LOG_LEVEL = "WARNING"
37-
FEED_TIMEOUT = 20 # seconds
34+
LOG_LEVEL = "WARNING"
35+
FEED_TIMEOUT = 20 # seconds
3836

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

4240

43-
4441
def config_get(config, section, option, default=None, raw=0, vars=None):
4542
"""Get a value from the configuration, with a default."""
4643
if config.has_option(section, option):
4744
return config.get(section, option, raw=raw, vars=None)
4845
else:
4946
return default
5047

48+
5149
def main():
5250
config_file = CONFIG_FILE
5351
offline = 0
@@ -81,24 +79,23 @@ def main():
8179
sys.exit(1)
8280

8381
# Read the [Planet] config section
84-
planet_name = config_get(config, "Planet", "name", PLANET_NAME)
85-
planet_link = config_get(config, "Planet", "link", PLANET_LINK)
86-
planet_feed = config_get(config, "Planet", "feed", PLANET_FEED)
87-
owner_name = config_get(config, "Planet", "owner_name", OWNER_NAME)
82+
planet_name = config_get(config, "Planet", "name", PLANET_NAME)
83+
planet_link = config_get(config, "Planet", "link", PLANET_LINK)
84+
planet_feed = config_get(config, "Planet", "feed", PLANET_FEED)
85+
owner_name = config_get(config, "Planet", "owner_name", OWNER_NAME)
8886
owner_email = config_get(config, "Planet", "owner_email", OWNER_EMAIL)
8987
if verbose:
9088
log_level = "DEBUG"
9189
else:
92-
log_level = config_get(config, "Planet", "log_level", LOG_LEVEL)
93-
feed_timeout = config_get(config, "Planet", "feed_timeout", FEED_TIMEOUT)
94-
template_files = config_get(config, "Planet", "template_files",
95-
TEMPLATE_FILES).split(" ")
90+
log_level = config_get(config, "Planet", "log_level", LOG_LEVEL)
91+
feed_timeout = config_get(config, "Planet", "feed_timeout", FEED_TIMEOUT)
92+
template_files = config_get(config, "Planet", "template_files", TEMPLATE_FILES).split(" ")
9693

9794
# Default feed to the first feed for which there is a template
9895
if not planet_feed:
9996
for template_file in template_files:
10097
name = os.path.splitext(os.path.basename(template_file))[0]
101-
if name.find('atom')>=0 or name.find('rss')>=0:
98+
if name.find("atom") >= 0 or name.find("rss") >= 0:
10299
planet_feed = urljoin(planet_link, name)
103100
break
104101

@@ -107,7 +104,7 @@ def main():
107104
# The user can specify more than one locale (separated by ":") as
108105
# fallbacks.
109106
locale_ok = False
110-
for user_locale in config.get("Planet", "locale").split(':'):
107+
for user_locale in config.get("Planet", "locale").split(":"):
111108
user_locale = user_locale.strip()
112109
try:
113110
locale.setlocale(locale.LC_ALL, user_locale)
@@ -144,10 +141,8 @@ def main():
144141
my_planet = planet.Planet(config)
145142
my_planet.run(planet_name, planet_link, template_files, offline)
146143

147-
my_planet.generate_all_files(template_files, planet_name,
148-
planet_link, planet_feed, owner_name, owner_email)
144+
my_planet.generate_all_files(template_files, planet_name, planet_link, planet_feed, owner_name, owner_email)
149145

150146

151147
if __name__ == "__main__":
152148
main()
153-

0 commit comments

Comments
 (0)