Exemplo n.º 1
0
def init_tasks():
    for task_name, task_info in tasks.items():
        task_module, task_time, task_interval, task_ttl = task_info

        log("TASK", "Starting up [" + task_name + "]")
        libsigma.safe_mode(task_module.task_init)
        tasks[task_name] = (task_module, time.time(), task_interval, task_ttl)
Exemplo n.º 2
0
def run_tasks():
    for task_name, task_info in tasks.items():
        task_module, task_time, task_interval, task_ttl = task_info

        if time.time() >= (task_time + task_interval):
            libsigma.safe_mode(task_module.task_execute)
            if task_ttl > 1:
                tasks[task_name] = (task_module, time.time(), task_interval, task_ttl - 1)
            elif task_ttl == 1:
                del tasks[task_name]
            else:
                tasks[task_name] = (task_module, time.time(), task_interval, -1)
Exemplo n.º 3
0
def run_command(speaker, message):
    reduced = message.lstrip()
    if reduced:
        if reduced[0] == "'" and handler.specials["apostrophe"]:
            message = handler.specials["apostrophe"] + " " + reduced[1:]
        elif reduced[0] == ',' and handler.specials["comma"]:
            message = handler.specials["comma"] + " " + reduced[1:]
        elif reduced[0] == ':' and handler.specials["colon"]:
            message = handler.specials["colon"] + " " + reduced[1:]
        elif reduced[0] == '.' and handler.specials["period"]:
            message = handler.specials["period"] + " " + reduced[1:]
    else:
        return True

    try:
        tokens = message.lower().split()
    except:
        return False

    tail = message[(message.lower().find(tokens[0]) +
                    len(tokens[0])):].lstrip()

    if len(tokens):
        for (command, function) in handler.mappings:
            if command.startswith(tokens[0]):
                x = speaker.has_waits(function.priority)
                if not x:
                    libsigma.safe_mode(
                        function, {
                            "speaker": speaker,
                            "args": tokens,
                            "message": message,
                            "tail": tail,
                            "mapped": command
                        })
                else:
                    speaker.send_line("Please wait %d second%s." %
                                      (x, "s" if x != 1 else ""))
                return True
        return False
    else:
        return True
Exemplo n.º 4
0
def run_command(speaker, message):
    reduced = message.lstrip()
    if reduced:
        if reduced[0] == "'" and handler.specials["apostrophe"]:
            message = handler.specials["apostrophe"] + " " + reduced[1:]
        elif reduced[0] == ',' and handler.specials["comma"]:
            message = handler.specials["comma"] + " " + reduced[1:]
        elif reduced[0] == ':' and handler.specials["colon"]:
            message = handler.specials["colon"] + " " + reduced[1:]
        elif reduced[0] == '.' and handler.specials["period"]:
            message = handler.specials["period"] + " " + reduced[1:]
    else:
        return True

    try:
        tokens = message.lower().split()
    except:
        return False

    tail = message[(message.lower().find(tokens[0]) + len(tokens[0])):].lstrip()

    if len(tokens):
        for (command, function) in handler.mappings:
            if command.startswith(tokens[0]):
                x = speaker.has_waits(function.priority)
                if not x:
                    libsigma.safe_mode(function, {
                          "speaker" : speaker,
                          "args" : tokens,
                          "message" : message,
                          "tail" : tail,
                          "mapped" : command
                          })
                else:
                    speaker.send_line("Please wait %d second%s." % (x, "s" if x!=1 else ""))
                return True
        return False
    else:
        return True
Exemplo n.º 5
0
def load_handlers():
    handler_modules = glob(os.path.join(directories["handlers_root"], "*.py"))
    for handler_file in handler_modules:
        n = len(functions)
        source = os.path.basename(handler_file)
        module_name = 'handler_' + os.path.splitext(source)[0]

        h = libsigma.safe_mode(imp.load_source, module_name, handler_file)
        if h:
            log("HANDLER", "Loaded %d handler(s) from [%s]" % (len(functions) - n, source))
        else:
            log("HANDLER", "Handler module [%s] is not functional" % source, problem=True)
            continue
Exemplo n.º 6
0
def load_tasks():
    task_modules = glob.glob(os.path.join(directories["tasks_root"], "*.py"))
    for task_file in task_modules:
        source = os.path.basename(task_file)
        module_name = 'task_' + os.path.splitext(source)[0]

        t = libsigma.safe_mode(imp.load_source, module_name, task_file)
        if not t:
            log('TASK', 'Not loading [%s]: errors detected' % source, problem=True)
            continue

        try:
            task_name = t.name
            task_interval = t.interval
            f_init = t.task_init
            f_execute = t.task_execute
            f_deinit = t.task_deinit
        except AttributeError:
            log("TASK", "Task module [%s] is missing one or more required members" % source, problem=True)
        else:
            tasks[task_name] = (t, 0, task_interval, -1)
            log("TASK", "Loaded task [%s]" % task_name)
Exemplo n.º 7
0
def deinit_tasks():
    for task_name, task_info in tasks.items():
        task_module, task_time, task_interval, task_ttl = task_info

        log("TASK", "Shutting down [" + task_name + "]")
        libsigma.safe_mode(task_module.task_deinit)