Example #1
0
def basic_response_by_index(yaml_file,
                            request_id,
                            parameter_index,
                            basic_index,
                            filename):
    """ Add basic response given the request id, parameter index, and basic index

    :param yaml_file: file containing basic response data
    :param request_id: associate request id
    :param parameter_index: index of the parameter
    :param basic_index: index of the basic response
    :param filename: file associated to the basic response

    :return: JSON object
    """
    recastapi.file_check(filename)
    
    # Find the point_response_id
    point_request_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    point_requests = recastapi.get(point_request_url)

    if parameter_index > len(point_requests['_items'])-1:
        print "*"*60
        print "Parameter index out of bounds"
        print "The request has ", len(point_requests['_items']), " elements"
        raise RuntimeError

    point_request_id = point_requests['_items'][parameter_index]['id']

    # Find the point response
    point_response_url = '{}?where=point_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_RESPONSES'], point_request_id)
    point_responses = recastapi.get(point_response_url)

    if len(point_responses['_items']) < 1:
        print "*"*60
        print "No Parameter response found!"
        print "Please add a Parameter response before a Basic response"
        raise RuntimeError

    point_response_id = point_responses['_items'][0]['id']

    #Find the basic request
    basic_request_url = '{}?where=point_request_id=="{}"'.format(
        recastapi.ENDPOINTS['BASIC_REQUESTS'], point_request_id)
    basic_requests = recastapi.get(basic_request_url)

    if basic_index > len(basic_requests['_items'])-1:
        print "*"*60
        print "Basic index out of bounds"
        print "The parameter has ", len(basic_requests['_items']), " basic elements"
        raise RuntimeError

    basic_request_id = basic_requests['_items'][basic_index]['id']

    return basic_response(yaml_file,
                          point_response_id,
                          basic_request_id,
                          filename)
Example #2
0
def scan_response(scan_response_id =  None, scan_request_id = None):
    """List request depending on criteria
       If all variables are none all requests returned
    :param uuid: ID of the request(optional)
    :param analysis_id: Analysis ID to query. Returns all requests associated to the analysis

    :return: JSON object
    """
    if (scan_response_id is None) and (scan_request_id is None):
        response = recastapi.get(recastapi.ENDPOINTS['SCAN_RESPONSES'])
        return response['_items']

    if (scan_response_id is not None) and (scan_request_id is not None):
        raise RuntimeError('Cannot fetch scan_response_id and scan_request_id simultaneously')

    elif scan_response_id is not None:
        url = '{}/{}'.format(recastapi.ENDPOINTS['SCAN_RESPONSES'], scan_response_id)
        response = recastapi.get(url)
        return response

    elif scan_request_id is not None:
        selection_str = '?where=scan_request_id=="{}"'.format(scan_request_id)
        url = '{}{}'.format(recastapi.ENDPOINTS['SCAN_RESPONSES'], selection_str)
        response = recastapi.get(url)
        assert(len(response['_items']) < 2)
        if not response['_items']:
            return None
        return response['_items'][0]
Example #3
0
def download_all(request_id, download_path=None, dry_run=False):
    """Downloads all files associated with a given request.
    

    :param request_id: ID of the request to query.
    :param download_path: Filename of the files(still have to come up with logical)
                  naming convention. Currently, files are downloaded with their original
                  file name in the current directory
    :param dry_run: whether to download file or not

    :return: JSON object with metadata of files downloaded on disk
    """
    url_point_request = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    # Get all point requests
    response_point_request = recastapi.get(url_point_request)
    
    responses = []
    for response in response_point_request['_items']:
    
        url_basic_request = '{}?where=point_request_id=="{}"'.format(
            recastapi.ENDPOINTS['BASIC_REQUESTS'], response['id'])
        # Get all basic requests
        response_basic_request = recastapi.get(url_basic_request)
    
        for response_basic in response_basic_request['_items']:
      
            if download_path:
                """Have to set download path, otherwise files will be overwritten """
                pass
        
            r = download_file(response_basic['id'], download_path=None, dry_run=dry_run)
            responses.append(r)
      
    return responses
Example #4
0
def request(uuid=None,
            analysis_id=None,
            query='?max_results=1000&page=1'):    
    """List request depending on criteria
       If all variables are none all requests returned

    
    :param uuid: ID of the request(optional)
    :param analysis_id: Analysis ID to query. Returns all requests associated to the analysis        

    :return: JSON object
    """
    if analysis_id and uuid:
        print "*"*60
        print "Cannot fetch analysis and uuid simultaneously"
        print "Please provide either an analysis or uuid"
        raise RuntimeError
    
    elif uuid:
        single_request = '/{}'.format(uuid)
        url = '{}{}'.format(recastapi.ENDPOINTS['REQUESTS'], single_request)
        response = recastapi.get(url)
        return response
    
    elif analysis_id:
        analysis_str = '?where=analysis_id=="{}"'.format(analysis_id)
        url = '{}{}'.format(recastapi.ENDPOINTS['REQUESTS'], analysis_str)
        response = recastapi.get(url)
        return response['_items']
    
    else:
        url = '{}{}'.format(recastapi.ENDPOINTS['REQUESTS'], query)
        response = recastapi.get(url)
        return response['_items']
Example #5
0
def scan_request(scan_request_id = None, analysis_id = None, query='?max_results=1000&page=1'):
    """List request depending on criteria
       If all variables are none all requests returned
    :param uuid: ID of the request(optional)
    :param analysis_id: Analysis ID to query. Returns all requests associated to the analysis

    :return: JSON object
    """
    if (analysis_id is not None) and (scan_request_id is not None):
        raise RuntimeError('Cannot fetch analysis_id and scan_request_id simultaneously')

    elif scan_request_id is not None:
        single_request = '/{}'.format(scan_request_id)
        url = '{}{}'.format(recastapi.ENDPOINTS['SCAN_REQUESTS'], single_request)
        response = recastapi.get(url)
        return response

    elif analysis_id is not None:
        analysis_str = '?where=analysis_id=="{}"'.format(analysis_id)
        url = '{}{}'.format(recastapi.ENDPOINTS['SCAN_REQUESTS'], analysis_str)
        response = recastapi.get(url)
        return response['_items']
    else:
        url = '{}{}'.format(recastapi.ENDPOINTS['SCAN_REQUESTS'], query)
        response = recastapi.get(url)
        return response['_items']
Example #6
0
def request(uuid=None, analysis_id=None, query='?max_results=1000&page=1'):
    """List request depending on criteria
       If all variables are none all requests returned

    
    :param uuid: ID of the request(optional)
    :param analysis_id: Analysis ID to query. Returns all requests associated to the analysis        

    :return: JSON object
    """
    if analysis_id and uuid:
        print "*" * 60
        print "Cannot fetch analysis and uuid simultaneously"
        print "Please provide either an analysis or uuid"
        raise RuntimeError

    elif uuid:
        single_request = '/{}'.format(uuid)
        url = '{}{}'.format(recastapi.ENDPOINTS['REQUESTS'], single_request)
        response = recastapi.get(url)
        return response

    elif analysis_id:
        analysis_str = '?where=analysis_id=="{}"'.format(analysis_id)
        url = '{}{}'.format(recastapi.ENDPOINTS['REQUESTS'], analysis_str)
        response = recastapi.get(url)
        return response['_items']

    else:
        url = '{}{}'.format(recastapi.ENDPOINTS['REQUESTS'], query)
        response = recastapi.get(url)
        return response['_items']
