Example #1
0
def function(fname, *args, **kwargs):
    func = settings.get_var('FUNCTIONS/'+fname)
    if func:
        if args or kwargs:
            return import_attr(func)(*args, **kwargs)
        else:
            return import_attr(func)
    else:
        raise UliwebError("Can't find the function [%s] in settings" % fname)
Example #2
0
def function(fname, *args, **kwargs):
    func = settings.get_var('FUNCTIONS/'+fname)
    if func:
        if args or kwargs:
            return import_attr(func)(*args, **kwargs)
        else:
            return import_attr(func)
    else:
        raise UliwebError("Can't find the function [%s] in settings" % fname)
Example #3
0
def has_role(user, *roles, **kwargs):
    """
    Judge is the user belongs to the role, and if does, then return the role object
    if not then return False. kwargs will be passed to role_func.
    """
    Role = get_model("role")
    if isinstance(user, (unicode, str)):
        User = get_model("user")
        user = User.get(User.c.username == user)

    for role in roles:
        if isinstance(role, (str, unicode)):
            role = Role.get(Role.c.name == role)
            if not role:
                continue
        name = role.name

        func = __role_funcs__.get(name, None)
        if func:
            if isinstance(func, (unicode, str)):
                func = import_attr(func)

            assert callable(func)

            para = kwargs.copy()
            para["user"] = user
            flag = call_func(func, para)
            if flag:
                return role
        flag = role.users.has(user)
        if flag:
            return role
    return False
Example #4
0
    def dispatch_event(self, event_type, **attrs):
        if isinstance(event_type, Event):
            e = event_type
        else:
            e = Event(event_type, self)

        if event_type in self._events.keys():
            for k, v in attrs.iteritems():
                setattr(e, k, v)
            listeners = self._events[e.type]
            for listener in listeners:
                _f = listener['func']
                if not _f:
                    try:
                        _f = import_attr(listener['func_name'])
                    except (ImportError, AttributeError) as e:
                        LOG.error("Can't import function %s" % listener['func_name'])
                        raise ImportError
                    listener['func'] = _f
                if callable(_f):
                    try:
                        _f(e)
                    except:
                        func = _f.__module__ + '.' + _f.__name__
                        LOG.exception('Calling dispatch event [%s] %s() error!' % (event_type, func))
                        raise
Example #5
0
def startup_installed(sender):
    from uliweb import settings
    import uliweb.form.uliform as form
    from uliweb.utils.common import import_attr

    for k, v in settings.get_var('FORM_FIELDS_MAP', {}).items():
        form.fields_mapping[k] = import_attr(v)
Example #6
0
def linci_artifact_types():
    for id,sort_num,type_class in settings.LINCI.artifact_type:
        if type_class:
            type_class = import_attr(type_class)
        else:
            type_class = None
        yield id,sort_num,type_class
Example #7
0
def _validate(menu, context, validators=None):
    #validate permission
    validators = validators or []

    check = menu.get('check')
    if check and not isinstance(check, (list, tuple)):
        check = [check]
    else:
        check = []

    validators = validators + check

    if validators:
        flag = False
        for v in validators:
            if not v: continue
            if isinstance(v, string_types):
                func = import_attr(v)
            else:
                func = v
            flag = func(menu, context)
            if flag:
                flag = True
                break
    else:
        flag = True

    return flag
Example #8
0
    def install_middlewares(self):
        #middleware process
        #middleware can be defined as
        #middleware_name = middleware_class_path[, order]
        #middleware_name = <empty> will be skip
        middlewares = []
        index = {}
        for middleware_name, v in settings.get('MIDDLEWARES', {}).iteritems():
            #process duplication of middleware, later will replace former
            if middleware_name in index:
                middlewares.pop(middleware_name)

            if not v:
                continue
            
            order = None
            if isinstance(v, (list, tuple)):
                if len(v) > 2:
                    raise UliwebError('Middleware %s difinition is not right' % middleware_name)
                middleware_path = v[0]
                if len(v) == 2:
                    order = v[1]
            else:
                middleware_path = v
            cls = import_attr(middleware_path)
            
            if order is None:
                order = getattr(cls, 'ORDER', 500)
            middlewares.append((order, cls))
            #remember the middleware index, so that can be used for easily remove
            index[middleware_name] = len(middlewares) - 1
        
        middlewares.sort(cmp=lambda x, y: cmp(x[0], y[0]))
            
        return [x[1] for x in middlewares]
Example #9
0
 def sort_middlewares(self, middlewares):
     #middleware process
     #middleware can be defined as
     #middleware_name = middleware_class_path[, order]
     #middleware_name = <empty> will be skip
     m = []
     for v in middlewares:
         if not v:
             continue
         
         order = None
         if isinstance(v, (list, tuple)):
             if len(v) > 2:
                 raise UliwebError('Middleware %r difinition is not right' % v)
             middleware_path = v[0]
             if len(v) == 2:
                 order = v[1]
         else:
             middleware_path = v
         cls = import_attr(middleware_path)
         
         if order is None:
             order = getattr(cls, 'ORDER', 500)
         m.append((order, cls))
     
     m.sort(cmp=lambda x, y: cmp(x[0], y[0]))
         
     return [x[1] for x in m]
