# 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)
# 
# 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
# 
# def create_new_revision(args, msg=None):
#     """Will create a new autogenerated revision file. An returns the
#     path of the new generated file."""
#     cfg = get_alembic_config(args)
#     command.revision(cfg, message=msg, autogenerate=True)
#     revision_file = get_last_revision_file(args)
#     while is_locked(revision_file):
#         time.sleep(1)
#     return revision_file


import os
import fnmatch
import json
import subprocess
import uuid
import transaction
import pkg_resources
from sqlalchemy import func
from mako.lookup import TemplateLookup
from ringo.lib.helpers import get_app_location, dynamic_import
from ringo.scripts.db import (
    get_session, create_new_revision
)
from ringo.model.modul import ModulItem, ActionItem, ACTIONS
    else:
        id = session.query(
            func.max(ModulItem.id)).filter(ModulItem.id > 999).one()[0]
    if id:
        return id + 1
    else:
        return 1000


def get_next_actionid(session):
    id = session.query(func.max(ActionItem.id)).one()[0]
    return id + 1


def _get_fixture_file(path, pattern):
    for root, dirs, files in os.walk(path):
        for name in files:
            if fnmatch.fnmatch(name, pattern):
                return os.path.join(root, name)


def replace_sql_in_revision(path, package, name, session):
    content = None
    sql = []
    modul_fixture = get_modul_fixture(name, package, session)
    modul_sql = ("INSERT INTO modules (id, name, description, label, ",
                 "label_plural, clazzpath, display, str_repr, uuid) ",
                 "VALUES ({id}, '{name}', '', '{label}', '{label_plural}', ",
                 "'{clazzpath}', '{display}', '{str_repr}', '{uuid}');")
    sql.append("".join(modul_sql).format(**modul_fixture))

# 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)
# 
# 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
# 
# def create_new_revision(args, msg=None):
#     """Will create a new autogenerated revision file. An returns the
#     path of the new generated file."""
#     cfg = get_alembic_config(args)
#     command.revision(cfg, message=msg, autogenerate=True)
#     revision_file = get_last_revision_file(args)
#     while is_locked(revision_file):
#         time.sleep(1)
#     return revision_file


# Path: ringo/scripts/modul.py
import os
import fnmatch
import json
import subprocess
import uuid
import transaction
import pkg_resources
from sqlalchemy import func
from mako.lookup import TemplateLookup
from ringo.lib.helpers import get_app_location, dynamic_import
from ringo.scripts.db import (
    get_session, create_new_revision
)
from ringo.model.modul import ModulItem, ActionItem, ACTIONS

    else:
        id = session.query(
            func.max(ModulItem.id)).filter(ModulItem.id > 999).one()[0]
    if id:
        return id + 1
    else:
        return 1000


def get_next_actionid(session):
    id = session.query(func.max(ActionItem.id)).one()[0]
    return id + 1


def _get_fixture_file(path, pattern):
    for root, dirs, files in os.walk(path):
        for name in files:
            if fnmatch.fnmatch(name, pattern):
                return os.path.join(root, name)


def replace_sql_in_revision(path, package, name, session):
    content = None
    sql = []
    modul_fixture = get_modul_fixture(name, package, session)
    modul_sql = ("INSERT INTO modules (id, name, description, label, ",
                 "label_plural, clazzpath, display, str_repr, uuid) ",
                 "VALUES ({id}, '{name}', '', '{label}', '{label_plural}', ",
                 "'{clazzpath}', '{display}', '{str_repr}', '{uuid}');")
    sql.append("".join(modul_sql).format(**modul_fixture))

    modul_id = modul_fixture["id"]