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