Ejemplo n.º 1
0
def convert_hed_xml_2_wiki(hed_xml_url, local_xml_file=None):
    """Converts the local HED xml file into a wikimedia file

    Parameters
    ----------
        hed_xml_url: string
            url pointing to the .xml file to use
        local_xml_file: string
            filepath to local xml hed schema(overrides hed_xml_url)
    Returns
    -------
    dictionary
        Contains xml, wiki, and version info.
    """
    if local_xml_file is None:
        local_xml_file = file_util.url_to_file(hed_xml_url)

    try:
        hed_xml_tree = parse(local_xml_file)
    except lxml.etree.XMLSyntaxError as e:
        raise SchemaError(e.msg)
    hed_xml_tree = hed_xml_tree.getroot()
    xml2wiki = HEDXml2Wiki()
    output_strings = xml2wiki.process_tree(hed_xml_tree)
    local_mediawiki_file = file_util.write_strings_to_file(output_strings, ".mediawiki")
    hed_version = file_util.get_version_from_xml(hed_xml_tree)
    hed_info_dictionary = {constants.HED_XML_TREE_KEY: hed_xml_tree,
                           constants.HED_XML_VERSION_KEY: hed_version,
                           constants.HED_CHANGE_LOG_KEY: None,
                           constants.HED_WIKI_PAGE_KEY: None,
                           constants.HED_OUTPUT_LOCATION_KEY: local_mediawiki_file,
                           constants.HED_INPUT_LOCATION_KEY: local_xml_file}
    return hed_info_dictionary
Ejemplo n.º 2
0
def convert_hed_wiki_2_xml(hed_wiki_url, local_wiki_file=None):
    """Converts the HED wiki into a XML file.

    Parameters
    ----------
        hed_wiki_url: string
            url pointing to the .mediawiki file to use
        use_local_wiki_file: string
            local wiki file to use(overrides hed_wiki_url)
    Returns
    -------
    dictionary
        Contains xml, wiki, and version info.
    """
    if local_wiki_file is None:
        local_wiki_file = file_util.url_to_file(hed_wiki_url)

    hed_xml_file_location, hed_xml_tree = _create_hed_xml_file(local_wiki_file)
    hed_change_log = parsewiki.get_hed_change_log(local_wiki_file)
    hed_version = file_util.get_version_from_xml(hed_xml_tree)
    hed_info_dictionary = {
        constants.HED_XML_TREE_KEY: hed_xml_tree,
        constants.HED_XML_VERSION_KEY: hed_version,
        constants.HED_CHANGE_LOG_KEY: hed_change_log,
        constants.HED_WIKI_PAGE_KEY: hed_wiki_url,
        constants.HED_INPUT_LOCATION_KEY: local_wiki_file,
        constants.HED_OUTPUT_LOCATION_KEY: hed_xml_file_location
    }
    return hed_info_dictionary
Ejemplo n.º 3
0
def cache_specific_url(hed_xml_url,
                       xml_version_number=None,
                       library_name=None,
                       cache_folder=None):
    """Cache a file from a URL.

    Parameters
    ----------
    hed_xml_url: str
        Path to an exact file at a URL, or a GitHub API url to a directory.
    xml_version_number: str
        If not None and hed_xml_url is a directory, return this version or None.
    library_name: str or None, optional
        The schema library name
    cache_folder:
        hed cache folder: Defaults to HED_CACHE_DIRECTORY
    Returns
    -------
    str
        Path to local hed XML file to use.
    """
    if not cache_folder:
        cache_folder = HED_CACHE_DIRECTORY

    if not _check_if_url(hed_xml_url):
        return None

    if _check_if_api_url(hed_xml_url):
        return _download_latest_hed_xml_version_from_url(
            hed_xml_url,
            xml_version_number=xml_version_number,
            library_name=library_name,
            cache_folder=cache_folder)

    if not _check_if_specific_xml(hed_xml_url):
        return None

    filename = hed_xml_url.split('/')[-1]
    cache_filename = os.path.join(cache_folder, filename)

    os.makedirs(cache_folder, exist_ok=True)
    temp_hed_xml_file = url_to_file(hed_xml_url)
    if temp_hed_xml_file:
        cache_filename = _safe_copy_tmp_to_folder(temp_hed_xml_file,
                                                  cache_filename)
        return cache_filename
    else:
        return None
Ejemplo n.º 4
0
def _cache_hed_version(version, version_info, cache_folder):
    sha_hash, download_url = version_info

    possible_cache_filename = _create_xml_filename(version, cache_folder)
    local_sha_hash = _calculate_sha1(possible_cache_filename)

    if sha_hash == local_sha_hash:
        return possible_cache_filename

    os.makedirs(cache_folder, exist_ok=True)
    temp_hed_xml_file = url_to_file(download_url)
    if temp_hed_xml_file:
        cache_filename = _safe_copy_tmp_to_folder(temp_hed_xml_file,
                                                  possible_cache_filename)
        return cache_filename
    else:
        return None
Ejemplo n.º 5
0
def _get_uploaded_file_paths_from_forms(form_request_object):
    """Gets the other paths of the uploaded files in the form.

    Parameters
    ----------
    form_request_object: Request object
        A Request object containing user data from the conversion form.

    Returns
    -------
    string
        The local file path, if exists.
    """
    hed_file_path = ''
    if hed_present_in_form(form_request_object) and _file_has_valid_extension(
            form_request_object.files[js_form_constants.HED_FILE],
            file_extension_constants.HED_FILE_EXTENSIONS):
        hed_file_path = _save_hed_to_upload_folder_if_present(
            form_request_object.files[js_form_constants.HED_FILE])
    elif url_present_in_form(form_request_object):
        hed_file_path = url_to_file(
            form_request_object.values[js_form_constants.HED_URL])
    return hed_file_path
Ejemplo n.º 6
0
 def test_url_to_file(self):
     downloaded_file = file_util.url_to_file(self.default_test_url)
     self.assertTrue(downloaded_file)
     file_util.delete_file_if_it_exist(downloaded_file)