Is there an easy way to find runtime in Pyramid

In the pyramid, I need to display my templates in accordance with different runtime environments - enable google analytics, use minified code, etc. (in production). Is there an easy way to find out the current environment - perhaps an existing flag to find out which ini file was used?

+5
source share
2 answers

Pyramid INI files can contain arbitrary configuration entries , so why not include a flag in your files that distinguishes between production and deployment deployments?

I would do it like this: in your .ini file.

[app:main]
production_deployment = True # Set to False in your development .ini file

Pass this value to the Pyramid configurator:

def main(global_config, **settings):
    # ...
    from pyramid.settings import asbool
    production_deployment = asbool(settings.get(
               'production_deployment', 'false'))
    settings['production_deployment'] = production_deployment
    config = Configurator(settings=settings)

. , :

settings = request.registry.settings
if settings['production_deployment']:
    # Enable some production code here.

; Google Analytics, .. , , ..

+15

​​ , PYRAMID_ENV, os.environ. , :

import os

pyramid_env = os.environ.get('PYRAMID_ENV', 'debug')

if pyramid_env == 'debug':
    # Setup debug things...
else:
    # Setup production things...

init script :

PYRAMID_ENV=production python server.py

: http://docs.python.org/library/os.html#os.environ

+3
source

All Articles