Example #1
0
def wait_for_first(condition):
    selector_factory = lambda: first(condition)

    items, lock = wait_for(selector_factory)

    assert len(items) == 1

    return items[0], lock
Example #2
0
def wait_for_first(condition, reason):
    selector_factory = lambda: first(condition)

    items, lock = wait_for(selector_factory, reason)

    assert len(items) == 1

    return items[0], lock
Example #3
0
def lock_items(filename, lock, term_generator=None, lock_reason=None):
    """
    >>> import os
    >>> if os.path.exists('test_db'): os.unlink('test_db')
    >>> set_database('test_db', '[dict(HOST=\"1\"), dict(HOST=\"2\"), dict(HOST=\"3\")]')
    >>> from selectors import first
    >>> lock_items('test_db', 'a', first(lambda x: x['HOST'] == '1'))
    [{'HOST': '1'}]
    >>> lock_items('test_db', 'b', first(lambda x: x['HOST'] == '2'))
    [{'HOST': '2'}]
    >>> lock_items('test_db', '', first(lambda x: x['HOST'] == '3'))
    Traceback (most recent call last):
    ...
    ValueError: Invalid lock value: ''
    >>> set_database('test_db', '[]')
    >>> lock_items('test_db', 'c')
    []
    >>> set_database('test_db', '[dict(HOST=\"1\"), dict(HOST=\"2\"), dict(HOST=\"3\")]')
    >>> seen_items = []
    >>> lock_items('test_db', 'a', lambda x: lambda: seen_items.append(x))
    []
    >>> seen_items.sort()
    >>> seen_items
    [{'HOST': '1'}, {'HOST': '2'}, {'HOST': '3'}]
    """

    if lock == "":
        raise ValueError("Invalid lock value: %s" % repr(lock))

    import sqlite3
    conn = sqlite3.connect(filename, isolation_level='EXCLUSIVE')

    c = conn.cursor()

    term_generator = term_generator or first()

    terms_ids_items = []
    for id, data in c.execute('SELECT id, data FROM stuff WHERE lock = :empty_lock ORDER by random()', dict(empty_lock="")).fetchall():
        item = eval(data)
        terms_ids_items.append((term_generator(item), id, item))

    results = []
    for term, id, item in terms_ids_items:
        if term():
            c.execute('UPDATE stuff SET lock = :lock, lock_date = datetime(\'now\') WHERE id = :id', dict(lock=lock, id=id))
            assert c.rowcount == 1
            results.append(item)
            
            if lock_reason:
                c.execute('UPDATE stuff SET lock_reason = :lock_reason WHERE id = :id', dict(lock_reason=lock_reason, id=id))
                assert c.rowcount == 1

    conn.commit()
    conn.close()

    return results
Example #4
0
def lock_items(filename, lock, term_generator=None):
    """
    >>> set_database('test_db', '[1, 2, 3]')
    >>> lock_items('test_db', 'a')
    [1]
    >>> lock_items('test_db', 'b')
    [2]
    >>> lock_items('test_db', '')
    Traceback (most recent call last):
    ...
    ValueError: Invalid lock value: ''
    >>> set_database('test_db', '[]')
    >>> lock_items('test_db', 'c')
    []
    >>> set_database('test_db', '[1, 2, 3]')
    >>> seen_items = []
    >>> lock_items('test_db', 'a', lambda x: lambda: seen_items.append(x))
    []
    >>> seen_items
    [1, 2, 3]
    """

    if lock == "":
        raise ValueError("Invalid lock value: %s" % repr(lock))

    import sqlite3
    conn = sqlite3.connect(filename)

    c = conn.cursor()

    term_generator = term_generator or first()

    stmt = 'SELECT id, data FROM stuff WHERE lock = :empty_lock ORDER by id'
    terms_ids_items = []
    for id, data in c.execute(stmt, dict(empty_lock="")).fetchall():
        item = eval(data)
        terms_ids_items.append((term_generator(item), id, item))

    results = []
    for term, id, item in terms_ids_items:
        if term():
            c.execute(
                'UPDATE stuff SET lock = :lock WHERE id = :id',
                dict(lock=lock, id=id)
            )
            assert c.rowcount == 1
            results.append(item)

    conn.commit()
    conn.close()

    return results
Example #5
0
def lock_items(filename, lock, term_generator=None, lock_reason=None):
    """
    >>> import os
    >>> if os.path.exists('test_db'): os.unlink('test_db')
    >>> set_database('test_db', '[dict(HOST=\"1\"), dict(HOST=\"2\"), dict(HOST=\"3\")]')
    >>> from selectors import first
    >>> lock_items('test_db', 'a', first(lambda x: x['HOST'] == '1'))
    [{'HOST': '1'}]
    >>> lock_items('test_db', 'b', first(lambda x: x['HOST'] == '2'))
    [{'HOST': '2'}]
    >>> lock_items('test_db', '', first(lambda x: x['HOST'] == '3'))
    Traceback (most recent call last):
    ...
    ValueError: Invalid lock value: ''
    >>> set_database('test_db', '[]')
    >>> lock_items('test_db', 'c')
    []
    >>> set_database('test_db', '[dict(HOST=\"1\"), dict(HOST=\"2\"), dict(HOST=\"3\")]')
    >>> seen_items = []
    >>> lock_items('test_db', 'a', lambda x: lambda: seen_items.append(x))
    []
    >>> seen_items.sort()
    >>> seen_items
    [{'HOST': '1'}, {'HOST': '2'}, {'HOST': '3'}]
    """

    if lock == "":
        raise ValueError("Invalid lock value: %s" % repr(lock))

    import sqlite3
    conn = sqlite3.connect(filename, isolation_level='EXCLUSIVE')

    c = conn.cursor()

    term_generator = term_generator or first()

    terms_ids_items = []
    for id, data in c.execute(
            'SELECT id, data FROM stuff WHERE lock = :empty_lock ORDER by random()',
            dict(empty_lock="")).fetchall():
        item = eval(data)
        terms_ids_items.append((term_generator(item), id, item))

    results = []
    for term, id, item in terms_ids_items:
        if term():
            c.execute(
                'UPDATE stuff SET lock = :lock, lock_date = datetime(\'now\') WHERE id = :id',
                dict(lock=lock, id=id))
            assert c.rowcount == 1
            results.append(item)

            lock_reason = lock_reason or ''
            c.execute(
                'UPDATE stuff SET lock_reason = :lock_reason WHERE id = :id',
                dict(lock_reason=lock_reason, id=id))
            assert c.rowcount == 1

    conn.commit()
    conn.close()

    return results