##// END OF EJS Templates
search: cython compatible configurator
ergo -
r7:e5ee6e0d
parent child Browse files
Show More
@@ -0,0 +1,61 b''
1 import inspect
2
3 from pyramid.config import Configurator
4
5
6 class InspectProxy(object):
7 """
8 Proxy to the `inspect` module that allows us to use the pyramid include
9 mechanism for cythonized modules without source file.
10 """
11
12 def _get_cyfunction_func_code(self, cyfunction):
13 """
14 Unpack the `func_code` attribute of a cython function.
15 """
16 if inspect.ismethod(cyfunction):
17 cyfunction = cyfunction.im_func
18 return getattr(cyfunction, 'func_code')
19
20 def getmodule(self, *args, **kwds):
21 """
22 Simple proxy to `inspect.getmodule`.
23 """
24 return inspect.getmodule(*args, **kwds)
25
26 def getsourcefile(self, obj):
27 """
28 Proxy to `inspect.getsourcefile` or `inspect.getfile` depending on if
29 it's called to look up the source file that contains the magic pyramid
30 `includeme` callable.
31
32 For cythonized modules the source file may be deleted. Therefore we
33 return the result of `inspect.getfile` instead. In the case of the
34 `configurator.include` method this is OK, because the result is passed
35 to `os.path.dirname` which strips the file name. So it doesn't matter
36 if we return the path to the source file or another file in the same
37 directory.
38 """
39 # Check if it's called to look up the source file that contains the
40 # magic pyramid `includeme` callable.
41 if getattr(obj, '__name__') == 'includeme':
42 try:
43 return inspect.getfile(obj)
44 except TypeError as e:
45 # Cython functions are not recognized as functions by the
46 # inspect module. We have to unpack the func_code attribute
47 # ourself.
48 if 'cyfunction' in e.message:
49 obj = self._get_cyfunction_func_code(obj)
50 return inspect.getfile(obj)
51 raise
52 else:
53 return inspect.getsourcefile(obj)
54
55
56 class CythonCompatConfigurator(Configurator):
57 """
58 Customized configurator to replace the inspect class attribute with
59 a custom one that is cython compatible.
60 """
61 inspect = InspectProxy()
@@ -31,7 +31,7 b' import appenlight.lib.encryption as encryption'
31 31
32 32 from authomatic.providers import oauth2, oauth1
33 33 from authomatic import Authomatic
34 from pyramid.config import Configurator, PHASE3_CONFIG
34 from pyramid.config import PHASE3_CONFIG
35 35 from pyramid.authentication import AuthTktAuthenticationPolicy
36 36 from pyramid.authorization import ACLAuthorizationPolicy
37 37 from pyramid_mailer.mailer import Mailer
@@ -44,6 +44,7 b' from redlock import Redlock'
44 44 from sqlalchemy import engine_from_config
45 45
46 46 from appenlight.celery import configure_celery
47 from appenlight.lib.configurator import CythonCompatConfigurator
47 48 from appenlight.lib import cache_regions
48 49 from appenlight.lib.ext_json import json
49 50 from appenlight.security import groupfinder, AuthTokenAuthenticationPolicy
@@ -91,11 +92,12 b' def main(global_config, **settings):'
91 92
92 93 # Create the Pyramid Configurator.
93 94 settings['_mail_url'] = settings['mailing.app_url']
94 config = Configurator(settings=settings,
95 authentication_policy=authentication_policy,
96 authorization_policy=authorization_policy,
97 root_factory='appenlight.security.RootFactory',
98 default_permission='view')
95 config = CythonCompatConfigurator(
96 settings=settings,
97 authentication_policy=authentication_policy,
98 authorization_policy=authorization_policy,
99 root_factory='appenlight.security.RootFactory',
100 default_permission='view')
99 101 config.set_default_csrf_options(require_csrf=True, header='X-XSRF-TOKEN')
100 102 config.add_view_deriver('appenlight.predicates.csrf_view',
101 103 name='csrf_view')
@@ -131,8 +133,8 b' def main(global_config, **settings):'
131 133 config.include('ziggurat_foundations.ext.pyramid.sign_in')
132 134 es_server_list = aslist(settings['elasticsearch.nodes'])
133 135 redis_url = settings['redis.url']
134 log.info('Elasticsearch server list: {}'.format(es_server_list))
135 log.info('Redis server: {}'.format(redis_url))
136 log.warning('Elasticsearch server list: {}'.format(es_server_list))
137 log.warning('Redis server: {}'.format(redis_url))
136 138 config.registry.es_conn = pyelasticsearch.ElasticSearch(es_server_list)
137 139 config.registry.redis_conn = redis.StrictRedis.from_url(redis_url)
138 140
General Comments 0
You need to be logged in to leave comments. Login now