Skip to content
This repository was archived by the owner on May 28, 2022. It is now read-only.

Commit a10ea7b

Browse files
Fixes import formatting and docstrings.
1 parent 81bd6a4 commit a10ea7b

File tree

1 file changed

+126
-46
lines changed

1 file changed

+126
-46
lines changed

src/freeseer/frontend/controller/configuration.py

Lines changed: 126 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@
2222
# For support, questions, suggestions or any other inquiries, visit:
2323
# http://wiki.github.com/Freeseer/freeseer/
2424
import signal
25-
26-
from flask import Blueprint, request
2725
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
2932
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
3135
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
3338

3439
log = logging.getLogger(__name__)
3540
configuration = Blueprint('configuration', __name__)
@@ -72,6 +77,9 @@
7277
def map_plugin_name(plugin):
7378
"""
7479
Maps a resource name to a plugin name.
80+
81+
Raises:
82+
HTTPError: If no plugin exists by given name.
7583
"""
7684
try:
7785
name = plugin_names_map[plugin]
@@ -80,22 +88,33 @@ def map_plugin_name(plugin):
8088
return name
8189

8290

83-
def map_plugin_category(plugin_type):
91+
def map_plugin_category(category):
8492
"""
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.
8697
"""
8798
try:
88-
category = plugin_types_map[plugin_type]
99+
category = plugin_types_map[category]
89100
except KeyError:
90-
raise HTTPError('Invalid Plugin Category: {}'.format(plugin_type), 400)
101+
raise HTTPError('Invalid Plugin Category: {}'.format(category), 400)
91102
return category
92103

93104

94105
def update_config(config, data):
95106
"""
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.
97115
"""
98116
for key, value in data.items():
117+
# TODO: Add json-schema validation when pr #646 is merged.
99118
try:
100119
opt_instance = config.options[key]
101120
value = opt_instance.decode(value)
@@ -110,23 +129,28 @@ def update_config(config, data):
110129
@configuration.before_app_first_request
111130
def configure_configuration():
112131
"""
113-
Initializes the profile, configuration, and plugin manager.
114132
Runs on first call to server.
115133
"""
116134
signal.signal(signal.SIGINT, teardown_configuration)
117135

118136

119-
def load_configuration(profile):
137+
def load_profile_configuration(profile):
120138
"""
121139
Returns the configuration for a given profile.
140+
141+
Raises:
142+
HTTPError: If profile does not exist.
122143
"""
123144
profile_instance = load_profile(profile)
124145
return profile_instance.get_config('freeseer.conf', settings.FreeseerConfig, storage_args=['Global'], read_only=False)
125146

126147

127148
def load_profile(profile):
128149
"""
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.
130154
"""
131155
try:
132156
profile = settings.profile_manager.get(profile, create_if_needed=False)
@@ -135,6 +159,31 @@ def load_profile(profile):
135159
return profile
136160