Example #7
0
def archives(request_id, parameter_index=0, basic_index=None):
    """ Returns list of files given the request id and parameter index
        
    :param request_id: ID of the request
    :param parameter_index: index of the parameter
    :param basic_index: index of the basic request


    :return: JSON object `or` list if basic_index is None
    """
    if not parameter_index and not parameter_index == 0:
        print '-' * 60
        print "Exception in user code:"
        print "\t ******* Parameter index not valid"
        print '\n'
        raise RuntimeError

    parameters_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    parameter_responses = recastapi.get(parameters_url)

    parameter_id = parameter_responses

    #check if we have the right parameter_index
    if parameter_index > len(parameter_responses['_items']):
        print '-' * 60
        print "Excepton in user code:"
        print "\t ********* Parameter index out of bound"
        print '\n'
        raise RuntimeError

    parameter_id = parameter_responses['_items'][parameter_index]['id']

    basic_url = '{}?where=point_request_id=="{}"'.format(
        recastapi.ENDPOINTS['BASIC_REQUESTS'], parameter_id)

    basic_response = recastapi.get(basic_url)

    if not basic_index and not basic_index == 0:
        #return all basic request index
        basic_responses = []
        for i, basic in enumerate(basic_response['_items']):
            response = download(request_id=request_id,
                                point_request_index=parameter_index,
                                basic_request_index=i,
                                download_path=None,
                                dry_run=True)

            basic_responses.append(response)
        return basic_responses

    else:
        #return single indexed basic request
        basic_response = download(request_id=request_id,
                                  point_request_index=parameter_index,
                                  basic_request_index=basic_index,
                                  download_path=None,
                                  dry_run=True)
        return basic_response
Example #8
0
def download_file(basic_request_id, download_path=None, dry_run=False):
    """ Worker function that actually downloads file
    
    
    :param basic_request_id: ID of the basic request associated with a file.
    :param donwload_path: User specified download path(optional), if not
                   provided, file takes original file_name.
    :param dry_run: whether to download file or not. If false link of file provided
                    in response object

    
    :return: JSON object containing the metadata of the file, and file downloaded saved on disk.
    """
    local_path_key_name = 'local_path'  #path of the downloaded file on local machine
    files_urls = '{}?where=basic_request_id=="{}"'.format(
        recastapi.ENDPOINTS['FILES'], basic_request_id)

    responses = None
    url_response = recastapi.get(files_urls)

    if len(url_response['_items']) == 1:
        # File exist
        url = ('{}/{}'.format(recastapi.ENDPOINTS['FILES'],
                              url_response['_items'][0]['id']))
        response = recastapi.get(url)
        link = response['file_link']
        if link:
            if not dry_run:
                zip_file = urllib.URLopener()
                zip_file.retrieve(
                    link, download_path or response['original_file_name'])
                response['local_path_key_name'] = download_path or response[
                    'original_file_name']
                print colored(
                    'Successfully downloaded file {}'.format(
                        download_path or response['original_file_name']),
                    'green')
            else:
                print colored('File link: {}'.format(response['file_link']),
                              'green')
        else:
            response[local_path_key_name] = None
            print colored(
                'Failed to download file {}'.format(
                    download_path or response['original_file_name']), 'red')
            print colored(
                '\t Please check if this request is associated with a file',
                'red')
        responses = response

    elif len(url_response['_items']) == 0:
        # No files for this basic request
        print colored('No files for this basic request', 'red')

    else:
        # File length greater than 1 (this should never happen)
        print colored('BASIC REQUEST ERROR', 'red')
    return responses
Example #9
0
def basic_response_by_index(yaml_file, request_id, parameter_index,
                            basic_index, filename):
    """ Add basic response given the request id, parameter index, and basic index

    :param yaml_file: file containing basic response data
    :param request_id: associate request id
    :param parameter_index: index of the parameter
    :param basic_index: index of the basic response
    :param filename: file associated to the basic response

    :return: JSON object
    """
    recastapi.file_check(filename)

    # Find the point_response_id
    point_request_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    point_requests = recastapi.get(point_request_url)

    if parameter_index > len(point_requests['_items']) - 1:
        print "*" * 60
        print "Parameter index out of bounds"
        print "The request has ", len(point_requests['_items']), " elements"
        raise RuntimeError

    point_request_id = point_requests['_items'][parameter_index]['id']

    # Find the point response
    point_response_url = '{}?where=point_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_RESPONSES'], point_request_id)
    point_responses = recastapi.get(point_response_url)

    if len(point_responses['_items']) < 1:
        print "*" * 60
        print "No Parameter response found!"
        print "Please add a Parameter response before a Basic response"
        raise RuntimeError

    point_response_id = point_responses['_items'][0]['id']

    #Find the basic request
    basic_request_url = '{}?where=point_request_id=="{}"'.format(
        recastapi.ENDPOINTS['BASIC_REQUESTS'], point_request_id)
    basic_requests = recastapi.get(basic_request_url)

    if basic_index > len(basic_requests['_items']) - 1:
        print "*" * 60
        print "Basic index out of bounds"
        print "The parameter has ", len(
            basic_requests['_items']), " basic elements"
        raise RuntimeError

    basic_request_id = basic_requests['_items'][basic_index]['id']

    return basic_response(yaml_file, point_response_id, basic_request_id,
                          filename)
Example #10
0
def basic(request_id, parameter_index, basic_index):
    """ Gets the basic response

    :param request_id: ID of the request
    :param parameter_index: ID of the parameter
    :param basic_index: ID of the basic response
    
    :return: JSON object
    """

    # get parameter ID
    parameter_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    parameter_response = recastapi.get(parameter_url)

    if parameter_index > len(parameter_response['_items']) - 1:
        # index out of bounds
        print '-' * 60
        print "Exception in user code:"
        print "\t ******** parameter index out of bounds"
        print '\n'
        raise RuntimeError

    parameter_id = parameter_response['_items'][parameter_index]['id']

    # get basic request
    basic_url = '{}?where=point_request_id=="{}"'.format(
        recastapi.ENDPOINTS['BASIC_REQUESTS'], parameter_id)
    basic_response = recastapi.get(basic_url)

    if basic_index > len(basic_response['_items']) - 1:
        # basic index out of bounds
        print '-' * 60
        print "Exception in user code:"
        print "\t ******* basic index out of bounds"
        print '\n'
        raise RuntimeError

    basic_id = basic_response['_items'][basic_index]['id']

    # finally fetch the basic response
    response_url = '{}?where=basic_request_id=="{}"'.format(
        recastapi.ENDPOINTS['BASIC_RESPONSES'], basic_id)
    response = recastapi.get(response_url)

    if len(response['_items']) == 0:
        # No basic response found
        print '-' * 60
        print "Exception in user code:"
        print "\t ******** No basic response found for the indices"
        print '\n'
        raise RuntimeError

    return response['_items'][0]
