]> git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
Provide a sensible default configuration
[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 zeiterfassung
23
24 DEFAULT_CONFIG = StringIO.StringIO("""
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
33 class Backend(object):
34 def __init__(self, configfile, debug=False):
35 # Read configuration file.
36 self.config = self.read_config(configfile)
37
38 # Enable debug logging if configured
39 self.debug = debug or self.config.getboolean("global", "debug")
40
41 # Setup database.
42 self.setup_database()
43
44 # Initialize settings first.
45 self.settings = settings.Settings(self)
46 self.memcache = memcached.Memcached(self)
47
48 # Initialize backend modules.
49 self.accounts = accounts.Accounts(self)
50 self.advertisements = ads.Advertisements(self)
51 self.downloads = mirrors.Downloads(self)
52 self.geoip = geoip.GeoIP(self)
53 self.fireinfo = fireinfo.Fireinfo(self)
54 self.iuse = iuse.IUse(self)
55 self.mirrors = mirrors.Mirrors(self)
56 self.netboot = netboot.NetBoot(self)
57 self.nopaste = nopaste.Nopaste(self)
58 self.news = news.News(self)
59 self.planet = planet.Planet(self)
60 self.releases = releases.Releases(self)
61 self.talk = talk.Talk(self)
62
63 self.zeiterfassung = zeiterfassung.ZeiterfassungClient(self)
64
65 def read_config(self, configfile):
66 cp = configparser.ConfigParser()
67
68 # Initialize configuration with some sensible defaults
69 cp.readfp(DEFAULT_CONFIG)
70
71 # Parse file
72 cp.read(configfile)
73
74 return cp
75
76 def setup_database(self):
77 """
78 Sets up the database connection.
79 """
80 credentials = {
81 "host" : self.config.get("database", "server"),
82 "database" : self.config.get("database", "database"),
83 "user" : self.config.get("database", "username"),
84 "password" : self.config.get("database", "password"),
85 }
86
87 self.db = database.Connection(**credentials)