Beispiel #1
0
def develop_app_conf():
    module = request.GET['module']
    app_path = pkg.resource_filename(module, '')
    
    form = '<h3>Nothing need to configure!</h3>'
    message = ''
    if is_pyfile_exist(app_path, 'conf'):
        try:
            mod = __import__(module + '.conf', {}, {}, [''])
            f = getattr(mod, 'ManageForm', None)
            if f:
                form = f(action=url_for(develop_app_conf)+'?module=%s' % module, method='post')
                if request.method == 'POST':
                    ini = Ini(os.path.join(application.apps_dir, 'settings.ini'))
                    default_ini = Ini(os.path.join(app_path, 'settings.ini'))
                    r = form.validate(request.POST)
                    if r:
                        flag = form_to_ini(form, ini, default_ini)
                        if flag:
                            message = '<div class="note">Changes have been saved!</div>'
                            ini.save()
                        else:
                            message = '<div class="important">There are no changes.</div>'
                    else:
                        message = '<div class="warning">There are some errors.</div>'
                elif request.method == 'GET':
                    ini = Ini()
                    ini_file = os.path.join(app_path, 'settings.ini')
                    if os.path.exists(ini_file):
                        ini.read(ini_file)
                    ini.read(os.path.join(application.apps_dir, 'settings.ini'))
                    ini_to_form(form, ini)
        
        except ImportError, e:
            log.exception(e)
Beispiel #2
0
    def handle(self, options, global_options, *args):
        from uliweb.core.SimpleFrame import get_app_dir, Dispatcher
        from uliweb import orm

        app = Dispatcher(project_dir=global_options.project, start=False)

        if not args:
            apps_list = self.get_apps(global_options)
        else:
            apps_list = args
        
        con = orm.get_connection()
        
        for p in apps_list:
            if not is_pyfile_exist(get_app_dir(p), 'dbinit'):
                continue
            m = '%s.dbinit' % p
            try:
                if global_options.verbose:
                    print "Processing %s..." % m
                con.begin()
                mod = __import__(m, {}, {}, [''])
                con.commit()
            except ImportError:
                con.rollback()
                log.exception("There are something wrong when importing module [%s]" % m)
Beispiel #3
0
    def handle(self, options, global_options, *args):
        from uliweb.core.SimpleFrame import get_app_dir
        from uliweb import orm

        engine = get_engine(options, global_options)

        if not args:
            apps_list = self.get_apps(global_options)
        else:
            apps_list = args

        for p in apps_list:
            if not is_pyfile_exist(get_app_dir(p), 'dbinit'):
                continue
            m = '%s.dbinit' % p
            try:
                if global_options.verbose:
                    print "[%s] Processing %s..." % (options.engine, m)
                orm.Begin()
                mod = __import__(m, fromlist=['*'])
                orm.Commit()
            except ImportError:
                orm.Rollback()
                log.exception(
                    "There are something wrong when importing module [%s]" % m)
Beispiel #4
0
    def use(vars, env, plugin, *args, **kwargs):
        from uliweb.core.SimpleFrame import get_app_dir
        from uliweb import application as app, settings
        from uliweb.utils.common import is_pyfile_exist

        if plugin in UseNode.__saved_template_plugins_modules__:
            mod = UseNode.__saved_template_plugins_modules__[plugin]
        else:
            #add settings support, only support simple situation
            #so for complex cases you should still write module
            #format just like:
            #
            #[TEMPLATE_USE]
            #name = {
            #   'toplinks':[
            #       'myapp/jquery.myapp.{version}.min.js',
            #   ],
            #   'depends':[xxxx],
            #   'config':{'version':'UI_CONFIG/test'},
            #   'default':{'version':'1.2.0'},
            #}
            #
            mod = None
            c = settings.get_var('TEMPLATE_USE/' + plugin)
            if c:
                config = c.pop('config', {})
                default = c.pop('default', {})
                #evaluate config value
                config = dict([(k, settings.get_var(v, default.get(k, '')))
                               for k, v in config.items()])
                #merge passed arguments
                config.update(kwargs)
                for t in ['toplinks', 'bottomlinks']:
                    if t in c:
                        c[t] = [x.format(**config) for x in c[t]]
                mod = c
            else:
                for p in app.apps:
                    if not is_pyfile_exist(
                            os.path.join(get_app_dir(p), 'template_plugins'),
                            plugin):
                        continue
                    module = '.'.join([p, 'template_plugins', plugin])
                    try:
                        mod = __import__(module, {}, {}, [''])
                    except ImportError, e:
                        log.exception(e)
                        mod = None
            if mod:
                UseNode.__saved_template_plugins_modules__[plugin] = mod
            else:
                log.error(
                    "Can't find the [%s] template plugin, please check if you've installed special app already"
                    % plugin)
                if settings.get_var('TEMPLATE/RAISE_USE_EXCEPTION'):
                    raise UseModuleNotFound(
                        "Can't find the %s template plugin, check if you've installed special app already"
                        % plugin)
