]>
Commit | Line | Data |
---|---|---|
9137135a MT |
1 | #!/usr/bin/python |
2 | ||
2e1b81e0 MT |
3 | from .decorators import * |
4 | ||
9137135a MT |
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 | """ | |
020e0a37 MT |
12 | def __init__(self, backend, *args, **kwargs): |
13 | self.backend = backend | |
9137135a | 14 | |
5e432e87 MT |
15 | # Call custom constructor |
16 | self.init(*args, **kwargs) | |
17 | ||
18 | def init(self, *args, **kwargs): | |
19 | """ | |
20 | Custom constructor to be overwritten by inheriting class | |
21 | """ | |
22 | pass | |
23 | ||
5c8b4bfd MT |
24 | @lazy_property |
25 | def db(self): | |
26 | """ | |
27 | Shortcut to database | |
28 | """ | |
29 | return self.backend.db | |
30 | ||
020e0a37 MT |
31 | @lazy_property |
32 | def pakfire(self): | |
33 | """ | |
34 | DEPRECATED: This attribute is only kept until | |
35 | all other code has been updated to use self.backend. | |
36 | """ | |
37 | return self.backend | |
38 | ||
38a24b58 MT |
39 | @lazy_property |
40 | def settings(self): | |
41 | return self.backend.settings | |
42 | ||
2e1b81e0 MT |
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 |