def deletefile(f_id): from uliweb.utils.common import log fileserving = AttachmentsFileServing() Attachment = functions.get_model('generic_attachment') Tables = functions.get_model('tables') obj = Attachment.get(int(f_id)) if obj: #get tablename tablename = Tables.get(obj.table_id) check_func = settings.Generic_Attachment_Download_Checking.get(tablename) if check_func: enable = check_func(obj.content_object, request.user, 'delete') else: enable = True if enable: filename = obj.filepath obj.delete() try: fileserving.delete_filename(filename) except Exception as e: log.exception(e) else: raise Forbidden("You have no permission to delete the file.") return json({'success':True})
def delete(self, key): _file = os.path.join(self.path_to, self.subdir, self.get_key(key)) if os.path.exists(_file): try: os.remove(_file) except: log.exception()
def delete_filename(self, filename): f = self.get_filename(filename, filesystem=True, convert=False) if os.path.exists(f): try: os.unlink(f) except Exception, e: log.exception(e)
def get_redis(): from uliweb import settings from uliweb.utils.common import log import redis options = settings.REDIS if 'unix_socket_path' in options: client = redis.Redis(unix_socket_path=options['unix_socket_path']) else: global __connection_pool__ if not __connection_pool__ or __connection_pool__[0] != options['connection_pool']: d = {'host':'localhost', 'port':6379} d.update(options['connection_pool']) __connection_pool__ = (d, redis.ConnectionPool(**d)) client = redis.Redis(connection_pool=__connection_pool__[1]) if settings.REDIS.test_first: try: client.info() except Exception as e: log.exception(e) client = None return client
def get_redis(): from uliweb import settings from uliweb.utils.common import log import redis options = settings.REDIS if 'unix_socket_path' in options: client = redis.Redis(unix_socket_path=options['unix_socket_path']) else: global __connection_pool__ if not __connection_pool__ or __connection_pool__[0] != options[ 'connection_pool']: d = {'host': 'localhost', 'port': 6379} d.update(options['connection_pool']) __connection_pool__ = (d, redis.ConnectionPool(**d)) client = redis.Redis(connection_pool=__connection_pool__[1]) if settings.REDIS.test_first: try: client.info() except Exception as e: log.exception(e) client = None return client
def get_redis(**options): """ if no options defined, then it'll use settings options #unix_socket_path = '/tmp/redis.sock' connection_pool = {'host':'localhost', 'port':6379} #if test after created redis client object test_first = False """ from uliweb import settings from uliweb.utils.common import log import redis options = (options or {}) options.update(settings.REDIS) if 'unix_socket_path' in options: client = redis.Redis(unix_socket_path=options['unix_socket_path']) else: global __connection_pool__ if not __connection_pool__ or __connection_pool__[0] != options[ 'connection_pool']: d = {'host': 'localhost', 'port': 6379} d.update(options['connection_pool']) __connection_pool__ = (d, redis.ConnectionPool(**d)) client = redis.Redis(connection_pool=__connection_pool__[1]) if settings.REDIS.test_first: try: client.info() except Exception as e: log.exception(e) client = None return client
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 get_object(model, tablename, id): """ Get cached object from redis if id is None then return None: """ from uliweb.utils.common import log if not id: return if not check_enable(): return redis = get_redis() if not redis: return _id = get_id(model.get_engine_name(), tablename, id) try: if redis.exists(_id): v = redis.hgetall(_id) o = model.load(v) log.debug("objcache:get:id="+_id) return o except Exception, e: log.exception(e)
def get_redis(**options): """ if no options defined, then it'll use settings options #unix_socket_path = '/tmp/redis.sock' connection_pool = {'host':'localhost', 'port':6379} #if test after created redis client object test_first = False """ from uliweb import settings from uliweb.utils.common import log import redis options = (options or {}) options.update(settings.REDIS) if 'unix_socket_path' in options: client = redis.Redis(unix_socket_path=options['unix_socket_path']) else: global __connection_pool__ if not __connection_pool__ or __connection_pool__[0] != options['connection_pool']: d = {'host':'localhost', 'port':6379} d.update(options['connection_pool']) __connection_pool__ = (d, redis.ConnectionPool(**d)) client = redis.Redis(connection_pool=__connection_pool__[1]) if settings.REDIS.test_first: try: client.info() except Exception as e: log.exception(e) client = None return client
def execute(self, args, options, global_options): from uliweb.utils.common import check_apps_dir #add apps_dir to global_options and insert it to sys.path global_options.apps_dir = apps_dir = os.path.normpath(os.path.join(global_options.project, 'apps')) if apps_dir not in sys.path: sys.path.insert(0, apps_dir) if self.check_apps_dirs: check_apps_dir(global_options.apps_dir) if self.check_apps and args: #then args should be apps all_apps = self.get_apps(global_options) apps = args args = [] for p in apps: if p not in all_apps: print 'Error: Appname %s is not a valid app' % p sys.exit(1) else: args.append(p) try: self.handle(options, global_options, *args) except CommandError, e: log.exception(e) sys.exit(1)
def handle(self, options, global_options, *args): from uliweb import orm if args: message = """This command will delete all data of [%s] before loading, are you sure to load data""" % ','.join(args) else: message = """This command will delete whole database before loading, are you sure to load data""" get_answer(message) if not os.path.exists(options.dir): os.makedirs(options.dir) engine = get_engine(global_options.apps_dir) con = orm.get_connection(engine) for name, t in get_tables(global_options.apps_dir, args, engine=engine, settings_file=global_options.settings, local_settings_file=global_options.local_settings).items(): if global_options.verbose: print 'Loading %s...' % name try: con.begin() filename = os.path.join(options.dir, name+'.txt') if options.text: format = 'txt' else: format = None load_table(t, filename, con, delimiter=options.delimiter, format=format, encoding=options.encoding) con.commit() except: log.exception("There are something wrong when loading table [%s]" % name) con.rollback()
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)
def xmlrpc(): import uliweb.contrib.xmlrpc as rpc import xmlrpclib from werkzeug import Response from uliweb.utils.common import log from uliweb import application as app, response p, m = xmlrpclib.loads(request.data) try: f = rpc.__xmlrpc_functions__.get(m) if f: mod, handler_cls, handler = app.prepare_request(request, f) result = app.call_view(mod, handler_cls, handler, request, response, _wrap_result, args=p) xml = xmlrpclib.dumps((result, ), methodresponse=1) else: xml = xmlrpclib.dumps(xmlrpclib.Fault( -32400, 'system error: Cannot find or call %s' % m), methodresponse=1) log.debug('xmlrpc error: Cannot find or call %s' % m) except Exception as e: xml = xmlrpclib.dumps(xmlrpclib.Fault(-32400, 'system error: %s' % e), methodresponse=1) log.exception('xmlrpc error') response = Response(xml, content_type='text/xml; charset=utf-8') return response
def enable_attachments(slug, obj, path, movefile=False): """ Used to process new object files upload it'll rename the filename to new path and also add content_object according obj and if slug is None, then it'll do nothing """ fileserving = AttachmentsFileServing() for f in get_attachments(slug or obj): f.enabled = True f.content_object = obj if slug and movefile: r_filename = fileserving.get_filename(f.filepath, convert=False) dir = os.path.dirname(f.filepath) _file = os.path.basename(f.filepath) new_filename = os.path.join(dir, str(path), _file) f.filepath = new_filename r_new_filename = fileserving.get_filename(new_filename, convert=False) #check if the new directory is existed new_dir = os.path.dirname(r_new_filename) if not os.path.exists(new_dir): os.makedirs(new_dir) try: os.rename(r_filename, r_new_filename) except Exception, e: log.exception(e) log.info("from %s to %s", r_filename, r_new_filename) f.save()
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 import orm if args: message = """This command will delete all data of [%s]-[%s] before loading, are you sure to load data""" % (options.engine, ','.join(args)) else: print "Failed! You should pass one or more tables name." sys.exit(1) ans = get_answer(message, answers='Yn', quit='q') path = os.path.join(options.dir, options.engine) if not os.path.exists(path): os.makedirs(path) engine = get_engine(options, global_options) tables = get_sorted_tables( get_tables(global_options.apps_dir, engine_name=options.engine, settings_file=global_options.settings, tables=args, local_settings_file=global_options.local_settings)) _len = len(tables) for i, (name, t) in enumerate(tables): if t.__mapping_only__: if global_options.verbose: msg = 'SKIPPED(Mapping Table)' print '[%s] Loading %s...%s' % ( options.engine, show_table(name, t, i, _len), msg) continue if global_options.verbose: print '[%s] Loading %s...' % (options.engine, show_table(name, t, i, _len)), try: orm.Begin() filename = os.path.join(path, name + '.txt') if options.text: format = 'txt' else: format = None t = load_table(t, filename, engine, delimiter=options.delimiter, format=format, encoding=options.encoding, delete=ans == 'Y', bulk=int(options.bulk), engine_name=engine.engine_name) orm.Commit() if global_options.verbose: print t except: log.exception( "There are something wrong when loading table [%s]" % name) orm.Rollback()
def internal_error(self, e): tmp_file = template.get_templatefile('500'+settings.GLOBAL.TEMPLATE_SUFFIX, self.template_dirs) if tmp_file: response = self.render(tmp_file, {'url':local.request.path}, status=500) else: response = e log.exception(e) return response
def install_apps(self): for p in self.apps: try: myimport(p) except ImportError, e: pass except BaseException, e: log.exception(e)
def internal_error(self, e): tmp_file = self.template_loader.resolve_path('500'+settings.GLOBAL.TEMPLATE_SUFFIX) if tmp_file: response = self.render(tmp_file, {'url':local.request.path}, status=500) else: response = InternalServerError() log.exception(e) return response
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 handle(self, options, global_options, *args): from uliweb import orm if len(args) != 2: print self.print_help(self.prog_name, 'loadtablefile') sys.exit(1) if args: message = """Do you want to delete all data of [%s]-[%s] before loading, if you choose N, the data will not be deleted""" % ( options.engine, args[0]) else: print "Failed! You should pass one or more tables name." sys.exit(1) ans = 'Y' if global_options.yes else get_answer(message, quit='q') engine = get_engine(options, global_options) name = args[0] tables = get_tables(global_options.apps_dir, engine_name=options.engine, settings_file=global_options.settings, tables=[name], local_settings_file=global_options.local_settings) t = tables[name] if t.__mapping_only__: if global_options.verbose: msg = 'SKIPPED(Mapping Table)' print '[%s] Loading %s...%s' % ( options.engine, show_table(name, t, i, _len), msg) return if global_options.verbose: print '[%s] Loading %s...' % (options.engine, show_table(name, t, 0, 1)), try: orm.Begin() if options.text: format = 'txt' else: format = None t = load_table(t, args[1], engine, delimiter=options.delimiter, format=format, encoding=options.encoding, delete=ans == 'Y', bulk=int(options.bulk), engine_name=engine.engine_name) orm.Commit() if global_options.verbose: print t except: log.exception("There are something wrong when loading table [%s]" % name) orm.Rollback()
def f(): _id = get_id(model.get_engine_name(), tablename, instance.id) redis = get_redis() if not redis: return try: redis.delete(_id) log.debug("objcache:post_delete:id="+_id) except Exception, e: log.exception(e)
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 handle(self, options, global_options, *args): from uliweb import orm if args: message = """This command will delete all data of [%s]-[%s] before loading, are you sure to load data""" % ( options.engine, ",".join(args), ) else: print "Failed! You should pass one or more tables name." sys.exit(1) ans = get_answer(message, answers="Yn", quit="q") path = os.path.join(options.dir, options.engine) if not os.path.exists(path): os.makedirs(path) engine = get_engine(options, global_options) tables = get_sorted_tables( get_tables( global_options.apps_dir, engine_name=options.engine, settings_file=global_options.settings, tables=args, local_settings_file=global_options.local_settings, ) ) _len = len(tables) for i, (name, t) in enumerate(tables): if global_options.verbose: print "[%s] Loading %s..." % (options.engine, show_table(name, t, i, _len)) try: orm.Begin() filename = os.path.join(path, name + ".txt") if options.text: format = "txt" else: format = None load_table( t, filename, engine, delimiter=options.delimiter, format=format, encoding=options.encoding, delete=ans == "Y", ) orm.Commit() except: log.exception("There are something wrong when loading table [%s]" % name) orm.Rollback()
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 handle(self, options, global_options, *args): from uliweb import orm if len(args) != 2: print self.print_help(self.prog_name, "loadtablefile") sys.exit(1) if args: message = ( """Do you want to delete all data of [%s]-[%s] before loading, if you choose N, the data will not be deleted""" % (options.engine, args[0]) ) else: print "Failed! You should pass one or more tables name." sys.exit(1) ans = get_answer(message, answers="Yn", quit="q") engine = get_engine(options, global_options) name = args[0] tables = get_tables( global_options.apps_dir, engine_name=options.engine, settings_file=global_options.settings, tables=[name], local_settings_file=global_options.local_settings, ) t = tables[name] if global_options.verbose: print "[%s] Loading %s..." % (options.engine, show_table(name, t, 0, 1)) try: orm.Begin() if options.text: format = "txt" else: format = None load_table( t, args[1], engine, delimiter=options.delimiter, format=format, encoding=options.encoding, delete=ans == "Y", ) orm.Commit() except: log.exception("There are something wrong when loading table [%s]" % name) orm.Rollback()
def handle(self, options, global_options, *args): from uliweb import orm if args: message = """This command will delete all data of [%s]-[%s] before loading, are you sure to load data""" % (options.engine, ','.join(args)) else: print "Failed! You should pass one or more tables name." sys.exit(1) ans = get_answer(message, answers='Yn', quit='q') path = os.path.join(options.dir, options.engine) if not os.path.exists(path): os.makedirs(path) engine = get_engine(options, global_options) tables = get_sorted_tables(get_tables(global_options.apps_dir, engine_name=options.engine, settings_file=global_options.settings, tables=args, local_settings_file=global_options.local_settings)) _len = len(tables) for i, (name, t) in enumerate(tables): if t.__mapping_only__: if global_options.verbose: msg = 'SKIPPED(Mapping Table)' print '[%s] Loading %s...%s' % (options.engine, show_table(name, t, i, _len), msg) continue if global_options.verbose: print '[%s] Loading %s...' % (options.engine, show_table(name, t, i, _len)), try: orm.Begin() filename = os.path.join(path, name+'.txt') if options.text: format = 'txt' else: format = None t = load_table(t, filename, engine, delimiter=options.delimiter, format=format, encoding=options.encoding, delete=ans=='Y', bulk=int(options.bulk), engine_name=engine.engine_name) orm.Commit() if global_options.verbose: print t except: log.exception("There are something wrong when loading table [%s]" % name) orm.Rollback()
def form_validate(self, data): from uliweb.utils.common import import_attr, log from uliweb.orm import Model errors = {} if data['basemodel']: try: m = functions.get_model(data['basemodel']) if not (isinstance(m, type) and issubclass(m, Model)): errors['basemodel'] = _("Object is not a subclass of Model") except Exception as e: log.exception(e) errors['basemodel'] = _("Model can't be imported") return errors
def handle(self, options, global_options, *args): from uliweb import orm if args: message = """This command will delete all data of [%s]-[%s] before loading, are you sure to load data""" % (options.engine, ','.join(args)) else: print "Failed! You should pass one or more tables name." sys.exit(1) ans = get_answer(message, answers='Yn', quit='q') path = os.path.join(options.dir, options.engine) if not os.path.exists(path): os.makedirs(path) engine = get_engine(options, global_options) tables = get_tables(global_options.apps_dir, engine=options.engine, settings_file=global_options.settings, tables=args, local_settings_file=global_options.local_settings) for name, t in tables.items(): if global_options.verbose: print '[%s] Loading %s...' % (options.engine, name) try: orm.Begin() filename = os.path.join(path, name + '.txt') if options.text: format = 'txt' else: format = None load_table(t, filename, engine, delimiter=options.delimiter, format=format, encoding=options.encoding, delete=ans == 'Y') orm.Commit() except: log.exception( "There are something wrong when loading table [%s]" % name) orm.Rollback()
def handle(self, options, global_options, *args): from uliweb import orm if len(args) != 2: print self.print_help(self.prog_name, 'loadtablefile') sys.exit(1) if args: message = """Do you want to delete all data of [%s]-[%s] before loading, if you choose N, the data will not be deleted""" % (options.engine, args[0]) else: print "Failed! You should pass one or more tables name." sys.exit(1) ans = 'Y' if global_options.yes else get_answer(message, quit='q') engine = get_engine(options, global_options) name = args[0] tables = get_tables(global_options.apps_dir, engine_name=options.engine, settings_file=global_options.settings, tables=[name], local_settings_file=global_options.local_settings) t = tables[name] if t.__mapping_only__: if global_options.verbose: msg = 'SKIPPED(Mapping Table)' print '[%s] Loading %s...%s' % (options.engine, show_table(name, t, i, _len), msg) return if global_options.verbose: print '[%s] Loading %s...' % (options.engine, show_table(name, t, 0, 1)), try: orm.Begin() if options.text: format = 'txt' else: format = None t = load_table(t, args[1], engine, delimiter=options.delimiter, format=format, encoding=options.encoding, delete=ans=='Y', bulk=int(options.bulk), engine_name=engine.engine_name) orm.Commit() if global_options.verbose: print t except: log.exception("There are something wrong when loading table [%s]" % name) orm.Rollback()
def get_app_dir(app): """ Get an app's directory """ path = __app_dirs__.get(app) if path is not None: return path else: p = app.split('.') try: path = pkg.resource_filename(p[0], '') except ImportError, e: log.exception(e) path = '' if len(p) > 1: path = os.path.join(path, *p[1:]) __app_dirs__[app] = path return path
def after_init_apps(sender): """ Check redis version """ from uliweb import settings from uliweb.utils.common import log check = settings.get_var('REDIS/check_version') if check: client = get_redis() try: info = client.info() except Exception as e: log.exception(e) log.error('Redis is not started!') return redis_version = info['redis_version'] version = tuple(map(int, redis_version.split('.'))) op = re_compare_op.search(check) if op: _op = op.group() _v = check[op.end() + 1:].strip() else: _op = '=' _v = check nv = tuple(map(int, _v.split('.'))) if _op == '=': flag = version[:len(nv)] == nv elif _op == '>=': flag = version >= nv elif _op == '>': flag = version > nv elif _op == '<=': flag = version <= nv elif _op == '<': flag = version < nv else: log.error("Can't support operator %s when check redis version" % _op) if not flag: log.error("Redis version %s is not matched what you want %s" % (redis_version, _v))
def xmlrpc(): import uliweb.contrib.xmlrpc as rpc import xmlrpclib from werkzeug import Response from uliweb.utils.common import log from uliweb import application as app, response p, m = xmlrpclib.loads(request.data) try: f = rpc.__xmlrpc_functions__.get(m) if f: mod, handler_cls, handler = app.prepare_request(request, f) result = app.call_view(mod, handler_cls, handler, request, response, _wrap_result, args=p) xml = xmlrpclib.dumps((result,), methodresponse=1) else: xml = xmlrpclib.dumps(xmlrpclib.Fault(-32400, 'system error: Cannot find or call %s' % m), methodresponse=1) log.debug('xmlrpc error: Cannot find or call %s' % m) except Exception, e: xml = xmlrpclib.dumps(xmlrpclib.Fault(-32400, 'system error: %s' % e), methodresponse=1) log.exception('xmlrpc error')
def get_app_dir(app): """ Get an app's directory """ path = __app_dirs__.get(app) if path is not None: return path else: p = app.split('.') try: path = pkg.resource_filename(p[0], '') except ImportError, e: log.error("Can't import app %s" % p[0]) log.exception(e) path = '' if len(p) > 1: path = os.path.join(path, *p[1:]) __app_dirs__[app] = path return path
def after_init_apps(sender): """ Check redis version """ from uliweb import settings from uliweb.utils.common import log check = settings.get_var('REDIS/check_version') if check: client = get_redis() try: info = client.info() except Exception as e: log.exception(e) log.error('Redis is not started!') return redis_version = info['redis_version'] version = tuple(map(int, redis_version.split('.'))) op = re_compare_op.search(check) if op: _op = op.group() _v = check[op.end()+1:].strip() else: _op = '=' _v = check nv = tuple(map(int, _v.split('.'))) if _op == '=': flag = version[:len(nv)] == nv elif _op == '>=': flag = version >= nv elif _op == '>': flag = version > nv elif _op == '<=': flag = version <= nv elif _op == '<': flag = version < nv else: log.error("Can't support operator %s when check redis version" % _op) if not flag: log.error("Redis version %s is not matched what you want %s" % (redis_version, _v))
def handle(self, options, global_options, *args): from uliweb import orm if args: message = """This command will delete all data of [%s]-[%s] before loading, are you sure to load data""" % (options.engine, ','.join(args)) else: message = """This command will delete whole database [%s] before loading, are you sure to load data""" % options.engine get_answer(message) path = os.path.join(options.dir, options.engine) if not os.path.exists(path): os.makedirs(path) engine = get_engine(options, global_options) tables = get_sorted_tables(get_tables(global_options.apps_dir, args, engine_name=options.engine, settings_file=global_options.settings, local_settings_file=global_options.local_settings)) _len = len(tables) for i, (name, t) in enumerate(tables): if global_options.verbose: print '[%s] Loading %s...' % (options.engine, show_table(name, t, i, _len)) try: orm.Begin() filename = os.path.join(path, name+'.txt') if options.text: format = 'txt' else: format = None load_table(t, filename, engine, delimiter=options.delimiter, format=format, encoding=options.encoding) orm.Commit() except: log.exception("There are something wrong when loading table [%s]" % name) orm.Rollback()
def handle(self, options, global_options, *args): from uliweb import orm if args: message = """This command will delete all data of [%s] before loading, are you sure to load data""" % ','.join(args) else: print "Failed! You should pass one or more tables name." sys.exit(1) get_answer(message) if not os.path.exists(options.dir): os.makedirs(options.dir) engine = get_engine(global_options.apps_dir) con = orm.get_connection(engine) for name in args: m = orm.get_model(name) if not m: print "Error! Can't find the table %s...Skipped!" % name continue t = m.table if global_options.verbose: print 'Loading %s...' % name try: con.begin() filename = os.path.join(options.dir, name+'.txt') if options.text: format = 'txt' else: format = None load_table(t, filename, con, delimiter=options.delimiter, format=format, encoding=options.encoding) con.commit() except: log.exception("There are something wrong when loading table [%s]" % name) con.rollback()
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)
def handle(self, options, global_options, *args): from uliweb import orm if len(args) != 2: print self.print_help(self.prog_name, 'loadtablefile') sys.exit(1) if args: message = """Do you want to delete all data of [%s] before loading, if you choose N, the data will not be deleted""" % args[0] else: print "Failed! You should pass one or more tables name." sys.exit(1) ans = get_answer(message, answers='Yn', quit='q') engine = get_engine(global_options.apps_dir) con = orm.get_connection(engine) name = args[0] m = orm.get_model(name) if not m: print "Error! Can't find the table %s...Skipped!" % name t = m.table if global_options.verbose: print 'Loading %s...' % name try: con.begin() if options.text: format = 'txt' else: format = None load_table(t, args[1], con, delimiter=options.delimiter, format=format, encoding=options.encoding, delete=ans=='Y') con.commit() except: log.exception("There are something wrong when loading table [%s]" % name) con.rollback()
def handle(self, options, global_options, *args): from uliweb import orm if args: message = """This command will delete all data of [%s]-[%s] before loading, are you sure to load data""" % (options.engine, ','.join(args)) else: print "Failed! You should pass one or more tables name." sys.exit(1) ans = get_answer(message, answers='Yn', quit='q') path = os.path.join(options.dir, options.engine) if not os.path.exists(path): os.makedirs(path) engine = get_engine(options, global_options) tables = get_tables(global_options.apps_dir, engine=options.engine, settings_file=global_options.settings, tables=args, local_settings_file=global_options.local_settings) for name, t in tables.items(): if global_options.verbose: print '[%s] Loading %s...' % (options.engine, name) try: orm.Begin() filename = os.path.join(path, name+'.txt') if options.text: format = 'txt' else: format = None load_table(t, filename, engine, delimiter=options.delimiter, format=format, encoding=options.encoding, delete=ans=='Y') orm.Commit() except: log.exception("There are something wrong when loading table [%s]" % name) orm.Rollback()
def execute(self, args, options, global_options): from uliweb.utils.common import check_apps_dir #add apps_dir to global_options and insert it to sys.path if global_options.apps_dir not in sys.path: sys.path.insert(0, global_options.apps_dir) if self.check_apps_dirs: check_apps_dir(global_options.apps_dir) if self.check_apps and args: #then args should be apps all_apps = self.get_apps(global_options) apps = args args = [] for p in apps: if p not in all_apps: print 'Error: Appname %s is not a valid app' % p sys.exit(1) else: args.append(p) try: self.handle(options, global_options, *args) except CommandError, e: log.exception(e) sys.exit(1)
def set_object(model, tablename, instance, fields=None): """ Only support simple condition, for example: Model.c.id == n """ from uliweb import settings from uliweb.utils.common import log if not check_enable(): return if not fields: fields = get_fields(tablename) if fields: redis = get_redis() if not redis: return v = instance.dump(fields) _id = get_id(model.get_engine_name(), tablename, instance.id) try: pipe = redis.pipeline() r = pipe.delete(_id).hmset(_id, v).expire(_id, settings.get_var('OBJCACHE/timeout')).execute() log.debug("objcache:set:id="+_id) except Exception, e: log.exception(e)
def socketio(path): try: socketio_manage(request.environ, {'/shell': ShellNamespace}, request) except: log.exception("Exception while handling socketio connection") return response
def handle(self, options, global_options, *args): from uliweb import orm from zipfile import ZipFile import shutil if args: message = """This command will delete all data of [%s]-[%s] before loading, are you sure to load data""" % (options.engine, ','.join(args)) else: message = """This command will delete whole database [%s] before loading, are you sure to load data""" % options.engine ans = 'Y' if global_options.yes else get_answer(message) if ans != 'Y': return # extract zip file to path if options.zipfile: if options.dir and not os.path.exists(options.dir): os.makedirs(options.dir) path = get_temppath(prefix='dump', dir=options.dir) if global_options.verbose: print "Extract path is %s" % path zipfile = None try: zipfile = ZipFile(options.zipfile, 'r') zipfile.extractall(path) except: log.exception( "There are something wrong when extract zip file [%s]" % options.zipfile) sys.exit(1) finally: if zipfile: zipfile.close() else: path = os.path.join(options.dir, options.engine) if not os.path.exists(path): os.makedirs(path) engine = get_engine(options, global_options) tables = get_sorted_tables( get_tables(global_options.apps_dir, args, engine_name=options.engine, settings_file=global_options.settings, local_settings_file=global_options.local_settings, all=options.all)) _len = len(tables) for i, (name, t) in enumerate(tables): if hasattr(t, '__mapping_only__') and t.__mapping_only__: if global_options.verbose: msg = 'SKIPPED(Mapping Table)' print '[%s] Loading %s...%s' % ( options.engine, show_table(name, t, i, _len), msg) continue if global_options.verbose: print '[%s] Loading %s...' % (options.engine, show_table(name, t, i, _len)), try: orm.Begin() filename = os.path.join(path, name + '.txt') if options.text: format = 'txt' else: format = None t = load_table(t, filename, engine, delimiter=options.delimiter, format=format, encoding=options.encoding, bulk=int(options.bulk), engine_name=engine.engine_name) orm.Commit() if global_options.verbose: print t except: log.exception( "There are something wrong when loading table [%s]" % name) orm.Rollback() if options.zipfile: shutil.rmtree(path)
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 handle(self, options, global_options, *args): from uliweb import orm from zipfile import ZipFile import shutil if args: message = """This command will delete all data of [%s]-[%s] before loading, are you sure to load data""" % (options.engine, ','.join(args)) else: print("Failed! You should pass one or more tables name.") sys.exit(1) ans = 'Y' if global_options.yes else get_answer(message, quit='q') # extract zip file to path if options.zipfile: if options.dir and not os.path.exists(options.dir): os.makedirs(options.dir) path = get_temppath(prefix='dump', dir=options.dir) if global_options.verbose: print("Extract path is %s" % path) zipfile = None try: zipfile = ZipFile(options.zipfile, 'r') zipfile.extractall(path) except: log.exception("There are something wrong when extract zip file [%s]" % options.zipfile) sys.exit(1) finally: if zipfile: zipfile.close() else: path = os.path.join(options.dir, options.engine) if not os.path.exists(path): os.makedirs(path) engine = get_engine(options, global_options) tables = get_sorted_tables(get_tables(global_options.apps_dir, engine_name=options.engine, settings_file=global_options.settings, tables=args, local_settings_file=global_options.local_settings)) _len = len(tables) def _f(table, filename, msg): session = orm.get_session() if session: session.close() orm.Begin() try: result = load_table(table, filename, engine, delimiter=options.delimiter, format=format, encoding=options.encoding, delete=ans=='Y', bulk=int(options.bulk), engine_name=engine.engine_name) if global_options.verbose: print(msg, result) orm.Commit() except: orm.Rollback() for i, (name, t) in enumerate(tables): if t.__mapping_only__: if global_options.verbose: msg = 'SKIPPED(Mapping Table)' print('[%s] Loading %s...%s' % (options.engine, show_table(name, t, i, _len), msg)) continue if global_options.verbose: msg = '[%s] Loading %s...' % (options.engine, show_table(name, t, i, _len)) else: msg = '' filename = os.path.join(path, name+'.txt') if options.text: format = 'txt' else: format = None #fork process to run if sys.platform != 'win32' and options.multi>1 and format != 'txt': load_table_file(t, filename, options.multi, bulk=options.bulk, engine=engine, delete=ans=='Y') else: _f(t, filename, msg) if options.zipfile: shutil.rmtree(path)
def install_views(self, views): for v in views: try: myimport(v) except Exception, e: log.exception(e)
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
def get_redis(): try: redis = functions.get_redis() except Exception, e: log.exception(e) redis = None