Beispiel #5
0
 def handle(self, options, global_options, *args):
     from uliweb.utils.common import is_pyfile_exist
     from uliweb.core.SimpleFrame import get_app_dir
     
     if not args:
         print "Error: There is no command module name behind call command."
         return
     else:
         command = args[0]
     
     if options.gevent:
         from gevent import monkey
         
         monkey.patch_all()
         
     if options.application:
         self.get_application(global_options)
         
     if not options.appname:
         apps = self.get_apps(global_options)
     else:
         apps = [options.appname]
     exe_flag = False
     
     def get_module(command, apps):
         if '.' in command:
             yield 'mod', '', command
         else:
             for f in apps:
                 yield 'app', f, command
             
     for _type, app, m in get_module(command, apps):
         mod = None
         if _type == 'mod':
             mod_name = m
             if global_options.verbose:
                 print "Importing... %s" % mod_name
             mod = __import__(m, fromlist=['*'])
         else:
             path = get_app_dir(app)
             if is_pyfile_exist(path, m):
                 mod_name = app + '.' + m
                 if global_options.verbose:
                     print "Importing... %s" % mod_name
                 mod = __import__('%s.%s' % (app, m), fromlist=['*'])
         
         if mod:
             if hasattr(mod, 'call'):
                 getattr(mod, 'call')(args, options, global_options)
             elif hasattr(mod, 'main'):
                 getattr(mod, 'main')(args, options, global_options)
             else:
                 print "Can't find call() or main() function in module %s" % mod_name
             exe_flag = True
         
     if not exe_flag:
         print "Error: Can't import the [%s], please check the file and try again." % command
Beispiel #6
0
 def handle(self, options, global_options, *args):
     from uliweb.utils.common import is_pyfile_exist
     from uliweb.core.SimpleFrame import get_app_dir
     
     if not args:
         print "Error: There is no command module name behind call command."
         return
     else:
         command = args[0]
     
     if options.gevent:
         from gevent import monkey
         
         monkey.patch_all()
         
     if options.application:
         self.get_application(global_options)
         
     if not options.appname:
         apps = self.get_apps(global_options)
     else:
         apps = [options.appname]
     exe_flag = False
     
     def get_module(command, apps):
         if '.' in command:
             yield 'mod', '', command
         else:
             for f in apps:
                 yield 'app', f, command
             
     for _type, app, m in get_module(command, apps):
         mod = None
         if _type == 'mod':
             mod_name = m
             if global_options.verbose:
                 print "Importing... %s" % mod_name
             mod = __import__(m, fromlist=['*'])
         else:
             path = get_app_dir(app)
             if is_pyfile_exist(path, m):
                 mod_name = app + '.' + m
                 if global_options.verbose:
                     print "Importing... %s" % mod_name
                 mod = __import__('%s.%s' % (app, m), fromlist=['*'])
         
         if mod:
             if hasattr(mod, 'call'):
                 getattr(mod, 'call')(args, options, global_options)
             elif hasattr(mod, 'main'):
                 getattr(mod, 'main')(args, options, global_options)
             else:
                 print "Can't find call() or main() function in module %s" % mod_name
             exe_flag = True
         
     if not exe_flag:
         print "Error: Can't import the [%s], please check the file and try again." % command
