From 340ba07c9d856589d950852fa9a69c5f7df49b26 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 15 Sep 2014 01:22:31 -0500 Subject: [PATCH] Generate API results as ordered dicts. --- copyvios/api.py | 49 +++++++++++++++++++++++++++---------------------- templates/api.mako | 4 ++-- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/copyvios/api.py b/copyvios/api.py index cd8c323..671846b 100644 --- a/copyvios/api.py +++ b/copyvios/api.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +from collections import OrderedDict + from .checker import do_check, T_POSSIBLE, T_SUSPECT from .misc import Query from .sites import get_sites @@ -19,19 +21,20 @@ _CHECK_ERRORS = { } def _serialize_page(page): - return {"title": page.title, "url": page.url} + return OrderedDict(("title", page.title), ("url", page.url)) def _serialize_source(source, show_skip=True): if not source: - return {"url": None, "confidence": 0.0, "violation": "none"} + return OrderedDict(("url", None), ("confidence", 0.0), + ("violation", "none")) conf = source.confidence - data = { - "url": source.url, - "confidence": conf, - "violation": "suspected" if conf >= T_SUSPECT else - "possible" if conf >= T_POSSIBLE else "none" - } + data = OrderedDict( + ("url", source.url), + ("confidence", conf), + ("violation", "suspected" if conf >= T_SUSPECT else + "possible" if conf >= T_POSSIBLE else "none") + ) if show_skip: data["skipped"] = source.skipped return data @@ -41,7 +44,8 @@ def format_api_error(code, info): info = type(info).__name__ + ": " + str(info) elif isinstance(info, unicode): info = info.encode("utf8") - return {"status": "error", "error": {"code": code, "info": info}} + error_inner = OrderedDict(("code", code), ("info", info)) + return OrderedDict(("status", "error"), ("error", error_inner)) def _hook_default(query): info = u"Unknown action: '{0}'".format(query.action.lower()) @@ -69,27 +73,28 @@ def _hook_check(query): return format_api_error("bad_title", info.format(query.page.title)) result = query.result - data = { - "status": "ok", - "meta": { - "time": result.time, - "queries": result.queries, - "cached": result.cached, - "redirected": bool(query.redirected_from) - }, - "page": _serialize_page(query.page), - "best": _serialize_source(result.best, show_skip=False), - "sources": [_serialize_source(source) for source in result.sources] - } + data = OrderedDict( + ("status", "ok"), + ("meta", OrderedDict( + ("time", result.time), + ("queries", result.queries), + ("cached", result.cached), + ("redirected", bool(query.redirected_from)) + )), + ("page", _serialize_page(query.page)) + ) if result.cached: data["meta"]["cache_time"] = result.cache_time if query.redirected_from: data["original_page"] = _serialize_page(query.redirected_from) + data["best"] = _serialize_source(result.best, show_skip=False) + data["sources"] = [_serialize_source(source) for source in result.sources] return data def _hook_sites(query): langs, projects = get_sites() - return {"status": "ok", "langs": langs, "projects": projects} + return OrderedDict(("status", "ok"), ("langs", langs), + ("projects", projects)) _HOOKS = { "compare": _hook_check, diff --git a/templates/api.mako b/templates/api.mako index 9c647b4..6ff191f 100644 --- a/templates/api.mako +++ b/templates/api.mako @@ -192,8 +192,8 @@ "time": float time to generate results, in seconds, "queries": int number of search engine queries made, "cached": boolean whether or not these results are cached from an earlier search (always false in the case of action=compare), - only if cached=true "cache_time": string human-readable time of the original search that the results are cached from, - "redirected": boolean whether or not a redirect was followed + "redirected": boolean whether or not a redirect was followed, + only if cached=true "cache_time": string human-readable time of the original search that the results are cached from }, "page": { "title": string the normalized title of the page checked,