Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import os 

2 

3from pyramid.settings import asbool, aslist 

4 

5 

6class SettingsConfiguratorMixin(object): 

7 def _set_settings(self, mapping): 

8 if mapping is None: 

9 mapping = {} 

10 settings = Settings(mapping) 

11 self.registry.settings = settings 

12 return settings 

13 

14 def add_settings(self, settings=None, **kw): 

15 """Augment the :term:`deployment settings` with one or more 

16 key/value pairs. 

17 

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 

45 :class:`pyramid.config.Configurator` constructor. 

46 

47 .. note:: the :attr:`pyramid.registry.Registry.settings` API 

48 performs the same duty. 

49 """ 

50 return self.registry.settings 

51 

52 

53def Settings(d=None, _environ_=os.environ, **kw): 

54 """ Deployment settings. Update application settings (usually 

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).""" 

58 if d is None: 

59 d = {} 

60 d = dict(d) 

61 d.update(**kw) 

62 

63 eget = _environ_.get 

64 

65 def expand_key(key): 

66 keys = [key] 

67 if not key.startswith('pyramid.'): 

68 keys.append('pyramid.' + key) 

69 return keys 

70 

71 def S(settings_key, env_key=None, type_=str, default=False): 

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}) 

80 

81 def O(settings_key, override_key): # noqa: E743 

82 for key in expand_key(settings_key): 

83 d[key] = d[key] or d[override_key] 

84 

85 S('debug_all', 'PYRAMID_DEBUG_ALL', asbool) 

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') 

94 

95 S('reload_all', 'PYRAMID_RELOAD_ALL', asbool) 

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'] 

105 

106 S('default_locale_name', 'PYRAMID_DEFAULT_LOCALE_NAME', str, 'en') 

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, []) 

110 

111 return d