Example #10
0
    def do(self, command):
        from uliweb import settings

        cmd = command['cmd']
        _id = command['id']
        last_process = self.shells.get(_id)
        if last_process and last_process.process and last_process.process.poll(
        ) is None:
            process = last_process.process
            self.log('Using last process %r' % process)
            process.stdin.write(cmd + '\n')
            process.stdin.flush()
        else:
            cmd_args = shlex.split(cmd)
            cmd_path = settings.COMMANDS.get(cmd_args[0])
            if cmd_path:
                cmd_cls = import_attr(cmd_path)
            else:
                cmd_cls = Command

            p = cmd_cls(cmd_args, command, self)
            self.shells[_id] = p
            #p.process may be None, so that it can do other thing more than commad line
            if p.process and p.process.poll() is None:
                p.output('cwd', self.safe_encode(p.cwd))
Example #11
0
def get(sender, topic, *args, **kwargs):
    """
    Invoke receiver functions according topic, it'll invoke receiver functions one by one,
    and if one receiver function return non-None value, it'll return it and break
    the loop.
    """
    if not topic in _receivers:
        return
    items = _receivers[topic]
    items.sort()
    for i in range(len(items)):
        nice, f = items[i]
        _f = f['func']
        if not _f:
            try:
                _f = import_attr(f['func_name'])
            except ImportError:
                logging.error("Can't import function %s" % f['func_name'])
                raise
            f['func'] = _f
        if callable(_f):
            if not _test(kwargs, f):
                continue
            try:
                v = _f(sender, *args, **kwargs)
            except:
                logging.exception('Calling dispatch point [%s] error!' % topic)
                raise
            if v is not None:
                return v
        else:
            raise "Dispatch point [%s] can't been invoked" % topic
Example #12
0
 def sort_middlewares(self, middlewares):
     #middleware process
     #middleware can be defined as
     #middleware_name = middleware_class_path[, order]
     #middleware_name = <empty> will be skip
     m = []
     for v in middlewares:
         if not v:
             continue
         
         order = None
         if isinstance(v, (list, tuple)):
             if len(v) > 2:
                 raise UliwebError('Middleware %r difinition is not right' % v)
             middleware_path = v[0]
             if len(v) == 2:
                 order = v[1]
         else:
             middleware_path = v
         cls = import_attr(middleware_path)
         
         if order is None:
             order = getattr(cls, 'ORDER', 500)
         m.append((order, cls))
     
     m.sort(cmp=lambda x, y: cmp(x[0], y[0]))
         
     return [x[1] for x in m]
Example #13
0
def call(sender, topic, *args, **kwargs):
    """
    Invoke receiver functions according topic, it'll invoke receiver functions one by one,
    and it'll not return anything, so if you want to return a value, you should
    use get function.
    """
    if not topic in _receivers:
        return
    items = _receivers[topic]
    def _cmp(x, y):
        return cmp(x[0], y[0])
    
    items.sort(_cmp)
    for i in range(len(items)):
        nice, f = items[i]
        _f = f['func']
        if not _f:
            try:
                _f = import_attr(f['func_name'])
            except (ImportError, AttributeError) as e:
                logging.error("Can't import function %s" % f['func_name'])
                raise
            f['func'] = _f
        if callable(_f):
            kw = kwargs.copy()
            if not _test(kw, f):
                continue
            try:
                _f(sender, *args, **kw)
            except:
                func = _f.__module__ + '.' + _f.__name__
                logging.exception('Calling dispatch point [%s] %s(%r, %r) error!' % (topic, func, args, kw))
                raise
        else:
            raise Exception, "Dispatch point [%s] %r can't been invoked" % (topic, _f)
Example #14
0
def _validate(menu, context, validators=None):
    from uliweb.utils.common import import_attr
    
    #validate permission
    validators = validators or []
    
    check = menu.get('check')
    if check and not isinstance(check, (list, tuple)):
        check = [check]
    else:
        check = []
    
    validators = validators + check
    
    if validators:
        flag = False
        for v in validators:
            if not v: continue
            if isinstance(v, (str, unicode)):
                func = import_attr(v)
            else:
                func = v
            flag = func(menu, context)
            if flag:
                flag = True
                break
    else:
        flag = True
        
    return flag
