-
Notifications
You must be signed in to change notification settings - Fork 2
/
planet.py
executable file
·147 lines (122 loc) · 4.34 KB
/
planet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
"""The Planet aggregator.
A flexible and easy-to-use aggregator for generating websites.
"""
import argparse
import configparser
import locale
import os
import socket
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_EMAIL = ""
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=False, 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
verbose = 0
parser = argparse.ArgumentParser(description="The Planet aggregator")
parser.add_argument(
"-v", "--verbose", action="store_true", help="DEBUG level logging during update"
)
parser.add_argument(
"-o",
"--offline",
action="store_true",
help="Update the Planet from the cache only",
)
parser.add_argument(
"config_file", nargs="?", help="Configuration file", default=CONFIG_FILE
)
args = parser.parse_args()
verbose = args.verbose
offline = args.offline
config_file = args.config_file
# Read the configuration file
config = configparser.ConfigParser()
config.read(config_file)
if not config.has_section("Planet"):
print("Configuration missing [Planet] section.", file=sys.stderr)
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)
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(" ")
# 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:
planet_feed = urljoin(planet_link, name)
break
# Define locale
if config.has_option("Planet", "locale"):
# The user can specify more than one locale (separated by ":") as
# fallbacks.
locale_ok = False
for user_locale in config.get("Planet", "locale").split(":"):
user_locale = user_locale.strip()
try:
locale.setlocale(locale.LC_ALL, user_locale)
except locale.Error:
pass
else:
locale_ok = True
break
if not locale_ok:
print("Unsupported locale setting.", file=sys.stderr)
sys.exit(1)
# Activate logging
planet.logging.basicConfig()
planet.logging.getLogger().setLevel(planet.logging.getLevelName(log_level))
log = planet.logging.getLogger("planet.runner")
try:
log.warning
except:
log.warning = log.warn
if feed_timeout:
try:
feed_timeout = float(feed_timeout)
except:
log.warning(
"Feed timeout set to invalid value '%s', skipping", feed_timeout
)
feed_timeout = None
if feed_timeout and not offline:
socket.setdefaulttimeout(feed_timeout)
log.debug("Socket timeout set to %d seconds", feed_timeout)
# run the planet
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
)
if __name__ == "__main__":
main()