Example #11
0
def basic(request_id, parameter_index, basic_index):
    """ Gets the basic response

    :param request_id: ID of the request
    :param parameter_index: ID of the parameter
    :param basic_index: ID of the basic response
    
    :return: JSON object
    """

    # get parameter ID
    parameter_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    parameter_response = recastapi.get(parameter_url)

    if parameter_index > len(parameter_response['_items'])-1:
        # index out of bounds
        print '-'*60
        print "Exception in user code:"
        print "\t ******** parameter index out of bounds"
        print '\n'
        raise RuntimeError

    parameter_id = parameter_response['_items'][parameter_index]['id']

    # get basic request
    basic_url = '{}?where=point_request_id=="{}"'.format(
        recastapi.ENDPOINTS['BASIC_REQUESTS'], parameter_id)
    basic_response = recastapi.get(basic_url)

    if basic_index > len(basic_response['_items'])-1:
        # basic index out of bounds
        print '-'*60
        print "Exception in user code:"
        print "\t ******* basic index out of bounds"
        print '\n'
        raise RuntimeError

    basic_id = basic_response['_items'][basic_index]['id']

    # finally fetch the basic response
    response_url = '{}?where=basic_request_id=="{}"'.format(
        recastapi.ENDPOINTS['BASIC_RESPONSES'], basic_id)
    response = recastapi.get(response_url)

    if len(response['_items']) == 0:
        # No basic response found
        print '-'*60
        print "Exception in user code:"
        print "\t ******** No basic response found for the indices"
        print '\n'
        raise RuntimeError

    return response['_items'][0]
Example #12
0
def download_file(basic_request_id, download_path=None, dry_run=False):  
    """ Worker function that actually downloads file
    
    
    :param basic_request_id: ID of the basic request associated with a file.
    :param donwload_path: User specified download path(optional), if not
                   provided, file takes original file_name.
    :param dry_run: whether to download file or not. If false link of file provided
                    in response object

    
    :return: JSON object containing the metadata of the file, and file downloaded saved on disk.
    """
    local_path_key_name = 'local_path' #path of the downloaded file on local machine
    files_urls = '{}?where=basic_request_id=="{}"'.format(
        recastapi.ENDPOINTS['FILES'], basic_request_id)
  
    responses = None
    url_response = recastapi.get(files_urls)
    
    if len(url_response['_items']) == 1:
        # File exist
        url = ('{}/{}'.format(recastapi.ENDPOINTS['FILES'],
                              url_response['_items'][0]['id']))
        response = recastapi.get(url)
        link = response['file_link']
        if link:
            if not dry_run:
                zip_file = urllib.URLopener()
                zip_file.retrieve(link, download_path or response['original_file_name'])
                response['local_path_key_name'] = download_path or response['original_file_name']
                print colored('Successfully downloaded file {}'.format(
                    download_path or response['original_file_name']), 'green')
            else:
                print colored('File link: {}'.format(
                    response['file_link']), 'green')
        else:
            response[local_path_key_name] = None
            print colored('Failed to download file {}'.format(
                download_path or response['original_file_name']), 'red')
            print colored('\t Please check if this request is associated with a file', 'red')
        responses = response
        
    elif len(url_response['_items']) == 0:
        # No files for this basic request
        print colored('No files for this basic request', 'red')
        
    else:
        # File length greater than 1 (this should never happen)
        print colored('BASIC REQUEST ERROR', 'red')
    return responses
Example #13
0
def download(response_id,
             point_response_index=0,
             basic_response_index=0,
             download_path=None,
             dry_run=False):
    """Downloads file associated with a given request, indexed through point and basic responses.
    

    :param response_id: ID of the response.
    :param point_response_index: index of the point request 0..N-1.
    :param basic_response_index: index of the basic request 0..M-1.
    :param download_path: User specified download filename `if` none keeps original name
    :param dry_run: whether to download file or not
    
    :return: JSON object with metadata of files downloaded on disk.
    """

    print colored('Download...', 'cyan')
    print colored(
        'Response: {}.\n\t Point response index: {}.\n\t\t Basic response index: {}.'
        .format(response_id, point_response_index,
                basic_response_index), 'cyan')

    url_point_response = '{}?where=scan_response_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_RESPONSES'], response_id)

    response_point = recastapi.get(url_point_response)
    if len(response_point['_items']) < point_response_index:
        print colored(
            'ERR: Point response index out range. Max index value is {}'.
            format(len(response_point['_items'])), 'red')
        return

    url_basic_response = '{}?where=point_response_id=="{}"'.format(
        recastapi.ENDPOINTS['BASIC_RESPONSES'],
        response_point['_items'][point_response_index]['id'])

    response_basic = recastapi.get(url_basic_response)

    if len(response_basic['_items']) < basic_response_index:
        print colored(
            'ERR: Basic response index out of range. Max index value is {}'.
            format(len(response_basic['_items'])), 'red')
        return

    response = download_file(
        response_basic['_items'][basic_response_index]['id'], download_path,
        dry_run)
    return response
Example #14
0
def upload_file(parameter_id, filename):
    """Uploads zip file and associates it with a request and basic request.
  
    :param request_id: ID of the request to be associated to this file.
    :param basic_request_id: ID of the basic request to be asso
    :param filename: Path to file to be uploaded.
      

    :return: JSON object    
    """
    recastapi.file_check(filename)
    
    basic_request_response = basic_request(parameter_id)
    basic_request_id = basic_request_response['id']
    
    file_uuid = str(uuid.uuid1())

    #get request ID, so deposition can be retrieved
    point_request_url = '{}/{}'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'],
        parameter_id)
    point_response = recastapi.get(point_request_url)

    request_id = point_response['scan_request_id']
    
    request_url = '{}/{}'.format(
        recastapi.ENDPOINTS['REQUESTS'],
        request_id
    )
    request_response = recastapi.get(request_url)
    deposition_id = request_response['zenodo_deposition_id']

    payload = {
        'file_name': file_uuid,
        'zenodo_file_id': None,
        'original_file_name': filename,
        'basic_request_id': basic_request_id,
    }
  
    files = {'file': open(filename, 'rb')}
    url = '{}/'.format(recastapi.ENDPOINTS['FILES'])
    file_response = recastapi.post(
        url, 
        data=payload, 
        files=files,
        params = {'deposition_id': deposition_id}
    )
    basic_request_response['metadata'] = file_response
    return basic_request_response
Example #15
0
def coordinate(point_request_id, coordinate_index=None):
    """Returns list of coordinates given a parameter index and request_id
    or single coordinate

    :param request_id: ID of the request
    :param parameter_index: index of the parameter
    :param coordinate_index: index of the coordinate.
                            `if` none given returns list of coordinate
    :return: JSON object
    """

    filtered_coordinate_url = '{}?where=point_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_COORDINATES'],
        point_request_id
    )

    filtered_coordinate_response = recastapi.get(filtered_coordinate_url)

    if coordinate_index is None:
        #No coordinate index given, return all coords.
        return filtered_coordinate_response['_items']

    if coordinate_index in range(len(filtered_coordinate_response['_items'])):
        #return coords index
        return filtered_coordinate_response['_items'][coordinate_index]
    else:
        raise RuntimeError('Coordinate index out of bounds')
