##// END OF EJS Templates
plugins: allow more custom keys into js configs
ergo -
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,91 +1,92 b''
1 import inspect
1 import inspect
2 import logging
2 import logging
3
3
4 from pyramid.config import Configurator
4 from pyramid.config import Configurator
5
5
6 log = logging.getLogger(__name__)
6 log = logging.getLogger(__name__)
7
7
8 class InspectProxy(object):
8 class InspectProxy(object):
9 """
9 """
10 Proxy to the `inspect` module that allows us to use the pyramid include
10 Proxy to the `inspect` module that allows us to use the pyramid include
11 mechanism for cythonized modules without source file.
11 mechanism for cythonized modules without source file.
12 """
12 """
13
13
14 def _get_cyfunction_func_code(self, cyfunction):
14 def _get_cyfunction_func_code(self, cyfunction):
15 """
15 """
16 Unpack the `func_code` attribute of a cython function.
16 Unpack the `func_code` attribute of a cython function.
17 """
17 """
18 if inspect.ismethod(cyfunction):
18 if inspect.ismethod(cyfunction):
19 cyfunction = cyfunction.im_func
19 cyfunction = cyfunction.im_func
20 return getattr(cyfunction, 'func_code')
20 return getattr(cyfunction, 'func_code')
21
21
22 def getmodule(self, *args, **kwds):
22 def getmodule(self, *args, **kwds):
23 """
23 """
24 Simple proxy to `inspect.getmodule`.
24 Simple proxy to `inspect.getmodule`.
25 """
25 """
26 return inspect.getmodule(*args, **kwds)
26 return inspect.getmodule(*args, **kwds)
27
27
28 def getsourcefile(self, obj):
28 def getsourcefile(self, obj):
29 """
29 """
30 Proxy to `inspect.getsourcefile` or `inspect.getfile` depending on if
30 Proxy to `inspect.getsourcefile` or `inspect.getfile` depending on if
31 it's called to look up the source file that contains the magic pyramid
31 it's called to look up the source file that contains the magic pyramid
32 `includeme` callable.
32 `includeme` callable.
33
33
34 For cythonized modules the source file may be deleted. Therefore we
34 For cythonized modules the source file may be deleted. Therefore we
35 return the result of `inspect.getfile` instead. In the case of the
35 return the result of `inspect.getfile` instead. In the case of the
36 `configurator.include` method this is OK, because the result is passed
36 `configurator.include` method this is OK, because the result is passed
37 to `os.path.dirname` which strips the file name. So it doesn't matter
37 to `os.path.dirname` which strips the file name. So it doesn't matter
38 if we return the path to the source file or another file in the same
38 if we return the path to the source file or another file in the same
39 directory.
39 directory.
40 """
40 """
41 # Check if it's called to look up the source file that contains the
41 # Check if it's called to look up the source file that contains the
42 # magic pyramid `includeme` callable.
42 # magic pyramid `includeme` callable.
43 if getattr(obj, '__name__') == 'includeme':
43 if getattr(obj, '__name__') == 'includeme':
44 try:
44 try:
45 return inspect.getfile(obj)
45 return inspect.getfile(obj)
46 except TypeError as e:
46 except TypeError as e:
47 # Cython functions are not recognized as functions by the
47 # Cython functions are not recognized as functions by the
48 # inspect module. We have to unpack the func_code attribute
48 # inspect module. We have to unpack the func_code attribute
49 # ourself.
49 # ourself.
50 if 'cyfunction' in e.message:
50 if 'cyfunction' in e.message:
51 obj = self._get_cyfunction_func_code(obj)
51 obj = self._get_cyfunction_func_code(obj)
52 return inspect.getfile(obj)
52 return inspect.getfile(obj)
53 raise
53 raise
54 else:
54 else:
55 return inspect.getsourcefile(obj)
55 return inspect.getsourcefile(obj)
56
56
57
57
58 class CythonCompatConfigurator(Configurator):
58 class CythonCompatConfigurator(Configurator):
59 """
59 """
60 Customized configurator to replace the inspect class attribute with
60 Customized configurator to replace the inspect class attribute with
61 a custom one that is cython compatible.
61 a custom one that is cython compatible.
62 """
62 """
63 inspect = InspectProxy()
63 inspect = InspectProxy()
64
64
65
65
66 def register_appenlight_plugin(config, plugin_name, plugin_config):
66 def register_appenlight_plugin(config, plugin_name, plugin_config):
67 def register():
67 def register():
68 log.warning('Registering plugin: {}'.format(plugin_name))
68 log.warning('Registering plugin: {}'.format(plugin_name))
69 if plugin_name not in config.registry.appenlight_plugins:
69 if plugin_name not in config.registry.appenlight_plugins:
70 config.registry.appenlight_plugins[plugin_name] = {
70 config.registry.appenlight_plugins[plugin_name] = {
71 'javascript': None,
71 'javascript': None,
72 'static': None,
72 'static': None,
73 'css': None,
73 'css': None,
74 'celery_tasks': None,
74 'celery_tasks': None,
75 'celery_beats': None,
75 'celery_beats': None,
76 'fulltext_indexer': None,
76 'fulltext_indexer': None,
77 'sqlalchemy_migrations': None,
77 'sqlalchemy_migrations': None,
78 'default_values_setter': None,
78 'default_values_setter': None,
79 'header_html': None,
79 'resource_types': [],
80 'resource_types': [],
80 'url_gen': None
81 'url_gen': None
81 }
82 }
82 config.registry.appenlight_plugins[plugin_name].update(
83 config.registry.appenlight_plugins[plugin_name].update(
83 plugin_config)
84 plugin_config)
84 # inform AE what kind of resource types we have available
85 # inform AE what kind of resource types we have available
85 # so we can avoid failing when a plugin is removed but data
86 # so we can avoid failing when a plugin is removed but data
86 # is still present in the db
87 # is still present in the db
87 if plugin_config.get('resource_types'):
88 if plugin_config.get('resource_types'):
88 config.registry.resource_types.extend(
89 config.registry.resource_types.extend(
89 plugin_config['resource_types'])
90 plugin_config['resource_types'])
90
91
91 config.action('appenlight_plugin={}'.format(plugin_name), register)
92 config.action('appenlight_plugin={}'.format(plugin_name), register)
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now