|
1 # |
|
2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net> |
|
3 # All Rights Reserved. |
|
4 # |
|
5 # This software is subject to the provisions of the Zope Public License, |
|
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
10 # FOR A PARTICULAR PURPOSE. |
|
11 # |
|
12 |
|
13 __docformat__ = 'restructuredtext' |
|
14 |
|
15 |
|
16 # import standard library |
|
17 import atexit |
|
18 import logging |
|
19 logger = logging.getLogger('PyAMS (media)') |
|
20 |
|
21 import sys |
|
22 |
|
23 # import interfaces |
|
24 from pyams_media.interfaces import CONVERTER_HANDLER_KEY, CONVERTER_NAME |
|
25 from pyams_utils.interfaces import PYAMS_APPLICATION_SETTINGS_KEY, PYAMS_APPLICATION_DEFAULT_NAME |
|
26 |
|
27 # import packages |
|
28 from pyams_media.process import MediaConversionProcess, MediaConversionMessageHandler |
|
29 from pyams_utils.registry import set_local_registry |
|
30 from pyams_utils.zodb import get_connection_from_settings |
|
31 from pyams_zmq.process import process_exit_func |
|
32 |
|
33 |
|
34 def include_package(config): |
|
35 """Pyramid include""" |
|
36 |
|
37 # add translations |
|
38 config.add_translation_dirs('pyams_media:locales') |
|
39 |
|
40 # load registry components |
|
41 try: |
|
42 import pyams_zmi |
|
43 except ImportError: |
|
44 config.scan(ignore='pyams_media.zmi') |
|
45 else: |
|
46 config.scan() |
|
47 |
|
48 # check for upgrade mode |
|
49 if sys.argv[0].endswith('pyams_upgrade'): |
|
50 return |
|
51 |
|
52 settings = config.registry.settings |
|
53 start_handler = settings.get(CONVERTER_HANDLER_KEY, False) |
|
54 if start_handler: |
|
55 # get database connection |
|
56 connection = get_connection_from_settings(settings) |
|
57 root = connection.root() |
|
58 # get application |
|
59 application_name = settings.get(PYAMS_APPLICATION_SETTINGS_KEY, PYAMS_APPLICATION_DEFAULT_NAME) |
|
60 application = root.get(application_name) |
|
61 process = None |
|
62 if application is not None: |
|
63 sm = application.getSiteManager() |
|
64 set_local_registry(sm) |
|
65 try: |
|
66 conversion_util = sm.get(CONVERTER_NAME) |
|
67 if conversion_util is not None: |
|
68 # create medias converter process |
|
69 process = MediaConversionProcess(start_handler, MediaConversionMessageHandler, config.registry) |
|
70 logger.debug('Starting medias conversion process {0!r}...'.format(process)) |
|
71 process.start() |
|
72 if process.is_alive(): |
|
73 atexit.register(process_exit_func, process=process) |
|
74 finally: |
|
75 if process and not process.is_alive(): |
|
76 process.terminate() |
|
77 process.join() |
|
78 set_local_registry(None) |