Beispiel #7
0
def get_tables(apps_dir, apps=None, engine=None, import_models=False, tables=None,
    settings_file='settings.ini', local_settings_file='local_settings.ini'):
    from uliweb.core.SimpleFrame import get_apps, get_app_dir
    from uliweb import orm
    from StringIO import StringIO
    
    engine = orm.engine_manager[engine]
    e = engine.options['connection_string']
    engine_name = e[:e.find('://')+3]
    
    buf = StringIO()
    
    if import_models:
        apps = get_apps(apps_dir, settings_file=settings_file, local_settings_file=local_settings_file)
        if apps:
            apps_list = apps
        else:
            apps_list = apps[:]
        models = []
        for p in apps_list:
            if p not in apps:
                log.error('Error: Appname %s is not a valid app' % p)
                continue
            if not is_pyfile_exist(get_app_dir(p), 'models'):
                continue
            m = '%s.models' % p
            try:
                mod = __import__(m, {}, {}, [''])
                models.append(mod)
            except ImportError:
                log.exception("There are something wrong when importing module [%s]" % m)
        
    else:
        old_models = orm.__models__.keys()
        try:
            for tablename, m in orm.__models__.items():
                orm.get_model(tablename)
        except:
            print "Problems to models like:", list(set(old_models) ^ set(orm.__models__.keys()))
            raise
            
    if apps:
        t = {}
        for tablename, m in engine.metadata.tables.items():
            if hasattr(m, '__appname__') and m.__appname__ in apps:
                t[tablename] = engine.metadata.tables[tablename]
    elif tables:
        t = {}
        for tablename, m in engine.metadata.tables.items():
            if tablename in tables:
                t[tablename] = engine.metadata.tables[tablename]
    else:
        t = engine.metadata.tables
                
    return t
Beispiel #8
0
def get_tables(apps_dir, apps=None, engine=None, import_models=False, settings_file='settings.ini', local_settings_file='local_settings.ini'):
    from uliweb.core.SimpleFrame import get_apps, get_app_dir
    from uliweb import orm
    from sqlalchemy import create_engine
    from StringIO import StringIO
    
    if not engine:
        engine = get_engine(apps_dir)
    
    _engine = engine[:engine.find('://')+3]
    
    buf = StringIO()
    
    con = create_engine(_engine, strategy='mock', executor=lambda s, p='': buf.write(str(s) + p))
    db = orm.get_connection(con)
    
    if import_models:
        apps = get_apps(apps_dir, settings_file=settings_file, local_settings_file=local_settings_file)
        if apps:
            apps_list = apps
        else:
            apps_list = apps[:]
        models = []
        for p in apps_list:
            if p not in apps:
                log.error('Error: Appname %s is not a valid app' % p)
                continue
            if not is_pyfile_exist(get_app_dir(p), 'models'):
                continue
            m = '%s.models' % p
            try:
                mod = __import__(m, {}, {}, [''])
                models.append(mod)
            except ImportError:
                log.exception("There are something wrong when importing module [%s]" % m)
        
    else:
        old_models = orm.__models__.keys()
        try:
            for tablename, m in orm.__models__.items():
                orm.get_model(tablename)
        except:
            print "Problems to models like:", list(set(old_models) ^ set(orm.__models__.keys()))
            raise
            
    if apps:
        tables = {}
        for tablename, m in db.metadata.tables.iteritems():
            if hasattr(m, '__appname__') and m.__appname__ in apps:
                tables[tablename] = db.metadata.tables[tablename]
    else:
        tables = db.metadata.tables
                
    return tables
Beispiel #9
0
 def collect_commands():
     from uliweb import get_apps, get_app_dir
     from uliweb.utils.common import is_pyfile_exist
     
     apps = get_apps(global_options.apps_dir, settings_file=global_options.settings,
             local_settings_file=global_options.local_settings)
     for f in apps:
         path = get_app_dir(f)
         if is_pyfile_exist(path, 'commands'):
             m = '%s.commands' % f
             mod = __import__(m, fromlist=['*'])
         
             find_mod_commands(mod)
Beispiel #10
0
 def collect_commands():
     from uliweb import get_apps, get_app_dir
     from uliweb.utils.common import is_pyfile_exist
     
     apps = get_apps(global_options.apps_dir, settings_file=global_options.settings,
             local_settings_file=global_options.local_settings)
     for f in apps:
         path = get_app_dir(f)
         if is_pyfile_exist(path, 'commands'):
             m = '%s.commands' % f
             mod = __import__(m, fromlist=['*'])
         
             find_mod_commands(mod)