Example #16
0
def upload_file_by_index(request_id, parameter_index, filename):
    """ Uploads zip file given the parameter index

    :param request_id: ID of the request
    :param parameter_index: index of the parameter
    :param filename: file to upload. Must be zip format
    
    :return: JSON object
    """
    recastapi.file_check(filename)

    #get parameter id
    parameter_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    parameters = recastapi.get(parameter_url)

    if parameter_index > len(parameters['_items']) - 1:
        print "*" * 60
        print "Parameter index out of bounds"
        print "The request has ", len(parameters['_items']), " parameters"
        raise RuntimeError

    parameter_id = parameter_index['_items'][parameter_index]['id']

    return upload_file(parameter_id=parameter_id, filename=filename)
Example #17
0
def coordinate_by_index(request_id, parameter_index, coordinate_name,
                        coordinate_value):
    """Adds coordinate to a give parameter 

    :param request_id: request ID
    :param parameter_index: value of the coordinate
    :param coordinate_name: name of the coordinate
    :param coordinate_value: value

    :return: JSON
    """

    #get the parameter id
    parameter_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    parameters = recastapi.get(parameter_url)

    if parameter_index > len(parameters['_items']) - 1:
        print "*" * 60
        print "Parameter index out of bounds"
        print "The request has ", len(parameters['_items']), " parameters"
        raise RuntimeError

    parameter_id = parameter_index['_items'][parameter_index]['id']

    return coordinate(parameter_id=parameter_id,
                      coordinate_name=coordinate_name,
                      coordinate_value=coordinate_value)
Example #18
0
def coordinate_by_index(request_id,
                            parameter_index,
                            coordinate_name,
                            coordinate_value):
    """Adds coordinate to a give parameter 

    :param request_id: request ID
    :param parameter_index: value of the coordinate
    :param coordinate_name: name of the coordinate
    :param coordinate_value: value

    :return: JSON
    """
               
    #get the parameter id
    parameter_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    parameters = recastapi.get(parameter_url)

    if parameter_index > len(parameters['_items'])-1:
        print "*"*60
        print "Parameter index out of bounds"
        print "The request has ", len(parameters['_items']), " parameters"
        raise RuntimeError

    parameter_id = parameter_index['_items'][parameter_index]['id']    

    return coordinate(parameter_id=parameter_id,
                      coordinate_name=coordinate_name,
                      coordinate_value=coordinate_value)
Example #19
0
def upload_file_by_index(request_id, parameter_index, filename):
    """ Uploads zip file given the parameter index

    :param request_id: ID of the request
    :param parameter_index: index of the parameter
    :param filename: file to upload. Must be zip format
    
    :return: JSON object
    """
    recastapi.file_check(filename)

    #get parameter id
    parameter_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    parameters = recastapi.get(parameter_url)

    if parameter_index > len(parameters['_items'])-1:
        print "*"*60
        print "Parameter index out of bounds"
        print "The request has ", len(parameters['_items']), " parameters"
        raise RuntimeError

    parameter_id = parameter_index['_items'][parameter_index]['id']

    return upload_file(parameter_id=parameter_id,
                       filename=filename)
Example #20
0
def analysis_by_pub_identifier(pubtype, pubid):
    """List analysis given uuid or all analyses.

    :param uuid: analysis_id.

    :return: JSON object containing all analyses retrieved.
    """

    query = None
    if pubtype == 'cds':
        query = 'cds_id=="{}"'.format(pubid)
    elif pubtype == 'arxiv':
        query = 'arxiv_id=="{}"'.format(pubid)
    elif pubtype == 'inspire':
        query = 'inspire_id=="{}"'.format(pubid)
    elif pubtype == 'recast':
        query = 'id=="{}"'.format(pubid)
    else:
        raise NotImplementedError()

    url = '{}{}'.format(recastapi.ENDPOINTS['ANALYSIS'], '?where={}'.format(query))
    responses = recastapi.get(url)

    if not responses['_items']:
        return None

    return responses['_items'][0]
Example #21
0
def download(request_id,
             point_request_index=0,
             basic_request_index=0,
             download_path=None,
             dry_run=False):
    """Downloads file associated with a given request, index through point and basic requests.
    

    :param request_id: ID of the request.
    :param point_request_index: index of the point request 0..N-1.
    :param basic_request_index: index of basic request 0..M-1.
    :param dry_run: whether to download file or not

    
    :return: JSON object with metadata of files downloaded on disk.
    """
    print colored('Downloading....', 'cyan')
    print colored(
        'Request: {}.\n\t Point request index: {}. \n\t\t Basic request index: {}.'
        .format(request_id, point_request_index, basic_request_index), 'cyan')

    url_point_request = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)

    response_point_request = recastapi.get(url_point_request)
    if point_request_index > len(response_point_request['_items']) - 1:
        print colored(
            'ERR: Point request index out of range. Max range is {}'.format(
                len(response_point_request['_items'])), 'red')
        return

    url_basic_request = '{}?where=point_request_id=="{}"'.format(
        recastapi.ENDPOINTS['BASIC_REQUESTS'],
        response_point_request['_items'][point_request_index]['id'])

    response_basic_request = recastapi.get(url_basic_request)

    if basic_request_index > len(response_basic_request['_items']) - 1:
        print colored(
            'ERR: Basic request index out of range. Max range is {}'.format(
                len(response_basic_request['_items'])), 'red')
        return

    response = download_file(
        response_basic_request['_items'][basic_request_index]['id'],
        download_path, dry_run)
    return response
Example #22
0
def query(query=None):
    """ customized query. """
    url = '{}{}'.format(recastapi.ENDPOINTS['REQUESTS'], query)
    response = recastapi.get(url)
    if response.has_key('_items'):
        return response['_items']
    else:
        return response
Example #23
0
def this_user():
    """Lists user given his ORCID ID
    :return: JSON object
    """
    if not recastapi.ORCID_ID:
        raise RuntimeError('No ORCID ID set')
    url = '{}?where=orcid_id=="{}"'.format(recastapi.ENDPOINTS['USERS'], recastapi.ORCID_ID)
    return recastapi.get(url)['_items'][0]
Example #24
0
def user(user_id = None):
    """Lists user information.

    :param id: Optional, if provided will return data of a single user.
    :return: JSON object containing all data that could be retrieved.
    """
    single_user = '******'.format(user_id) if user_id is not None else ''
    url = '{}{}'.format(recastapi.ENDPOINTS['USERS'], single_user)
    return recastapi.get(url)
Example #25
0
def get_coordinate(point_request_id):
    """ Private function

    """
    coordinate_url = '{}?where=point_request_id=="{}"'.format(
        recastapi.ENDPOINTS['PARAMETER_POINTS'], point_request_id)

    coordinate_responses = recastapi.get(coordinate_url)
    return coordinate_responses['_items']