137161

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+
138187
def teardown_configuration(signum, frame):
139188
"""
140189
Teardown method for configuration api.
@@ -161,9 +210,12 @@ def list_profiles():
161210
@http_response(200)
162211
def view_profile(profile):
163212
"""
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.
165217
"""
166-
profile_configuration = load_configuration(profile)
218+
profile_configuration = load_profile_configuration(profile)
167219
return {'profile_configuration': profile_configuration.values}
168220

169221

@@ -172,6 +224,9 @@ def view_profile(profile):
172224
def create_profile():
173225
"""
174226
Create new profile under 'name' specified in request arg.
227+
228+
Raises:
229+
HTTPError: If profile name is invalid, or profile already exists..
175230
"""
176231
pattern = '^\w+$'
177232
profile_name = request.form['name']
@@ -189,7 +244,10 @@ def create_profile():
189244
@http_response(204)
190245
def delete_profile(profile):
191246
"""
192-
Delete the profile specified by :profile.
247+
Delete the profile specified by profile.
248+
249+
Raises:
250+
HTTPError: If profile doesn't exist.
193251
"""
194252
try:
195253
settings.profile_manager.delete(profile)
@@ -202,9 +260,13 @@ def delete_profile(profile):
202260
@http_response(200)
203261
def modify_profile(profile):
204262
"""
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.
206268
"""
207-
profile_configuration = load_configuration(profile)
269+
profile_configuration = load_profile_configuration(profile)
208270
changes = request.form
209271
update_config(profile_configuration, changes)
210272
return ''
@@ -223,9 +285,12 @@ def modify_profile(profile):
223285
@http_response(200)
224286
def view_general_configuration(profile):
225287
"""
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.
227292
"""
228-
profile_configuration = load_configuration(profile)
293+
profile_configuration = load_profile_configuration(profile)
229294
return {
230295
'default_language': profile_configuration.default_language,
231296
'auto_hide': profile_configuration.auto_hide,
@@ -236,9 +301,13 @@ def view_general_configuration(profile):
236301
@http_response(200)
237302
def modify_general_configuration(profile):
238303
"""
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.
240309
"""
241-
profile_configuration = load_configuration(profile)
310+
profile_configuration = load_profile_configuration(profile)
242311
changes = request.form
243312
update_config(profile_configuration, changes)
244313
return ''
@@ -255,9 +324,12 @@ def modify_general_configuration(profile):
255324
@http_response(200)
256325
def view_recording_configuration(profile):
257326
"""
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.
259331
"""
260-
profile_configuration = load_configuration(profile)
332+
profile_configuration = load_profile_configuration(profile)
261333
return {
262334
'record_to_file': profile_configuration.record_to_file,
263335
'videodir': profile_configuration.videodir,
@@ -275,9 +347,13 @@ def view_recording_configuration(profile):
275347
@http_response(200)
276348
def modify_recording_configuration(profile):
277349
"""
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.
279355
"""
280-
profile_configuration = load_configuration(profile)
356+
profile_configuration = load_profile_configuration(profile)
281357
changes = request.form
282358
update_config(profile_configuration, changes)
283359
return ''
@@ -293,9 +369,12 @@ def modify_recording_configuration(profile):
293369
@http_response(200)
294370
def list_plugin_category(profile, category):
295371
"""
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.
297376
"""
298-
profile_configuration = load_configuration(profile)
377+
profile_configuration = load_profile_configuration(profile)
299378
plugin_manager = PluginManager(profile_configuration)
300379
plugin_infos = plugin_manager.get_plugins_of_category(map_plugin_category(category))
301380
return {
@@ -314,7 +393,8 @@ def list_plugin_category(profile, category):
314393
@http_response(200)
315394
def list_plugin_instances(profile, category, plugin):
316395
"""
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.
318398
"""
319399
raise HTTPError('Unimplemented', 501)
320400

@@ -323,24 +403,24 @@ def list_plugin_instances(profile, category, plugin):
323403
@http_response(200)
324404
def view_plugin_instance(profile, category, plugin, id):
325405
"""
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.
327411
"""
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)
334413
return {
335-
'configuration': plugin_object.config.values
414+
'configuration': plugin_config.values
336415
}
337416

338417

339418
@configuration.route('/profiles/<string:profile>/recording/<string:category>/<string:plugin>', methods=['POST'])
340419
@http_response(200)
341420
def create_plugin_instance(profile, category, plugin):
342421
"""
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.
344424
"""
345425
raise HTTPError('Unimplemented', 501)
346426

@@ -350,16 +430,16 @@ def create_plugin_instance(profile, category, plugin):
350430
@http_response(200)
351431
def modify_plugin_instance(profile, category, plugin, id):
352432
"""
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.
354439
"""
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)
361441
changes = request.form
362-
update_config(plugin_object.config, changes)
442+
update_config(plugin_config, changes)
363443
return ''
364444
#
365445
# End of plugin endpoints.

0 commit comments

Comments
 (0)