Example #15
0
def authenticate(username, password, auth_type=None):
    from uliweb import settings

    auth_type = auth_type or settings.AUTH.AUTH_DEFAULT_TYPE

    err_msg = ''
    if not isinstance(auth_type, (list, tuple)):
        auth_type = [auth_type]

    for t in auth_type:
        if t in settings.AUTH_CONFIG:
            func_path = settings.AUTH_CONFIG[t].get('authenticate')
            if func_path:
                func = import_attr(func_path)
                f, d = func(username, password)
                if f:
                    log.info("login successfully, auth_type: %s"%(t))
                    return f, d
                else:
                    log.error("fail to login, auth_type: %s, err: %s"%(t,d))
                    err_msg = d
        else:
            log.error("auth_type %s not in config"%(t))

    return False, {'username':err_msg}
Example #16
0
 def install_template_processors(self):
     for v in settings.TEMPLATE_PROCESSORS.values():
         for ext in v.get('file_exts', []):
             self.template_processors[ext] = {
                 'func': import_attr(v['processor']),
                 'args': v.get('args', {})
             }
Example #17
0
def has_role(user, *roles, **kwargs):
    """
    Judge is the user belongs to the role, and if does, then return the role object
    if not then return False. kwargs will be passed to role_func.
    """
    Role = get_model('role')
    if isinstance(user, (unicode, str)):
        User = get_model('user')
        user = User.get(User.c.username == user)

    for role in roles:
        if isinstance(role, (str, unicode)):
            role = Role.get(Role.c.name == role)
            if not role:
                return False
        name = role.name

        func = __role_funcs__.get(name, None)
        if func:
            if isinstance(func, (unicode, str)):
                func = import_attr(func)

            assert callable(func)

            para = kwargs.copy()
            para['user'] = user
            flag = call_func(func, para)
            if flag:
                return role
        flag = role.users.has(user)
        if flag:
            return role
    return False
Example #18
0
def authenticate(username, password, auth_type=None):
    from uliweb import settings

    auth_type = auth_type or settings.AUTH.AUTH_DEFAULT_TYPE

    err_msg = ''
    if not isinstance(auth_type, (list, tuple)):
        auth_type = [auth_type]

    for t in auth_type:
        if t in settings.AUTH_CONFIG:
            func_path = settings.AUTH_CONFIG[t].get('authenticate')
            if func_path:
                func = import_attr(func_path)
                f, d = func(username, password)
                if f:
                    log.info("login successfully, auth_type: %s" % (t))
                    return f, d
                else:
                    log.error("fail to login, auth_type: %s, err: %s" % (t, d))
                    err_msg = d
        else:
            log.error("auth_type %s not in config" % (t))

    return False, {'username': err_msg}
Example #19
0
def get_cache(**kwargs):
    from uliweb import settings
    from weto.cache import Cache
    from uliweb.utils.common import import_attr, application_path

    serial_cls_path = settings.get_var('CACHE/serial_cls')
    if serial_cls_path:
        serial_cls = import_attr(serial_cls_path)
        if settings.GLOBAL.PICKLE_PROTOCAL_LEVEL is not None:
            serial_cls.protocal_level = settings.GLOBAL.PICKLE_PROTOCAL_LEVEL
    else:
        serial_cls = None

    options = dict(settings.get_var('CACHE_STORAGE', {}))
    options['data_dir'] = application_path(options['data_dir'])
    args = {
        'storage_type': settings.get_var('CACHE/type'),
        'options': options,
        'expiry_time': settings.get_var('CACHE/expiretime'),
        'serial_cls': serial_cls
    }

    args.update(kwargs)
    cache = Cache(**args)
    return cache
Example #20
0
def get_session(key=None):
    options = dict(settings.get('SESSION_STORAGE', {}))
    options['data_dir'] = application_path(options['data_dir'])
    if 'url' not in options:
        _url = (settings.get_var('ORM/CONNECTION', '') or settings.get_var(
            'ORM/CONNECTIONS', {}).get('default', {}).get('CONNECTION', ''))
        if _url:
            options['url'] = _url

    #process Session options
    session_storage_type = settings.SESSION.type
    Session.force = settings.SESSION.force

    serial_cls_path = settings.SESSION.serial_cls
    if serial_cls_path:
        serial_cls = import_attr(serial_cls_path)
    else:
        serial_cls = None

    session = Session(key,
                      storage_type=session_storage_type,
                      options=options,
                      expiry_time=settings.SESSION.timeout,
                      serial_cls=serial_cls)
    return session
Example #21
0
def authenticate(username, password, auth_type=None):
    from uliweb import settings, request

    auth_type = auth_type or settings.AUTH.AUTH_DEFAULT_TYPE

    errors = {}
    if not isinstance(auth_type, (list, tuple)):
        auth_type = [auth_type]

    ip = request.environ['REMOTE_ADDR']

    for t in auth_type:
        if t in settings.AUTH_CONFIG:
            func_path = settings.AUTH_CONFIG[t].get('authenticate')
            if func_path:
                func = import_attr(func_path)
                f, d = func(username, password)
                if f:
                    if hasattr(d,"locked") and d.locked:
                        log.error("'%s' fail to login, err: user '%s' is locked"%(ip, d))
                        return False, {'username': _('"{}" is locked!').format(username)}
                    if hasattr(d,"deleted") and d.deleted:
                        log.error("'%s' fail to login, err: user '%s' is deleted"%(ip, d))
                        return False, {'username': _('"{}" is deleted!').format(username)}
                    log.info("'%s' login successfully as user '%s', auth_type: %s"%(ip, username, t))
                    return f, d
                else:
                    log.error("'%s' fail to login, auth_type: %s, err: %s"%(ip, t, d))
                    errors = d
        else:
            log.error("auth_type %s not in config"%(t))

    return False, errors