Beispiel #11
0
    def use(vars, env, plugin, *args, **kwargs):
        from uliweb.core.SimpleFrame import get_app_dir
        from uliweb import application as app, settings
        from uliweb.utils.common import is_pyfile_exist

        if plugin in UseNode.__saved_template_plugins_modules__:
            mod = UseNode.__saved_template_plugins_modules__[plugin]
        else:
            #add settings support, only support simple situation
            #so for complex cases you should still write module
            #format just like:
            #
            #[TEMPLATE_USE]
            #name = {
            #   'toplinks':[
            #       'myapp/jquery.myapp.{version}.min.js',
            #   ], 
            #   'depends':[xxxx], 
            #   'config':{'version':'UI_CONFIG/test'},
            #   'default':{'version':'1.2.0'},
            #}
            #
            mod = None
            c = settings.get_var('TEMPLATE_USE/'+plugin)
            if c:
                config = c.pop('config', {})
                default = c.pop('default', {})
                #evaluate config value
                config = dict([(k, settings.get_var(v, default.get(k, ''))) for k, v in config.items()])
                #merge passed arguments
                config.update(kwargs)
                for t in ['toplinks', 'bottomlinks']:
                    if t in c:
                        c[t] = [x.format(**config) for x in c[t]]
                mod = c
            else:
                for p in app.apps:
                    if not is_pyfile_exist(os.path.join(get_app_dir(p), 'template_plugins'), plugin):
                        continue
                    module = '.'.join([p, 'template_plugins', plugin])
                    try:
                        mod = __import__(module, {}, {}, [''])
                    except ImportError, e:
                        log.exception(e)
                        mod = None
            if mod:
                UseNode.__saved_template_plugins_modules__[plugin] = mod
            else:
                log.error("Can't find the [%s] template plugin, please check if you've installed special app already" % plugin)
                if settings.get_var('TEMPLATE/RAISE_USE_EXCEPTION'):
                    raise UseModuleNotFound("Can't find the %s template plugin, check if you've installed special app already" % plugin)
Beispiel #12
0
def _get_apps(application, path, parent_module, catalogs, apps, app_apps):
    if not os.path.exists(path):
        return
    for p in os.listdir(path):
        if p in ['.svn', 'CVS'] or p.startswith('.') or p.startswith('_'):
            continue
        app_path = os.path.join(path, p)

        if not os.path.isdir(app_path):
            continue

        _get_app(app_path, p, apps, catalogs, app_apps, parent_module)

        _path = os.path.join(path, p)
        if is_pyfile_exist(_path, '__init__'):
            if parent_module:
                m = parent_module + '.' + p
            else:
                m = p
            _get_apps(application, _path, m, catalogs, apps, app_apps)
Beispiel #13
0
def _get_apps(application, path, parent_module, catalogs, apps, app_apps):
    if not os.path.exists(path):
        return
    for p in os.listdir(path):
        if p in ['.svn', 'CVS'] or p.startswith('.') or p.startswith('_'):
            continue
        app_path = os.path.join(path, p)

        if not os.path.isdir(app_path):
            continue

        _get_app(app_path, p, apps, catalogs, app_apps, parent_module)

        _path = os.path.join(path, p)
        if is_pyfile_exist(_path, '__init__'):
            if parent_module:
                m = parent_module + '.' + p
            else:
                m = p
            _get_apps(application, _path, m, catalogs, apps, app_apps)
