<commit_before>from flask.ext.classy import FlaskView, route
from flask import Blueprint, url_for, current_app
from marvin.tools.cube import Cube
import json
import urllib
from marvin import config
from marvin.utils.general import parseRoutePath, parseName

config.drpver = 'v1_5_1'  # FIX THIS

''' stuff that runs server-side '''

api = Blueprint("api", __name__)

# JSON results to return from API
result = {'data': None, 'status': -1, 'error': None}


def _getCube(name):
    ''' Retrieve a cube using marvin tools '''

    cube = None

    # parse name into either mangaid or plateifu
    try:
        mangaid, plateifu = parseName(name)
    except Exception as e:
        result['error'] = 'Failed to parse input name {0}: {1}'.format(name, str(e))
        return cube

    try:
        cube = Cube(mangaid=mangaid, plateifu=plateifu, mode='local')
        result['status'] = 1
    except Exception as e:
        result['error'] = 'Failed to retrieve cube {0}: {1}'.format(name, str(e))

    return cube


class CubeView(FlaskView):
    ''' Class describing API calls related to MaNGA Cubes '''

    route_base = '/cubes/'
    # decorators = [parseRoutePath]

    def index(self):
        # return json.dumps({'data': 'this is a cube!'})
        func_list = {}
        output = []
        for rule in current_app.url_map.iter_rules():
            options = {}
            for arg in rule.arguments:
                options[arg] = "[{0}]".format(arg)

            methods = ','.join(rule.methods)
            url = url_for(rule.endpoint, **options)
            line = urllib.unquote("{:50s} {:20s} {}".format(rule.endpoint, methods, url))
            output.append(line)

        res = {'data': []}
        for line in sorted(output):
            res['data'].append(line)

        return json.dumps(res)

    def get(self, name):

        cube = _getCube(name)
        if cube:
            result['data'] = {name: '{0},{1},{2},{3}'.format(name, cube.plate, cube.ra, cube.dec)}
        return json.dumps(result)

    @route('/<name>/spectra', defaults={'path': None})
    @route('/<name>/spectra/<path:path>', endpoint='getspectra')
    @parseRoutePath
    def getSpectra(self, name=None, x=None, y=None, ra=None, dec=None, ext=None):
        # Add ability to grab spectra from fits files

        cube = _getCube(name)
        if not cube:
            result['error'] = 'getSpectra: No cube: {0}'.format(result['error'])
            return json.dumps(result)<commit_msg>changes during the skype meeting<commit_after>from flask.ext.classy import FlaskView, route
from flask import Blueprint, url_for, current_app
from marvin.tools.cube import Cube
import json
import urllib
from marvin import config
from marvin.utils.general import parseRoutePath, parseName

config.drpver = 'v1_5_1'  # FIX THIS

''' stuff that runs server-side '''

api = Blueprint("api", __name__)

# JSON results to return from API
# result = {'data': None, 'status': -1, 'error': None}


def _getCube(name):
    ''' Retrieve a cube using marvin tools '''

    cube = None
    result = {'data': None, 'status': -1, 'error': None}
    # parse name into either mangaid or plateifu
    try:
        mangaid, plateifu = parseName(name)
    except Exception as e:
        result['error'] = 'Failed to parse input name {0}: {1}'.format(name, str(e))
        return cube

    try:
        cube = Cube(mangaid=mangaid, plateifu=plateifu, mode='local')
        result['status'] = 1
    except Exception as e:
        result['error'] = 'Failed to retrieve cube {0}: {1}'.format(name, str(e))

    return cube, result


class CubeView(FlaskView):
    ''' Class describing API calls related to MaNGA Cubes '''

    route_base = '/cubes/'
    # decorators = [parseRoutePath]

    def index(self):
        result = {'data': None, 'status': -1, 'error': None}
        return json.dumps({'data': 'this is a cube!'})
        '''
        func_list = {}
        output = []
        for rule in current_app.url_map.iter_rules():
            options = {}
            for arg in rule.arguments:
                options[arg] = "[{0}]".format(arg)

            methods = ','.join(rule.methods)
            url = url_for(rule.endpoint, **options)
            line = urllib.unquote("{:50s} {:20s} {}".format(rule.endpoint, methods, url))
            output.append(line)

        res = {'data': []}
        for line in sorted(output):
            res['data'].append(line)

        return json.dumps(res)
        '''

    def get(self, name):
        result = {'data': None, 'status': -1, 'error': None}
        cube, res = _getCube(name)
        if cube:
            result['data'] = {name: '{0},{1},{2},{3}'.format(name, cube.plate, cube.ra, cube.dec)}
        return json.dumps(result)

    @route('/<name>/spectra', defaults={'path': None})
    @route('/<name>/spectra/<path:path>', endpoint='getspectra')
    @parseRoutePath
    def getSpectra(self, name=None, x=None, y=None, ra=None, dec=None, ext=None):
        # Add ability to grab spectra from fits files
        result = {'data': None, 'status': -1, 'error': None}
        cube, res = _getCube(name)
        if not cube:
            result['error'] = 'getSpectra: No cube: {0}'.format(result['error'])
            return json.dumps(result)
{'fraction_non_alphanumeric': 0.14767530487804878, 'fraction_numerical': 0.008003048780487805, 'mean_word_length': 2.6835087719298247, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2818776', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1650, 'n_words': 462}