def collect_files(options, apps_dir, apps): files = [os.path.join(apps_dir, options.settings), os.path.join(apps_dir, options.local_settings)] def f(path): if not os.path.exists(path): log.error("Path %s is not existed!" % path) return for r in os.listdir(path): if r in ['.svn', '_svn', '.git'] or r.startswith('.'): continue fpath = os.path.join(path, r) if os.path.isdir(fpath): f(fpath) else: ext = os.path.splitext(fpath)[1] if ext in ['.py', '.ini']: files.append(fpath) from uliweb import get_app_dir for p in apps: path = get_app_dir(p) files.append(os.path.join(path, 'config.ini')) files.append(os.path.join(path, 'settings.ini')) f(path) return files
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)
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)
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)
def get_requirements(): for app in apps: path = get_app_dir(app) r_file = os.path.join(path, 'requirements.txt') if os.path.exists(r_file): yield r_file r_file = os.path.join(global_options.project, 'requirements.txt') if os.path.exists(r_file): yield r_file
def handle(self, options, global_options, *args): from uliweb.utils.common import extract_dirs from uliweb import get_app_dir if not args: extract_dirs('uliweb', 'template_files/command', '.', verbose=global_options.verbose) else: for f in args: p = get_app_dir(f) extract_dirs('uliweb', 'template_files/command', p, verbose=global_options.verbose)
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
def _find_static(self, global_options, static): from uliweb import get_app_dir apps = self.get_apps(global_options) for appname in reversed(apps): path = os.path.join(get_app_dir(appname), 'static', static) if os.path.exists(path): print '%s' % path return print 'Not Found'
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
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)
def startup_installed(sender): """ @LANGUAGE_CODE """ import os from uliweb.core.SimpleFrame import get_app_dir from uliweb.i18n import install, set_default_language from uliweb.utils.common import pkg, expand_path path = pkg.resource_filename('uliweb', '') _dirs = [] for d in sender.settings.get_var('I18N/LOCALE_DIRS', []): _dirs.append(expand_path(d)) localedir = ([os.path.normpath(sender.apps_dir + '/..')] + [get_app_dir(appname) for appname in sender.apps] + [path] + _dirs) install('uliweb', localedir) set_default_language(sender.settings.I18N.LANGUAGE_CODE)
def loadspec(apps_list, global_options): from uliweb.core.SimpleFrame import get_app_dir from uliweb import settings from redbreast.core.spec import parse, parseFile SPEC_DIR = settings.REDBREAST.SPEC_DIR SPEC_SUFFIX = settings.REDBREAST.SPEC_SUFFIX all_tasks = {} all_workflows = {} for p in apps_list: spec_dir = os.path.join(get_app_dir(p), SPEC_DIR) if os.path.isdir(spec_dir): for f in os.listdir(spec_dir): if f.endswith(SPEC_SUFFIX): file = os.path.join(spec_dir, f) print "\n* Parsing file %s ...." % file try: tasks, processes = parseFile(file) print tasks print processes except Exception, e: print "[ERROR] ParseError, %s" % e tasks, processes = {}, {} if global_options.verbose: print " tasks, %s, %s" % (len(tasks), [t for t in tasks]) print " workflow, %s, %s" % (len(processes), [t for t in processes]) for name in tasks: if name in all_tasks: print "[WARNING] duplicate definition of Task %s" % ( name) print " will be overwriten by lastest one in %s" % file all_tasks[name] = (tasks[name], file) for name in processes: if name in all_workflows: print "[WARNING] duplicate definition of Workflow %s" % ( name) print " will be overwriten by lastest one in %s" % file all_workflows[name] = (processes[name], file)
def loadspec(apps_list, global_options): from uliweb.core.SimpleFrame import get_app_dir from uliweb import settings from redbreast.core.spec import parse, parseFile SPEC_DIR = settings.GLOBAL.SPEC_DIR SPEC_SUFFIX = settings.GLOBAL.SPEC_SUFFIX all_tasks = {} all_workflows = {} for p in apps_list: spec_dir =os.path.join(get_app_dir(p), SPEC_DIR) if os.path.isdir(spec_dir): for f in os.listdir(spec_dir): if f.endswith(SPEC_SUFFIX): file = os.path.join(spec_dir, f) print "\n* Parsing file %s ...." % file try: tasks, processes = parseFile(file) print tasks print processes except Exception, e: print "[ERROR] ParseError, %s" % e tasks, processes = {}, {} if global_options.verbose: print " tasks, %s, %s" % (len(tasks), [t for t in tasks]) print " workflow, %s, %s" % (len(processes), [t for t in processes]) for name in tasks: if name in all_tasks: print "[WARNING] duplicate definition of Task %s" % (name) print " will be overwriten by lastest one in %s" % file all_tasks[name] = (tasks[name], file) for name in processes: if name in all_workflows: print "[WARNING] duplicate definition of Workflow %s" % (name) print " will be overwriten by lastest one in %s" % file all_workflows[name] = (processes[name], file)
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)
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)
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
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
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