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