Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add your app specific settings in pyramid rest framework?

I am new to pyramid.

The Issue is I am not able to figure out how app specific settings (key value pairs) work in pyramid.

This is what I have done after various google searches and other stackoverflow answers:

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    if '__file__' in global_config:
        settings.update(
            load_sensitive_settings(global_config['__file__'], global_config))
    config = Configurator(settings=settings)
    config.include('pyramid_chameleon')
    # config.add_static_view('static', 'static', cache_max_age=3600)
    # config.add_route('home', '/')
    config.add_route(
        'tags',
        '/tags', request_method='POST', accept='application/json', )
    config.scan()
    return config.make_wsgi_app()


def load_sensitive_settings(configurationPath, defaultByKey):
    'Load sensitive settings from hidden configuration file'
    # configFolder, configName = os.path.split(configurationPath)
    # sensitivePath = os.path.join(configFolder, '.' + configName)
    sensitivePath = configurationPath
    settings = {}
    configParser = ConfigParser.ConfigParser(defaultByKey)
    if not configParser.read(sensitivePath):
        log.warn('Could not open %s' % sensitivePath)
        return settings
    settings.update(configParser.items('custom'))
    return settings

I have a file where I try to fetch settings like this:

from pyramid.threadlocal import get_current_registry
settings = get_current_registry().settings
value = settings['my_key']

But I always get settings object as None.

This is how I am defining my custom settings in development.ini

[custom]
my_key = ''

This is how I start my server in develpoment

pserve development.ini

I have read that request.settings can give me settings, But that approach is not feasible for me as my key contains the name of a file which is 1.5GBs and it has to be present in memory all the time. It takes around 5 minutes to load that file in server, hence cannot load the file on demand.

Please advice.

Thanks a lot for all the help in advance.

Update:

Thanks to all the answers provided, I finally made it work.

This is how my main function looks like:

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(settings=settings)
    config.include('pyramid_chameleon')
    if '__file__' in global_config:
        init_config(global_config['__file__'])

And I made a config file, this is how my config file looks like:

import ConfigParser

settings = dict()

def init_config(filename):
    config = ConfigParser.ConfigParser()
    config.read(filename)
    settings_dict = config.items('custom')
    settings.update(settings_dict)

Now wherever I want settings, I just do:

from projectname.config import settings
settings.get('my_key')

And I put my app specific settings (development/production.py) like this

[custom]
my_key = value

Regards

HM

like image 671
Harsh M Avatar asked Dec 12 '25 14:12

Harsh M


2 Answers

Easiest way is putting your settings to the app main section with dot separated names. Example:

[app:main]
websauna.site_name = Trees
websauna.site_tag_line = Enjoy
websauna.site_url = http://localhost:6543
websauna.site_author = Trees team

Then you can do:

my_settings_value = request.registry.settings.get("websauna.site_name", "Default value)

WSGI pipeline does not bring you settings from other sections and you need to reparse the INI file with ConfigParser if you want to access the other sections (as far as I know).

If you need to load a lot of data during development time just store a filename in settings and load the file when you need to access the data, so that you don't slow the web server startup.

like image 72
Mikko Ohtamaa Avatar answered Dec 15 '25 05:12

Mikko Ohtamaa


Here is my working solution:

config.ini

[APP.CONFIG]
url = http://....


[SMTP.CONFIG]
smtp.server = ...
smtp.port = 25
smtp.login = ...
smtp.password = ...
smtp.from = ...


[DB.CONFIG] 
db.database=...
db.host=...
db.port=..
db.user=...
db.password=...

config.py

import configparser


config = configparser.ConfigParser()
config._interpolation = configparser.ExtendedInterpolation()
config.read(encoding='utf-8', filenames=['path to file/config.ini'])

smtp = config['SMTP.CONFIG']
db = config['DB.CONFIG']
mail = config['APP.CONFIG']

And how i use it in APP

from config import db
host = db['db.host']
like image 24
Chebevara Avatar answered Dec 15 '25 03:12

Chebevara