mirror of
https://github.com/pkkid/python-plexapi
synced 2024-12-11 20:52:28 +00:00
65 lines
2.5 KiB
Python
65 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
from collections import defaultdict
|
|
from plexapi.compat import ConfigParser
|
|
|
|
|
|
class PlexConfig(ConfigParser):
|
|
""" 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.
|
|
"""
|
|
def __init__(self, path):
|
|
ConfigParser.__init__(self)
|
|
self.read(path)
|
|
self.data = self._asDict()
|
|
|
|
def __getattr__(self, attr):
|
|
if attr not in ('get', '_asDict', 'data'):
|
|
for section in self._sections:
|
|
for name, value in self._sections[section].items():
|
|
if name == attr:
|
|
return value
|
|
raise Exception('Config attr not found: %s' % attr)
|
|
|
|
def get(self, key, default=None, cast=None):
|
|
""" 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.
|
|
"""
|
|
try:
|
|
section, name = key.split('.')
|
|
value = self.data.get(section.lower(), {}).get(name.lower(), default)
|
|
return cast(value) if cast else value
|
|
except:
|
|
return default
|
|
|
|
def _asDict(self):
|
|
""" Returns all configuration values as a dictionary. """
|
|
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)
|
|
|
|
|
|
def reset_base_headers():
|
|
""" Convenience function returns a dict of all base X-Plex-* headers for session requests. """
|
|
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,
|
|
'X-Plex-Device-Name': plexapi.X_PLEX_DEVICE_NAME,
|
|
'X-Plex-Client-Identifier': plexapi.X_PLEX_IDENTIFIER,
|
|
}
|