<commit_before># -*- coding: utf-8  -*-

from markupsafe import escape

from .cookies import set_cookie, delete_cookie
from .misc import get_bot, Query
from .sites import get_sites

def main(context, environ, headers, cookies):
    query = Query(environ, method="POST")

    if query.action == "set":
        status = _do_set(query, headers, cookies)
    elif query.action == "delete":
        status = _do_delete(query, headers, cookies)
    else:
        status = None

    bot = get_bot()
    langs, projects = get_sites(bot)
    return bot, status, langs, projects

def _do_set(query, headers, cookies):
    changes = set()
    if query.lang:
        key = "EarwigDefaultLang"
        if key not in cookies or cookies[key].value != query.lang:
            set_cookie(headers, cookies, key, query.lang, 365)
            changes.add("site")
    if query.project:
        key = "EarwigDefaultProject"
        if key not in cookies or cookies[key].value != query.project:
            set_cookie(headers, cookies, key, query.project, 365)
            changes.add("site")
    if query.background:
        key = "EarwigBackground"
        if key not in cookies or cookies[key].value != query.background:
            set_cookie(headers, cookies, key, query.background, 365)
            changes.add("background")
    if changes:
        changes = ", ".format(sorted(list(changes)))
        return "Updated {0}.".format(changes)
    return None

def _do_delete(query, headers, cookies):
    if query.cookie in cookies:
        delete_cookie(headers, cookies, query.cookie.encode("utf8"))
        template = "Deleted cookie <b><tt>{0}</tt></b>."
        return template.format(escape(query.cookie))
    elif query.all:
        number = len(cookies)
        for cookie in cookies.values():
            delete_cookie(headers, cookies, cookie.key)
        return "Deleted <b>{0}</b> cookies.".format(number)
    return None
<commit_msg>Fix; delete background cache when updating background setting.<commit_after># -*- coding: utf-8  -*-

from markupsafe import escape

from .cookies import set_cookie, delete_cookie
from .misc import get_bot, Query
from .sites import get_sites

def main(context, environ, headers, cookies):
    query = Query(environ, method="POST")

    if query.action == "set":
        status = _do_set(query, headers, cookies)
    elif query.action == "delete":
        status = _do_delete(query, headers, cookies)
    else:
        status = None

    bot = get_bot()
    langs, projects = get_sites(bot)
    return bot, status, langs, projects

def _do_set(query, headers, cookies):
    changes = set()
    if query.lang:
        key = "EarwigDefaultLang"
        if key not in cookies or cookies[key].value != query.lang:
            set_cookie(headers, cookies, key, query.lang, 365)
            changes.add("site")
    if query.project:
        key = "EarwigDefaultProject"
        if key not in cookies or cookies[key].value != query.project:
            set_cookie(headers, cookies, key, query.project, 365)
            changes.add("site")
    if query.background:
        key = "EarwigBackground"
        if key not in cookies or cookies[key].value != query.background:
            set_cookie(headers, cookies, key, query.background, 365)
            delete_cookie(headers, cookies, "EarwigBackgroundCache")
            changes.add("background")
    if changes:
        changes = ", ".join(sorted(list(changes)))
        return "Updated {0}.".format(changes)
    return None

def _do_delete(query, headers, cookies):
    if query.cookie in cookies:
        delete_cookie(headers, cookies, query.cookie.encode("utf8"))
        template = "Deleted cookie <b><tt>{0}</tt></b>."
        return template.format(escape(query.cookie))
    elif query.all:
        number = len(cookies)
        for cookie in cookies.values():
            delete_cookie(headers, cookies, cookie.key)
        return "Deleted <b>{0}</b> cookies.".format(number)
    return None

{'fraction_non_alphanumeric': 0.11794354838709678, 'fraction_numerical': 0.007056451612903226, 'mean_word_length': 2.7728136882129277, 'pattern_counts': {'":': 4, '<': 15, '<?xml version=': 0, '>': 15, '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': '1423168', 'n_tokens_mistral': 1162, 'n_tokens_neox': 1139, 'n_words': 328}