2017-02-23 06:33:30 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from collections import defaultdict
|
2020-05-12 21:15:16 +00:00
|
|
|
from urllib.parse import quote
|
2017-02-28 21:49:49 +00:00
|
|
|
|
2017-02-23 06:33:30 +00:00
|
|
|
from plexapi import log, utils
|
|
|
|
from plexapi.base import PlexObject
|
|
|
|
from plexapi.exceptions import BadRequest, NotFound
|
|
|
|
|
|
|
|
|
|
|
|
class Settings(PlexObject):
|
|
|
|
""" Container class for all settings. Allows getting and setting PlexServer settings.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
key (str): '/:/prefs'
|
|
|
|
"""
|
|
|
|
key = '/:/prefs'
|
|
|
|
|
|
|
|
def __init__(self, server, data, initpath=None):
|
|
|
|
self._settings = {}
|
|
|
|
super(Settings, self).__init__(server, data, initpath)
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
if attr.startswith('_'):
|
2020-11-21 22:49:21 +00:00
|
|
|
try:
|
|
|
|
return self.__dict__[attr]
|
|
|
|
except KeyError:
|
|
|
|
raise AttributeError
|
2017-02-23 06:55:22 +00:00
|
|
|
return self.get(attr).value
|
2017-02-23 06:33:30 +00:00
|
|
|
|
|
|
|
def __setattr__(self, attr, value):
|
|
|
|
if not attr.startswith('_'):
|
|
|
|
return self.get(attr).set(value)
|
|
|
|
self.__dict__[attr] = value
|
2017-02-23 07:08:04 +00:00
|
|
|
|
2017-02-23 06:33:30 +00:00
|
|
|
def _loadData(self, data):
|
2017-02-24 16:18:54 +00:00
|
|
|
""" Load attribute values from Plex XML response. """
|
2017-02-23 06:33:30 +00:00
|
|
|
self._data = data
|
|
|
|
for elem in data:
|
|
|
|
id = utils.lowerFirst(elem.attrib['id'])
|
|
|
|
if id in self._settings:
|
|
|
|
self._settings[id]._loadData(elem)
|
|
|
|
continue
|
|
|
|
self._settings[id] = Setting(self._server, elem, self._initpath)
|
|
|
|
|
|
|
|
def all(self):
|
2017-02-24 16:18:54 +00:00
|
|
|
""" Returns a list of all :class:`~plexapi.settings.Setting` objects available. """
|
2021-02-24 17:55:53 +00:00
|
|
|
return [v for id, v in sorted(self._settings.items())]
|
2017-02-23 06:33:30 +00:00
|
|
|
|
|
|
|
def get(self, id):
|
2017-02-24 16:18:54 +00:00
|
|
|
""" Return the :class:`~plexapi.settings.Setting` object with the specified id. """
|
2017-02-23 06:33:30 +00:00
|
|
|
id = utils.lowerFirst(id)
|
|
|
|
if id in self._settings:
|
|
|
|
return self._settings[id]
|
2022-08-28 05:56:01 +00:00
|
|
|
raise NotFound(f'Invalid setting id: {id}')
|
2017-02-23 06:33:30 +00:00
|
|
|
|
|
|
|
def groups(self):
|
2017-02-24 16:18:54 +00:00
|
|
|
""" Returns a dict of lists for all :class:`~plexapi.settings.Setting`
|
|
|
|
objects grouped by setting group.
|
|
|
|
"""
|
2017-02-23 06:33:30 +00:00
|
|
|
groups = defaultdict(list)
|
|
|
|
for setting in self.all():
|
|
|
|
groups[setting.group].append(setting)
|
|
|
|
return dict(groups)
|
|
|
|
|
|
|
|
def group(self, group):
|
2017-02-24 16:18:54 +00:00
|
|
|
""" Return a list of all :class:`~plexapi.settings.Setting` objects in the specified group.
|
2017-02-25 04:14:52 +00:00
|
|
|
|
2017-02-24 16:18:54 +00:00
|
|
|
Parameters:
|
|
|
|
group (str): Group to return all settings.
|
|
|
|
"""
|
2017-02-23 06:33:30 +00:00
|
|
|
return self.groups().get(group, [])
|
|
|
|
|
|
|
|
def save(self):
|
2022-02-27 03:26:08 +00:00
|
|
|
""" Save any outstanding setting changes to the :class:`~plexapi.server.PlexServer`. This
|
2017-02-24 16:18:54 +00:00
|
|
|
performs a full reload() of Settings after complete.
|
|
|
|
"""
|
2017-02-23 06:33:30 +00:00
|
|
|
params = {}
|
|
|
|
for setting in self.all():
|
|
|
|
if setting._setValue:
|
2022-11-08 21:40:18 +00:00
|
|
|
log.info('Saving PlexServer setting %s = %s', setting.id, setting._setValue)
|
2017-02-23 06:33:30 +00:00
|
|
|
params[setting.id] = quote(setting._setValue)
|
|
|
|
if not params:
|
|
|
|
raise BadRequest('No setting have been modified.')
|
2023-07-27 21:45:23 +00:00
|
|
|
querystr = '&'.join(f'{k}={v}' for k, v in params.items())
|
2022-08-28 05:56:01 +00:00
|
|
|
url = f'{self.key}?{querystr}'
|
2017-02-23 06:33:30 +00:00
|
|
|
self._server.query(url, self._server._session.put)
|
|
|
|
self.reload()
|
|
|
|
|
|
|
|
|
|
|
|
class Setting(PlexObject):
|
2017-02-24 16:18:54 +00:00
|
|
|
""" Represents a single Plex setting.
|
2017-02-25 04:14:52 +00:00
|
|
|
|
2017-02-24 16:18:54 +00:00
|
|
|
Attributes:
|
|
|
|
id (str): Setting id (or name).
|
|
|
|
label (str): Short description of what this setting is.
|
|
|
|
summary (str): Long description of what this setting is.
|
|
|
|
type (str): Setting type (text, int, double, bool).
|
|
|
|
default (str): Default value for this setting.
|
|
|
|
value (str,bool,int,float): Current value for this setting.
|
|
|
|
hidden (bool): True if this is a hidden setting.
|
|
|
|
advanced (bool): True if this is an advanced setting.
|
|
|
|
group (str): Group name this setting is categorized as.
|
2022-02-27 03:26:08 +00:00
|
|
|
enumValues (list,dict): List or dictionary of valid values for this setting.
|
2017-02-24 16:18:54 +00:00
|
|
|
"""
|
2021-02-24 17:55:53 +00:00
|
|
|
_bool_cast = lambda x: bool(x == 'true' or x == '1')
|
2017-02-23 06:33:30 +00:00
|
|
|
_bool_str = lambda x: str(x).lower()
|
|
|
|
TYPES = {
|
|
|
|
'bool': {'type': bool, 'cast': _bool_cast, 'tostr': _bool_str},
|
2020-07-28 15:13:51 +00:00
|
|
|
'double': {'type': float, 'cast': float, 'tostr': str},
|
|
|
|
'int': {'type': int, 'cast': int, 'tostr': str},
|
|
|
|
'text': {'type': str, 'cast': str, 'tostr': str},
|
2017-02-23 06:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def _loadData(self, data):
|
2017-02-24 16:18:54 +00:00
|
|
|
""" Load attribute values from Plex XML response. """
|
2022-01-24 00:15:10 +00:00
|
|
|
self.type = data.attrib.get('type')
|
|
|
|
self.advanced = utils.cast(bool, data.attrib.get('advanced'))
|
|
|
|
self.default = self._cast(data.attrib.get('default'))
|
|
|
|
self.enumValues = self._getEnumValues(data)
|
|
|
|
self.group = data.attrib.get('group')
|
|
|
|
self.hidden = utils.cast(bool, data.attrib.get('hidden'))
|
2017-02-23 06:33:30 +00:00
|
|
|
self.id = data.attrib.get('id')
|
|
|
|
self.label = data.attrib.get('label')
|
2022-01-24 00:15:10 +00:00
|
|
|
self.option = data.attrib.get('option')
|
|
|
|
self.secure = utils.cast(bool, data.attrib.get('secure'))
|
2017-02-23 06:33:30 +00:00
|
|
|
self.summary = data.attrib.get('summary')
|
2017-02-24 16:18:54 +00:00
|
|
|
self.value = self._cast(data.attrib.get('value'))
|
2022-01-24 00:15:10 +00:00
|
|
|
self._setValue = None
|
2017-02-23 06:33:30 +00:00
|
|
|
|
2017-02-24 16:18:54 +00:00
|
|
|
def _cast(self, value):
|
2020-03-16 15:13:51 +00:00
|
|
|
""" Cast the specific value to the type of this setting. """
|
|
|
|
if self.type != 'enum':
|
2017-02-24 16:18:54 +00:00
|
|
|
value = utils.cast(self.TYPES.get(self.type)['cast'], value)
|
|
|
|
return value
|
|
|
|
|
2017-02-23 06:33:30 +00:00
|
|
|
def _getEnumValues(self, data):
|
2022-01-24 00:15:10 +00:00
|
|
|
""" Returns a list or dictionary of values for this setting. """
|
|
|
|
enumstr = data.attrib.get('enumValues') or data.attrib.get('values')
|
2017-02-23 06:33:30 +00:00
|
|
|
if not enumstr:
|
|
|
|
return None
|
|
|
|
if ':' in enumstr:
|
2022-12-21 19:34:50 +00:00
|
|
|
d = {}
|
|
|
|
for kv in enumstr.split('|'):
|
|
|
|
try:
|
|
|
|
k, v = kv.split(':')
|
|
|
|
d[self._cast(k)] = v
|
|
|
|
except ValueError:
|
|
|
|
d[self._cast(kv)] = kv
|
|
|
|
return d
|
2017-02-23 06:33:30 +00:00
|
|
|
return enumstr.split('|')
|
|
|
|
|
|
|
|
def set(self, value):
|
2022-02-27 03:26:08 +00:00
|
|
|
""" Set a new value for this setting. NOTE: You must call plex.settings.save() for before
|
2017-02-24 16:18:54 +00:00
|
|
|
any changes to setting values are persisted to the :class:`~plexapi.server.PlexServer`.
|
|
|
|
"""
|
2017-02-23 06:33:30 +00:00
|
|
|
# check a few things up front
|
|
|
|
if not isinstance(value, self.TYPES[self.type]['type']):
|
|
|
|
badtype = type(value).__name__
|
2022-08-28 05:56:01 +00:00
|
|
|
raise BadRequest(f'Invalid value for {self.id}: a {self.type} is required, not {badtype}')
|
2017-02-23 06:33:30 +00:00
|
|
|
if self.enumValues and value not in self.enumValues:
|
2022-08-28 05:56:01 +00:00
|
|
|
raise BadRequest(f'Invalid value for {self.id}: {value} not in {list(self.enumValues)}')
|
2017-02-23 06:33:30 +00:00
|
|
|
# store value off to the side until we call settings.save()
|
|
|
|
tostr = self.TYPES[self.type]['tostr']
|
|
|
|
self._setValue = tostr(value)
|
2017-02-28 21:49:49 +00:00
|
|
|
|
|
|
|
def toUrl(self):
|
|
|
|
"""Helper for urls"""
|
2022-08-28 05:56:01 +00:00
|
|
|
return f'{self.id}={self._value or self.value}'
|
2020-05-24 03:04:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
@utils.registerPlexObject
|
|
|
|
class Preferences(Setting):
|
|
|
|
""" Represents a single Preferences.
|
|
|
|
|
|
|
|
Attributes:
|
2021-05-25 02:36:26 +00:00
|
|
|
TAG (str): 'Setting'
|
2020-05-24 03:04:55 +00:00
|
|
|
FILTER (str): 'preferences'
|
|
|
|
"""
|
2021-05-25 00:28:11 +00:00
|
|
|
TAG = 'Setting'
|
2020-05-24 03:04:55 +00:00
|
|
|
FILTER = 'preferences'
|
2020-06-05 18:12:59 +00:00
|
|
|
|
|
|
|
def _default(self):
|
2020-06-05 18:18:35 +00:00
|
|
|
""" Set the default value for this setting."""
|
2022-08-28 05:56:01 +00:00
|
|
|
key = f'{self._initpath}/prefs?'
|
|
|
|
url = key + f'{self.id}={self.default}'
|
2020-06-05 18:12:59 +00:00
|
|
|
self._server.query(url, method=self._server._session.put)
|