Example #22
0
def startup_installed(sender):
    from uliweb import settings
    import uliweb.form.uliform as form
    from uliweb.utils.common import import_attr

    for k, v in settings.get_var('FORM_FIELDS_MAP', {}).items():
        form.fields_mapping[k] = import_attr(v)
Example #23
0
def call(sender, topic, *args, **kwargs):
    """
    Invoke receiver functions according topic, it'll invoke receiver functions one by one,
    and it'll not return anything, so if you want to return a value, you should
    use get function.
    """
    if not topic in _receivers:
        return
    items = _receivers[topic]
    def _cmp(x, y):
        return cmp(x[0], y[0])
    
    items.sort(_cmp)
    for i in range(len(items)):
        nice, f = items[i]
        _f = f['func']
        if not _f:
            try:
                _f = import_attr(f['func_name'])
            except (ImportError, AttributeError) as e:
                logging.error("Can't import function %s" % f['func_name'])
                raise
            f['func'] = _f
        if callable(_f):
            kw = kwargs.copy()
            if not _test(kw, f):
                continue
            try:
                _f(sender, *args, **kw)
            except:
                func = _f.__module__ + '.' + _f.__name__
                logging.exception('Calling dispatch point [%s] %s(%r, %r) error!' % (topic, func, args, kw))
                raise
        else:
            raise Exception, "Dispatch point [%s] %r can't been invoked" % (topic, _f)
Example #24
0
    def __init__(self,
                 default_filename_converter_cls=UUIDFilenameConverter,
                 config=None):
        self.config = config or self.default_config
        for k, v in self.options.items():
            item, default = v
            #if there is no '/' in option, then combine config with option
            #else assume the option is just like 'SECTION/OPTION', then skip it
            if '/' not in item:
                item = self.config + '/' + item
            value = settings.get_var(item, default)
            setattr(self, k, value)

        if self.x_sendfile and not self.x_header_name:
            if self.x_sendfile == 'nginx':
                self.x_header_name = 'X-Accel-Redirect'
            elif self.x_sendfile == 'apache':
                self.x_header_name = 'X-Sendfile'
            else:
                raise Exception(
                    "X_HEADER can't be None, or X_SENDFILE is not supprted")
        if isinstance(self._filename_converter, (str, unicode)):
            self._filename_converter_cls = import_attr(
                self._filename_converter)
        else:
            self._filename_converter_cls = self._filename_converter or default_filename_converter_cls
Example #25
0
 def run(self):
     """
     直接运行当前的命令
     """
     f = import_attr(self.function)
     ret = f(**self.parameters)
     return ret
Example #26
0
def after_init_apps(sender):
    from uliweb import orm
    from uliweb.utils.common import import_attr
    from uliweb.core.SimpleFrame import __app_alias__
    
    orm.set_debug_query(settings.get_var('ORM/DEBUG_LOG'))
    orm.set_auto_create(settings.get_var('ORM/AUTO_CREATE'))
    orm.set_pk_type(settings.get_var('ORM/PK_TYPE'))
    orm.set_auto_set_model(False)
    orm.set_lazy_model_init(True)
    orm.set_check_max_length(settings.get_var('ORM/CHECK_MAX_LENGTH'))
    orm.set_nullable(settings.get_var('ORM/NULLABLE'))
    orm.set_server_default(settings.get_var('ORM/SERVER_DEFAULT'))
    orm.set_manytomany_index_reverse(settings.get_var('ORM/MANYTOMANY_INDEX_REVERSE'))
    convert_path = settings.get_var('ORM/TABLENAME_CONVERTER')
    convert = import_attr(convert_path) if convert_path else None
    orm.set_tablename_converter(convert)
    
    #judge if transaction middle has not install then set
    #AUTO_DOTRANSACTION is False
    if 'transaction' in settings.MIDDLEWARES:
        orm.set_auto_dotransaction(False)
    else:
        orm.set_auto_dotransaction(settings.get_var('ORM/AUTO_DOTRANSACTION'))
    
    d = {'connection_string':settings.get_var('ORM/CONNECTION'),
        'connection_type':settings.get_var('ORM/CONNECTION_TYPE'),
        'debug_log':settings.get_var('ORM/DEBUG_LOG'),
        'connection_args':settings.get_var('ORM/CONNECTION_ARGS'),
        'strategy':settings.get_var('ORM/STRATEGY'),
        }
    orm.engine_manager.add('default', d)
    
    for name, d in settings.get_var('ORM/CONNECTIONS').items():
        x = {'connection_string':d.get('CONNECTION', ''),
            'debug_log':d.get('DEBUG_LOG', None),
            'connection_args':d.get('CONNECTION_ARGS', {}),
            'strategy':d.get('STRATEGY', 'threadlocal'),
            'connection_type':d.get('CONNECTION_TYPE', 'long')
        }
        orm.engine_manager.add(name, x)

    if 'MODELS' in settings:
        for name, model_path in settings.MODELS.items():
            if not model_path: continue
            if isinstance(model_path, (str, unicode)):
                path = model_path
                engine_name = 'default'
            else:
                path, engine_name = model_path
            for k, v in __app_alias__.iteritems():
                if path.startswith(k):
                    path = v + path[len(k):]
                    break
            orm.set_model(path, name, engine_name=engine_name)

    if 'MODELS_CONFIG' in settings:
        for name, v in settings.MODELS_CONFIG.items():
            orm.set_model_config(name, v)
