#-*- coding: utf-8 -*-
"""
Initialize a python shell with a given environment (a config file).
"""

import os
import sys

from sqlalchemy import engine_from_config

from pyramid.paster import get_appsettings, setup_logging
from pyramid.config import Configurator

from pyshop.models import DBSession, initialize_sql
from pyshop.helpers import pypi



def usage(argv):
    cmd = os.path.basename(argv[0])
    print('usage: %s <config_uri>\n'
          '(example: "%s development.ini")' % (cmd, cmd))
    sys.exit(1)


def main(argv=sys.argv):
    if len(argv) != 2:
        usage(argv)
    config_uri = argv[1]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, 'main')
    engine = engine_from_config(settings, 'sqlalchemy.')
    initialize_sql(engine)
    pypi.set_proxy(settings['pypi.url'])

    config = Configurator(settings=settings)
    config.end()

    # XXX Configure pyramid_celery, something looks wrong, the bad way ???
    # from pyramid_celery.commands.celeryctl import CeleryCtl
    # CeleryCtl().setup_app_from_commandline(argv)

    # add models and session to locals
    from pyshop.models import (User, Group,
                               Package, Release, ReleaseFile)

    session = DBSession()
    try:
        from IPython import embed
        from IPython.config.loader import Config
        cfg = Config()
        cfg.InteractiveShellEmbed.confirm_exit = False
        embed(config=cfg, banner1="pyshop shell.")
    except ImportError:
        import code
        code.interact("pyshop shell", local=locals())


if __name__ == '__main__':
    main()

@@ -257,16 +257,78 @@
 urator%0A%0A
+from pyshop.helpers.sqla import create_engine, dispose_engine%0A
 from pys
@@ -358,24 +358,8 @@
 sion
-, initialize_sql
 %0Afro
@@ -653,38 +653,8 @@
 %5B1%5D%0A
-    setup_logging(config_uri)%0A
     
@@ -694,16 +694,8 @@
 _uri
-, 'main'
 )%0A  
@@ -709,411 +709,119 @@
 e = 
-engine_from_config(settings, 'sqlalchemy.')%0A    initialize_sql(engine)%0A    pypi.set_proxy(settings%5B'pypi.url'%5D)%0A%0A    config = Configurator(settings=settings)%0A    config.end()%0A%0A    # XXX Configure pyramid_celery, something looks wrong, the bad way ???%0A    # from pyramid_celery.commands.celeryctl import CeleryCtl%0A    # CeleryCtl().setup_app_from_commandline(argv)%0A%0A    # add models and session to locals
+create_engine('pyshop', settings, scoped=False)%0A%0A    config = Configurator(settings=settings)%0A    config.end()%0A
 %0A   
@@ -1155,19 +1155,30 @@
 anner1=%22
-pys
+Welcome to PyS
 hop shel

Initialize engine correctly in the shell