<commit_before>import json
import bleach
from bottle import Bottle, redirect, response, request
import common as c

app = Bottle()


def v2to3(items):
    convert = {
        'plugname': 'plugin_name',
        'name': 'slug',
        'repo': 'server',
        'link': 'dbo_page',
        'slug': 'name',
    }
    data = []
    for item in items:
        if item in convert:
            data.append(convert[item])
        else:
            data.append(item)
    return data


def v3to2(items):
    data = []
    for item in items:
        if 'dbo_page' in item:
            item['link'] = item['dbo_page']
            del(item['dbo_page'])
        if 'server' in item:
            item['repo'] = item['server']
            del(item['server'])
        if 'plugin_name' in item:
            item['plugname'] = item['plugin_name']
            del(item['plugin_name'])
        if 'slug' in item:
            item['name'] = item['slug']
            del(item['slug'])
        if '_use_dbo' in item: del(item['_use_dbo'])
        if 'logo' in item: del(item['logo'])
        if 'logo_full' in item: del(item['logo_full'])
        if 'versions' in item:
            versions = []
            for version in item['versions']:
                if 'slug' in version: del(version['slug'])
                if 'dbo_version' in version: del(version['dbo_version'])
                if 'changelog' in version: del(version['changelog'])
                versions.append(version)
            item['versions'] = versions
        data.append(item)
    return data        


@app.hook('before_request')
def set_json_header():
    # We will need these set for everything in the API ;)
    response.set_header('Content-Type', 'application/json')
    response.set_header('Access-Control-Allow-Origin', '*')


@app.get('/')
@app.get('/geninfo')
@app.get('/geninfo/')
def generation_info():
    '''Generation Information
    Returns the generation information as requested.  User can optionall request
    to look X number of versions back.
    '''
    size = c.sint(bleach.clean(request.query.size or None))
    return c.jsonify(c.list_geninfo(size))


@app.get('/<server>/plugins')
@app.get('/<server>/plugins/')
def plugin_list(server=None):
    '''Plugin Listing
    Returns the plugin listing.  Can optionally be limited to a specific server
    binary compatability type.
    '''
    fields = bleach.clean(request.query.fields or 'slug,plugin_name,description').split(',')
    fields = v2to3(fields)
    start = c.sint(bleach.clean(request.query.start or None))
    size = c.sint(bleach.clean(request.query.size or None))
    sort = bleach.clean(request.query.sort or 'slug')
    data = c.list_plugins(server, fields, sort)
    if size is not None and start is not None:
        return c.jsonify(v3to2(data[start:start+size]))
    return c.jsonify(v3to2(data))


@app.get('/<server>/plugin/<slug>')
@app.get('/<server>/plugin/<slug>/')
@app.get('/<server>/plugin/<slug>/<version>')
@app.get('/<server>/plugin/<slug>/<version>/')
def plugin_details(server, slug, version=None):
    '''Plugin Details 
    Returns the document for a specific plugin.  Optionally can return only a
    specific version as part of the data as well.
    '''
    fields = bleach.clean(request.query.fields or '').split(',')
    fields = v2to3(fields)
    data = c.plugin_details(server, slug, version, fields)
    return c.jsonify(v3to2([data])[0])




@app.get('/<server>/plugin/<slug>/<version>/download')
def plugin_download(server, slug, version):
    '''Plugin Download Redirector
    Will attempt to redirect to the plugin download for the version specified.
    If no version exists, then it will throw a 404 error.
    '''
    plugin = c.plugin_details(server, slug, version, {})
    if version.lower() == 'latest':
        link = [plugin['versions'][0]['download'],]
    else:
        versions = plugin['versions']
        link = [v['download'] for v in versions if v['version'] == version]
    if len(link) > 0:
        redirect(link[0])
    else:
        raise bottle.HTTPError(404, '{"error": "could not find version"}')

<commit_msg>Added check to see if only 1 version comes back.  This should resolve Issue #80.<commit_after>import json
import bleach
from bottle import Bottle, redirect, response, request
import common as c

app = Bottle()


