]> git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
downloads: Refactor mirror handling
[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 mirrors
12 from . import netboot
13 from . import nopaste
14 from . import releases
15 from . import settings
16 from . import talk
17
18 from . import blog
19 from . import zeiterfassung
20
21 DEFAULT_CONFIG = io.StringIO("""
22 [global]
23 debug = false
24
25 data_dir =
26 static_dir = %(data_dir)s/static
27 templates_dir = %(data_dir)s/templates
28 """)
29
30 class Backend(object):
31 def __init__(self, configfile, debug=False):
32 # Read configuration file.
33 self.config = self.read_config(configfile)
34
35 # Enable debug logging if configured
36 self.debug = debug or self.config.getboolean("global", "debug")
37
38 # Setup database.
39 self.setup_database()
40
41 # Initialize settings first.
42 self.settings = settings.Settings(self)
43
44 # Initialize backend modules.
45 self.accounts = accounts.Accounts(self)
46 self.geoip = geoip.GeoIP(self)
47 self.fireinfo = fireinfo.Fireinfo(self)
48 self.iuse = iuse.IUse(self)
49 self.mirrors = mirrors.Mirrors(self)
50 self.netboot = netboot.NetBoot(self)
51 self.nopaste = nopaste.Nopaste(self)
52 self.releases = releases.Releases(self)
53 self.talk = talk.Talk(self)
54
55 self.blog = blog.Blog(self)
56 self.zeiterfassung = zeiterfassung.ZeiterfassungClient(self)
57
58 def read_config(self, configfile):
59 cp = configparser.ConfigParser()
60
61 # Initialize configuration with some sensible defaults
62 cp.readfp(DEFAULT_CONFIG)
63
64 # Parse file
65 cp.read(configfile)
66
67 return cp
68
69 def setup_database(self):
70 """
71 Sets up the database connection.
72 """
73 credentials = {
74 "host" : self.config.get("database", "server"),
75 "database" : self.config.get("database", "database"),
76 "user" : self.config.get("database", "username"),
77 "password" : self.config.get("database", "password"),
78 }
79
80 self.db = database.Connection(**credentials)