]> git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
Run 2to3 to move to Python 3
[ipfire.org.git] / src / backend / base.py
1 #!/usr/bin/python
2
3 import configparser as configparser
4 import io
5
6 from . import accounts
7 from . import database
8 from . import geoip
9 from . import fireinfo
10 from . import iuse
11 from . import memcached
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 self.memcache = memcached.Memcached(self)
45
46 # Initialize backend modules.
47 self.accounts = accounts.Accounts(self)
48 self.downloads = mirrors.Downloads(self)
49 self.geoip = geoip.GeoIP(self)
50 self.fireinfo = fireinfo.Fireinfo(self)
51 self.iuse = iuse.IUse(self)
52 self.mirrors = mirrors.Mirrors(self)
53 self.netboot = netboot.NetBoot(self)
54 self.nopaste = nopaste.Nopaste(self)
55 self.releases = releases.Releases(self)
56 self.talk = talk.Talk(self)
57
58 self.blog = blog.Blog(self)
59 self.zeiterfassung = zeiterfassung.ZeiterfassungClient(self)
60
61 def read_config(self, configfile):
62 cp = configparser.ConfigParser()
63
64 # Initialize configuration with some sensible defaults
65 cp.readfp(DEFAULT_CONFIG)
66
67 # Parse file
68 cp.read(configfile)
69
70 return cp
71
72 def setup_database(self):
73 """
74 Sets up the database connection.
75 """
76 credentials = {
77 "host" : self.config.get("database", "server"),
78 "database" : self.config.get("database", "database"),
79 "user" : self.config.get("database", "username"),
80 "password" : self.config.get("database", "password"),
81 }
82
83 self.db = database.Connection(**credentials)