22
22
# For support, questions, suggestions or any other inquiries, visit:
23
23
# http://wiki.github.com/Freeseer/freeseer/
24
24
import signal
25
-
26
- from flask import Blueprint , request
27
25
import re
28
- from freeseer import settings , logging
26
+
27
+ from flask import Blueprint
28
+ from flask import request
29
+
30
+ from freeseer import settings
31
+ from freeseer import logging
29
32
from freeseer .framework .config .exceptions import InvalidOptionValueError
30
- from freeseer .framework .config .profile import ProfileDoesNotExist , ProfileAlreadyExists
33
+ from freeseer .framework .config .profile import ProfileDoesNotExist
34
+ from freeseer .framework .config .profile import ProfileAlreadyExists
31
35
from freeseer .framework .plugin import PluginManager
32
- from freeseer .frontend .controller .server import http_response , HTTPError
36
+ from freeseer .frontend .controller .server import http_response
37
+ from freeseer .frontend .controller .server import HTTPError
33
38
34
39
log = logging .getLogger (__name__ )
35
40
configuration = Blueprint ('configuration' , __name__ )
72
77
def map_plugin_name (plugin ):
73
78
"""
74
79
Maps a resource name to a plugin name.
80
+
81
+ Raises:
82
+ HTTPError: If no plugin exists by given name.
75
83
"""
76
84
try :
77
85
name = plugin_names_map [plugin ]
@@ -80,22 +88,33 @@ def map_plugin_name(plugin):
80
88
return name
81
89
82
90
83
- def map_plugin_category (plugin_type ):
91
+ def map_plugin_category (category ):
84
92
"""
85
- Maps a resource name to a plugin category name.
93
+ Maps a plugin category resource name to a plugin category name.
94
+
95
+ Raises:
96
+ HTTPError: If no plugin category exists by given name.
86
97
"""
87
98
try :
88
- category = plugin_types_map [plugin_type ]
99
+ category = plugin_types_map [category ]
89
100
except KeyError :
90
- raise HTTPError ('Invalid Plugin Category: {}' .format (plugin_type ), 400 )
101
+ raise HTTPError ('Invalid Plugin Category: {}' .format (category ), 400 )
91
102
return category
92
103
93
104
94
105
def update_config (config , data ):
95
106
"""
96
- Updates given Config instance with request data.
107
+ Updates given config instance with request data.
108
+
109
+ Args:
110
+ config: A Configuration instance.
111
+ data: A key, value dict containing changes to config.
112
+
113
+ Raises:
114
+ HTTPError: If given data changes don't conform to config's schema.
97
115
"""
98
116
for key , value in data .items ():
117
+ # TODO: Add json-schema validation when pr #646 is merged.
99
118
try :
100
119
opt_instance = config .options [key ]
101
120
value = opt_instance .decode (value )
@@ -110,23 +129,28 @@ def update_config(config, data):
110
129
@configuration .before_app_first_request
111
130
def configure_configuration ():
112
131
"""
113
- Initializes the profile, configuration, and plugin manager.
114
132
Runs on first call to server.
115
133
"""
116
134
signal .signal (signal .SIGINT , teardown_configuration )
117
135
118
136
119
- def load_configuration (profile ):
137
+ def load_profile_configuration (profile ):
120
138
"""
121
139
Returns the configuration for a given profile.
140
+
141
+ Raises:
142
+ HTTPError: If profile does not exist.
122
143
"""
123
144
profile_instance = load_profile (profile )
124
145
return profile_instance .get_config ('freeseer.conf' , settings .FreeseerConfig , storage_args = ['Global' ], read_only = False )
125
146
126
147
127
148
def load_profile (profile ):
128
149
"""
129
- Returns the profile instance for a given profile.
150
+ Returns the profile instance for a given profile name.
151
+
152
+ Raises:
153
+ HTTPError: If profile does not exist.
130
154
"""
131
155
try :
132
156
profile = settings .profile_manager .get (profile , create_if_needed = False )
@@ -135,6 +159,31 @@ def load_profile(profile):
135
159
return profile
136
160
137
161
162
+ def get_plugin_config (profile , category , plugin , id ):
163
+ """
164
+ Returns the config instance for the given id, plugin, category, and profile.
165
+
166
+ Args:
167
+ profile: Name of a freeseer profile.
168
+ category: Resource name for a category plugin.
169
+ plugin: Resource name for a plugin type.
170
+ id: The instance number for a plugin.
171
+
172
+ Raises:
173
+ HTTPError: If profile, category, or plugin don't exist.
174
+ """
175
+ profile_instance = load_profile (profile )
176
+ plugin_manager = PluginManager (profile_instance )
177
+ plugin = plugin_manager .get_plugin_by_name (map_plugin_name (plugin ),
178
+ map_plugin_category (category ))
179
+ if not plugin :
180
+ raise HTTPError ('No plugin {} of type {}' .format (plugin , category ))
181
+
182
+ plugin_object = plugin .plugin_object
183
+ plugin_object .set_instance (id )
184
+ return plugin_object .config
185
+
186
+
138
187
def teardown_configuration (signum , frame ):
139
188
"""
140
189
Teardown method for configuration api.
@@ -161,9 +210,12 @@ def list_profiles():
161
210
@http_response (200 )
162
211
def view_profile (profile ):
163
212
"""
164
- View the configuration profile specified by :profile.
213
+ View the configuration profile specified by profile.
214
+
215
+ Raises:
216
+ HTTPError: If profile doesn't exist.
165
217
"""
166
- profile_configuration = load_configuration (profile )
218
+ profile_configuration = load_profile_configuration (profile )
167
219
return {'profile_configuration' : profile_configuration .values }
168
220
169
221
@@ -172,6 +224,9 @@ def view_profile(profile):
172
224
def create_profile ():
173
225
"""
174
226
Create new profile under 'name' specified in request arg.
227
+
228
+ Raises:
229
+ HTTPError: If profile name is invalid, or profile already exists..
175
230
"""
176
231
pattern = '^\w+$'
177
232
profile_name = request .form ['name' ]
@@ -189,7 +244,10 @@ def create_profile():
189
244
@http_response (204 )
190
245
def delete_profile (profile ):
191
246
"""
192
- Delete the profile specified by :profile.
247
+ Delete the profile specified by profile.
248
+
249
+ Raises:
250
+ HTTPError: If profile doesn't exist.
193
251
"""
194
252
try :
195
253
settings .profile_manager .delete (profile )
@@ -202,9 +260,13 @@ def delete_profile(profile):
202
260
@http_response (200 )
203
261
def modify_profile (profile ):
204
262
"""
205
- Modify the profile specified by :profile.
263
+ Modify the profile specified by given profile name.
264
+
265
+ Raises:
266
+ HTTPError: If profile doesn't exist, or changes don't conform
267
+ to Config's schema.
206
268
"""
207
- profile_configuration = load_configuration (profile )
269
+ profile_configuration = load_profile_configuration (profile )
208
270
changes = request .form
209
271
update_config (profile_configuration , changes )
210
272
return ''
@@ -223,9 +285,12 @@ def modify_profile(profile):
223
285
@http_response (200 )
224
286
def view_general_configuration (profile ):
225
287
"""
226
- Returns the general configuration for the given :profile.
288
+ Returns the general configuration for the given profile.
289
+
290
+ Raises:
291
+ HTTPError: If profile does not exist.
227
292
"""
228
- profile_configuration = load_configuration (profile )
293
+ profile_configuration = load_profile_configuration (profile )
229
294
return {
230
295
'default_language' : profile_configuration .default_language ,
231
296
'auto_hide' : profile_configuration .auto_hide ,
@@ -236,9 +301,13 @@ def view_general_configuration(profile):
236
301
@http_response (200 )
237
302
def modify_general_configuration (profile ):
238
303
"""
239
- Modifies the general configuration for the given :profile.
304
+ Modifies the general configuration for the given profile.
305
+
306
+ Raises:
307
+ HTTPError: If profile does not exist or changes don't conform to
308
+ Config's schema.
240
309
"""
241
- profile_configuration = load_configuration (profile )
310
+ profile_configuration = load_profile_configuration (profile )
242
311
changes = request .form
243
312
update_config (profile_configuration , changes )
244
313
return ''
@@ -255,9 +324,12 @@ def modify_general_configuration(profile):
255
324
@http_response (200 )
256
325
def view_recording_configuration (profile ):
257
326
"""
258
- Returns the recording configuration for the given :profile.
327
+ Returns the recording configuration for the given profile.
328
+
329
+ Raises:
330
+ HTTPError: If profile does not exist.
259
331
"""
260
- profile_configuration = load_configuration (profile )
332
+ profile_configuration = load_profile_configuration (profile )
261
333
return {
262
334
'record_to_file' : profile_configuration .record_to_file ,
263
335
'videodir' : profile_configuration .videodir ,
@@ -275,9 +347,13 @@ def view_recording_configuration(profile):
275
347
@http_response (200 )
276
348
def modify_recording_configuration (profile ):
277
349
"""
278
- Modifies the recording configuration for the given :profile.
350
+ Modifies the recording configuration for the given profile.
351
+
352
+ Raises:
353
+ HTTPError: If profile does not exist or changes don't conform
354
+ to Config's schema.
279
355
"""
280
- profile_configuration = load_configuration (profile )
356
+ profile_configuration = load_profile_configuration (profile )
281
357
changes = request .form
282
358
update_config (profile_configuration , changes )
283
359
return ''
@@ -293,9 +369,12 @@ def modify_recording_configuration(profile):
293
369
@http_response (200 )
294
370
def list_plugin_category (profile , category ):
295
371
"""
296
- List the available plugins for the given :profile and plugin :category.
372
+ List the available plugins for the given profile and plugin category.
373
+
374
+ Raises:
375
+ HTTPError: If profile, or category do not exist.
297
376
"""
298
- profile_configuration = load_configuration (profile )
377
+ profile_configuration = load_profile_configuration (profile )
299
378
plugin_manager = PluginManager (profile_configuration )
300
379
plugin_infos = plugin_manager .get_plugins_of_category (map_plugin_category (category ))
301
380
return {
@@ -314,7 +393,8 @@ def list_plugin_category(profile, category):
314
393
@http_response (200 )
315
394
def list_plugin_instances (profile , category , plugin ):
316
395
"""
317
- List existing plugin instances for the given :profile, plugin :category, and :plugin type..
396
+ List existing plugin instances for the given profile, plugin category,
397
+ and plugin type.
318
398
"""
319
399
raise HTTPError ('Unimplemented' , 501 )
320
400
@@ -323,24 +403,24 @@ def list_plugin_instances(profile, category, plugin):
323
403
@http_response (200 )
324
404
def view_plugin_instance (profile , category , plugin , id ):
325
405
"""
326
- View the config for instance :id of the given :profile, plugin :category, and :plugin type..
406
+ View the config for instance id of the given profile, plugin category,
407
+ and plugin type.
408
+
409
+ Raises:
410
+ HTTPError: If profile, category, or plugin do not exist.
327
411
"""
328
- profile_instance = load_profile (profile )
329
- plugin_manager = PluginManager (profile_instance )
330
- plugin = plugin_manager .get_plugin_by_name (map_plugin_name (plugin ),
331
- map_plugin_category (category ))
332
- plugin_object = plugin .plugin_object
333
- plugin_object .set_instance (id )
412
+ plugin_config = get_plugin_config (profile , category , plugin , id )
334
413
return {
335
- 'configuration' : plugin_object . config .values
414
+ 'configuration' : plugin_config .values
336
415
}
337
416
338
417
339
418
@configuration .route ('/profiles/<string:profile>/recording/<string:category>/<string:plugin>' , methods = ['POST' ])
340
419
@http_response (200 )
341
420
def create_plugin_instance (profile , category , plugin ):
342
421
"""
343
- Create a new instance of a plugin for the given :profile, plugin :category, and :plugin type.
422
+ Create a new instance of a plugin for the given profile, plugin category,
423
+ and plugin type.
344
424
"""
345
425
raise HTTPError ('Unimplemented' , 501 )
346
426
@@ -350,16 +430,16 @@ def create_plugin_instance(profile, category, plugin):
350
430
@http_response (200 )
351
431
def modify_plugin_instance (profile , category , plugin , id ):
352
432
"""
353
- Modify the config for an instance :id of the plugin for the given :profile, plugin :category, and :plugin type.
433
+ Modify the config for an instance id of the plugin for the given profile,
434
+ and plugin category.
435
+
436
+ Raises:
437
+ HTTPError: If profile, category, or plugin do not exist, or if
438
+ changes do not conform to Config's schema.
354
439
"""
355
- profile_instance = load_profile (profile )
356
- plugin_manager = PluginManager (profile_instance )
357
- plugin = plugin_manager .get_plugin_by_name (map_plugin_name (plugin ),
358
- map_plugin_category (category ))
359
- plugin_object = plugin .plugin_object
360
- plugin_object .set_instance (id )
440
+ plugin_config = get_plugin_config (profile , category , plugin , id )
361
441
changes = request .form
362
- update_config (plugin_object . config , changes )
442
+ update_config (plugin_config , changes )
363
443
return ''
364
444
#
365
445
# End of plugin endpoints.
0 commit comments