<commit_before>""" Views for simple pip interaction """
import posixpath

import logging
import six
from pyramid.httpexceptions import HTTPBadRequest, HTTPFound, HTTPNotFound
from pyramid.view import view_config
from pyramid_duh import argify, addslash

from pypicloud.route import Root, SimplePackageResource, SimpleResource
from pypicloud.util import normalize_name


LOG = logging.getLogger(__name__)


@view_config(context=Root, request_method='POST', subpath=(), renderer='json')
@view_config(context=SimpleResource, request_method='POST', subpath=(),
             renderer='json')
@argify
def upload(request, name, version, content):
    """ Handle update commands """
    action = request.param(':action')
    name = normalize_name(name)
    if action == 'file_upload':
        if not request.access.has_permission(name, 'write'):
            return request.forbid()
        try:
            return request.db.upload(content.filename, content.file, name=name,
                                     version=version)
        except ValueError as e:
            return HTTPBadRequest(*e.args)
    else:
        return HTTPBadRequest("Unknown action '%s'" % action)


@view_config(context=SimpleResource, request_method='GET', subpath=(),
             renderer='simple.jinja2')
@addslash
def simple(request):
    """ Render the list of all unique package names """
    names = request.db.distinct()
    i = 0
    while i < len(names):
        name = names[i]
        if not request.access.has_permission(name, 'read'):
            del names[i]
            continue
        i += 1
    return {'pkgs': names}


@view_config(context=SimplePackageResource, request_method='GET', subpath=(),
             renderer='package.jinja2')
@addslash
def package_versions(context, request):
    """ Render the links for all versions of a package """
    normalized_name = normalize_name(context.name)
    if not request.access.has_permission(normalized_name, 'read'):
        return request.forbid()
    fallback = request.registry.fallback
    can_update_cache = request.access.can_update_cache()

    packages = request.db.all(normalized_name)
    pkgs = {}
    if fallback == 'mirror':
        if can_update_cache:
            pkgs = get_fallback_packages(request, context.name)
        if packages:
            # Overwrite upstream urls with cached urls
            for package in packages:
                pkgs[package.filename] = package.get_url(request)
        return {'pkgs': pkgs}
    elif packages:
        for package in packages:
            pkgs[package.filename] = package.get_url(request)
        return {'pkgs': pkgs}
    elif fallback == 'cache':
        if not can_update_cache:
            return request.forbid()
        pkgs = get_fallback_packages(request, context.name)
        if pkgs:
            return {'pkgs': pkgs}
        else:
            return HTTPNotFound()
    elif fallback == 'redirect':
        redirect_url = "%s/%s/" % (<commit_msg>More cleanup related to #48<commit_after>""" Views for simple pip interaction """
import posixpath

import logging
import six
from pyramid.httpexceptions import HTTPBadRequest, HTTPFound, HTTPNotFound
from pyramid.view import view_config
from pyramid_duh import argify, addslash

from pypicloud.route import Root, SimplePackageResource, SimpleResource
from pypicloud.util import normalize_name


LOG = logging.getLogger(__name__)


@view_config(context=Root, request_method='POST', subpath=(), renderer='json')
@view_config(context=SimpleResource, request_method='POST', subpath=(),
             renderer='json')
@argify
def upload(request, name, version, content):
    """ Handle update commands """
    action = request.param(':action')
    name = normalize_name(name)
    if action == 'file_upload':
        if not request.access.has_permission(name, 'write'):
            return request.forbid()
        try:
            return request.db.upload(content.filename, content.file, name=name,
                                     version=version)
        except ValueError as e:
            return HTTPBadRequest(*e.args)
    else:
        return HTTPBadRequest("Unknown action '%s'" % action)


@view_config(context=SimpleResource, request_method='GET', subpath=(),
             renderer='simple.jinja2')
@addslash
def simple(request):
    """ Render the list of all unique package names """
    names = request.db.distinct()
    i = 0
    while i < len(names):
        name = names[i]
        if not request.access.has_permission(name, 'read'):
            del names[i]
            continue
        i += 1
    return {'pkgs': names}


@view_config(context=SimplePackageResource, request_method='GET', subpath=(),
             renderer='package.jinja2')
@addslash
def package_versions(context, request):
    """ Render the links for all versions of a package """
    normalized_name = normalize_name(context.name)
    if not request.access.has_permission(normalized_name, 'read'):
        return request.forbid()
    fallback = request.registry.fallback
    can_update_cache = request.access.can_update_cache()

    packages = request.db.all(normalized_name)
    pkgs = {}
    if fallback == 'mirror':
        if can_update_cache:
            pkgs = get_fallback_packages(request, context.name)
        if packages:
            # Overwrite upstream urls with cached urls
            for package in packages:
                pkgs[package.filename] = package.get_url(request)
        if pkgs:
            return {'pkgs': pkgs}
        else:
            return HTTPNotFound("No packages found named %r" % normalized_name)
    elif packages:
        for package in packages:
            pkgs[package.filename] = package.get_url(request)
        return {'pkgs': pkgs}
    elif fallback == 'cache':
        if not can_update_cache:
            return request.forbid()
        pkgs = get_fallback_packages(request, context.name)
        if pkgs:
            return {'pkgs': pkgs}
        else:
            return HTTPNotFound("No packages found named %r" % normalized_name)
    elif fallback == 'redirect':
        redirect_url = "%s/%s/" % (
{'fraction_non_alphanumeric': 0.1044776119402985, 'fraction_numerical': 0.0016401508938822373, 'mean_word_length': 2.864385297845374, 'pattern_counts': {'":': 0, '<': 5, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2455624', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1684, 'n_words': 449}