Beispiel #14
0
def develop_app_conf():
    module = request.GET['module']
    app_path = pkg.resource_filename(module, '')

    form = '<h3>Nothing need to configure!</h3>'
    message = ''
    if is_pyfile_exist(app_path, 'conf'):
        try:
            mod = __import__(module + '.conf', {}, {}, [''])
            f = getattr(mod, 'ManageForm', None)
            if f:
                form = f(action=url_for(develop_app_conf) +
                         '?module=%s' % module,
                         method='post')
                if request.method == 'POST':
                    ini = Ini(
                        os.path.join(application.apps_dir, 'settings.ini'))
                    default_ini = Ini(os.path.join(app_path, 'settings.ini'))
                    r = form.validate(request.POST)
                    if r:
                        flag = form_to_ini(form, ini, default_ini)
                        if flag:
                            message = '<div class="note">Changes have been saved!</div>'
                            ini.save()
                        else:
                            message = '<div class="important">There are no changes.</div>'
                    else:
                        message = '<div class="warning">There are some errors.</div>'
                elif request.method == 'GET':
                    ini = Ini()
                    ini_file = os.path.join(app_path, 'settings.ini')
                    if os.path.exists(ini_file):
                        ini.read(ini_file)
                    ini.read(os.path.join(application.apps_dir,
                                          'settings.ini'))
                    ini_to_form(form, ini)

        except ImportError, e:
            log.exception(e)
Beispiel #15
0
    def use(vars, env, plugin, *args, **kwargs):
        from uliweb.core.SimpleFrame import get_app_dir
        from uliweb import application as app, settings

        if plugin in UseNode.__saved_template_plugins_modules__:
            mod = UseNode.__saved_template_plugins_modules__[plugin]
        else:
            from uliweb.utils.common import is_pyfile_exist
            mod = None
            for p in app.apps:
                if not is_pyfile_exist(os.path.join(get_app_dir(p), 'template_plugins'), plugin):
                    continue
                module = '.'.join([p, 'template_plugins', plugin])
                try:
                    mod = __import__(module, {}, {}, [''])
                except ImportError, e:
                    log.exception(e)
                    mod = None
            if mod:
                UseNode.__saved_template_plugins_modules__[plugin] = mod
            else:
                log.error("Can't find the [%s] template plugin, please check if you've installed special app already" % plugin)
                if settings.get_var('TEMPLATE/RAISE_USE_EXCEPTION'):
                    raise UseModuleNotFound("Can't find the %s template plugin, check if you've installed special app already" % plugin)
Beispiel #16
0
    def handle(self, options, global_options, *args):
        from uliweb.core.SimpleFrame import get_app_dir
        from uliweb import orm

        engine = get_engine(options, global_options)

        if not args:
            apps_list = self.get_apps(global_options)
        else:
            apps_list = args
        
        for p in apps_list:
            if not is_pyfile_exist(get_app_dir(p), 'dbinit'):
                continue
            m = '%s.dbinit' % p
            try:
                if global_options.verbose:
                    print "[%s] Processing %s..." % (options.engine, m)
                orm.Begin()
                mod = __import__(m, fromlist=['*'])
                orm.Commit()
            except ImportError:
                orm.Rollback()
                log.exception("There are something wrong when importing module [%s]" % m)
Beispiel #17
0
def get_tables(apps_dir,
               apps=None,
               engine=None,
               import_models=False,
               tables=None,
               settings_file='settings.ini',
               local_settings_file='local_settings.ini'):
    from uliweb.core.SimpleFrame import get_apps, get_app_dir
    from uliweb import orm
    from StringIO import StringIO

    engine = orm.engine_manager[engine]
    e = engine.options['connection_string']
    engine_name = e[:e.find('://') + 3]

    buf = StringIO()

    if import_models:
        apps = get_apps(apps_dir,
                        settings_file=settings_file,
                        local_settings_file=local_settings_file)
        if apps:
            apps_list = apps
        else:
            apps_list = apps[:]
        models = []
        for p in apps_list:
            if p not in apps:
                log.error('Error: Appname %s is not a valid app' % p)
                continue
            if not is_pyfile_exist(get_app_dir(p), 'models'):
                continue
            m = '%s.models' % p
            try:
                mod = __import__(m, {}, {}, [''])
                models.append(mod)
            except ImportError:
                log.exception(
                    "There are something wrong when importing module [%s]" % m)

    else:
        old_models = orm.__models__.keys()
        try:
            for tablename, m in orm.__models__.items():
                orm.get_model(tablename)
        except:
            print "Problems to models like:", list(
                set(old_models) ^ set(orm.__models__.keys()))
            raise

    if apps:
        t = {}
        for tablename, m in engine.metadata.tables.items():
            if hasattr(m, '__appname__') and m.__appname__ in apps:
                t[tablename] = engine.metadata.tables[tablename]
    elif tables:
        t = {}
        for tablename, m in engine.metadata.tables.items():
            if tablename in tables:
                t[tablename] = engine.metadata.tables[tablename]
    else:
        t = engine.metadata.tables

    return t