Example #26
0
def download(response_id, 
	     point_response_index=0,
	     basic_response_index=0, 
	     download_path=None,
	     dry_run=False):
    """Downloads file associated with a given request, indexed through point and basic responses.
    

    :param response_id: ID of the response.
    :param point_response_index: index of the point request 0..N-1.
    :param basic_response_index: index of the basic request 0..M-1.
    :param download_path: User specified download filename `if` none keeps original name
    :param dry_run: whether to download file or not
    
    :return: JSON object with metadata of files downloaded on disk.
    """
  
    print colored('Download...', 'cyan')
    print colored('Response: {}.\n\t Point response index: {}.\n\t\t Basic response index: {}.'.format(
	response_id, point_response_index, basic_response_index), 'cyan')

    url_point_response = '{}?where=scan_response_id=="{}"'.format(
	recastapi.ENDPOINTS['POINT_RESPONSES'], response_id)
    
    response_point = recastapi.get(url_point_response)
    if len(response_point['_items']) < point_response_index:
	print colored('ERR: Point response index out range. Max index value is {}'.format(
	    len(response_point['_items'])), 'red')
	return
            
    url_basic_response = '{}?where=point_response_id=="{}"'.format(
	recastapi.ENDPOINTS['BASIC_RESPONSES'],
	response_point['_items'][point_response_index]['id'])
    
    response_basic = recastapi.get(url_basic_response)
  
    if len(response_basic['_items']) < basic_response_index:
	print colored('ERR: Basic response index out of range. Max index value is {}'.format(
	    len(response_basic['_items'])), 'red')
	return
		
    response = download_file(response_basic['_items'][basic_response_index]['id'],
							  download_path, dry_run)
    return response
Example #27
0
def tree(request_id):
    """ Prints request tree, including the number of 
        coordinates for each parameter
    
    
    :param request_id: ID of the request
    """

    print colored('Tree for REQUEST: {}'.format(request_id), 'green')

    url_point_request = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    response_point_request = recastapi.get(url_point_request)

    for i, point_response in enumerate(response_point_request['_items']):

        for j in range(1):
            print "\n"

        print colored("Parameter: {}".format(i), 'green')

        
        url_coordinate ='{}?where=point_request_id=="{}"'.format(
            recastapi.ENDPOINTS['PARAMETER_POINTS'], point_response['id'])
        response_coordinate = recastapi.get(url_coordinate)

        for j in range(4):
            print colored("\t\t |", 'red')
            
        print colored("\t\t - coordinates: {}".format(
                len(response_coordinate['_items'])),
                      'red')


        url_basic_request = '{}?where=point_request_id=="{}"'.format(
            recastapi.ENDPOINTS['BASIC_REQUESTS'], point_response['id'])
        response_basic_request = recastapi.get(url_basic_request)

        for j in range(4):
            print colored("\t\t |", 'yellow')

        print colored("\t\t - archives: {}".format(
            len(response_basic_request['_items'])),
            'yellow')
Example #28
0
def upload_file(parameter_id, filename):
    """Uploads zip file and associates it with a request and basic request.
  
    :param request_id: ID of the request to be associated to this file.
    :param basic_request_id: ID of the basic request to be asso
    :param filename: Path to file to be uploaded.
      

    :return: JSON object    
    """
    recastapi.file_check(filename)

    basic_request_response = basic_request(parameter_id)
    basic_request_id = basic_request_response['id']

    file_uuid = str(uuid.uuid1())

    #get request ID, so deposition can be retrieved
    point_request_url = '{}/{}'.format(recastapi.ENDPOINTS['POINT_REQUESTS'],
                                       parameter_id)
    point_response = recastapi.get(point_request_url)

    request_id = point_response['scan_request_id']

    request_url = '{}/{}'.format(recastapi.ENDPOINTS['REQUESTS'], request_id)
    request_response = recastapi.get(request_url)
    deposition_id = request_response['zenodo_deposition_id']

    payload = {
        'file_name': file_uuid,
        'zenodo_file_id': None,
        'original_file_name': filename,
        'basic_request_id': basic_request_id,
    }

    files = {'file': open(filename, 'rb')}
    url = '{}/'.format(recastapi.ENDPOINTS['FILES'])
    file_response = recastapi.post(url,
                                   data=payload,
                                   files=files,
                                   params={'deposition_id': deposition_id})
    basic_request_response['metadata'] = file_response
    return basic_request_response
Example #29
0
def download(request_id,
             point_request_index=0,
             basic_request_index=0, 
             download_path=None, 
             dry_run=False):
    """Downloads file associated with a given request, index through point and basic requests.
    

    :param request_id: ID of the request.
    :param point_request_index: index of the point request 0..N-1.
    :param basic_request_index: index of basic request 0..M-1.
    :param dry_run: whether to download file or not

    
    :return: JSON object with metadata of files downloaded on disk.
    """
    print colored('Downloading....', 'cyan')
    print colored('Request: {}.\n\t Point request index: {}. \n\t\t Basic request index: {}.'
                  .format(request_id, point_request_index, basic_request_index), 'cyan')
    
    url_point_request = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    
    response_point_request = recastapi.get(url_point_request)
    if point_request_index > len(response_point_request['_items'])-1:
        print colored('ERR: Point request index out of range. Max range is {}'.format(
            len(response_point_request['_items'])), 'red')
        return
        
    url_basic_request = '{}?where=point_request_id=="{}"'.format(
        recastapi.ENDPOINTS['BASIC_REQUESTS'],
        response_point_request['_items'][point_request_index]['id'])
    
    response_basic_request = recastapi.get(url_basic_request)
    
    if basic_request_index > len(response_basic_request['_items'])-1:
        print colored('ERR: Basic request index out of range. Max range is {}'.format(
            len(response_basic_request['_items'])), 'red')
        return

    response = download_file(response_basic_request['_items'][basic_request_index]['id'],
                              download_path, dry_run)
    return response
Example #30
0
def parameter_response_by_index(yaml_file, request_id, parameter_index, filename):
    """Adds Point response/Parameter response given the request id and parameter index
        index with reference from 0

    :param yaml_file: file containing the data of the response
    :param request_id: request id
    :param parameter_index: index of the parameter
    :param filename: filename associated to the parameter

    :return: JSON object
    """
    recastapi.file_check(filename)

    # Query scan responses check if the response has already been created
    scan_response_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['RESPONSES'], request_id)
    scan_responses = recastapi.get(scan_response_url)

    if len(scan_responses['_items']) == 0:
        # make new scan response if none is associated to the request
        scan_responses = response(request_id)
        scan_response_id = scan_responses['id']
    else:
        scan_response_id = scan_responses['_items'][0]['id']

    # get the point request id
    # Query the point request for a given request_id get the index in the list
    point_request_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    point_requests = recastapi.get(point_request_url)    

    if parameter_index > len(point_requests['_items'])-1:
        print "*"*60
        print "Parameter index out of bounds"
        print "The request has ", len(point_requests['_items']), "elements"
        raise RuntimeError

    point_request_id = point_requests['_items'][parameter_index]['id']

    return parameter_response(yaml_file,
                              scan_response_id,
                              point_request_id,
                              filename)
