Exemplo n.º 1
0
    def query(cls, **args):
        data = {'model': cls.__name__}
        data.update(
            dict(('{0}_{1}'.format(cls.__name__, key), value)
                 for key, value in list(args.items())))

        code, reason, response = get_request(query_url, data, encode, decode,
                                             headers, timeout, monitor)

        if (code // 100 != 2):
            if monitor: monitor({'status': "error", 'reason': reason.lower()})
            raise BadResponseError(code, reason, response)
        if response is None:
            if monitor:
                monitor({
                    'status': "error",
                    'reason': "invalid response format"
                })
            raise BadResponseError(code, reason, response)
        message = None
        try:
            message = BaseMessage(response)
        except KeyError:
            if monitor:
                monitor({
                    'status': "error",
                    'reason': "invalid message format"
                })
            raise BadResponseError("response object isn't a valid BaseMessage",
                                   response)
        return message.query(cls).all()
Exemplo n.º 2
0
def repair(repair_url,
           include_extensions=True,
           extra_data=None,
           encode=None,
           decode=None,
           headers=None,
           timeout=None,
           monitor=None):
    """
    Fetches the server database and replaces the local one with it.

    *include_extensions* includes or excludes extension fields from
    the operation.

    *extra_data* can be used to add user credentials.

    By default, the *encode* function is ``json.dumps``, the *decode*
    function is ``json.loads``, and the *headers* are appropriate HTTP
    headers for JSON.
    """
    assert isinstance(repair_url, str), "repair url must be a string"
    assert bool(repair_url), "repair url can't be empty"
    if extra_data is not None:
        assert isinstance(extra_data, dict), "extra data must be a dictionary"
        assert 'exclude_extensions' not in extra_data, "reserved request key"
    data = {'exclude_extensions': ""} if not include_extensions else {}
    data.update(extra_data or {})

    code, reason, response = get_request(repair_url, data, encode, decode,
                                         headers, timeout, monitor)

    if (code // 100 != 2):
        if monitor: monitor({'status': "error", 'reason': reason.lower()})
        raise BadResponseError(code, reason, response)
    if response is None:
        if monitor:
            monitor({'status': "error", 'reason': "invalid response format"})
        raise BadResponseError(code, reason, response)
    message = None
    try:
        message = BaseMessage(response)
    except KeyError:
        if monitor:
            monitor({'status': "error", 'reason': "invalid message format"})
        raise BadResponseError("response object isn't a valid BaseMessage",
                               response)

    if monitor: monitor({'status': "repairing"})
    repair_database(message,
                    response.get("latest_version_id", None),
                    include_extensions=include_extensions)
    if monitor: monitor({'status': "done"})
    return response
Exemplo n.º 3
0
def repair(repair_url, include_extensions=True, extra_data=None,
           encode=None, decode=None, headers=None, timeout=None,
           monitor=None):
    """
    Fetches the server database and replaces the local one with it.

    *include_extensions* includes or excludes extension fields from
    the operation.

    *extra_data* can be used to add user credentials.

    By default, the *encode* function is ``json.dumps``, the *decode*
    function is ``json.loads``, and the *headers* are appropriate HTTP
    headers for JSON.
    """
    assert isinstance(repair_url, basestring), "repair url must be a string"
    assert bool(repair_url), "repair url can't be empty"
    if extra_data is not None:
        assert isinstance(extra_data, dict), "extra data must be a dictionary"
        assert 'exclude_extensions' not in extra_data, "reserved request key"
    data = {'exclude_extensions': ""} if not include_extensions else {}
    data.update(extra_data or {})

    code, reason, response = get_request(
        repair_url, data, encode, decode, headers, timeout, monitor)

    if (code // 100 != 2):
        if monitor: monitor({'status': "error", 'reason': reason.lower()})
        raise BadResponseError(code, reason, response)
    if response is None:
        if monitor: monitor({'status': "error",
                             'reason': "invalid response format"})
        raise BadResponseError(code, reason, response)
    message = None
    try:
        message = BaseMessage(response)
    except KeyError:
        if monitor: monitor({'status': "error",
                             'reason': "invalid message format"})
        raise BadResponseError(
            "response object isn't a valid BaseMessage", response)

    if monitor: monitor({'status': "repairing"})
    repair_database(
        message,
        response.get("latest_version_id", None),
        include_extensions=include_extensions)
    if monitor: monitor({'status': "done"})
    return response
Exemplo n.º 4
0
    def query(cls, **args):
        data = {'model': cls.__name__}
        data.update(dict(('{0}_{1}'.format(cls.__name__, key), value)
                         for key, value in args.iteritems()))

        code, reason, response = get_request(
            query_url, data, encode, decode, headers, timeout, monitor)

        if (code // 100 != 2):
            if monitor: monitor({'status': "error", 'reason': reason.lower()})
            raise BadResponseError(code, reason, response)
        if response is None:
            if monitor: monitor({'status': "error",
                                 'reason': "invalid response format"})
            raise BadResponseError(code, reason, response)
        message = None
        try:
            message = BaseMessage(response)
        except KeyError:
            if monitor: monitor({'status': "error",
                                 'reason': "invalid message format"})
            raise BadResponseError(
                "response object isn't a valid BaseMessage", response)
        return message.query(cls).all()