Beispiel #18
0
def find(plugin, *args, **kwargs):
    from uliweb.core.SimpleFrame import get_app_dir
    from uliweb import application as app, settings
    from uliweb.utils.common import is_pyfile_exist, import_attr

    key = (plugin, repr(args) + repr(sorted(kwargs.items())))

    if key in __use_cached__:
        return __use_cached__[key]

    if plugin in __saved_template_plugins_modules__:
        mod = __saved_template_plugins_modules__[plugin]
    else:
        #add settings support, only support simple situation
        #so for complex cases you should still write module
        #format just like:
        #
        #[TEMPLATE_USE]
        #name = {
        #   'toplinks':[
        #       'myapp/jquery.myapp.{version}.min.js',
        #   ],
        #   'depends':[xxxx],
        #   'config':{'version':'UI_CONFIG/test'},
        #   'default':{'version':'1.2.0'},
        #}
        #
        # add toplinks and bottomlinks could be config by a function path,
        # just like:
        #
        #[TEMPLATE_USE]
        #name = {
        #   'toplinks':'#{appname}.load_js',
        #   'config':{'version':'UI_CONFIG/test'},
        #}
        mod = None
        c = settings.get_var('TEMPLATE_USE/' + plugin)
        if c:
            config = c.pop('config', {})
            default = c.pop('default', {})
            #evaluate config value
            config = dict([(k, settings.get_var(v, default.get(k, '')))
                           for k, v in config.items()])
            #merge passed arguments
            config.update(kwargs)
            for t in ['toplinks', 'bottomlinks']:
                if t in c:
                    links = c[t]
                    if isinstance(links, (tuple, list)):
                        c[t] = [x.format(**config) for x in c[t]]
                    elif isinstance(links, string_types):
                        m = import_attr(links)
                        c[t] = [x for x in m(**config)]
            mod = c
        else:
            for p in reversed(app.apps):
                if not is_pyfile_exist(
                        os.path.join(get_app_dir(p), 'template_plugins'),
                        plugin):
                    continue
                module = '.'.join([p, 'template_plugins', plugin])
                try:
                    mod = __import__(module, fromlist=['*'])
                    break
                except ImportError as e:
                    log.info('Module path is {}'.format(
                        os.path.join(get_app_dir(p), 'template_plugins',
                                     plugin)))
                    log.exception(e)
                    mod = None
        if mod:
            __saved_template_plugins_modules__[plugin] = mod
        else:
            log.error(
                "Can't find the [%s] template plugin, please check if you've installed special app already"
                % plugin)
            raise UseModuleNotFound(
                "Can't find the %s template plugin, check if you've installed special app already"
                % plugin)

    #mod maybe an dict
    if isinstance(mod, dict):
        v = mod
    else:
        v = None
        call = getattr(mod, 'call', None)
        call.__name__ = call.__module__
        if call:
            para = inspect.getargspec(call)[0]
            #test if the funtion is defined as old style
            if ['app', 'var', 'env'] == para[:3]:
                warnings.simplefilter('default')
                warnings.warn(
                    "Tmplate plugs call function(%s) should be defined"
                    " as call(*args, **kwargs) not need (app, var, env) any more"
                    % call.__module__, DeprecationWarning)
                v = call(app, {}, {}, *args, **kwargs)

            else:
                v = call(*args, **kwargs)

    toplinks = []
    bottomlinks = []
    if v:
        if 'depends' in v:
            for _t in v['depends']:
                if isinstance(_t, str):
                    t, b = find(_t)
                else:
                    d, kw = _t
                    t, b = find(d, **kw)
                toplinks.extend(t)
                bottomlinks.extend(b)
        if 'toplinks' in v:
            links = v['toplinks']
            if not isinstance(links, (tuple, list)):
                links = [links]
            toplinks.extend(links)
        if 'bottomlinks' in v:
            links = v['bottomlinks']
            if not isinstance(links, (tuple, list)):
                links = [links]
            bottomlinks.extend(links)
        if 'depends_after' in v:
            for _t in v['depends_after']:
                if isinstance(_t, str):
                    t, b = use(env, _t)
                else:
                    d, kw = _t
                    t, b = use(env, d, **kw)
                toplinks.extend(t)
                bottomlinks.extend(b)

    __use_cached__[key] = toplinks, bottomlinks
    return toplinks, bottomlinks
