Michael Merickel
2018-10-15 0c29cf2df41600d3906d521c72991c7686018b71
commit | author | age
5f5a7e 1 import os
CM 2
d35851 3 from pyramid.settings import asbool, aslist
5bf23f 4
0c29cf 5
5bf23f 6 class SettingsConfiguratorMixin(object):
CM 7     def _set_settings(self, mapping):
7b58c0 8         if mapping is None:
5bf23f 9             mapping = {}
CM 10         settings = Settings(mapping)
11         self.registry.settings = settings
12         return settings
13
14     def add_settings(self, settings=None, **kw):
adfc23 15         """Augment the :term:`deployment settings` with one or more
659630 16         key/value pairs.
5bf23f 17
CM 18         You may pass a dictionary::
19
20            config.add_settings({'external_uri':'http://example.com'})
21
22         Or a set of key/value pairs::
23
24            config.add_settings(external_uri='http://example.com')
25
26         This function is useful when you need to test code that accesses the
27         :attr:`pyramid.registry.Registry.settings` API (or the
28         :meth:`pyramid.config.Configurator.get_settings` API) and
29         which uses values from that API.
30         """
31         if settings is None:
32             settings = {}
33         utility = self.registry.settings
34         if utility is None:
35             utility = self._set_settings(settings)
36         utility.update(settings)
37         utility.update(kw)
38
39     def get_settings(self):
40         """
41         Return a :term:`deployment settings` object for the current
42         application.  A deployment settings object is a dictionary-like
43         object that contains key/value pairs based on the dictionary passed
44         as the ``settings`` argument to the
adfc23 45         :class:`pyramid.config.Configurator` constructor.
5bf23f 46
CM 47         .. note:: the :attr:`pyramid.registry.Registry.settings` API
48            performs the same duty.
49            """
50         return self.registry.settings
51
5f5a7e 52
d35851 53 def Settings(d=None, _environ_=os.environ, **kw):
5f5a7e 54     """ Deployment settings.  Update application settings (usually
CM 55     from PasteDeploy keywords) with framework-specific key/value pairs
56     (e.g. find ``PYRAMID_DEBUG_AUTHORIZATION`` in os.environ and jam into
57     keyword args)."""
d35851 58     if d is None:
MM 59         d = {}
0edf02 60     d = dict(d)
d35851 61     d.update(**kw)
5f5a7e 62
d35851 63     eget = _environ_.get
0c29cf 64
d35851 65     def expand_key(key):
MM 66         keys = [key]
67         if not key.startswith('pyramid.'):
68             keys.append('pyramid.' + key)
69         return keys
0c29cf 70
d35851 71     def S(settings_key, env_key=None, type_=str, default=False):
MM 72         value = default
73         keys = expand_key(settings_key)
74         for key in keys:
75             value = d.get(key, value)
76         if env_key:
77             value = eget(env_key, value)
78         value = type_(value)
79         d.update({k: value for k in keys})
0c29cf 80
87b7b7 81     def O(settings_key, override_key):  # noqa: E743
d35851 82         for key in expand_key(settings_key):
MM 83             d[key] = d[key] or d[override_key]
5f5a7e 84
d35851 85     S('debug_all', 'PYRAMID_DEBUG_ALL', asbool)
MM 86     S('debug_authorization', 'PYRAMID_DEBUG_AUTHORIZATION', asbool)
87     O('debug_authorization', 'debug_all')
88     S('debug_notfound', 'PYRAMID_DEBUG_NOTFOUND', asbool)
89     O('debug_notfound', 'debug_all')
90     S('debug_routematch', 'PYRAMID_DEBUG_ROUTEMATCH', asbool)
91     O('debug_routematch', 'debug_all')
92     S('debug_templates', 'PYRAMID_DEBUG_TEMPLATES', asbool)
93     O('debug_templates', 'debug_all')
5f5a7e 94
d35851 95     S('reload_all', 'PYRAMID_RELOAD_ALL', asbool)
MM 96     S('reload_templates', 'PYRAMID_RELOAD_TEMPLATES', asbool)
97     O('reload_templates', 'reload_all')
98     S('reload_assets', 'PYRAMID_RELOAD_ASSETS', asbool)
99     O('reload_assets', 'reload_all')
100     S('reload_resources', 'PYRAMID_RELOAD_RESOURCES', asbool)
101     O('reload_resources', 'reload_all')
102     # reload_resources is an older alias for reload_assets
103     for k in expand_key('reload_assets') + expand_key('reload_resources'):
104         d[k] = d['reload_assets'] or d['reload_resources']
5f5a7e 105
d35851 106     S('default_locale_name', 'PYRAMID_DEFAULT_LOCALE_NAME', str, 'en')
MM 107     S('prevent_http_cache', 'PYRAMID_PREVENT_HTTP_CACHE', asbool)
108     S('prevent_cachebust', 'PYRAMID_PREVENT_CACHEBUST', asbool)
109     S('csrf_trusted_origins', 'PYRAMID_CSRF_TRUSTED_ORIGINS', aslist, [])
c151ad 110
d35851 111     return d