Example #31
0
def parameter_response_by_index(yaml_file, request_id, parameter_index,
                                filename):
    """Adds Point response/Parameter response given the request id and parameter index
        index with reference from 0

    :param yaml_file: file containing the data of the response
    :param request_id: request id
    :param parameter_index: index of the parameter
    :param filename: filename associated to the parameter

    :return: JSON object
    """
    recastapi.file_check(filename)

    # Query scan responses check if the response has already been created
    scan_response_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['RESPONSES'], request_id)
    scan_responses = recastapi.get(scan_response_url)

    if len(scan_responses['_items']) == 0:
        # make new scan response if none is associated to the request
        scan_responses = response(request_id)
        scan_response_id = scan_responses['id']
    else:
        scan_response_id = scan_responses['_items'][0]['id']

    # get the point request id
    # Query the point request for a given request_id get the index in the list
    point_request_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    point_requests = recastapi.get(point_request_url)

    if parameter_index > len(point_requests['_items']) - 1:
        print "*" * 60
        print "Parameter index out of bounds"
        print "The request has ", len(point_requests['_items']), "elements"
        raise RuntimeError

    point_request_id = point_requests['_items'][parameter_index]['id']

    return parameter_response(yaml_file, scan_response_id, point_request_id,
                              filename)
Example #32
0
def tree(request_id):
    """ Prints request tree, including the number of 
        coordinates for each parameter
    
    
    :param request_id: ID of the request
    """

    print colored('Tree for REQUEST: {}'.format(request_id), 'green')

    url_point_request = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    response_point_request = recastapi.get(url_point_request)

    for i, point_response in enumerate(response_point_request['_items']):

        for j in range(1):
            print "\n"

        print colored("Parameter: {}".format(i), 'green')

        url_coordinate = '{}?where=point_request_id=="{}"'.format(
            recastapi.ENDPOINTS['PARAMETER_POINTS'], point_response['id'])
        response_coordinate = recastapi.get(url_coordinate)

        for j in range(4):
            print colored("\t\t |", 'red')

        print colored(
            "\t\t - coordinates: {}".format(len(
                response_coordinate['_items'])), 'red')

        url_basic_request = '{}?where=point_request_id=="{}"'.format(
            recastapi.ENDPOINTS['BASIC_REQUESTS'], point_response['id'])
        response_basic_request = recastapi.get(url_basic_request)

        for j in range(4):
            print colored("\t\t |", 'yellow')

        print colored(
            "\t\t - archives: {}".format(len(
                response_basic_request['_items'])), 'yellow')
Example #33
0
def point_response_by_id(point_request_id):
    """ returns point response JSON given point request id. """

    url = '{}?where=point_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_RESPONSES'], point_request_id)
    response = recastapi.get(url)
    if len(response['_items']) > 1:
        raise Exception('Duplicated data! Internal Server error')
    if not response['_items']:
        raise Exception('No record found!')
    return response['_items'][0]
Example #34
0
def get_coordinate(point_request_id):
    """ Private function

    """
    coordinate_url = '{}?where=point_request_id=="{}"'.format(
        recastapi.ENDPOINTS['PARAMETER_POINTS'],
        point_request_id
    )
    
    coordinate_responses = recastapi.get(coordinate_url)
    return coordinate_responses['_items']
Example #35
0
def user(user_id=None):
    """Lists user information.
    

    :param id: Optional, if provided will return data of a single user.

    :return: JSON object containing all data that could be retrieved.    
    """
    single_user = '******'.format(user_id) if user_id else ''
    url = '{}{}'.format(recastapi.ENDPOINTS['USERS'], single_user)
    return recastapi.get(url)
Example #36
0
def point_response_by_id(point_request_id):
    """ returns point response JSON given point request id. """
    
    url = '{}?where=point_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_RESPONSES'], point_request_id)
    response = recastapi.get(url)
    if len(response['_items']) > 1:
        raise Exception('Duplicated data! Internal Server error')
    if not response['_items']:
        raise Exception('No record found!')
    return response['_items'][0]
Example #37
0
def my_subscriptions():
    """Lists your subscriptions.
    
    """
    if not recastapi.ORCID_ID:
        print "Can't list your subscriptions."
        print "Please provide an ORCID_ID and ACCESS_TOKEN"
        raise RuntimeError        
    user = recastapi.user.userData()
    user_id = user['_items'][0]['id']
    url = '{}?where=subscriber_id=="{}"'.format(recastapi.ENDPOINTS['SUBSCRIPTIONS'], user_id)
    return recastapi.get(url)
Example #38
0
def response_archive(basic_response_id = None, basic_request_id = None, filename = None):
    if basic_response_id is None:
        basic_response_id = recastapi.response.read.basic_response(basic_request_id = basic_request_id )['id']

    selection_str = '?where=basic_response_id=="{}"'.format(basic_response_id)
    url = '{}{}'.format(recastapi.ENDPOINTS['RESPONSE_ARCHIVES'], selection_str)
    response = recastapi.get(
        url
    )
    archives = response['_items']
    assert len(archives)<2
    return archives[0] if archives else None
Example #39
0
def user_data():
    """Lists user given his ORCID ID
    
    :return: JSON object
    """
    if not recastapi.ORCID_ID:
        print '-'*60
        print "No ORCID ID and ACCESS TOKEN provide"
        print "Please provide an ORCID ID"
        raise RuntimeError
    url = '{}?where=orcid_id=="{}"'.format(recastapi.ENDPOINTS['USERS'], recastapi.ORCID_ID)
    return recastapi.get(url)
Example #40
0
def response(uuid=None,
             request_id=None,
             query='?max_results=1000&page=1'):
    """ List response depending on criteria.
        If all args are None alll responses returned.

    :param uuid: Response ID to query(optional). Returns a single request with the ID
    :param request_id: Request ID of request to query(optional). Returns request
                     associated to response
    :param query: additional query 

    :return: JSON object       
    """
    
    if uuid and request_id:
        print "*"*60
        print "Cannot fetch request and uuid simultaneously"
        print "Please provide either a request ID or uuid"
        raise RuntimeError

    elif uuid:
         single_response = '/{}'.format(uuid)
         url = '{}{}'.format(recastapi.ENDPOINTS['RESPONSES'], single_response)
         response = recastapi.get(url)
         return response

    elif request_id:
        request_str = '?where=scan_request_id=="{}"'.format(request_id)
        url = '{}{}'.format(recastapi.ENDPOINTS['RESPONSES'], request_str)
        print url
        response = recastapi.get(url)
        if len(response) == 0:
            return None
        else:
            return response['_items'][0]

    else:
        url = '{}{}'.format(recastapi.ENDPOINTS['RESPONSES'], query)
        response = recastapi.get(url)
        return response
