]> git.ipfire.org Git - people/jschlag/pbs.git/blob - src/buildservice/base.py
Replace geoip database by local database
[people/jschlag/pbs.git] / src / buildservice / base.py
1 #!/usr/bin/python
2
3 from .decorators import *
4
5 class Object(object):
6 """
7 Main object where all other objects inherit from.
8
9 This is used to access the global instance of Pakfire
10 and hold the database connection.
11 """
12 def __init__(self, backend, *args, **kwargs):
13 self.backend = backend
14
15 # Shortcut to settings.
16 if hasattr(self.pakfire, "settings"):
17 self.settings = self.backend.settings
18
19 # Call custom constructor
20 self.init(*args, **kwargs)
21
22 def init(self, *args, **kwargs):
23 """
24 Custom constructor to be overwritten by inheriting class
25 """
26 pass
27
28 @lazy_property
29 def db(self):
30 """
31 Shortcut to database
32 """
33 return self.backend.db
34
35 @lazy_property
36 def pakfire(self):
37 """
38 DEPRECATED: This attribute is only kept until
39 all other code has been updated to use self.backend.
40 """
41 return self.backend
42
43
44 class DataObject(Object):
45 # Table name
46 table = None
47
48 def init(self, id, data=None):
49 self.id = id
50
51 if data:
52 self.data = data
53
54 @lazy_property
55 def data(self):
56 assert self.table, "Table name is not set"
57 assert self.id
58
59 return self.db.get("SELECT * FROM %s \
60 WHERE id = %%s" % self.table, self.id)
61
62 def _set_attribute(self, key, val):
63 # Detect if an update is needed
64 if self.data[key] == val:
65 return
66
67 self.db.execute("UPDATE %s SET %s = %%s \
68 WHERE id = %%s" % (self.table, key), val, self.id)
69
70 # Update the cached attribute
71 self.data[key] = val