Esempio n. 1
0
def lock(keys, wait_time=0, time_out=DEFAULT_LOCKED_TIMED_OUT):
    """ lock certain resource with (MULTI)KEY """

    result = True
    end_time = time() + wait_time
    locked_keys = []

    if type(keys) in [list, tuple, set]:

        for key in keys:
            locked_keys.append(key)

            if not _lock(key, wait_time, time_out, end_time):
                # unlock all
                for locked_key in locked_keys:
                    _unlock(locked_key)
                result = False

    elif type(keys) in (str, int):
        result = _lock(str(keys), wait_time, time_out, end_time)

    else:
        raise_dev_exc(DEV_REDIS_LOCKED_ERR)

    return result
Esempio n. 2
0
    def __getattr__(self, name):
        if name not in self._services.keys():
            raise_dev_exc(DEV_THRIFT_ERR, thrift_name=name)

        if not self._services.get(name):
            self._services[name] = _load_service(name)

        return self._services[name]
Esempio n. 3
0
def unlock(keys):
    """ unlock certain resource with (MULTI)KEY """

    if type(keys) in [list, tuple, set]:
        for key in keys:
            _unlock(key)

    elif type(keys) in (str, int):
        _unlock(str(keys))

    else:
        raise_dev_exc(DEV_REDIS_LOCKED_ERR)
Esempio n. 4
0
def process_event(event_id, status, handler_id=None):
    if status is None:
        return False

    if status == CSEvent.STATUS_FORWARD and handler_id is None:
        raise_dev_exc(DEV_BAD_REQUEST_ERROR, arg='handler_id')

    event = inner.get_event(event_id)

    if not event:
        raise_user_exc(CS_EVENT_NOT_EXIST, event_id=event_id)

    if event.status == CSEvent.STATUS_DONE:
        raise_dev_exc(CS_EVENT_PROCESS_STATUS_INVALID, event_id=event_id,
                      status=event.status)

    event.update(status=status, handler_id=handler_id)
    return True
Esempio n. 5
0
def load_obj(path):
    """Load an object given its absolute object path, and return it.

    object can be a class, function, variable o instance.
    path ie: 'scrapy.contrib.downloadermiddelware.redirect.RedirectMiddleware'
    """

    try:
        dot = path.rindex('.')
    except ValueError:
        raise_dev_exc(DEV_MODULE_PATH_ERR, path=path)

    module, name = path[:dot], path[dot + 1:]
    try:
        mod = import_module(module)
    except ImportError as e:
        raise_dev_exc(DEV_MODULE_LOAD_ERR, path=path, exc=e)

    try:
        obj = getattr(mod, name)
    except AttributeError:
        raise_dev_exc(DEV_MODULE_ATTR_ERR, module=module, name=name)

    return obj
Esempio n. 6
0
def load_obj(path):
    """Load an object given its absolute object path, and return it.

    object can be a class, function, variable o instance.
    path ie: 'scrapy.contrib.downloadermiddelware.redirect.RedirectMiddleware'
    """

    try:
        dot = path.rindex('.')
    except ValueError:
        raise_dev_exc(DEV_MODULE_PATH_ERR, path=path)

    module, name = path[:dot], path[dot+1:]
    try:
        mod = import_module(module)
    except ImportError as e:
        raise_dev_exc(DEV_MODULE_LOAD_ERR, path=path, exc=e)

    try:
        obj = getattr(mod, name)
    except AttributeError:
        raise_dev_exc(DEV_MODULE_ATTR_ERR, module=module, name=name)

    return obj
