1
|
1 |
|
from plugin.core.exceptions import PluginDisabledError |
2
|
1 |
|
from plugin.core.message import InterfaceMessages |
3
|
|
|
from plugin.models import db |
4
|
1 |
|
|
5
|
1 |
|
import apsw |
|
|
|
|
6
|
1 |
|
import inspect |
7
|
1 |
|
import logging |
8
|
|
|
import peewee |
9
|
1 |
|
|
10
|
|
|
log = logging.getLogger(__name__) |
11
|
|
|
|
12
|
1 |
|
|
13
|
1 |
|
class Method(object): |
14
|
1 |
|
def __init__(self, manager, *args, **kwargs): |
|
|
|
|
15
|
|
|
self.manager = manager |
16
|
1 |
|
|
17
|
|
|
@property |
18
|
1 |
|
def model(self): |
19
|
|
|
return self.manager.model |
20
|
|
|
|
21
|
1 |
|
|
22
|
1 |
|
class Get(Method): |
23
|
|
|
def __call__(self, *query, **kwargs): |
24
|
|
|
if InterfaceMessages.critical: |
|
|
|
|
25
|
|
|
raise PluginDisabledError() |
26
|
|
|
|
27
|
|
|
obj = self.model.get(*query, **kwargs) |
28
|
|
|
|
29
|
|
|
if obj: |
30
|
1 |
|
obj._created = False |
|
|
|
|
31
|
|
|
|
32
|
|
|
return obj |
33
|
1 |
|
|
34
|
|
|
def all(self): |
35
|
|
|
if InterfaceMessages.critical: |
|
|
|
|
36
|
1 |
|
raise PluginDisabledError() |
37
|
1 |
|
|
38
|
1 |
|
return self.model.select() |
39
|
|
|
|
40
|
|
|
def by_id(self, id): |
|
|
|
|
41
|
|
|
return self(self.model.id == id) |
42
|
|
|
|
43
|
|
|
def or_create(self, *query, **kwargs): |
44
|
1 |
|
try: |
45
|
|
|
return self.manager.create(**kwargs) |
46
|
|
|
except (apsw.ConstraintError, peewee.IntegrityError) as ex: |
47
|
|
|
log.debug('or_create() - ex: %r', ex) |
48
|
1 |
|
|
49
|
1 |
|
return self(*query) |
50
|
1 |
|
|
51
|
|
|
def where(self, *query): |
52
|
|
|
if InterfaceMessages.critical: |
|
|
|
|
53
|
1 |
|
raise PluginDisabledError() |
54
|
1 |
|
|
55
|
|
|
return self.model.select().where(*query) |
56
|
1 |
|
|
57
|
|
|
|
58
|
1 |
|
class Create(Method): |
59
|
|
|
def __call__(self, **kwargs): |
60
|
1 |
|
if not self.model: |
61
|
|
|
raise Exception('Manager %r has no "model" attribute defined' % self.manager) |
62
|
|
|
|
63
|
1 |
|
if InterfaceMessages.critical: |
|
|
|
|
64
|
1 |
|
raise PluginDisabledError() |
65
|
|
|
|
66
|
1 |
|
with db.transaction(): |
67
|
|
|
obj = self.model.create(**kwargs) |
68
|
|
|
|
69
|
|
|
if obj: |
70
|
|
|
# Set flag |
71
|
|
|
obj._created = True |
|
|
|
|
72
|
|
|
|
73
|
|
|
return obj |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
class Update(Method): |
77
|
|
|
keys = [] |
78
|
|
|
|
79
|
|
|
def __call__(self, obj, data, save=True): |
80
|
|
|
changed = False |
81
|
|
|
|
82
|
|
|
for key, value in data.items(): |
83
|
|
|
if not hasattr(obj, key): |
84
|
|
|
raise KeyError('%r has no key %r' % (obj, key)) |
85
|
|
|
|
86
|
|
|
if getattr(obj, key) == value: |
87
|
1 |
|
continue |
88
|
|
|
|
89
|
|
|
changed = True |
90
|
|
|
setattr(obj, key, value) |
91
|
|
|
|
92
|
|
|
if not changed: |
93
|
|
|
return False |
94
|
|
|
|
95
|
|
|
if save: |
96
|
|
|
if InterfaceMessages.critical: |
|
|
|
|
97
|
|
|
raise PluginDisabledError() |
98
|
|
|
|
99
|
|
|
obj.save() |
100
|
|
|
|
101
|
|
|
return True |
102
|
|
|
|
103
|
|
|
def from_dict(self, obj, changes, save=True): |
104
|
|
|
if not changes: |
105
|
|
|
return False |
106
|
|
|
|
107
|
|
|
# Resolve `account` |
108
|
|
|
if inspect.isfunction(obj): |
109
|
|
|
obj = obj() |
110
|
1 |
|
|
111
|
1 |
|
# Update `TraktAccount` |
112
|
1 |
|
data = {} |
113
|
|
|
|
114
|
1 |
|
for key in self.keys: |
115
|
1 |
|
if key not in changes: |
116
|
|
|
continue |
117
|
|
|
|
118
|
1 |
|
data[key] = changes[key] |
119
|
1 |
|
|
120
|
|
|
if data and not self(obj, data, save=save): |
121
|
1 |
|
log.debug('Unable to update %r (nothing changed?)', obj) |
122
|
|
|
|
123
|
|
|
return True |
124
|
|
|
|
125
|
1 |
|
|
126
|
|
|
class ManagerMeta(type): |
127
|
|
|
def __init__(cls, name, bases, attributes): |
128
|
1 |
|
super(ManagerMeta, cls).__init__(name, bases, attributes) |
129
|
1 |
|
|
130
|
|
|
if '__metaclass__' in attributes: |
131
|
1 |
|
return |
132
|
1 |
|
|
133
|
1 |
|
# Construct manager methods |
134
|
|
|
for key in ['get', 'create', 'update']: |
135
|
1 |
|
value = getattr(cls, key) |
136
|
|
|
|
137
|
|
|
if not value or not inspect.isclass(value): |
138
|
|
|
continue |
139
|
|
|
|
140
|
|
|
# Construct method |
141
|
|
|
setattr(cls, key, value(cls)) |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
class Manager(object): |
145
|
|
|
__metaclass__ = ManagerMeta |
146
|
|
|
|
147
|
|
|
create = Create |
148
|
|
|
get = Get |
149
|
|
|
update = Update |
150
|
|
|
|
151
|
|
|
model = None |
152
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.