Example #41
0
def parameter(request_id, parameter_index):
    """ Gets the parameter response given an index
    
    :param request_id: ID of request
    :param parameter_index: index of the parameter
    
    :return: JSON object
    """

    # get parameter ID
    parameter_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    parameter_response = recastapi.get(parameter_url)

    if parameter_index > len(parameter_response['_items']) - 1:
        # index does not exist
        print '-' * 60
        print "Exception in user code:"
        print "\t ******* parameter index out of bounds"
        print "\n"
        raise RuntimeError

    parameter_id = parameter_response['_items'][parameter_index]['id']

    # check for parameter response

    response_url = '{}?where=point_request_id="{}"'.format(
        recastapi.ENDPOINTS['POINT_RESPONSES'], parameter_id)
    response = recastapi.get(response_url)

    if len(response['_items']) == 0:
        # No parameter response found
        print '-' * 60
        print "Exception in user code:"
        print "\t ****** No parameter response found"
        print '\n'
        raise RuntimeError

    return response['_items'][0]
Example #42
0
def user_data():
    """Lists user given his ORCID ID
    
    :return: JSON object
    """
    if not recastapi.ORCID_ID:
        print '-' * 60
        print "No ORCID ID and ACCESS TOKEN provide"
        print "Please provide an ORCID ID"
        raise RuntimeError
    url = '{}?where=orcid_id=="{}"'.format(recastapi.ENDPOINTS['USERS'],
                                           recastapi.ORCID_ID)
    return recastapi.get(url)
Example #43
0
def tree(response_id):
    """Prints response tree, nested with point and basic response.
    
    :param response_id: ID of the response
    """
    url_point_response = '{}?where=scan_response_id=="{}"'.format(
	recastapi.ENDPOINTS['POINT_RESPONSES'], 
	response_id
    )

    response_point = recastapi.get(url_point_response)
    
    for i, point_response in enumerate(response_point['_items']):
	print colored('Point response index: {} -- {}'.format(i, point_response), 'green')
	
	url_basic_response = '{}?where=point_response_id=="{}"'.format(
	    recastapi.ENDPOINTS['BASIC_RESPONSES'], point_response['id'])
        
	response_basic = recastapi.get(url_basic_response)
	for j, basic_response in enumerate(response_basic['_items']):
	    print colored('\t *Basic response index: {} -- {}'.format(
		j, basic_response), 'yellow')
Example #44
0
def parameter(request_id, parameter_index):
    """ Gets the parameter response given an index
    
    :param request_id: ID of request
    :param parameter_index: index of the parameter
    
    :return: JSON object
    """

    # get parameter ID
    parameter_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    parameter_response = recastapi.get(parameter_url)

    if parameter_index > len(parameter_response['_items'])-1:
        # index does not exist
        print '-'*60
        print "Exception in user code:"
        print "\t ******* parameter index out of bounds"
        print "\n"
        raise RuntimeError

    parameter_id = parameter_response['_items'][parameter_index]['id']

    # check for parameter response

    response_url = '{}?where=point_request_id="{}"'.format(
        recastapi.ENDPOINTS['POINT_RESPONSES'], parameter_id)
    response = recastapi.get(response_url)

    if len(response['_items']) == 0:
        # No parameter response found
        print '-'*60
        print "Exception in user code:"
        print "\t ****** No parameter response found"
        print '\n'
        raise RuntimeError
    
    return response['_items'][0]
Example #45
0
def point_response(point_response_id =  None, point_request_id = None):
    """" Returns basic JSON. """
    if (point_response_id is None) and (point_request_id is None):
        response = recastapi.get(recastapi.ENDPOINTS['POINT_RESPONSES'])
        return response['_items']

    if (point_response_id is not None) and (point_request_id is not None):
        raise RuntimeError('Cannot fetch point_response_id and point_request_id simultaneously')

    elif point_response_id is not None:
        url = '{}/{}'.format(recastapi.ENDPOINTS['POINT_RESPONSES'], point_response_id)
        response = recastapi.get(url)
        return response

    elif point_request_id is not None:
        selection_str = '?where=point_request_id=="{}"'.format(point_request_id)
        url = '{}{}'.format(recastapi.ENDPOINTS['POINT_RESPONSES'], selection_str)
        response = recastapi.get(url)
        assert(len(response['_items']) < 2)
        if not response['_items']:
            return None
        return response['_items'][0]
Example #46
0
def response(uuid=None, request_id=None, query='?max_results=1000&page=1'):
    """ List response depending on criteria.
        If all args are None alll responses returned.

    :param uuid: Response ID to query(optional). Returns a single request with the ID
    :param request_id: Request ID of request to query(optional). Returns request
                     associated to response
    :param query: additional query 

    :return: JSON object       
    """

    if uuid and request_id:
        print "*" * 60
        print "Cannot fetch request and uuid simultaneously"
        print "Please provide either a request ID or uuid"
        raise RuntimeError

    elif uuid:
        single_response = '/{}'.format(uuid)
        url = '{}{}'.format(recastapi.ENDPOINTS['RESPONSES'], single_response)
        response = recastapi.get(url)
        return response

    elif request_id:
        request_str = '?where=scan_request_id=="{}"'.format(request_id)
        url = '{}{}'.format(recastapi.ENDPOINTS['RESPONSES'], request_str)
        print url
        response = recastapi.get(url)
        if len(response) == 0:
            return None
        else:
            return response['_items'][0]

    else:
        url = '{}{}'.format(recastapi.ENDPOINTS['RESPONSES'], query)
        response = recastapi.get(url)
        return response
Example #47
0
def request_archive_for_request(basic_request_id, download_path = None, dry_run = False):
    files_url = '{}?where=basic_request_id=="{}"'.format(recastapi.ENDPOINTS['REQUEST_ARCHIVES'], basic_request_id)
    files_url_response = recastapi.get(files_url)
    links =  [request_archive(x['id'])['file_link'] for x in files_url_response['_items']]
    if len(links) > 1:
        raise RuntimeError('more than one request archive? not downloading...')
    link = links[0]
    if dry_run:
        return link
    else:
        if download_path:
            download_file(link,download_path)
        else:
            raise RuntimeError('no download path given')
Example #48
0
def tree(response_id):
    """Prints response tree, nested with point and basic response.
    
    :param response_id: ID of the response
    """
    url_point_response = '{}?where=scan_response_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_RESPONSES'], response_id)

    response_point = recastapi.get(url_point_response)

    for i, point_response in enumerate(response_point['_items']):
        print colored(
            'Point response index: {} -- {}'.format(i, point_response),
            'green')

        url_basic_response = '{}?where=point_response_id=="{}"'.format(
            recastapi.ENDPOINTS['BASIC_RESPONSES'], point_response['id'])

        response_basic = recastapi.get(url_basic_response)
        for j, basic_response in enumerate(response_basic['_items']):
            print colored(
                '\t *Basic response index: {} -- {}'.format(j, basic_response),
                'yellow')