Esempio n. 7
0
    def register(cls, app, route_base=None, subdomain=None, route_prefix=None,
                 trailing_slash=None):

        if cls is WalisView:
            raise_dev_exc(DEV_CLASS_EXTENDS_ERR, class_name='WalisApi')

        if route_base:
            cls.orig_route_base = cls.route_base
            cls.route_base = route_base

        if route_prefix:
            cls.orig_route_prefix = cls.route_prefix
            cls.route_prefix = route_prefix

        if not subdomain:
            if hasattr(app, "subdomain") and app.subdomain is not None:
                subdomain = app.subdomain
            elif hasattr(cls, "subdomain"):
                subdomain = cls.subdomain

        if trailing_slash is not None:
            cls.orig_trailing_slash = cls.trailing_slash
            cls.trailing_slash = trailing_slash

        members = get_interesting_members(WalisView, cls)

        for name, value in members:
            view_func = getattr(cls(), name)
            route_name = cls._build_endpoint(name)

            if hasattr(value, "api_cache") and name in value.api_cache:
                for idx, cached_rule in enumerate(value.api_cache[name]):
                    rule, options = cached_rule
                    rule = cls._build_rule(rule)
                    sub, ep, options = cls.parse_options(options)

                    if not subdomain and sub:
                        subdomain = sub

                    if ep:
                        endpoint = ep
                    elif len(value.api_cache[name]) == 1:
                        endpoint = route_name
                    else:
                        endpoint = "%s_%d" % (route_name, idx,)

                    trailing_slash = options.pop('trailing_slash', True)
                    if not trailing_slash:
                        rule = rule.rstrip("/")

                    view_func = cls._make_deco_func(view_func)
                    app.add_url_rule(rule, endpoint, view_func,
                                     subdomain=subdomain, **options)
            elif name in buildin_methods:
                view_func = api(rule=None)(view_func)
                view_func = cls._make_deco_func(view_func)

                if name in ["gets"]:
                    methods = ["GET"]
                else:
                    methods = [name.upper()]

                rule = cls._build_rule("/", value)
                if not cls.trailing_slash:
                    rule = rule.rstrip("/")

                app.add_url_rule(rule, route_name, view_func, methods=methods,
                                 subdomain=subdomain)
            else:
                # TODO
                raise_dev_exc(DEV_METHOD_VALID, msg=u'函数必须是<WalisApi>支持的内建函数或者由@api修饰')

        if hasattr(cls, "orig_route_base"):
            cls.route_base = cls.orig_route_base
            del cls.orig_route_base

        if hasattr(cls, "orig_route_prefix"):
            cls.route_prefix = cls.orig_route_prefix
            del cls.orig_route_prefix

        if hasattr(cls, "orig_trailing_slash"):
            cls.trailing_slash = cls.orig_trailing_slash
            del cls.orig_trailing_slash
Esempio n. 8
0
 def __init__(self, *args, **kwargs):
     super(RoutingSession, self).__init__()
     self.engines = kwargs.pop('engines', None)
     if self.engines is None:
         raise_dev_exc(DEV_ARGS_ERR, args="engines")
Esempio n. 9
0
 def __init__(self, *args, **kwargs):
     super(RoutingSession, self).__init__()
     self.engines = kwargs.pop('engines', None)
     if self.engines is None:
         raise_dev_exc(DEV_ARGS_ERR, args="engines")
Esempio n. 10
0
    def register(cls,
                 app,
                 route_base=None,
                 subdomain=None,
                 route_prefix=None,
                 trailing_slash=None):

        if cls is WalisView:
            raise_dev_exc(DEV_CLASS_EXTENDS_ERR, class_name='WalisApi')

        if route_base:
            cls.orig_route_base = cls.route_base
            cls.route_base = route_base

        if route_prefix:
            cls.orig_route_prefix = cls.route_prefix
            cls.route_prefix = route_prefix

        if not subdomain:
            if hasattr(app, "subdomain") and app.subdomain is not None:
                subdomain = app.subdomain
            elif hasattr(cls, "subdomain"):
                subdomain = cls.subdomain

        if trailing_slash is not None:
            cls.orig_trailing_slash = cls.trailing_slash
            cls.trailing_slash = trailing_slash

        members = get_interesting_members(WalisView, cls)

        for name, value in members:
            view_func = getattr(cls(), name)
            route_name = cls._build_endpoint(name)

            if hasattr(value, "api_cache") and name in value.api_cache:
                for idx, cached_rule in enumerate(value.api_cache[name]):
                    rule, options = cached_rule
                    rule = cls._build_rule(rule)
                    sub, ep, options = cls.parse_options(options)

                    if not subdomain and sub:
                        subdomain = sub

                    if ep:
                        endpoint = ep
                    elif len(value.api_cache[name]) == 1:
                        endpoint = route_name
                    else:
                        endpoint = "%s_%d" % (
                            route_name,
                            idx,
                        )

                    trailing_slash = options.pop('trailing_slash', True)
                    if not trailing_slash:
                        rule = rule.rstrip("/")

                    view_func = cls._make_deco_func(view_func)
                    app.add_url_rule(rule,
                                     endpoint,
                                     view_func,
                                     subdomain=subdomain,
                                     **options)
            elif name in buildin_methods:
                view_func = api(rule=None)(view_func)
                view_func = cls._make_deco_func(view_func)

                if name in ["gets"]:
                    methods = ["GET"]
                else:
                    methods = [name.upper()]

                rule = cls._build_rule("/", value)
                if not cls.trailing_slash:
                    rule = rule.rstrip("/")

                app.add_url_rule(rule,
                                 route_name,
                                 view_func,
                                 methods=methods,
                                 subdomain=subdomain)
            else:
                # TODO
                raise_dev_exc(DEV_METHOD_VALID,
                              msg=u'函数必须是<WalisApi>支持的内建函数或者由@api修饰')

        if hasattr(cls, "orig_route_base"):
            cls.route_base = cls.orig_route_base
            del cls.orig_route_base

        if hasattr(cls, "orig_route_prefix"):
            cls.route_prefix = cls.orig_route_prefix
            del cls.orig_route_prefix

        if hasattr(cls, "orig_trailing_slash"):
            cls.trailing_slash = cls.orig_trailing_slash
            del cls.orig_trailing_slash