def schema_convert(hed_schema, display_name): """Return a string representation of hed_schema in the desired format Parameters ---------- hed_schema: HedSchema object A HedSchema object containing the schema to be processed. display_name: str The display name associated with this schema object. Returns ------- result: dict A dictionary in the standard results format containing the results of the operation """ schema_version = hed_schema.header_attributes.get('version', 'Unknown') schema_format = get_file_extension(display_name) if schema_format == file_constants.SCHEMA_XML_EXTENSION: data = hed_schema.get_as_mediawiki_string() extension = '.mediawiki' else: data = hed_schema.get_as_xml_string() extension = '.xml' file_name = generate_filename(display_name, extension=extension) return { 'command': base_constants.COMMAND_CONVERT, 'data': data, 'output_display_name': file_name, 'schema_version': schema_version, 'msg_category': 'success', 'msg': 'Schema was successfully converted' }
def _run_tag_compare(local_xml_path): """Runs tag compare for the given XML file. returns: A dictionary with converter.constants filled in. """ input_extension = get_file_extension(local_xml_path) if input_extension != file_extension_constants.HED_XML_EXTENSION: raise ValueError(f"Invalid extension type: {input_extension}") return duplicate_tags.check_for_duplicate_tags(local_xml_path)
def _run_conversion(hed_file_path): """Runs the appropriate xml<>mediawiki converter depending on input filetype. returns: A dictionary with converter.constants filled in. """ input_extension = get_file_extension(hed_file_path) if input_extension == file_extension_constants.HED_XML_EXTENSION: conversion_function = xml2wiki.convert_hed_xml_2_wiki elif input_extension == file_extension_constants.HED_WIKI_EXTENSION: conversion_function = wiki2xml.convert_hed_wiki_2_xml else: raise ValueError(f"Invalid extension type: {input_extension}") return conversion_function(None, hed_file_path)
def get_input_from_form(request): """Gets input argument dictionary from the spreadsheet form request. Parameters ---------- request: Request object A Request object containing user data from the spreadsheet form. Returns ------- dictionary A dictionary containing input arguments for calling the underlying spreadsheet functions """ arguments = { base_constants.SCHEMA: get_hed_schema_from_pull_down(request), base_constants.SPREADSHEET: None, base_constants.SPREADSHEET_TYPE: file_constants.TSV_EXTENSION, base_constants.WORKSHEET_NAME: request.form.get(base_constants.WORKSHEET_SELECTED, None), base_constants.COMMAND: request.form.get(base_constants.COMMAND_OPTION, ''), base_constants.HAS_COLUMN_NAMES: form_has_option(request, base_constants.HAS_COLUMN_NAMES, 'on'), base_constants.CHECK_FOR_WARNINGS: form_has_option(request, base_constants.CHECK_FOR_WARNINGS, 'on'), } tag_columns, prefix_dict = get_prefix_dict(request.form) filename = request.files[base_constants.SPREADSHEET_FILE].filename file_ext = get_file_extension(filename) if file_ext in file_constants.EXCEL_FILE_EXTENSIONS: arguments[ base_constants.SPREADSHEET_TYPE] = file_constants.EXCEL_EXTENSION spreadsheet = models.HedInput( file=request.files[base_constants.SPREADSHEET_FILE], file_type=arguments[base_constants.SPREADSHEET_TYPE], worksheet_name=arguments.get(base_constants.WORKSHEET_NAME, None), tag_columns=tag_columns, has_column_names=arguments.get(base_constants.HAS_COLUMN_NAMES, None), column_prefix_dictionary=prefix_dict, name=filename) arguments[base_constants.SPREADSHEET] = spreadsheet return arguments
def save_hed_to_upload_folder(hed_file_object): """Save an spreadsheet other to the upload folder. Parameters ---------- hed_file_object: File object A other object that points to a HED XML other that was first saved in a temporary location. Returns ------- string The path to the HED XML other that was saved to the upload folder. """ hed_file_extension = get_file_extension(hed_file_object.filename) hed_file_path = _save_file_to_upload_folder(hed_file_object, hed_file_extension) return hed_file_path
def report_eeg_events_validation_status(request): """Reports validation status of hed strings associated with EEG events received from EEGLAB plugin HEDTools Parameters ---------- request: Request object A Request object containing user data submitted by HEDTools. Keys include "hed_strings", "check_for_warnings", and "hed_xml_file" Returns ------- string A serialized JSON string containing information related to the hed strings validation result. If the validation fails then a 500 error message is returned. """ validation_status = {} # Parse uploaded data form_data = request.form check_for_warnings = form_data["check_for_warnings"] == '1' if "check_for_warnings" in form_data else False # if hed_xml_file was submitted, it's accessed by request.files, otherwise empty if "hed_xml_file" in request.files and get_file_extension(request.files["hed_xml_file"].filename) == "xml": hed_xml_file = _save_hed_to_upload_folder_if_present(request.files["hed_xml_file"]) else: hed_xml_file = '' try: # parse hed_strings from json hed_strings = json.loads(form_data["hed_strings"]) # hed_strings is a list of HED strings associated with events in EEG.event (order preserved) hed_input_reader = HedValidator(hed_strings, check_for_warnings=check_for_warnings, hed_xml_file=hed_xml_file) # issues is a list of lists. Element list is empty if no error, # else is a list of dictionaries, each dictionary contains an error-message key-value pair issues = hed_input_reader.get_validation_issues() # Prepare response validation_status["issues"] = issues except: validation_status[error_constants.ERROR_KEY] = traceback.format_exc() finally: delete_file_if_it_exist(hed_xml_file) return validation_status
def _get_column_delimiter_based_on_file_extension(file_name_or_path): """Gets the spreadsheet column delimiter based on the other extension. Parameters ---------- file_name_or_path: string A other name or path. Returns ------- string The spreadsheet column delimiter based on the other extension. """ column_delimiter = '' file_extension = get_file_extension(file_name_or_path) if file_extension in spreadsheet_constants.SPREADSHEET_FILE_EXTENSION_TO_DELIMITER_DICTIONARY: column_delimiter = spreadsheet_constants.SPREADSHEET_FILE_EXTENSION_TO_DELIMITER_DICTIONARY.get(file_extension) return column_delimiter
def test_get_file_extension(self): expected_extension = '.xml' file_extension = file_util.get_file_extension(self.hed_xml_file) self.assertTrue(file_extension) self.assertEqual(expected_extension, file_extension)