]> git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
Add bot that automatically tweets some things
[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 tweets
22 from . import wiki
23 from . import zeiterfassung
24 from .decorators import *
25
26 DEFAULT_CONFIG = io.StringIO("""
27 [global]
28 debug = false
29
30 data_dir =
31 static_dir = %(data_dir)s/static
32 templates_dir = %(data_dir)s/templates
33 """)
34
35 class Backend(object):
36 def __init__(self, configfile, debug=False):
37 # Read configuration file.
38 self.config = self.read_config(configfile)
39
40 # Enable debug logging if configured
41 self.debug = debug or self.config.getboolean("global", "debug")
42
43 # Setup database.
44 self.setup_database()
45
46 # Initialize settings first.
47 self.settings = settings.Settings(self)
48 self.memcache = memcached.Memcached(self)
49
50 # Initialize backend modules.
51 self.accounts = accounts.Accounts(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.releases = releases.Releases(self)
59 self.talk = talk.Talk(self)
60
61 self.blog = blog.Blog(self)
62 self.wiki = wiki.Wiki(self)
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)
88
89 @tornado.gen.coroutine
90 def run_task(self, task, *args, **kwargs):
91 tasks = {
92 "check-mirrors" : self.mirrors.check_all,
93 "cleanup-messages" : self.messages.queue.cleanup,
94 "scan-files" : self.releases.scan_files,
95 "send-all-messages" : self.messages.queue.send_all,
96 "tweet" : self.tweets.tweet,
97 "update-blog-feeds" : self.blog.update_feeds,
98 }
99
100 # Get the task from the list of all tasks
101 func = tasks.get(task, None)
102 if not func:
103 raise ValueError("Unknown task: %s" % task)
104
105 # Run the task
106 r = yield func(*args, **kwargs)
107
108 # If any error code has been returned,
109 # we will end the program
110 if r:
111 raise SystemExit(r)
112
113 @lazy_property
114 def messages(self):
115 return messages.Messages(self)
116
117 @lazy_property
118 def tweets(self):
119 return tweets.Tweets(self)