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