<commit_before>from cheeseprism import pipext
from cheeseprism import resources
from cheeseprism import utils
from path import path
from pyramid.httpexceptions import HTTPFound
from pyramid.i18n import TranslationStringFactory
from pyramid.view import view_config
from werkzeug import secure_filename
import tempfile

_ = TranslationStringFactory('CheesePrism')


@view_config(renderer='index.html', context=resources.App)
def index(context, request):
    return {}

@view_config(name='instructions', renderer='instructions.html', context=resources.App)
def instructions(context, request):
    return {'page': 'instructions'}


@view_config(name='simple', context=resources.App)
def upload(context, request):
    if not (hasattr(request.POST['content'], 'file')):
        raise RuntimeError('No file attached') 

    ##TODO: check out body_file attribute of request
    fieldstorage = request.POST['content']
    dest = path(request.file_root) / secure_filename(fieldstorage.filename)

    dest.write_bytes(fieldstorage.file.read())

    utils.regenerate_index(request.file_root, 'index.html')
    request.response.headers['X-Swalow-Status'] = 'SUCCESS'
    return request.response


@view_config(name='find-packages', renderer='find_packages.html', context=resources.App)
def find_packages(context, request):
    #search pypi on post
    releases = None
    search_term = None
    if request.method == "POST":
        search_term = request.form['search_box']
        releases = utils.search_pypi(search_term)
    return dict(releases=releases, search_term=search_term)


def package(request):
    """
    refactor to use http://docs.python-requests.org/en/latest/index.html
    """
    name = request.matchdict['name']
    version = request.matchdict['version']
    details = utils.package_details(name, version)
    if details:
        details = details[0]
        url = details['url']
        filename = details['filename']
        try:
            req = Request(url)
            f = urlopen(req)
            print "downloading " + url

        	# Open our local file for writing
            local_file = open(os.path.join(app.config['FILE_ROOT'],filename), "w")
            #Write to our local file
            local_file.write(f.read())
            local_file.close()
            print 'finished downloading'
        #handle errors
        except HTTPError, e:
            print "HTTP Error:", e.code , url
        except URLError, e:
            print "URL Error:", e.reason , url
        regenerate_index(app.config['FILE_ROOT'],'index.html')
        flash('%s-%s was installed into the index successfully.' % (name, version))
    #@@ would be cool to return to the package in the index
    return HTTPFound('/index')


@view_config(name='regenerate-index', renderer='regenerate.html', context=resources.App)
def regenerate(context, request):
    if request.method == 'POST':
        utils.regenerate_index(request.file_root, 'index.html')
        return HTTPFound('/index')
    return {}

#tag_build = dev
#tag_svn_revision = true


def flash(msg):
    session.flash(msg)


@view_config(name='requirements', renderer='requirements_upload.html', context=resources.App)
def from_requirements(context, request):
    if request.method == "POST":
        f = request.files['req_file']
        
        filename = path(tempfile.gettempdir()) / 'temp-req.txt'
        f.save(filename)
        try:
            names = pipext.parse_reqs(filename, request.file_root)
        except:
            flash('There were some errors getting files from the uploaded requirements')
        else:
            flash('packages were installed from the requirements file. %s' % names)
        finally:
            utils.regenerate_index(request.file_root,'index.html')
        
        return HTTPFound('/requirements')
    return {}<commit_msg>rename to use new classmethod<commit_after>from cheeseprism import pipext
from cheeseprism import index
from cheeseprism import resources
from cheeseprism import utils
from path import path
from pyramid.httpexceptions import HTTPFound
from pyramid.i18n import TranslationStringFactory
from pyramid.view import view_config
from werkzeug import secure_filename
import tempfile

_ = TranslationStringFactory('CheesePrism')


@view_config(renderer='index.html', context=resources.App)
def index(context, request):
    return {}

@view_config(name='instructions', renderer='instructions.html', context=resources.App)
def instructions(context, request):
    return {'page': 'instructions'}


@view_config(name='simple', context=resources.App)
def upload(context, request):
    if not (hasattr(request.POST['content'], 'file')):
        raise RuntimeError('No file attached') 

    ##TODO: check out body_file attribute of request
    fieldstorage = request.POST['content']
    dest = path(request.file_root) / secure_filename(fieldstorage.filename)

    dest.write_bytes(fieldstorage.file.read())

    index.regenerate(request.file_root)
    request.response.headers['X-Swalow-Status'] = 'SUCCESS'
    return request.response


@view_config(name='find-packages', renderer='find_packages.html', context=resources.App)
def find_packages(context, request):
    #search pypi on post
    releases = None
    search_term = None
    if request.method == "POST":
        search_term = request.form['search_box']
        releases = utils.search_pypi(search_term)
    return dict(releases=releases, search_term=search_term)


def package(request):
    """
    refactor to use http://docs.python-requests.org/en/latest/index.html
    """
    name = request.matchdict['name']
    version = request.matchdict['version']
    details = utils.package_details(name, version)
    if details:
        details = details[0]
        url = details['url']
        filename = details['filename']
        try:
            req = Request(url)
            f = urlopen(req)
            print "downloading " + url

        	# Open our local file for writing
            local_file = open(os.path.join(app.config['FILE_ROOT'],filename), "w")
            #Write to our local file
            local_file.write(f.read())
            local_file.close()
            print 'finished downloading'
        #handle errors
        except HTTPError, e:
            print "HTTP Error:", e.code , url
        except URLError, e:
            print "URL Error:", e.reason , url
        index.regenerate(app.config['FILE_ROOT'])
        flash('%s-%s was installed into the index successfully.' % (name, version))
    #@@ would be cool to return to the package in the index
    return HTTPFound('/index')


@view_config(name='regenerate-index', renderer='regenerate.html', context=resources.App)
def regenerate(context, request):
    if request.method == 'POST':
        index.regenerate(request.file_root)
        return HTTPFound('/index')
    return {}

#tag_build = dev
#tag_svn_revision = true


def flash(msg):
    session.flash(msg)


@view_config(name='requirements', renderer='requirements_upload.html', context=resources.App)
def from_requirements(context, request):
    if request.method == "POST":
        f = request.files['req_file']
        
        filename = path(tempfile.gettempdir()) / 'temp-req.txt'
        f.save(filename)
        try:
            names = pipext.parse_reqs(filename, request.file_root)
        except:
            flash('There were some errors getting files from the uploaded requirements')
        else:
            flash('packages were installed from the requirements file. %s' % names)
        finally:
            index.regenerate(request.file_root)
        
        return HTTPFound('/requirements')
    return {}
{'fraction_non_alphanumeric': 0.11229035639412997, 'fraction_numerical': 0.0007861635220125787, 'mean_word_length': 3.278587443946188, 'pattern_counts': {'":': 4, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7419996', 'n_tokens_mistral': 2199, 'n_tokens_neox': 2133, 'n_words': 528}