Pylons

Feb 8, 2010 6:19:40 PM

old restore.py

Language: Python
Tags: registry
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Restore the Pylons StackedObjectProxies and paste.deploy.CONFIG from a request
to /_test_vars
"""
import os

import pkg_resources

import paste.deploy
import paste.fixture

def restoration_context(config, func=None, *args, **kwargs):
    """Run the specified function in a context containing restored Pylons
    StackedObjectProxies

    e.g.:

        def setup(drop_db_tables=False):
            from paste.deploy import CONFIG
            from pylons import cache, g
            uri = CONFIG.get('sqlalchemy.dburi')
            if drop_db_tables:
                drop_tables(uri)
            create_tables_and_init_data(uri)
            cache.get_cache('main').put('init_data',
                                        get_init_data(uri),
                                        type='disk')

        restoration_context('config:/app/production.ini', setup,
                            drop_db_tables=True)
    """
    restoration_begin(config)
    try:
        func(*args, **kwargs)
    finally:
        restoration_end()

def restoration_begin(config_uri):
    """Restore Pylons StackedObjectProxies"""
    if ':' not in config_uri:
        raise LookupError("URI has no scheme: %r" % config_uri)
    path = os.path.dirname(config_uri.split(':', 1)[1])
    pkg_resources.working_set.add_entry(path)

    # Load app config into paste.deploy to simulate request config
    conf = paste.deploy.appconfig(config_uri, relative_to=path)
    conf.update(dict(app_conf=conf.local_conf, global_conf=conf.global_conf))
    paste.deploy.config.CONFIG.push_thread_config(conf)

    # Query the test app to setup the environment
    app = paste.deploy.loadapp(config_uri, relative_to=path)
    app = paste.fixture.TestApp(app)
    request_id = int(app.get('/_test_vars').body)

    # Restore the state of the Pylons special objects (StackedObjectProxies)
    paste.registry.restorer.restoration_begin(request_id)

def restoration_end():
    """End restoration of Pylons StackedObjectProxies"""
    paste.registry.restorer.restoration_end()
    paste.deploy.config.CONFIG.pop_thread_config()

Download

Comments (0)

You must login before you can comment.

Powered by Pylons - Contact Administrators