Пример #1
0
    def _declare_command (self,
        name, callback_function, doc, overwrite, is_pre_flight):

        if (not utils.is_string(name)):
            raise ValueError(
                "Invalid value for magic command name: "
                "Must be a string")

        if (not utils.is_callable(callback_function)):
            raise ValueError(
                "Invalid value for magic command callback function: "
                "Must be a callable")

        if (doc is None):
            doc = callback_function.__doc__

        if (self.has_command(name) and (not overwrite)):
            raise Exception(
                "Invalid value for magic command name: "
                "Name '%s' already taken" % name)

        def _wrapper (doc, mc_args, *args_from_kernel):
            if (doc is None):
                kwargs = {}
            else:
                # we parse the arguments using docopt
                try:
                    if (mc_args is None):
                        mc_args = ''
                    kwargs = docopt.docopt(doc, mc_args, help = True)

                except docopt.DocoptExit as exception:
                    usage = ' '.join(map(
                        lambda x: x.strip(), str(exception).splitlines()))
                    raise Exception("Invalid syntax, %s" % usage)

                # we explicitly remove keys without values
                for (key, value) in kwargs.items():
                    if (value is None):
                        del kwargs[key]
                    else:
                        # remove any surrounding quotes
                        # and whitespaces from the value
                        kwargs[key] = re.sub(r"^['\"\s]|['\"\s]$", '', value)

            _logger.debug(
                "executing %s-flight command '%s' "
                "(callback function: %s)" % (
                    "pre" if is_pre_flight else "post",
                    name.lower(), callback_function))

            return callback_function(*args_from_kernel, **kwargs)

        self._magic_commands[name.lower()] = (
            functools.partial(_wrapper, doc), is_pre_flight)

        _logger.debug(
            "added %s-flight command '%s' (callback function: %s)" % (
                "pre" if is_pre_flight else "post",
                name.lower(), callback_function))
Пример #2
0
def get_value(value, v):
    if utils.is_callable(value):
        return value()
    elif value == "$self":
        return v
    elif value == "$clock":
        return Clock.get()
    elif utils.is_string(value) and value.startswith("$"):
        return res_manager.get(value[1:])
    else:
        return value
Пример #3
0
 def _get_gids(self, **kwargs):
     gribs = []
     try:
         self.scan_grib(gribs, kwargs)
         if (len(gribs)
                 == 0) and ('startStep' in kwargs and utils.is_callable(
                     kwargs['startStep']) and not kwargs['startStep'](0)):
             kwargs['startStep'] = lambda s: s >= 0
             self.scan_grib(gribs, kwargs)
         return gribs
     except ValueError:
         raise ValueError('No messages in grib file')
Пример #4
0
 def _find(gid, **kwargs):
     for k, v in kwargs.iteritems():
         if not grib_is_defined(gid, k):
             return False
         iscontainer = utils.is_container(v)
         iscallable = utils.is_callable(v)
         if (not iscontainer and not iscallable and grib_get(gid, k) == v) or\
                 (iscontainer and grib_get(gid, k) in v) or \
                 (iscallable and v(grib_get(gid, k))):
             continue
         else:
             return False
     return True
Пример #5
0
 def update(self, param=None):
     if self.update_func is None:
         return
     if not utils.is_callable(self.update_func):
         log().warn("update_func of res[%s] is not callable, update_func=%s", self.name, self.update_func)
         return
     arg_cnt = self.update_func.__code__.co_argcount
     if arg_cnt == 0:
         new_value = self.update_func()
     elif arg_cnt == 1:
         new_value = self.update_func(self.get_value())
     else:
         new_value = self.update_func(self.get_value(), param)
     self.set_value(new_value)
Пример #6
0
def add_res_with_update_preprocess(name, model, update):
    """
    An wrapper of __add_res. Do some pre-processing of update.
    """

    if utils.is_callable(update):
        update_function = {protocal.FUNCTION_TYPE_CALLABLE, utils.function_to_string(update)}
    elif type(update) is dict:
        update_function = {protocal.FUNCTION_TYPE_DEFAULT: update}
    else:
        update_function = None
        log().warn("invalid update function, update=%s", update)

    add_res(name, model, update_function)
Пример #7
0
    def register_handler(self, method_name, version, fn):
        if not utils.is_callable(fn):
            raise HandlerNotCallableException()

        try:
            if version in self._queues[method_name]:
                logger.warn('Duplicate handler registered for [{}/{}]').format(method_name, version)
        except KeyError:
            # Register version handler and method dispatcher
            self._queues[method_name] = {version: fn}
            self._register_handler(method_name)
        else:
            # Register version handler
            self._queues[method_name][version] = fn
Пример #8
0
def add_res_with_update_preprocess(name, model, update):
    """
    An wrapper of __add_res. Do some pre-processing of update.
    """

    if utils.is_callable(update):
        update_function = {protocal.FUNCTION_TYPE_CALLABLE, utils.function_to_string(update)}
    elif type(update) is dict:
        update_function = {protocal.FUNCTION_TYPE_DEFAULT: update}
    else:
        update_function = None
        log().warn("invalid update function, update=%s", update)

    add_res(name, model, update_function)
Пример #9
0
 def update(self, param=None):
     if self.update_func is None:
         return
     if not utils.is_callable(self.update_func):
         log().warn("update_func of res[%s] is not callable, update_func=%s", self.name, self.update_func)
         return
     arg_cnt = self.update_func.__code__.co_argcount
     if arg_cnt == 0:
         new_value = self.update_func()
     elif arg_cnt == 1:
         new_value = self.update_func(self.get_value())
     else:
         new_value = self.update_func(self.get_value(), param)
     self.set_value(new_value)
Пример #10
0
def register_listener(ref_res, condition, action):
    """
    register an event listener,
    :param ref_res: list of res names.
    :param condition: can be an callable object, or One of Default Condition, see in default_condition.py
    :param action: an callable object.
    :return: event_id
    """

    if utils.is_callable(condition):
        condition_dict = {protocal.FUNCTION_TYPE_CALLABLE: utils.function_to_string(condition)}
        event_id = utils.calc_function_hash([condition])
    else:
        condition_dict = {protocal.FUNCTION_TYPE_DEFAULT: condition}
        event_id = condition + "@" + RECEIVER

    event_book[event_id] = action
    log().info("new event id:%s", event_id)
    add_event_listener(event_id, ref_res, condition_dict)
Пример #11
0
def register_listener(ref_res, condition, action):
    """
    register an event listener,
    :param ref_res: list of res names.
    :param condition: can be an callable object, or One of Default Condition, see in default_condition.py
    :param action: an callable object.
    :return: event_id
    """

    if utils.is_callable(condition):
        condition_dict = {protocal.FUNCTION_TYPE_CALLABLE: utils.function_to_string(condition)}
        event_id = utils.calc_function_hash([condition])
    else:
        condition_dict = {protocal.FUNCTION_TYPE_DEFAULT: condition}
        event_id = condition + "@" + RECEIVER

    event_book[event_id] = action
    log().info("new event id:%s", event_id)
    add_event_listener(event_id, ref_res, condition_dict)