def v2to3(items):
    convert = {
        'plugname': 'plugin_name',
        'name': 'slug',
        'repo': 'server',
        'link': 'dbo_page',
        'slug': 'name',
    }
    data = []
    for item in items:
        if item in convert:
            data.append(convert[item])
        else:
            data.append(item)
    return data


def v3to2(items):
    data = []
    for item in items:
        if 'dbo_page' in item:
            item['link'] = item['dbo_page']
            del(item['dbo_page'])
        if 'server' in item:
            item['repo'] = item['server']
            del(item['server'])
        if 'plugin_name' in item:
            item['plugname'] = item['plugin_name']
            del(item['plugin_name'])
        if 'slug' in item:
            item['name'] = item['slug']
            del(item['slug'])
        if '_use_dbo' in item: del(item['_use_dbo'])
        if 'logo' in item: del(item['logo'])
        if 'logo_full' in item: del(item['logo_full'])
        if 'versions' in item:
            versions = []
            for version in item['versions']:
                if 'slug' in version: del(version['slug'])
                if 'dbo_version' in version: del(version['dbo_version'])
                if 'changelog' in version: del(version['changelog'])
                versions.append(version)
            item['versions'] = versions
        data.append(item)
    return data        


@app.hook('before_request')
def set_json_header():
    # We will need these set for everything in the API ;)
    response.set_header('Content-Type', 'application/json')
    response.set_header('Access-Control-Allow-Origin', '*')


@app.get('/')
@app.get('/geninfo')
@app.get('/geninfo/')
def generation_info():
    '''Generation Information
    Returns the generation information as requested.  User can optionall request
    to look X number of versions back.
    '''
    size = c.sint(bleach.clean(request.query.size or None))
    return c.jsonify(c.list_geninfo(size))


@app.get('/<server>/plugins')
@app.get('/<server>/plugins/')
def plugin_list(server=None):
    '''Plugin Listing
    Returns the plugin listing.  Can optionally be limited to a specific server
    binary compatability type.
    '''
    fields = bleach.clean(request.query.fields or 'slug,plugin_name,description').split(',')
    fields = v2to3(fields)
    start = c.sint(bleach.clean(request.query.start or None))
    size = c.sint(bleach.clean(request.query.size or None))
    sort = bleach.clean(request.query.sort or 'slug')
    data = c.list_plugins(server, fields, sort)
    if size is not None and start is not None:
        return c.jsonify(v3to2(data[start:start+size]))
    return c.jsonify(v3to2(data))


@app.get('/<server>/plugin/<slug>')
@app.get('/<server>/plugin/<slug>/')
@app.get('/<server>/plugin/<slug>/<version>')
@app.get('/<server>/plugin/<slug>/<version>/')
def plugin_details(server, slug, version=None):
    '''Plugin Details 
    Returns the document for a specific plugin.  Optionally can return only a
    specific version as part of the data as well.
    '''
    fields = bleach.clean(request.query.fields or '').split(',')
    fields = v2to3(fields)
    data = c.plugin_details(server, slug, version, fields)
    data = v3to2([data])[0]
    if 'versions' in data and len(data['versions']) == 1:
        data['versions'] = data['versions'][0] 
    return c.jsonify(data)




@app.get('/<server>/plugin/<slug>/<version>/download')
def plugin_download(server, slug, version):
    '''Plugin Download Redirector
    Will attempt to redirect to the plugin download for the version specified.
    If no version exists, then it will throw a 404 error.
    '''
    plugin = c.plugin_details(server, slug, version, {})
    if version.lower() == 'latest':
        link = [plugin['versions'][0]['download'],]
    else:
        versions = plugin['versions']
        link = [v['download'] for v in versions if v['version'] == version]
    if len(link) > 0:
        redirect(link[0])
    else:
        raise bottle.HTTPError(404, '{"error": "could not find version"}')


{'fraction_non_alphanumeric': 0.14733579687686255, 'fraction_numerical': 0.006317797115269997, 'mean_word_length': 2.804988662131519, 'pattern_counts': {'":': 2, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5243650', 'n_tokens_mistral': 2607, 'n_tokens_neox': 2578, 'n_words': 695}