]> git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
blog: Implement read-only backend
[ipfire.org.git] / src / backend / base.py
1 #!/usr/bin/python
2
3 import ConfigParser as configparser
4 import StringIO
5
6 import accounts
7 import ads
8 import database
9 import geoip
10 import fireinfo
11 import iuse
12 import memcached
13 import mirrors
14 import netboot
15 import nopaste
16 import news
17 import planet
18 import releases
19 import settings
20 import talk
21
22 from . import blog
23 from . import zeiterfassung
24
25 DEFAULT_CONFIG = StringIO.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.advertisements = ads.Advertisements(self)
52 self.downloads = mirrors.Downloads(self)
53 self.geoip = geoip.GeoIP(self)
54 self.fireinfo = fireinfo.Fireinfo(self)
55 self.iuse = iuse.IUse(self)
56 self.mirrors = mirrors.Mirrors(self)
57 self.netboot = netboot.NetBoot(self)
58 self.nopaste = nopaste.Nopaste(self)
59 self.news = news.News(self)
60 self.planet = planet.Planet(self)
61 self.releases = releases.Releases(self)
62 self.talk = talk.Talk(self)
63
64 self.blog = blog.Blog(self)
65 self.zeiterfassung = zeiterfassung.ZeiterfassungClient(self)
66
67 def read_config(self, configfile):
68 cp = configparser.ConfigParser()
69
70 # Initialize configuration with some sensible defaults
71 cp.readfp(DEFAULT_CONFIG)
72
73 # Parse file
74 cp.read(configfile)
75
76 return cp
77
78 def setup_database(self):
79 """
80 Sets up the database connection.
81 """
82 credentials = {
83 "host" : self.config.get("database", "server"),
84 "database" : self.config.get("database", "database"),
85 "user" : self.config.get("database", "username"),
86 "password" : self.config.get("database", "password"),
87 }
88
89 self.db = database.Connection(**credentials)