aboutsummaryrefslogtreecommitdiffstats
path: root/youtube_podcaster/config.py
diff options
context:
space:
mode:
Diffstat (limited to 'youtube_podcaster/config.py')
-rw-r--r--youtube_podcaster/config.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/youtube_podcaster/config.py b/youtube_podcaster/config.py
new file mode 100644
index 0000000..bfce902
--- /dev/null
+++ b/youtube_podcaster/config.py
@@ -0,0 +1,64 @@
1import sys
2import os
3import json
4
5
6class ConfigException(Exception):
7 def __init__(self, msg):
8 super(ConfigException, self).__init__("Config exception: %s" % (msg))
9
10
11class Config:
12 instance = None
13
14 def parse_config(config_file=None):
15 if not Config.instance:
16 Config.instance = Config(config_file)
17
18 return Config.instance
19
20 def __init__(self, args):
21 if args.config:
22 config = args.config
23 else:
24 config_file = "youtube-podcaster.json"
25
26 if sys.platform == "linux" and not hasattr(sys, "real_prefix"):
27 config = "/etc/%s" % (config_file)
28 else:
29 config = "%s/etc/%s" % (sys.prefix, config_file)
30
31 if not os.path.isfile(config):
32 raise ConfigException("%s not found" % (config))
33
34 try:
35 config = json.load(open(config))
36 except json.decoder.JSONDecodeError as e:
37 raise ConfigException("%s is not valid json: %s" % (
38 config, str(e)))
39
40 try:
41 self.server = config["server"]
42 self.youtube = config["youtube"]
43 self.podcasts = config["podcasts"]
44 except KeyError as e:
45 raise ConfigException("Missing %s-section in %s" % (
46 str(e), config))
47
48 for arg, value in vars(args).items():
49 if not value:
50 continue
51
52 if arg == "interface":
53 self.server["interface"] = value
54 elif arg == "port":
55 self.server["port"] = value
56 elif arg == "apikey":
57 self.youtube["api-key"] = value
58
59 def get_server_address(self):
60 interface = str(self.server["interface"])
61 port = int(self.server["port"])
62 return interface, port
63
64# vim: set ts=8 sw=4 tw=0 et :