]> git.ipfire.org Git - ipfire.org.git/blame - src/backend/base.py
wiki: Add wrapper to send emails to watchers
[ipfire.org.git] / src / backend / base.py
CommitLineData
a6dc0bad
MT
1#!/usr/bin/python
2
c5ddbd67 3import configparser
11347e46 4import io
c5ddbd67 5import tornado.gen
11347e46
MT
6
7from . import accounts
8from . import database
9from . import geoip
10from . import fireinfo
11from . import iuse
e28b082e 12from . import memcached
11347e46
MT
13from . import mirrors
14from . import netboot
15from . import nopaste
16from . import releases
17from . import settings
18from . import talk
a6dc0bad 19
0a6875dc 20from . import blog
181d08f3 21from . import wiki
2c361abc
MT
22from . import zeiterfassung
23
11347e46 24DEFAULT_CONFIG = io.StringIO("""
9ed02e3b
MT
25[global]
26debug = false
27
28data_dir =
29static_dir = %(data_dir)s/static
30templates_dir = %(data_dir)s/templates
31""")
32
a6dc0bad 33class 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)