Example #27
0
 def __getattr__(self, name):
     if name in self.__objects:
         return self.__objects[name]
     if name not in settings[self.__section]:
         raise UliwebError("Object %s is not existed!" % name)
     obj = import_attr(settings[self.__section].get(name))
     self.__objects[name] = obj
     return obj
Example #28
0
 def __getattr__(self, name):
     if name in self.__objects:
         return self.__objects[name]
     if name not in settings[self.__section]:
         raise UliwebError("Object %s is not existed!" % name)
     obj = import_attr(settings[self.__section].get(name))
     self.__objects[name] = obj
     return obj
Example #29
0
 def __getattr__(self, name):
     if name in self.__decorators__:
         return self.__decorators__[name]
     if name not in conf.settings.DECORATORS:
         raise Exception, "decorator %s is not existed!" % name
     func = import_attr(conf.settings.DECORATORS.get(name))
     self.__decorators__[name] = func
     return func
Example #30
0
 def __getattr__(self, name):
     if name in self.__functions__:
         return self.__functions__[name]
     if name not in conf.settings.FUNCTIONS:
         raise Exception, "function %s is not existed!" % name
     func = import_attr(conf.settings.FUNCTIONS.get(name))
     self.__functions__[name] = func
     return func
Example #31
0
 def install_global_objects(self):
     """
     Process [GLOBAL_OBJECTS], and inject all object to uliweb module, so
     user can import from uliweb
     """
     import uliweb
     for k, v in settings.GLOBAL_OBJECTS.items():
         setattr(uliweb, k, import_attr(v))
Example #32
0
 def __getattr__(self, name):
     if name in self.__functions__:
         return self.__functions__[name]
     if name not in settings.FUNCTIONS:
         raise UliwebError("function %s is not existed!" % name)
     func = import_attr(settings.FUNCTIONS.get(name))
     self.__functions__[name] = func
     return func
Example #33
0
 def install_global_objects(self):
     """
     Process [GLOBAL_OBJECTS], and inject all object to uliweb module, so
     user can import from uliweb
     """
     import uliweb
     for k, v in settings.GLOBAL_OBJECTS.items():
         setattr(uliweb, k, import_attr(v))
Example #34
0
def print_menu_html(name, type='side'):
    from uliweb import settings

    if type == 'side':
        _menu = settings.MENUS_CONFIG.menu_render or default_menu
    elif type == 'main':
        _menu = settings.MENUS_CONFIG.menu_render or default_navigation
    print(import_attr(_menu)(name=name))
Example #35
0
 def __getattr__(self, name):
     if name in self.__decorators__:
         return self.__decorators__[name]
     if name not in settings.DECORATORS:
         raise UliwebError("decorator %s is not existed!" % name)
     func = import_attr(settings.DECORATORS.get(name))
     self.__decorators__[name] = func
     return func
Example #36
0
    def install_template_loader(self, dirs):
        Loader = import_attr(settings.get_var('TEMPLATE_PROCESSOR/loader'))
        args = settings.get_var('TEMPLATE')

        if self.debug:
            args['check_modified_time'] = True
            args['log'] = log
            args['debug'] = settings.get_var('GLOBAL/DEBUG_TEMPLATE', False)
        return Loader(dirs, **args)
Example #37
0
 def create_client(sns_name):
     from uliweb import settings
     from uliweb.utils.common import import_attr
     servers = settings.get_var("Parrot/ServerList")
     server = servers[sns_name]
     return import_attr(server['info'][1])(
         server['client_id'], 
         server['client_secret'],
         server['redirect_uri'])
Example #38
0
    def install_template_loader(self, dirs):
        Loader = import_attr(settings.get_var('TEMPLATE_PROCESSOR/loader'))
        args = settings.get_var('TEMPLATE')

        if self.debug:
            args['check_modified_time'] = True
            args['log'] = log
            args['debug'] = settings.get_var('GLOBAL/DEBUG_TEMPLATE', False)
        return Loader(dirs, **args)
