Esempio n. 1
0
def test_messaging_from_timer(c):
    instance = Timer(None)
    match = Match(None, None, c)
    timers = []

    instance.send_messages((datetime.timedelta(seconds=3), 'User', 'Hi'),
                           match, timers)
    assert len(timers) == 1
    assert isinstance(timers[0], Timer)
    assert timers[0].scheduled == datetime.timedelta(seconds=3)
    assert timers[0].msg_tuple == ('User', 'Hi', (), {},)

    instance.send_messages(('User', 'Hello {}', 'world'), match, timers)
    assert c.last == 'PRIVMSG User :Hello world'
Esempio n. 2
0
def test_timer_matches_function():
    now = datetime.datetime.now()
    connect_time = now - datetime.timedelta(seconds=4)

    timer_with_last_time = Timer(datetime.timedelta(seconds=3))
    timer_with_last_time.last_time = connect_time

    timers = [
        (Timer(datetime.timedelta(seconds=3)), True),
        (Timer(datetime.timedelta(seconds=5)), False),
        (Timer(datetime.timedelta(seconds=5), direct=True), True),
        (timer_with_last_time, True),
        (Timer(now - datetime.timedelta(days=1)), True),
        (Timer(now + datetime.timedelta(days=1)), False),
        (Timer(now + datetime.timedelta(days=1),
               direct=True), True),
    ]

    for t, expected in timers:
        assert t.matches(connect_time) is expected
Esempio n. 3
0
def process_messages(result, with_target=False):
    from pyromancer.objects import Timer

    if isinstance(result, (list, GeneratorType)):
        messages = result
    else:
        messages = [result]

    for msg in messages:
        get_target = with_target

        if isinstance(msg, tuple):
            timer = False
            if isinstance(msg[0], (datetime.datetime, datetime.timedelta)):
                scheduled = msg[0]
                msg = msg[1:]
                timer = True

                if callable(msg[0]):
                    get_target = False
                    target = None
                else:
                    get_target = True

            if get_target:
                target = msg[0]
                msg = msg[1:]

            last = len(msg) - 1
            msg, args, kwargs = msg[0], list(msg[1:last]), msg[last]

            # If the result is (msg, positional argument,), make sure it
            # still works correctly as expected for the formatting.
            if not isinstance(kwargs, dict):
                if last > 0:
                    args.append(kwargs)

                kwargs = {}

            if timer:
                yield Timer(scheduled, msg, *args, target=target, **kwargs)
                continue
        elif isinstance(msg, Timer):
            yield msg
            continue
        else:
            target, args, kwargs = None, [], {}

        if get_target:
            yield target, msg, args, kwargs
        else:
            yield msg, args, kwargs
Esempio n. 4
0
def test_timer_matches_function():
    now = datetime.datetime.now()
    connect_time = now - datetime.timedelta(seconds=4)

    timer_with_last_time = Timer(datetime.timedelta(seconds=3))
    timer_with_last_time.last_time = connect_time

    timers = [
        (Timer(datetime.timedelta(seconds=3)), True),
        (Timer(datetime.timedelta(seconds=5)), False),
        (Timer(datetime.timedelta(seconds=5), direct=True), True),
        (timer_with_last_time, True),
        (Timer(now - datetime.timedelta(days=1)), True),
        (Timer(now + datetime.timedelta(days=1)), False),
        (Timer(now + datetime.timedelta(days=1), direct=True), True),
    ]

    for t, expected in timers:
        assert t.matches(connect_time) is expected
Esempio n. 5
0
def test_messaging_from_timer(c):
    instance = Timer(None)
    match = Match(None, None, c)
    timers = []

    instance.send_messages((datetime.timedelta(seconds=3), 'User', 'Hi'),
                           match, timers)
    assert len(timers) == 1
    assert isinstance(timers[0], Timer)
    assert timers[0].scheduled == datetime.timedelta(seconds=3)
    assert timers[0].msg_tuple == (
        'User',
        'Hi',
        (),
        {},
    )

    instance.send_messages(('User', 'Hello {}', 'world'), match, timers)
    assert c.last == 'PRIVMSG User :Hello world'
Esempio n. 6
0
    }), ('Hello {} and {red_one}', ['world'], {
        'red_one': 'Mars'
    })),
    (('Hello {}, {} and {red_one}', 'world', 'moon', {
        'red_one': 'Mars'
    }), ('Hello {}, {} and {red_one}', ['world', 'moon'], {
        'red_one': 'Mars'
    })),
    (('Hello {}, {} and {red_one}', ['world', 'moon'], {
        'red_one': 'Mars'
    }), ('Hello {}, {} and {red_one}', [['world', 'moon']], {
        'red_one': 'Mars'
    })),
    (('Hello {}', ['world', 'moon']), ('Hello {}', [['world', 'moon']], {})),
    ((timedelta(seconds=3), 'User', 'Hello world'),
     Timer(timedelta(seconds=3), 'Hello world', target='User')),
    ((timedelta(seconds=3), example_command),
     Timer(timedelta(seconds=3), example_command)),
    (Timer(timedelta(seconds=3),
           example_command), Timer(timedelta(seconds=3), example_command))
]

MESSAGES_WITH_TARGET = [
    ('Hello world', (None, 'Hello world', [], {})),
    (('User', 'Hello world'), ('User', 'Hello world', [], {})),
    (('User', 'Hello {}', 'world'), ('User', 'Hello {}', ['world'], {}))
]


def test_processing_messages():
    for result, expected in MESSAGES:
Esempio n. 7
0
 def __call__(self, fn):
     fn.timer = Timer(*self.initargs, msg_or_command=fn, **self.initkwargs)
     return fn