This repository has been archived by the owner on Sep 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
config.py
103 lines (85 loc) · 2.8 KB
/
config.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
import configparser
class Config:
def __init__(self):
self.debug = False
self.module = None
self.database = None
self.useragent = None
self.ratelimit = 1.0
self.subreddit = None
self.r_username = None
self.r_password = None
self.r_oauth_key = None
self.r_oauth_secret = None
self.services = dict()
self.new_show_types = list()
self.post_title = None
self.post_body = None
self.post_formats = dict()
def from_file(file_path):
config = Config()
parsed = configparser.ConfigParser()
success = parsed.read(file_path)
if len(success) == 0:
print("Failed to load config file")
return config
if "data" in parsed:
sec = parsed["data"]
config.database = sec.get("database", None)
if "connection" in parsed:
sec = parsed["connection"]
config.useragent = sec.get("useragent", None)
config.ratelimit = sec.getfloat("ratelimit", 1.0)
if "reddit" in parsed:
sec = parsed["reddit"]
config.subreddit = sec.get("subreddit", None)
config.r_username = sec.get("username", None)
config.r_password = sec.get("password", None)
config.r_oauth_key = sec.get("oauth_key", None)
config.r_oauth_secret = sec.get("oauth_secret", None)
# Dynamically load service configuration
service_prefix = "service."
for section_name in [sec for sec in parsed.sections() if sec.startswith(service_prefix)]:
sec = parsed[section_name]
service_key = section_name[len(service_prefix):]
service_config = config.services[service_key] = {}
for key in sec.keys():
service_config[key] = sec.get(key)
if "options" in parsed:
sec = parsed["options"]
config.debug = sec["debug"]
from data.models import str_to_showtype
config.new_show_types.extend(map(lambda s: str_to_showtype(s), sec.get("new_show_types", "").split(" ")))
if "post" in parsed:
sec = parsed["post"]
config.post_title = sec.get("title", None)
config.post_body = sec.get("body", None)
for key in sec:
if key.startswith("format_") and len(key) > 7:
config.post_formats[key[7:]] = sec[key]
return config
def validate(config):
def is_bad_str(s):
return s is None or len(s) == 0
if is_bad_str(config.database):
return "database missing"
if is_bad_str(config.useragent):
return "useragent missing"
if config.ratelimit < 0:
warning("Rate limit can't be negative, defaulting to 1.0")
config.ratelimit = 1.0
if is_bad_str(config.subreddit):
return "subreddit missing"
if is_bad_str(config.r_username):
return "reddit username missing"
if is_bad_str(config.r_password):
return "reddit password missing"
if is_bad_str(config.r_oauth_key):
return "reddit oauth key missing"
if is_bad_str(config.r_oauth_secret):
return "reddit oauth secret missing"
if is_bad_str(config.post_title):
return "post title missing"
if is_bad_str(config.post_body):
return "post title missing"
return False