Example #49
0
def download_all(request_id, download_path=None, dry_run=False):
    """Downloads all files associated with a given request.
    

    :param request_id: ID of the request to query.
    :param download_path: Filename of the files(still have to come up with logical)
                  naming convention. Currently, files are downloaded with their original
                  file name in the current directory
    :param dry_run: whether to download file or not

    :return: JSON object with metadata of files downloaded on disk
    """
    url_point_request = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    # Get all point requests
    response_point_request = recastapi.get(url_point_request)

    responses = []
    for response in response_point_request['_items']:

        url_basic_request = '{}?where=point_request_id=="{}"'.format(
            recastapi.ENDPOINTS['BASIC_REQUESTS'], response['id'])
        # Get all basic requests
        response_basic_request = recastapi.get(url_basic_request)

        for response_basic in response_basic_request['_items']:

            if download_path:
                """Have to set download path, otherwise files will be overwritten """
                pass

            r = download_file(response_basic['id'],
                              download_path=None,
                              dry_run=dry_run)
            responses.append(r)

    return responses
Example #50
0
def analysis(uuid = None):  
    """List analysis given uuid or all analyses.
        
    :param uuid: analysis_id.
    
    :return: JSON object containing all analyses retrieved.
    """
    
    single_analysis = '/{}'.format(uuid) if uuid else ''
    url = '{}{}'.format(recastapi.ENDPOINTS['ANALYSIS'], single_analysis)
    responses = recastapi.get(url)
    if responses.has_key('_items'):
        return responses['_items']
    else:
        return responses
Example #51
0
def analysis(analysis_id = None):
    """List analysis given uuid or all analyses.

    :param uuid: analysis_id.

    :return: JSON object containing all analyses retrieved.
    """

    single_analysis = '/{}'.format(analysis_id) if analysis_id is not None else ''
    url = '{}{}'.format(recastapi.ENDPOINTS['ANALYSIS'], single_analysis)
    responses = recastapi.get(url)
    if responses.has_key('_items'):
        return responses['_items']
    else:
        return responses
Example #52
0
def point_request_of_scan(scan_request_id, point_request_index=None):
    """Returns list of point requests or single point reqeusts
    :param scan_request_id: the request ID to query
    :param point_request_index: starting from 0
    """
    point_req_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'],
        scan_request_id
    )
    point_api_response = recastapi.get(point_req_url)

    if point_request_index is None:
        return point_api_response['_items']

    if point_request_index in range(len(point_api_response['_items'])):
        point_api_response['_items'][point_request_index]
        return point_api_response['_items'][point_request_index]
    else:
        raise RuntimeError('Pameter index out of bounds')
Example #53
0
def response(request_id, model_id=None):
    """create response.
    
    :param request_id: ID of the request

    :return: JSON object
    """
    # check if there is a response for this request
    scan_response_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['RESPONSES'], request_id)
    scan_responses = recastapi.get(scan_response_url)

    if not scan_responses['_items']:
        payload = {'scan_request_id': request_id, 'model_id': model_id}

        url = '{}/'.format(recastapi.ENDPOINTS['RESPONSES'])
        return recastapi.post(url, json=payload)
    else:
        return scan_responses['_items'][0]
Example #54
0
def parameter(request_id, parameter_index=None):
    """Returns list of parameters or single parameter

    :param request_id: the request ID to query
    :param parameter_index: starting from 0
    """

    parameters_url = '{}?where=scan_request_id=="{}"'.format(
        recastapi.ENDPOINTS['POINT_REQUESTS'], request_id)
    parameter_responses = recastapi.get(parameters_url)

    if not parameter_index and not parameter_index == 0:
        #No parameter index given, return all
        for i, parameter in enumerate(parameter_responses['_items']):
            parameter['coordinates'] = get_coordinate(parameter['id'])
            parameter['files'] = archives(request_id=request_id,
                                          parameter_index=i,
                                          basic_index=None)

        return parameter_responses['_items']

    if parameter_index >= 0 and parameter_index < len(
            parameter_responses['_items']):
        # if the parameter index is defined
        parameter_responses['_items'][parameter_index][
            'coordinates'] = get_coordinate(
                parameter_responses['_items'][parameter_index]['id'])

        parameter_responses['_items'][parameter_index]['files'] = archives(
            request_id=request_id,
            parameter_index=parameter_index,
            basic_index=None)

        return parameter_responses['_items'][parameter_index]

    else:
        #parameter index not found
        print '-' * 60
        print "Exception in user code:"
        print "\t ****** Pameter index not found"
        print '\n'
        raise RuntimeError
Example #55
0
def basic_request(basic_request_id):
    """" Returns basic JSON. """
    url = '{}/{}'.format(recastapi.ENDPOINTS['BASIC_REQUESTS'],
                         basic_request_id)
    response = recastapi.get(url)
    return response
Example #56
0
def download_file(basic_response_id, download_path=None, dry_run=False):
    """Downloads response file to user specified path.
    
    :param basic_response_id: ID of the basic response
    :param download_path: Used specified filename
    :param dry_run: whether to download file or not

    :return: JSON object
    
    """
    local_path_key_name = 'local_path'  #path of the downloaded file on local machine
    files_urls = '{}?where=basic_response_id=="{}"'.format(
        recastapi.ENDPOINTS['HISTOGRAMS'], basic_response_id)

    responses = []
    url_response = recastapi.get(files_urls)
    if len(url_response['_items']) < 2 and not len(
            url_response['_items']) == 0:
        url = ('{}/{}'.format(recastapi.ENDPOINTS['HISTOGRAMS'],
                              url_response['_items'][0]['id']))
        response = recastapi.get(url)
        link = response['file_link']
        if link:
            if not dry_run:
                zip_file = urllib.URLopener()
                zip_file.retrieve(
                    link, download_path or response['original_file_name'])
                response['file_link'] = download_path or response[
                    'original_file_name']
                print colored(
                    'Successfully downloaded file {}'.format(download_path),
                    'green')
            else:
                print colored('File link: {}'.format(response['file_link']),
                              'green')
        else:
            response[local_path_key_name] = None
            print colored(
                'Faile to download file {}'.format(
                    download_path or response['original_file_name']), 'red')
            print colored(
                '\t Please check if this request is associated with a file',
                'red')

        responses.append(response)
    else:
        for i, val in enumerate(url_response['_items']):
            url = ('{}/{}'.format(recastapi.ENDPOINTS['HISTOGRAMS'],
                                  url_response['_items'][i]['id']))
            response = recastapi.get(url)
            if not download_path:
                file_path = '{}_{}'.format(
                    response['_items'][i]['original_file_name'], str(i))
            else:
                file_path = '{}_{}'.format(download_path, str(i))

            link = response['file_link']
            if link:
                if not dry_run:
                    zip_file = urllib.URLopener()
                    zip_file.retrieve(link, file_path)
                    response[local_path_key_name] = file_path
                    print colored(
                        'Successfully downloaded file {}'.format(file_path),
                        'green')
                else:
                    response[local_path_key_name] = None
                    print colored(
                        'Failed to download file {}'.format(file_path), 'red')
                    print colored(
                        '\t Please check if this request is associated with a file',
                        'red')
                responses.append(response)
    return responses