<commit_before>import couchdb
import msg
import random
from base62 import b62encode, b62decode

documents = {
    123: "woot\nuber woot\n",
    432: "this is not a vimpaste\n\nyes it is",
}

design = {
    "_id": "_design/usage",
    "language": "javascript",
    "views": {
        "reusable": {
            "map": """
                function(doc) {
                    if (doc.expires >= (new Date()).getTime()) {
                        emit([doc.expires, doc.id], null)
                    } else if (doc.new) {
                        emit([0, doc.id], null)
                    }
                }"""
        },
        "highest_id": {
            "map": """function(doc) { emit("highest_id", parseInt(doc._id)) }""",
            "reduce": """
                function(keys, values) {
                    var max = 0;
                    for (var i = 0; i < values.length; i++) {
                        if (values[i] > max) {
                        max = values[i];
                        }
                    }
                    return max;
                }"""
        }
    }
}

def generate_blanks(amount=10):
    print("Generating %d blank documents..." % amount)

    res = db.view("usage/highest_id", reduce=True)
    if len(res) == 0:
        base_id = 0
    else:
        base_id = res.rows[0].value + 1

    for i in range(base_id, base_id+amount):
        doc = { "_id": str(i), "new": True }
        try:
            db.save(doc)
        except couchdb.ResourceConflict:
            pass

def get_available_doc():
    res = db.view("usage/reusable", limit=5, include_docs=True)

    # Generate a few more extra docs if needed.
    if len(res) == 0:
        generate_blanks()
        return get_available_doc()

    return random.choice([row.doc for row in res.rows])


def save_new_doc(id, data):
    doc = get_available_doc()
    new_doc = doc.copy()
    new_doc["raw"] = data
    new_doc["source"] = id
    new_doc["new"] = False
    try:
        db.save(new_doc)
    except ResourceConflict:
        return save_new_doc(id, data)
    return int(doc.id)


# Initialize couchdb db
print("Referencing CouchDB database.")
server = couchdb.Server()
if "vimpaste" not in server:
    print("Creating initial database.")
    server.create("vimpaste")
    generate_blanks()
db = server["vimpaste"]

# Make sure the design docs are in place.
current_design = db.get("_design/usage")
if not current_design:
    print("Creating initial design document.")
    db.save(design)
elif current_design["views"] != design["views"]:
    print("Updating design document...")
    new_design = current_design.copy()
    new_design["views"] = design["views"]
    db.save(new_design)


def app(env, start_response):
    method = env["REQUEST_METHOD"]
    path = env["PATH_INFO"]

    # Welcome page
    if path == "/":
        start_response("200 OK", [("Content-Type", "text/plain")])
        return [ msg.welcome ]

    # Wrong syntax
    try:
        id = b62decode(path[1:])
    except ValueError:
        start_response("400 Bad Request", [("Content-Type", "text/plain")])
        return [ "Invalid VimPaste Id Syntax" ]<commit_msg>Added plugin, force a two weeks retention for now.<commit_after>import couchdb
import time
import msg
import random
from base62 import b62encode, b62decode

__version__ = "0.1.0"

design = {
    "_id": "_design/usage",
    "language": "javascript",
    "views": {
        "reusable": {
            "map": """
                function(doc) {
                    if (doc.expires >= (new Date()).getTime()) {
                        emit([doc.expires, doc.id], null)
                    } else if (doc.new) {
                        emit([0, doc.id], null)
                    }
                }"""
        },
        "highest_id": {
            "map": """function(doc) { emit("highest_id", parseInt(doc._id)) }""",
            "reduce": """
                function(keys, values) {
                    var max = 0;
                    for (var i = 0; i < values.length; i++) {
                        if (values[i] > max) {
                        max = values[i];
                        }
                    }
                    return max;
                }"""
        }
    }
}

def generate_blanks(amount=10):
    print("Generating %d blank documents..." % amount)

    res = db.view("usage/highest_id", reduce=True)
    if len(res) == 0:
        base_id = 0
    else:
        base_id = res.rows[0].value + 1

    for i in range(base_id, base_id+amount):
        doc = { "_id": str(i), "new": True }
        try:
            db.save(doc)
        except couchdb.ResourceConflict:
            pass

def get_available_doc():
    res = db.view("usage/reusable", limit=5, include_docs=True)

    # Generate a few more extra docs if needed.
    if len(res) == 0:
        generate_blanks()
        return get_available_doc()

    return random.choice([row.doc for row in res.rows])


def save_new_doc(id, data):
    doc = get_available_doc()
    new_doc = doc.copy()
    new_doc["raw"] = data
    new_doc["source"] = id
    new_doc["new"] = False
    # Keep it two weeks by default
    new_doc["expires"] = int(time.time() + 60 * 60 * 24 * 14)
    try:
        db.save(new_doc)
    except ResourceConflict:
        return save_new_doc(id, data)
    return int(doc.id)


# Initialize couchdb db
print("Referencing CouchDB database.")
server = couchdb.Server()
if "vimpaste" not in server:
    print("Creating initial database.")
    server.create("vimpaste")
    generate_blanks()
db = server["vimpaste"]

# Make sure the design docs are in place.
current_design = db.get("_design/usage")
if not current_design:
    print("Creating initial design document.")
    db.save(design)
elif current_design["views"] != design["views"]:
    print("Updating design document...")
    new_design = current_design.copy()
    new_design["views"] = design["views"]
    db.save(new_design)


def app(env, start_response):
    method = env["REQUEST_METHOD"]
    path = env["PATH_INFO"]

    # Welcome page
    if path == "/":
        start_response("200 OK", [("Content-Type", "text/plain")])
        return [ msg.welcome % { "version": __version__ } ]
    elif path == "/vimpaste.vim":
        start_response("200 OK", [("Content-Type", "text/plain")])
        return [ msg.plugin % { "version": __version__ } ]

    # Wrong syntax
    try:
        id = b62decode(path[1:])
    except ValueError:
        start_response("400 Bad Request", [("Content-Type", "text/plain")])
        return [ "Invalid VimPaste Id Syntax" ]
{'fraction_non_alphanumeric': 0.1404983947408653, 'fraction_numerical': 0.011007491209295214, 'mean_word_length': 2.0064338235294117, 'pattern_counts': {'":': 25, '<': 5, '<?xml version=': 0, '>': 7, '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': '195260', 'n_tokens_mistral': 2102, 'n_tokens_neox': 1976, 'n_words': 495}