<commit_before>import json
import logging
import urlparse

from google.appengine.api import search


INDEX_NAME = "nuts"


def send_json(start_response, status, headers, data):
	headers.append(("Content-Type", "application/json"))
	start_response(status, headers)
	return json.dumps(data)


def find(environ, start_response):
	"""Search nuts."""

	response = {}

	if environ["REQUEST_METHOD"].upper() != "GET":
		response["Message"] = "405 Method Not Allowed: use GET"
		return send_json(start_response, "405 Method Not Allowed", [("Allow", "GET")], response)

	try:
		q = urlparse.parse_qs(environ["QUERY_STRING"])["q"][0]
	except (KeyError, IndexError) as e:
		logging.warning("Bad request: %r", e)
		response["Message"] = "400 Bad Request: %r" % e
		return send_json(start_response, "400 Bad Request", [], response)

	results = search.Index(name=INDEX_NAME).search(q)

	response["Nuts"] = []
	for res in results:
		nut = {"Rank": res.rank}
		for f in res.fields:
			nut[f.name] = f.value
		response["Nuts"].append(nut)
	response["Message"] = "OK"
	return send_json(start_response, "200 OK", [], response)


def add(environ, start_response):
	"""
	Add nut to search index.

	Input: {"Nut": {"Name": "test_nut1",
					"Doc": "Package test_nut1 is used to test nut."}}
	"""

	response = {}

	if environ["REQUEST_METHOD"].upper() != "POST":
		response["Message"] = "405 Method Not Allowed: use POST"
		return send_json(start_response, "405 Method Not Allowed", [("Allow", "POST")], response)

	data = json.load(environ["wsgi.input"])
	logging.info("Adding %r", data)

	name = data["Nut"]["Name"]
	doc  = data["Nut"]["Doc"]
	if doc.lower().startswith("package %s " % name.lower()):
		doc = " ".join(doc.split(" ")[2:])
	if doc.endswith("."):
		doc = doc[:-1]

	# TODO rank
	fields = [
		search.TextField(name="Name", value=name),
		search.TextField(name="Doc",  value=doc),
	]
	d = search.Document(doc_id=name, fields=fields)
	search.Index(name=INDEX_NAME).put(d)

	response["Message"] = "OK"
	return send_json(start_response, "201 Created", [], response)<commit_msg>Use spaces instead of tabs.<commit_after>import json
import logging
import urlparse

from google.appengine.api import search


INDEX_NAME = "nuts"


def send_json(start_response, status, headers, data):
    headers.append(("Content-Type", "application/json"))
    start_response(status, headers)
    return json.dumps(data)


def find(environ, start_response):
    """Search nuts."""

    response = {}

    if environ["REQUEST_METHOD"].upper() != "GET":
        response["Message"] = "405 Method Not Allowed: use GET"
        return send_json(start_response, "405 Method Not Allowed", [("Allow", "GET")], response)

    try:
        q = urlparse.parse_qs(environ["QUERY_STRING"])["q"][0]
    except (KeyError, IndexError) as e:
        logging.warning("Bad request: %r", e)
        response["Message"] = "400 Bad Request: %r" % e
        return send_json(start_response, "400 Bad Request", [], response)

    results = search.Index(name=INDEX_NAME).search(q)

    response["Nuts"] = []
    for res in results:
        nut = {"Rank": res.rank}
        for f in res.fields:
            nut[f.name] = f.value
        response["Nuts"].append(nut)
    response["Message"] = "OK"
    return send_json(start_response, "200 OK", [], response)


def add(environ, start_response):
    """
    Add nut to search index.

    Input: {"Nut": {"Name": "test_nut1",
                    "Doc": "Package test_nut1 is used to test nut."}}
    """

    response = {}

    if environ["REQUEST_METHOD"].upper() != "POST":
        response["Message"] = "405 Method Not Allowed: use POST"
        return send_json(start_response, "405 Method Not Allowed", [("Allow", "POST")], response)

    data = json.load(environ["wsgi.input"])
    logging.info("Adding %r", data)

    name = data["Nut"]["Name"]
    doc  = data["Nut"]["Doc"]
    if doc.lower().startswith("package %s " % name.lower()):
        doc = " ".join(doc.split(" ")[2:])
    if doc.endswith("."):
        doc = doc[:-1]

    # TODO rank
    fields = [
        search.TextField(name="Name", value=name),
        search.TextField(name="Doc",  value=doc),
    ]
    d = search.Document(doc_id=name, fields=fields)
    search.Index(name=INDEX_NAME).put(d)

    response["Message"] = "OK"
    return send_json(start_response, "201 Created", [], response)
{'fraction_non_alphanumeric': 0.17340376665135507, 'fraction_numerical': 0.0133210840606339, 'mean_word_length': 3.8713646532438477, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5464923', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1431, 'n_words': 351}