# Path: ringo/scripts/db.py
# def get_session(config_file, transactional=True):
#     """Will return a database session based on the application
#     configuration"""
#     engine = get_engine(config_file)
#     if transactional:
#         DBSession.configure(bind=engine)
#         return DBSession
#     else:
#         NTDBSession.configure(bind=engine)
#         return NTDBSession
# 
# Path: ringo/lib/helpers/appinfo.py
# def get_app_location(name=None):
#     if not name:
#         name = get_app_name()
#     return pkg_resources.get_distribution(name).location
# 
# Path: ringo/lib/helpers/misc.py
# def dynamic_import(cl):
#     d = cl.rfind(".")
#     classname = cl[d + 1:len(cl)]
#     m = __import__(cl[0:d], globals(), locals(), [classname])
#     return getattr(m, classname)
# 
# def get_modul_by_name(modulname, session=DBSession):
#     # FIXME:
#     # Compatibilty mode. Older versions of Ringo added a 's' to the
#     # extensions modul name as Ringo usally uses the plural form.
#     # Newer versions use the configured extension name. So there
#     # might be a mixture of old and new modul names in the database.
#     # This code will handle this. (ti) <2016-01-04 13:50>
#     from ringo.model.modul import ModulItem
#     return session.query(ModulItem).filter(
#         sa.or_(ModulItem.name == modulname,
#                ModulItem.name == modulname + 's')).scalar()
# 
# Path: ringo/lib/extension.py
# def _add_modul(config, actions, dbsession):
#     """Method to add the modul entries for the extension in the
#     database. This includes a entry in the modules table and the
#     configured actions.
# 
#     :config: Extension configuration as a dictionary
#     :actions: List of ActionItems
#     :dbsession: Database session
#     """
#     # Set defaults
#     name = config.get("name")
#     clazzpath = config.get("clazzpath")
#     str_repr = config.get("str_repr") or "%s|id"
#     display = config.get("display") or "hidden"
#     label = config.get("label") or name
#     label_plural = config.get("label_plural") or label
# 
#     # Add modul
#     modul = ModulItem(name=name)
#     modul.clazzpath = clazzpath
#     modul.label = label
#     modul.str_repr = str_repr
#     modul.label_plural = label_plural
#     modul.display = display
#     # Configure actions
#     modul = _configure_actions(modul, config, actions, dbsession)
#     dbsession.add(modul)
#     dbsession.flush()
#     transaction.commit()
#     log.debug("Adding new modul '%s'" % name)
#     return modul


import os
import transaction
from ringo.scripts.db import get_session
from ringo.lib.helpers import get_app_location, dynamic_import, get_modul_by_name
from ringo.lib.extension import _add_modul
from ringo.model import extensions
    template_path.append(args.app)
    template_path.append("scaffolds")
    template_path.append("basic")
    template_path.append("development.ini_tmpl")

    with open(os.path.join(*template_path)) as f:
        template = f.read()

    config_file = "production.ini"
    config_path = []
    config_path.append(args.name)
    config_path.append(config_file)
    with open(os.path.join(*config_path), "w") as c:
        template = template.replace("{{package}}", args.app)
        #template = template.replace("app.title = ringo", "app.title = %s"
        #                            % args.name)
        c.write(template)
    print("Created new configuration file %s in %s" % (config_file, args.name))

    print("\n")
    print("Next steps:")
    print("0. cd %s" % args.name)
    print("1. Adjust settings in your config")
    print("2. %s-admin db init --config %s" % (args.app, config_file))
    print("3. %s-admin fixtures load --config %s" % (args.app, config_file))
    print("4. Start application: pserve production.ini")


def handle_ext_init_command(args):
    path = []

# Path: ringo/scripts/db.py
# def get_session(config_file, transactional=True):
#     """Will return a database session based on the application
#     configuration"""
#     engine = get_engine(config_file)
#     if transactional:
#         DBSession.configure(bind=engine)
#         return DBSession
#     else:
#         NTDBSession.configure(bind=engine)
#         return NTDBSession
# 
# Path: ringo/lib/helpers/appinfo.py
# def get_app_location(name=None):
#     if not name:
#         name = get_app_name()
#     return pkg_resources.get_distribution(name).location
# 
# Path: ringo/lib/helpers/misc.py
# def dynamic_import(cl):
#     d = cl.rfind(".")
#     classname = cl[d + 1:len(cl)]
#     m = __import__(cl[0:d], globals(), locals(), [classname])
#     return getattr(m, classname)
# 
# def get_modul_by_name(modulname, session=DBSession):
#     # FIXME:
#     # Compatibilty mode. Older versions of Ringo added a 's' to the
#     # extensions modul name as Ringo usally uses the plural form.
#     # Newer versions use the configured extension name. So there
#     # might be a mixture of old and new modul names in the database.
#     # This code will handle this. (ti) <2016-01-04 13:50>
#     from ringo.model.modul import ModulItem
#     return session.query(ModulItem).filter(
#         sa.or_(ModulItem.name == modulname,
#                ModulItem.name == modulname + 's')).scalar()
# 
# Path: ringo/lib/extension.py
# def _add_modul(config, actions, dbsession):
#     """Method to add the modul entries for the extension in the
#     database. This includes a entry in the modules table and the
#     configured actions.
# 
#     :config: Extension configuration as a dictionary
#     :actions: List of ActionItems
#     :dbsession: Database session
#     """
#     # Set defaults
#     name = config.get("name")
#     clazzpath = config.get("clazzpath")
#     str_repr = config.get("str_repr") or "%s|id"
#     display = config.get("display") or "hidden"
#     label = config.get("label") or name
#     label_plural = config.get("label_plural") or label
# 
#     # Add modul
#     modul = ModulItem(name=name)
#     modul.clazzpath = clazzpath
#     modul.label = label
#     modul.str_repr = str_repr
#     modul.label_plural = label_plural
#     modul.display = display
#     # Configure actions
#     modul = _configure_actions(modul, config, actions, dbsession)
#     dbsession.add(modul)
#     dbsession.flush()
#     transaction.commit()
#     log.debug("Adding new modul '%s'" % name)
#     return modul


# Path: ringo/scripts/application.py
import os
import transaction
from ringo.scripts.db import get_session
from ringo.lib.helpers import get_app_location, dynamic_import, get_modul_by_name
from ringo.lib.extension import _add_modul
from ringo.model import extensions

    template_path.append(args.app)
    template_path.append("scaffolds")
    template_path.append("basic")
    template_path.append("development.ini_tmpl")

    with open(os.path.join(*template_path)) as f:
        template = f.read()

    config_file = "production.ini"
    config_path = []
    config_path.append(args.name)
    config_path.append(config_file)
    with open(os.path.join(*config_path), "w") as c:
        template = template.replace("{{package}}", args.app)
        #template = template.replace("app.title = ringo", "app.title = %s"
        #                            % args.name)
        c.write(template)
    print("Created new configuration file %s in %s" % (config_file, args.name))

    print("\n")
    print("Next steps:")
    print("0. cd %s" % args.name)
    print("1. Adjust settings in your config")
    print("2. %s-admin db init --config %s" % (args.app, config_file))
    print("3. %s-admin fixtures load --config %s" % (args.app, config_file))
    print("4. Start application: pserve production.ini")


def handle_ext_init_command(args):
    path = []

    path.append(get_app_location(args.app))