]>
Commit | Line | Data |
---|---|---|
a6dc0bad MT |
1 | #!/usr/bin/python |
2 | ||
c5ddbd67 | 3 | import configparser |
11347e46 | 4 | import io |
c5ddbd67 | 5 | import tornado.gen |
11347e46 MT |
6 | |
7 | from . import accounts | |
8 | from . import database | |
9 | from . import geoip | |
10 | from . import fireinfo | |
11 | from . import iuse | |
11347e46 MT |
12 | from . import mirrors |
13 | from . import netboot | |
14 | from . import nopaste | |
15 | from . import releases | |
16 | from . import settings | |
17 | from . import talk | |
a6dc0bad | 18 | |
0a6875dc | 19 | from . import blog |
181d08f3 | 20 | from . import wiki |
2c361abc MT |
21 | from . import zeiterfassung |
22 | ||
11347e46 | 23 | DEFAULT_CONFIG = io.StringIO(""" |
9ed02e3b MT |
24 | [global] |
25 | debug = false | |
26 | ||
27 | data_dir = | |
28 | static_dir = %(data_dir)s/static | |
29 | templates_dir = %(data_dir)s/templates | |
30 | """) | |
31 | ||
a6dc0bad | 32 | class Backend(object): |
2cd9af74 | 33 | def __init__(self, configfile, debug=False): |
9068dba1 MT |
34 | # Read configuration file. |
35 | self.config = self.read_config(configfile) | |
9ed02e3b MT |
36 | |
37 | # Enable debug logging if configured | |
38 | self.debug = debug or self.config.getboolean("global", "debug") | |
9068dba1 MT |
39 | |
40 | # Setup database. | |
41 | self.setup_database() | |
a6dc0bad | 42 | |
9068dba1 MT |
43 | # Initialize settings first. |
44 | self.settings = settings.Settings(self) | |
9068dba1 MT |
45 | |
46 | # Initialize backend modules. | |
a6dc0bad | 47 | self.accounts = accounts.Accounts(self) |
9068dba1 | 48 | self.geoip = geoip.GeoIP(self) |
66862195 | 49 | self.fireinfo = fireinfo.Fireinfo(self) |
9068dba1 MT |
50 | self.iuse = iuse.IUse(self) |
51 | self.mirrors = mirrors.Mirrors(self) | |
52 | self.netboot = netboot.NetBoot(self) | |
66862195 | 53 | self.nopaste = nopaste.Nopaste(self) |
9068dba1 | 54 | self.releases = releases.Releases(self) |
66862195 | 55 | self.talk = talk.Talk(self) |
9068dba1 | 56 | |
0a6875dc | 57 | self.blog = blog.Blog(self) |
181d08f3 | 58 | self.wiki = wiki.Wiki(self) |
2c361abc MT |
59 | self.zeiterfassung = zeiterfassung.ZeiterfassungClient(self) |
60 | ||
9068dba1 MT |
61 | def read_config(self, configfile): |
62 | cp = configparser.ConfigParser() | |
9ed02e3b MT |
63 | |
64 | # Initialize configuration with some sensible defaults | |
65 | cp.readfp(DEFAULT_CONFIG) | |
66 | ||
67 | # Parse file | |
9068dba1 MT |
68 | cp.read(configfile) |
69 | ||
70 | return cp | |
71 | ||
72 | def setup_database(self): | |
73 | """ | |
74 | Sets up the database connection. | |
75 | """ | |
76 | credentials = { | |
77 | "host" : self.config.get("database", "server"), | |
78 | "database" : self.config.get("database", "database"), | |
79 | "user" : self.config.get("database", "username"), | |
80 | "password" : self.config.get("database", "password"), | |
81 | } | |
82 | ||
83 | self.db = database.Connection(**credentials) | |
c5ddbd67 MT |
84 | |
85 | @tornado.gen.coroutine | |
86 | def run_task(self, task, *args, **kwargs): | |
87 | tasks = { | |
88 | "update-blog-feeds" : self.blog.update_feeds, | |
89 | } | |
90 | ||
91 | # Get the task from the list of all tasks | |
92 | func = tasks.get(task, None) | |
93 | if not func: | |
94 | raise ValueError("Unknown task: %s" % task) | |
95 | ||
96 | # Run the task | |
97 | r = yield func(*args, **kwargs) | |
98 | ||
99 | # If any error code has been returned, | |
100 | # we will end the program | |
101 | if r: | |
102 | raise SystemExit(r) |