-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathconfig.py
More file actions
88 lines (75 loc) · 3.28 KB
/
config.py
File metadata and controls
88 lines (75 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
import os
from collections import defaultdict
from configparser 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 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:
# First: check environment variable is set
envkey = f"PLEXAPI_{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)
return cast(value) if cast else value
except: # noqa: E722
return default
def _asDict(self):
""" Returns all configuration values as a dictionary. """
config = defaultdict(dict)
config.update(self._defaults())
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 _defaults(self):
from uuid import getnode
from platform import uname
from plexapi import PROJECT, VERSION
platform_name, device_name, platform_version = uname()[0:3]
return {
'header': {
'provides': 'controller',
'platform': platform_name,
'platform_version': platform_version,
'product': PROJECT,
'version': VERSION,
'device': platform_name,
'device_name': device_name,
'identifier': str(hex(getnode())),
}
}
def reset_base_headers():
""" Convenience function returns a dict of all base X-Plex-* headers for session requests. """
from plexapi import CONFIG
return {
'X-Plex-Platform': CONFIG.get('header.platorm', CONFIG.get('header.platform')),
'X-Plex-Platform-Version': CONFIG.get('header.platform_version'),
'X-Plex-Provides': CONFIG.get('header.provides'),
'X-Plex-Product': CONFIG.get('header.product'),
'X-Plex-Version': CONFIG.get('header.version'),
'X-Plex-Device': CONFIG.get('header.device'),
'X-Plex-Device-Name': CONFIG.get('header.device_name'),
'X-Plex-Client-Identifier': CONFIG.get('header.identifier'),
'X-Plex-Sync-Version': '2',
'X-Plex-Features': 'external-media',
}