Example #39
0
 def soap(self):
     from pysimplesoap.server import SoapDispatcher
     import uliweb.contrib.soap as soap
     from uliweb.utils.common import import_attr
     from uliweb import application as app, response, url_for
     from functools import partial
     
     global __soap_dispatcher__
     
     if not __soap_dispatcher__:
         location = "%s://%s%s" % (
             request.environ['wsgi.url_scheme'],
             request.environ['HTTP_HOST'],
             request.path)
         namespace = functions.get_var(self.config).get('namespace') or location
         documentation = functions.get_var(self.config).get('documentation')
         dispatcher = SoapDispatcher(
             name = functions.get_var(self.config).get('name'),
             location = location,
             action = '', # SOAPAction
             namespace = namespace,
             prefix=functions.get_var(self.config).get('prefix'),
             documentation = documentation,
             exception_handler = partial(exception_handler, response=response),
             ns = True)
         for name, (func, returns, args, doc) in soap.__soap_functions__.get(self.config, {}).items():
             if isinstance(func, (str, unicode)):
                 func = import_attr(func)
             dispatcher.register_function(name, func, returns, args, doc)
     else:
         dispatcher = __soap_dispatcher__
         
     if 'wsdl' in request.GET:
         # Return Web Service Description
         response.headers['Content-Type'] = 'text/xml'
         response.write(dispatcher.wsdl())
         return response
     elif request.method == 'POST':
         def _call(func, args):
             rule = SimpleRule()
             rule.endpoint = func
             mod, handler_cls, handler = app.prepare_request(request, rule)
             result = app.call_view(mod, handler_cls, handler, request, response, _wrap_result, kwargs=args)
             r = _fix_soap_datatype(result)
             return r
         # Process normal Soap Operation
         response.headers['Content-Type'] = 'text/xml'
         log.debug("---request message---")
         log.debug(request.data)
         result = dispatcher.dispatch(request.data, call_function=_call)
         log.debug("---response message---")
         log.debug(result)
         response.write(result)
         return response
Example #40
0
def menu(name, active='', check=None, id=None, _class=None):
    from uliweb import settings

    if check and not isinstance(check, (list, tuple)):
        check = [check]
    else:
        check = []
    validators = (settings.MENUS_CONFIG.validators or []) + list(check)
    
    _menu = settings.MENUS_CONFIG.menu_render or default_menu
    return import_attr(_menu)(name=name, active=active, validators=validators, id=id, _class=_class)
Example #41
0
def menu(name, active='', check=None, id=None, _class=None):
    from uliweb import settings

    if check and not isinstance(check, (list, tuple)):
        check = [check]
    else:
        check = []
    validators = (settings.MENUS_CONFIG.validators or []) + list(check)
    
    _menu = settings.MENUS_CONFIG.menu_render or default_menu
    return import_attr(_menu)(name=name, active=active, validators=validators, id=id, _class=_class)
Example #42
0
 def __init__(self, host=None, port=None, user=None, password=None, backend=None):
     from uliweb import settings
     from uliweb.utils.common import import_attr
     
     self.host = host or (settings and settings.get_var('MAIL/HOST'))
     self.port = port or (settings and settings.get_var('MAIL/PORT', 25))
     self.user = user or (settings and settings.get_var('MAIL/USER'))
     self.password = password or (settings and settings.get_var('MAIL/PASSWORD'))
     self.backend = backend or (settings and settings.get_var('MAIL/BACKEND', 'uliweb.mail.backends.smtp'))
     cls = import_attr(self.backend + '.MailConnection')
     self.con = cls(self)
Example #43
0
 def __init__(self, host=None, port=None, user=None, password=None, backend=None, sendmail_location=None):
     from uliweb import settings
     from uliweb.utils.common import import_attr
     
     self.host = host or settings.get_var('MAIL/HOST')
     self.port = port or settings.get_var('MAIL/PORT', 25)
     self.user = user or settings.get_var('MAIL/USER')
     self.password = password or settings.get_var('MAIL/PASSWORD')
     self.backend = backend or settings.get_var('MAIL/BACKEND', 'uliweb.mail.backends.smtp')
     self.sendmail_location = sendmail_location or settings.get_var('MAIL/SENDMAIL_LOCATION', '/usr/sbin/sendmail')
     cls = import_attr(self.backend + '.MailConnection')
     self.con = cls(self)
Example #44
0
 def process_request(self, request):
     key = request.cookies.get(SessionCookie.default_cookie_id)
     if not key:
         key = request.values.get(SessionCookie.default_cookie_id)
     serial_cls_path = settings.SESSION.serial_cls
     if serial_cls_path:
         serial_cls = import_attr(serial_cls_path)
     else:
         serial_cls = None
     session = Session(key, storage_type=self.session_storage_type, 
         options=self.options, expiry_time=self.timeout, serial_cls=serial_cls)
     request.session = session
