Ejemplo n.º 1
0
Archivo: pairs.py Proyecto: xmnr/atk
def replacePairs(pairs, indexes, name=None, value=None):
    """
    Interates through the pairs, alterPairing those at the supplied indexes. A
    generator of (name, value) pairs is returned.

    If the name and/or value parameters are not None, the pair is updated with
    the new value. If both are None, the pair is deleted. name and value must
    be strings, and all indexes must be positive integers & not outside of pairs.
    Duplicates in indexes are ignored.
    """

    if name is not None and not isinstance(name, str):
        raise TypeError("name must be a string or None.")
    if value is not None and not isinstance(value, str):
        raise TypeError("value must be a string or None.")

    indexes = set(indexes)

    for i, p in zip(range(len(pairs)), pairs):
        if i in indexes:
            indexes.remove(i)

            if name is None and value is None: continue
            else:
                p = (
                    valueOrDefault(name, pairs[i][NAME_IDX]),
                    valueOrDefault(value, pairs[i][VALUE_IDX])
                )

        yield p

    if indexes:
        raise IndexError("Not all supplied indexes were valid.")
Ejemplo n.º 2
0
Archivo: messages.py Proyecto: xmnr/atk
def request(method=None, path=None, headers=None, version=None, entity=None):

    return dict(
        method = valueOrDefault(method, "GET"),
        path = valueOrDefault(path, "/"),
        headers = valueOrDefault(headers, tuple()),
        version = valueOrDefault(version, "HTTP/1.0"),
        entity = valueOrDefault(entity, "")
    )
Ejemplo n.º 3
0
Archivo: messages.py Proyecto: xmnr/atk
def response(status=None, reason=None, headers=None, version=None, entity=None):

    return dict(
        status = valueOrDefault(status, "200"),
        reason = valueOrDefault(reason, "OK"),
        headers = valueOrDefault(headers, (
            ("Content-length", "0"),
            ("Content-type", "text/plain", ), 
        )),
        version = valueOrDefault(version, "HTTP/1.0"),
        entity = valueOrDefault(entity, "")
    )