def schema_version_results(): """Finds the information about the HED version of a file and returns as JSON. Parameters ---------- Returns ------- string A serialized JSON string containing information related to the spreadsheet columns. """ try: hed_info = {} if base_constants.SCHEMA_PATH in request.files: f = request.files[base_constants.SCHEMA_PATH] hed_schema = hedschema.from_string( f.stream.read(file_constants.BYTE_LIMIT).decode('ascii'), file_type=secure_filename(f.filename)) hed_info[base_constants. SCHEMA_VERSION] = hed_schema.header_attributes['version'] return json.dumps(hed_info) except Exception as ex: return handle_error(ex)
def test_handle_error(self): from hed.errors.exceptions import HedFileError, HedExceptions from hedweb.web_util import handle_error ex = HedFileError(HedExceptions.BAD_PARAMETERS, "This had bad parameters", 'my.file') output = handle_error(ex) self.assertIsInstance( output, str, "handle_error should return a string if return_as_str") output1 = handle_error(ex, return_as_str=False) self.assertIsInstance( output1, dict, "handle_error should return a dict if not return_as_str") self.assertTrue('message' in output1, "handle_error dict should have a message") output2 = handle_error(ex, {'mykey': 'blech'}, return_as_str=False) self.assertTrue('mykey' in output2, "handle_error dict should include passed dictionary")
def services_results(): """Perform the requested web service and return the results in JSON. Returns ------- string A serialized JSON string containing processed information. """ response = {} try: arguments = services.get_input_from_request(request) response = services.process(arguments) return json.dumps(response) except Exception as ex: errors = handle_error(ex) response['error_type'] = errors['error_type'] response['error_msg'] = errors['error_msg'] return handle_error(ex)
def columns_info_results(): """Gets the names of the spreadsheet columns and sheet_name names if any. Returns ------- string A serialized JSON string containing information related to the column and sheet_name information. """ try: columns_info = get_columns_request(request) return json.dumps(columns_info) except Exception as ex: return handle_error(ex)
def string_results(): """Process hed strings entered in a text box. Returns ------- A serialized JSON string """ try: input_arguments = strings.get_input_from_form(request) a = strings.process(input_arguments) return json.dumps(a) except Exception as ex: return handle_error(ex)
def schema_versions_results(): """Gets a list of hed versions from the hed_cache and returns as a serialized JSON string Returns ------- string A serialized JSON string containing a list of the HED versions. """ try: hedschema.cache_all_hed_xml_versions() hed_info = { base_constants.SCHEMA_VERSION_LIST: hedschema.get_all_hed_versions() } return json.dumps(hed_info) except Exception as ex: return handle_error(ex)