Example #45
0
        def pre_save(data):
            from uliweb.utils.common import get_uuid, import_attr
            from uliweb.contrib.model_config import get_model_fields, get_model_indexes

            data['uuid'] = get_uuid()[:6]
            if not data['table_name']:
                data['table_name'] = data['model_name'].lower()

            if not data['display_name']:
                data['display_name'] = data['model_name']

            #add import basemodel support
            if data['basemodel']:
                BM = import_attr(data['basemodel'])
                data['fields'] = get_model_fields(BM)
                data['indexes'] = get_model_indexes(BM)

            if data['extension_model']:
                EM = import_attr(data['extension_model'])
                data['extension_fields'] = get_model_fields(EM)
                data['extension_indexes'] = get_model_indexes(EM)
Example #46
0
 def process_request(self, request):
     key = request.cookies.get(SessionCookie.default_cookie_id)
     if not key:
         key = request.values.get(SessionCookie.default_cookie_id)
     serial_cls_path = settings.SESSION.serial_cls
     if serial_cls_path:
         serial_cls = import_attr(serial_cls_path)
     else:
         serial_cls = None
     session = Session(key, storage_type=self.session_storage_type, 
         options=self.options, expiry_time=self.timeout, serial_cls=serial_cls)
     request.session = session
Example #47
0
def get_backend():
    global default_fileserving

    if default_fileserving:
        return default_fileserving
    else:
        cls = settings.get_var('UPLOAD/BACKEND')
        if cls:
            default_fileserving = import_attr(cls)()
        else:
            default_fileserving = FileServing()
        return default_fileserving
Example #48
0
def get_backend():
    global default_fileserving
    
    if default_fileserving:
        return default_fileserving
    else:
        cls = settings.get_var('UPLOAD/BACKEND')
        if cls:
            default_fileserving = import_attr(cls)()
        else:
            default_fileserving = FileServing()
        return default_fileserving
Example #49
0
def after_init_apps(sender):
    from uliweb import orm
    from uliweb.utils.common import import_attr
    from uliweb.core.SimpleFrame import __app_alias__

    orm.set_debug_query(settings.get_var('ORM/DEBUG_LOG'))
    orm.set_auto_create(settings.get_var('ORM/AUTO_CREATE'))
    orm.set_pk_type(settings.get_var('ORM/PK_TYPE'))
    orm.set_auto_set_model(False)
    orm.set_check_max_length(settings.get_var('ORM/CHECK_MAX_LENGTH'))
    convert_path = settings.get_var('ORM/TABLENAME_CONVERTER')
    convert = import_attr(convert_path) if convert_path else None
    orm.set_tablename_converter(convert)

    #judge if transaction middle has not install then set
    #AUTO_DOTRANSACTION is False
    if 'transaction' in settings.MIDDLEWARES:
        orm.set_auto_dotransaction(False)
    else:
        orm.set_auto_dotransaction(settings.get_var('ORM/AUTO_DOTRANSACTION'))

    d = {
        'connection_string': settings.get_var('ORM/CONNECTION'),
        'connection_type': settings.get_var('ORM/CONNECTION_TYPE'),
        'debug_log': settings.get_var('ORM/DEBUG_LOG'),
        'connection_args': settings.get_var('ORM/CONNECTION_ARGS'),
        'strategy': settings.get_var('ORM/STRATEGY'),
    }
    orm.engine_manager.add('default', d)

    for name, d in settings.get_var('ORM/CONNECTIONS').items():
        x = {
            'connection_string': d.get('CONNECTION', ''),
            'debug_log': d.get('DEBUG_LOG', None),
            'connection_args': d.get('CONNECTION_ARGS', {}),
            'strategy': d.get('STRATEGY', 'threadlocal'),
            'connection_type': d.get('CONNECTION_TYPE', 'long')
        }
        orm.engine_manager.add(name, x)

    if 'MODELS' in settings:
        for name, model_path in settings.MODELS.items():
            if isinstance(model_path, (str, unicode)):
                path = model_path
                engine_name = 'default'
            else:
                path, engine_name = model_path
            for k, v in __app_alias__.iteritems():
                if path.startswith(k):
                    path = v + path[len(k):]
                    break
            orm.set_model(path, name, engine_name=engine_name)
Example #50
0
def after_init_apps(sender):
    global __schemas__, __default_limit__

    if 'JSONQL_SCHEMA' in settings:
        for name, model_path in settings.JSONQL_SCHEMA.items():
            if not model_path: continue
            if isinstance(model_path, (str, unicode)):
                path = model_path
            else:
                raise Exception("Schema path should be a string but %r found" % model_path)

            __schemas__[name] = import_attr(model_path)
    __default_limit__ = settings.JSONQL.get('limit', 10)
