1
|
1 |
|
from plugin.core.constants import PLUGIN_IDENTIFIER |
2
|
1 |
|
from plugin.models import Account |
3
|
1 |
|
from plugin.preferences.options.core.description import Description |
4
|
|
|
|
5
|
1 |
|
from plex import Plex |
6
|
1 |
|
import logging |
7
|
|
|
|
8
|
1 |
|
log = logging.getLogger(__name__) |
9
|
|
|
|
10
|
|
|
|
11
|
1 |
|
class Option(object): |
12
|
1 |
|
key = None |
13
|
1 |
|
type = None |
14
|
|
|
|
15
|
1 |
|
choices = None # enum |
16
|
1 |
|
default = None |
17
|
1 |
|
scope = 'account' |
18
|
|
|
|
19
|
|
|
# Display |
20
|
1 |
|
group = None |
21
|
1 |
|
label = None |
22
|
1 |
|
description = None |
23
|
1 |
|
order = 0 |
24
|
|
|
|
25
|
|
|
# Plex |
26
|
1 |
|
preference = None |
27
|
|
|
|
28
|
1 |
|
def __init__(self, preferences, option=None): |
29
|
|
|
# Validate class |
30
|
1 |
|
if not self.group or not self.label: |
31
|
|
|
raise ValueError('Missing "group" or "label" attribute on %r', self.__class__) |
32
|
|
|
|
33
|
1 |
|
if not self.type: |
34
|
|
|
raise ValueError('Missing "type" attribute on %r', self.__class__) |
35
|
|
|
|
36
|
1 |
|
if self.type == 'enum' and self.choices is None: |
37
|
|
|
raise ValueError('Missing enum "choices" attribute on %r', self.__class__) |
38
|
|
|
|
39
|
1 |
|
if self.scope not in ['account', 'server']: |
40
|
|
|
raise ValueError('Unknown value for scope: %r', self.scope) |
41
|
|
|
|
42
|
|
|
# Private attributes |
43
|
1 |
|
self._option = option |
44
|
1 |
|
self._preferences = preferences |
45
|
|
|
|
46
|
1 |
|
self._value = None |
47
|
|
|
|
48
|
1 |
|
@property |
49
|
|
|
def value(self): |
50
|
|
|
raise NotImplementedError |
51
|
|
|
|
52
|
1 |
|
def get(self, account=None): |
53
|
|
|
raise NotImplementedError |
54
|
|
|
|
55
|
1 |
|
def update(self, value, account=None, emit=True): |
56
|
|
|
raise NotImplementedError |
57
|
|
|
|
58
|
1 |
|
def on_database_changed(self, value, account=None): |
|
|
|
|
59
|
|
|
if self.preference is None: |
60
|
|
|
return |
61
|
|
|
|
62
|
|
|
log.warn('[%s] on_database_changed() not implemented, option may not be synchronized with plex', self.key) |
63
|
|
|
|
64
|
1 |
|
def on_plex_changed(self, value, account=None): |
65
|
|
|
raise NotImplementedError |
66
|
|
|
|
67
|
1 |
|
def on_changed(self, value, account=None): |
68
|
|
|
pass |
69
|
|
|
|
70
|
1 |
|
def to_dict(self): |
71
|
|
|
if not self.description: |
72
|
|
|
log.warn('No description defined for the %r option' % self.key, extra={ |
|
|
|
|
73
|
|
|
'event': { |
74
|
|
|
'module': __name__, |
75
|
|
|
'name': 'to_dict.no_description', |
76
|
|
|
'key': self.key |
77
|
|
|
} |
78
|
|
|
}) |
79
|
|
|
|
80
|
|
|
# Ensure descriptions have been built |
81
|
|
|
if self.description and isinstance(self.description, Description): |
82
|
|
|
self.description = self.description.build() |
83
|
|
|
|
84
|
|
|
# Build dictionary |
85
|
|
|
data = { |
86
|
|
|
'key': self.key, |
87
|
|
|
'type': self.type, |
88
|
|
|
|
89
|
|
|
'default': self.default, |
90
|
|
|
|
91
|
|
|
'group': self.group, |
92
|
|
|
'label': self.label, |
93
|
|
|
'order': self.order, |
94
|
|
|
'description': self.description, |
95
|
|
|
|
96
|
|
|
'value': self.value |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if self.type == 'enum': |
100
|
|
|
data['choices'] = self.choices |
101
|
|
|
|
102
|
|
|
return data |
103
|
|
|
|
104
|
|
|
# |
105
|
|
|
# Private functions |
106
|
|
|
# |
107
|
|
|
|
108
|
1 |
|
def _clone(self, *args): |
109
|
|
|
return self.__class__(self._preferences, *args) |
110
|
|
|
|
111
|
1 |
|
@classmethod |
112
|
1 |
|
def _update_preference(cls, value, account=None): |
113
|
|
|
if account is not None and account > 1: |
114
|
|
|
# Ignore change for non-administrator account |
115
|
|
|
return value |
116
|
|
|
|
117
|
|
|
# Disable preference migration when validated |
118
|
|
|
with Plex.configuration.headers({'X-Disable-Preference-Migration': '1'}): |
|
|
|
|
119
|
|
|
# Update preference |
120
|
|
|
Plex[':/plugins/%s/prefs' % PLUGIN_IDENTIFIER].set(cls.preference, value) |
121
|
|
|
|
122
|
|
|
return value |
123
|
|
|
|
124
|
1 |
|
@staticmethod |
125
|
|
|
def _validate_account(account): |
126
|
|
|
if type(account) is int and account < 1: |
127
|
|
|
return False |
128
|
|
|
|
129
|
|
|
if type(account) is Account and account.id < 1: |
130
|
|
|
return False |
131
|
|
|
|
132
|
|
|
return True |
133
|
|
|
|