Beispiel #19
0
def find(plugin, *args, **kwargs):
    from uliweb.core.SimpleFrame import get_app_dir
    from uliweb import application as app, settings
    from uliweb.utils.common import is_pyfile_exist

    key = (plugin, repr(args) + repr(sorted(kwargs.items())))
    if key in __use_cached__:
        return __use_cached__[key]

    if plugin in __saved_template_plugins_modules__:
        mod = __saved_template_plugins_modules__[plugin]
    else:
        #add settings support, only support simple situation
        #so for complex cases you should still write module
        #format just like:
        #
        #[TEMPLATE_USE]
        #name = {
        #   'toplinks':[
        #       'myapp/jquery.myapp.{version}.min.js',
        #   ],
        #   'depends':[xxxx],
        #   'config':{'version':'UI_CONFIG/test'},
        #   'default':{'version':'1.2.0'},
        #}
        #
        mod = None
        c = settings.get_var('TEMPLATE_USE/'+plugin)
        if c:
            config = c.pop('config', {})
            default = c.pop('default', {})
            #evaluate config value
            config = dict([(k, settings.get_var(v, default.get(k, ''))) for k, v in config.items()])
            #merge passed arguments
            config.update(kwargs)
            for t in ['toplinks', 'bottomlinks']:
                if t in c:
                    c[t] = [x.format(**config) for x in c[t]]
            mod = c
        else:
            for p in reversed(app.apps):
                if not is_pyfile_exist(os.path.join(get_app_dir(p), 'template_plugins'), plugin):
                    continue
                module = '.'.join([p, 'template_plugins', plugin])
                try:
                    mod = __import__(module, fromlist=['*'])
                    break
                except ImportError as e:
                    log.exception(e)
                    mod = None
        if mod:
            __saved_template_plugins_modules__[plugin] = mod
        else:
            log.error("Can't find the [%s] template plugin, please check if you've installed special app already" % plugin)
            raise UseModuleNotFound("Can't find the %s template plugin, check if you've installed special app already" % plugin)

    #mod maybe an dict
    if isinstance(mod, dict):
        v = mod
    else:
        v = None
        call = getattr(mod, 'call', None)
        call.__name__ = call.__module__
        if call:
            para = inspect.getargspec(call)[0]
            #test if the funtion is defined as old style
            if ['app', 'var', 'env'] == para[:3]:
                warnings.simplefilter('default')
                warnings.warn("Tmplate plugs call function(%s) should be defined"
                              " as call(*args, **kwargs) not need (app, var, env) any more" % call.__module__,
                              DeprecationWarning)
                v = call(app, {}, {}, *args, **kwargs)

            else:
                v = call(*args, **kwargs)

    toplinks = []
    bottomlinks = []
    if v:
        if 'depends' in v:
            for _t in v['depends']:
                if isinstance(_t, str):
                    t, b = find(_t)
                else:
                    d, kw = _t
                    t, b = find(d, **kw)
                toplinks.extend(t)
                bottomlinks.extend(b)
        if 'toplinks' in v:
            links = v['toplinks']
            if not isinstance(links, (tuple, list)):
                links = [links]
            toplinks.extend(links)
        if 'bottomlinks' in v:
            links = v['bottomlinks']
            if not isinstance(links, (tuple, list)):
                links = [links]
            bottomlinks.extend(links)
        if 'depends_after' in v:
            for _t in v['depends_after']:
                if isinstance(_t, str):
                    t, b = use(env, _t)
                else:
                    d, kw = _t
                    t, b = use(env, d, **kw)
                toplinks.extend(t)
                bottomlinks.extend(b)

    __use_cached__[key] = toplinks, bottomlinks
    return toplinks, bottomlinks