2016-03-21 04:26:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-02-20 03:18:23 +00:00
|
|
|
import os
|
2015-06-02 02:27:43 +00:00
|
|
|
from collections import defaultdict
|
2017-01-23 05:15:51 +00:00
|
|
|
from plexapi.compat import ConfigParser
|
2015-06-02 02:27:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PlexConfig(ConfigParser):
|
2017-01-23 05:15:51 +00:00
|
|
|
""" PlexAPI configuration object. Settings are stored in an INI file within the
|
|
|
|
user's home directory and can be overridden after importing plexapi by simply
|
|
|
|
setting the value. See the documentation section 'Configuration' for more
|
|
|
|
details on available options.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
path (str): Path of the configuration file to load.
|
|
|
|
"""
|
2015-06-02 02:27:43 +00:00
|
|
|
def __init__(self, path):
|
|
|
|
ConfigParser.__init__(self)
|
|
|
|
self.read(path)
|
2016-03-21 04:26:02 +00:00
|
|
|
self.data = self._asDict()
|
2015-06-02 02:27:43 +00:00
|
|
|
|
|
|
|
def get(self, key, default=None, cast=None):
|
2017-01-23 05:15:51 +00:00
|
|
|
""" Returns the specified configuration value or <default> if not found.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
key (str): Configuration variable to load in the format '<section>.<variable>'.
|
|
|
|
default: Default value to use if key not found.
|
|
|
|
cast (func): Cast the value to the specified type before returning.
|
|
|
|
"""
|
2015-06-02 02:27:43 +00:00
|
|
|
try:
|
2017-02-20 03:18:23 +00:00
|
|
|
# First: check environment variable is set
|
|
|
|
envkey = 'PLEXAPI_%s' % key.upper().replace('.', '_')
|
|
|
|
value = os.environ.get(envkey)
|
|
|
|
if value is None:
|
|
|
|
# Second: check the config file has attr
|
|
|
|
section, name = key.lower().split('.')
|
|
|
|
value = self.data.get(section, {}).get(name, default)
|
2015-06-02 02:27:43 +00:00
|
|
|
return cast(value) if cast else value
|
2017-10-25 16:34:59 +00:00
|
|
|
except: # noqa: E722
|
2015-06-02 02:27:43 +00:00
|
|
|
return default
|
|
|
|
|
2016-03-21 04:26:02 +00:00
|
|
|
def _asDict(self):
|
2017-01-23 05:15:51 +00:00
|
|
|
""" Returns all configuration values as a dictionary. """
|
2015-06-02 02:27:43 +00:00
|
|
|
config = defaultdict(dict)
|
|
|
|
for section in self._sections:
|
|
|
|
for name, value in self._sections[section].items():
|
|
|
|
if name != '__name__':
|
|
|
|
config[section.lower()][name.lower()] = value
|
|
|
|
return dict(config)
|
2016-03-16 03:47:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def reset_base_headers():
|
2017-01-23 05:15:51 +00:00
|
|
|
""" Convenience function returns a dict of all base X-Plex-* headers for session requests. """
|
2016-03-16 03:47:13 +00:00
|
|
|
import plexapi
|
|
|
|
return {
|
|
|
|
'X-Plex-Platform': plexapi.X_PLEX_PLATFORM,
|
|
|
|
'X-Plex-Platform-Version': plexapi.X_PLEX_PLATFORM_VERSION,
|
|
|
|
'X-Plex-Provides': plexapi.X_PLEX_PROVIDES,
|
|
|
|
'X-Plex-Product': plexapi.X_PLEX_PRODUCT,
|
|
|
|
'X-Plex-Version': plexapi.X_PLEX_VERSION,
|
|
|
|
'X-Plex-Device': plexapi.X_PLEX_DEVICE,
|
2016-04-02 06:19:32 +00:00
|
|
|
'X-Plex-Device-Name': plexapi.X_PLEX_DEVICE_NAME,
|
2016-03-16 03:47:13 +00:00
|
|
|
'X-Plex-Client-Identifier': plexapi.X_PLEX_IDENTIFIER,
|
2018-09-08 15:25:16 +00:00
|
|
|
'X-Plex-Sync-Version': '2',
|
2016-03-16 03:47:13 +00:00
|
|
|
}
|