Example #51
0
    def init(self):
        global __static_combine__, __static_mapping__
        from . import init_static_combine
        from uliweb import settings
        from uliweb.utils.common import import_attr

        if __static_combine__ is None:
            func = settings.get_var(
                'STATIC_COMBINE_CONFIG/init_static_combine',
                init_static_combine)
            __static_combine__ = import_attr(func)()
            for k, v in __static_combine__.items():
                for x in v:
                    __static_mapping__[x] = k
Example #52
0
def get_cipher(key=None):
    """
    Get cipher object, and then you can invoke:
        des = get_cipher()
        d = des.encrpy('Hello')
        print des.descrpy(d)
    """
    des_func = import_attr(settings.SECRETKEY.CIPHER_CLS)
    kwargs = settings.SECRETKEY.CIPHER_ARGS

    if not key:
        key = functions.get_cipher_key()
    cipher = des_func(key, **kwargs)
    return cipher
Example #53
0
    def run(self):
        import traceback

        Task = functions.get_model('async_Tasks')

        try:
            self.before()

            if not self.func:
                self.ret = 'empty'
                self.log.debug("Async task {} function is empty".format(self.task_id))
            else:
                f = import_attr(self.func)
                self.log.debug("Run Async task {} function {!r}({!r})".format(
                    self.task_id, f, self.parameters))
                #_args表示位置参数
                args = self.parameters.pop('_args', ())
                self.ret = f(*args, **self.parameters)
                self.log.debug("Execute {!r} with {!r} and the result is {!r}".format(
                    f, self.parameters, self.ret))

            self.on_finish()

            self.after()

            self.save()

        except Exception as e:
            type, value, tb = sys.exc_info()
            txt =  ''.join(traceback.format_exception(type, value, tb))
            if self.task.retry_times >= self.task.max_retry_times:
                status = FAILED
                finished_time = date.now()
                startup_time = self.task.startup_time
                msg =  'Async task {} reaches max retry times, status changes to FAILED'.format(self.task.task_id)
                self.log.info(msg)
            else:
                status = ERROR
                startup_time = (date.now() +
                    datetime.timedelta(milliseconds=self.task.retry_time_interval*(self.task.retry_times-1)))
                finished_time = None
                msg =  'Async task {} runs on error, it will startup at {}'.format(self.task.task_id, startup_time)
                self.log.info(msg)

            self.save(execution_info=txt,
                      finished_time=finished_time,
                      startup_time=startup_time,
                      status=status,
                      message=msg)
            self.log.exception(e)
Example #54
0
def get_backend(config=None):
    global default_fileserving

    if default_fileserving and not config:
        return default_fileserving
    else:
        config = config or 'UPLOAD'
        cls = settings.get_var('%s/BACKEND' % config)
        if cls:
            fileserving = import_attr(cls)(config=config)
        else:
            fileserving = FileServing(config=config)
        if config == 'UPLOAD':
            default_fileserving = fileserving
        return fileserving
Example #55
0
def set_object(model, instance, fields=None, engine_name=None):
    """
    Only support simple condition, for example: Model.c.id == n
    if not id provided, then use instance.id
    """
    from uliweb import settings

    if not check_enable():
        return

    redis = get_redis()
    if not redis: return

    tablename = model._alias or model.tablename
    exclude = []
    if not fields:
        fields, exclude = get_fields(tablename)

    v = instance.dump(fields, exclude=exclude)
    info = settings.get_var('OBJCACHE_TABLES/%s' % tablename, {})

    if info is None:
        return

    expire = settings.get_var('OBJCACHE/timeout', 0)
    key = 'id'
    if info and isinstance(info, dict):
        expire = info.get('expire', expire)
        key = info.get('key', key)

    if '.' in key or key not in model.properties:
        _key = import_attr(key)(instance)
    else:
        _key = getattr(instance, key)
    _id = get_id(engine_name or model.get_engine_name(), tablename, _key)
    try:
        pipe = redis.pipeline()
        p = pipe.delete(_id).hmset(_id, v)
        expire_msg = ''
        if expire:
            p = p.expire(_id, expire)
            expire_msg = ':expire=%d' % expire
        r = p.execute()
        log.debug("Saving to cache objcache:set:table=%s:id=[%s]:%s" %
                  (tablename, _id, expire_msg))
    except Exception, e:
        log.exception(e)
Example #56
0
    def __init__(self,
                 host=None,
                 port=None,
                 user=None,
                 password=None,
                 backend=None):
        from uliweb import settings
        from uliweb.utils.common import import_attr

        self.host = host or (settings and settings.get_var('MAIL/HOST'))
        self.port = port or (settings and settings.get_var('MAIL/PORT', 25))
        self.user = user or (settings and settings.get_var('MAIL/USER'))
        self.password = password or (settings
                                     and settings.get_var('MAIL/PASSWORD'))
        self.backend = backend or (settings and settings.get_var(
            'MAIL/BACKEND', 'uliweb.mail.backends.smtp'))
        cls = import_attr(self.backend + '.MailConnection')
        self.con = cls(self)