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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
"""
Pandora API Client Builders
This module provides a set of builder classes that can turn various
configuration formats into a fully built APIClient.
"""
import os.path
from configparser import ConfigParser
from .client import APIClient
from .transport import Encryptor, APITransport, DEFAULT_API_HOST
class TranslatingDict(dict):
"""Abstract Key/Value Translating Dictionary
Dictionary that translates keys using a static map of old key to new key
and values using a map of key to value translating function. The value
translating function will be called with the key and value read and is
expected to return a translated value for storage. Keys and values not
matched for translation are stored as provided.
This otherwise behaves as a standard dictionary.
Subclasses must provide KEY_TRANSLATIONS and VALUE_TRANSLATIONS even if
they are just empty dictionaries.
"""
KEY_TRANSLATIONS = None
VALUE_TRANSLATIONS = None
def __init__(self, initial=None):
super().__init__()
assert self.KEY_TRANSLATIONS is not None
assert self.VALUE_TRANSLATIONS is not None
if not initial:
return
if hasattr(initial, "items"):
values = initial.items()
else:
values = initial
for key, value in values:
self.put(key, value)
def was_translated(self, from_key, to_key):
pass
def translate_key(self, key):
key = key.strip().upper()
to_key = self.KEY_TRANSLATIONS.get(key, None)
if to_key:
self.was_translated(key, to_key)
return to_key
else:
return key
def translate_value(self, key, value):
if hasattr(value, "strip"):
value = value.strip()
return self.VALUE_TRANSLATIONS.get(key, lambda v: v)(value)
def put(self, key, value):
self[key] = value
def __setitem__(self, key, value):
key = self.translate_key(key)
super().__setitem__(
key, self.translate_value(key, value))
class APIClientBuilder:
"""Abstract API Client Builder
Provides the basic functions for building an API client. Expects a
dictionary of standard configuration options.
Required values:
* DECRYPTION_KEY - Pandora API decryption key
* ENCRYPTION_KEY - Pandora API encryption key
* PARTNER_USER - Pandora API partner username
* PARTNER_PASSWORD - Pandora API partner password
* DEVICE - Pandora API device type identifier
Optional values:
* API_HOST - API hostname and path to API
* PROXY - HTTP/HTTPS proxy hostname
* AUDIO_QUALITY - A supported audio quality (see APIClient)
"""
DEFAULT_CLIENT_CLASS = APIClient
def __init__(self, client_class=None):
self.client_class = client_class or self.DEFAULT_CLIENT_CLASS
def build_from_settings_dict(self, settings):
enc = Encryptor(settings["DECRYPTION_KEY"],
settings["ENCRYPTION_KEY"])
trans = APITransport(enc,
settings.get("API_HOST", DEFAULT_API_HOST),
settings.get("PROXY", None))
quality = settings.get("AUDIO_QUALITY",
self.client_class.MED_AUDIO_QUALITY)
return self.client_class(trans, settings["PARTNER_USER"],
settings["PARTNER_PASSWORD"],
settings["DEVICE"], quality)
class SettingsDict(TranslatingDict):
"""Settings Translating Dictionary
Maps old setting keys to new ones. Should be removed when ready to break
backwards compatibility.
"""
KEY_TRANSLATIONS = {
"USERNAME": "PARTNER_USER",
"PASSWORD": "PARTNER_PASSWORD",
"DEFAULT_AUDIO_QUALITY": "AUDIO_QUALITY",
}
VALUE_TRANSLATIONS = {}
def was_translated(self, from_key, to_key):
pass
class SettingsDictBuilder(APIClientBuilder):
"""Settings Dictionary Client Builder
Builds an API client based on a translated settings dictionary.
"""
def __init__(self, settings, **kwargs):
self.settings = settings
super().__init__(**kwargs)
def build(self):
settings = SettingsDict(self.settings)
return self.build_from_settings_dict(settings)
class FileBasedClientBuilder(APIClientBuilder):
"""Abstract File Based Client Builder
Provides base functionality for client builders that load their settings
from files.
"""
DEFAULT_CONFIG_FILE = ""
def __init__(self, path=None, authenticate=True, **kwargs):
self.path = path or self.DEFAULT_CONFIG_FILE
self.authenticate = authenticate
super().__init__(**kwargs)
@property
def file_exists(self):
return os.path.exists(self._path)
@property
def path(self):
return self._path
@path.setter
def path(self, path):
self._path = os.path.expanduser(path)
def parse_config(self):
raise NotImplementedError
def build(self):
if not self.file_exists:
raise IOError("File not found: {}".format(self.path))
config = self.parse_config()
client = self.build_from_settings_dict(config)
if self.authenticate:
client.login(config["USER"]["USERNAME"],
config["USER"]["PASSWORD"])
return client
class PydoraConfigFileBuilder(FileBasedClientBuilder):
"""Pydora Config Format Client Builder
Builds API client for original pydora configuration format.
"""
DEFAULT_CONFIG_FILE = "~/.pydora.cfg"
@staticmethod
def cfg_to_dict(cfg, key, kind=SettingsDict):
return kind((k.strip().upper(), v.strip())
for k, v in cfg.items(key, raw=True))
def parse_config(self):
cfg = ConfigParser()
with open(self.path) as file:
cfg.read_file(file)
settings = PydoraConfigFileBuilder.cfg_to_dict(cfg, "api")
settings["user"] = PydoraConfigFileBuilder.cfg_to_dict(
cfg, "user", dict)
return settings
class PianobarSettingsDict(TranslatingDict):
"""Pianobar Translating Dictionary
"""
KEY_TRANSLATIONS = {
"DECRYPT_PASSWORD": "DECRYPTION_KEY",
"ENCRYPT_PASSWORD": "ENCRYPTION_KEY",
"RPC_HOST": "API_HOST",
"CONTROL_PROXY": "PROXY",
}
VALUE_TRANSLATIONS = {
"API_HOST": lambda v: "{}/services/json/".format(v),
"AUDIO_QUALITY": lambda v: "{}Quality".format(v),
}
class PianobarConfigFileBuilder(FileBasedClientBuilder):
"""Pianobar Config File Client Builder
Builds an API client from a Pianobar config file.
"""
DEFAULT_CONFIG_FILE = "~/.config/pianobar/config"
def parse_config(self):
settings = PianobarSettingsDict()
with open(self.path, "r") as file:
for line in file.readlines():
line = line.strip()
if line and not line.startswith("#"):
settings.put(*line.split("=", 1))
settings["USER"] = {
"USERNAME": settings.pop("USER"),
"PASSWORD": settings.pop("PASSWORD"),
}
return settings
|