Exemplo n.º 1
0
def _build_rendered_html(download_url, cache_path, temp_path,
                         public_download_url):
    """
    :param str download_url: The url to download the file to be rendered
    :param str cache_path: Location to cache the rendered file
    :param str temp_path: Where the downloaded file will be cached
    """

    if render_is_done_or_happening(cache_path, temp_path):
        return

    # Ensure our paths exists
    # Note: Ensures that cache directories have the same owner
    # as the files inside them
    ensure_path(os.path.split(temp_path)[0])
    ensure_path(os.path.split(cache_path)[0])

    rendered = None
    try:
        save_to_file_or_error(download_url, temp_path)
    except exceptions.RenderNotPossibleException as e:
        # Write out unavoidable errors
        rendered = e.renderable_error
    else:
        encoding = None
        # Workaround for https://github.com/CenterForOpenScience/osf.io/issues/2389
        # Open text files as utf-8
        # Don't specify an encoding for other filetypes. Otherwise filetypes
        # such as docx will break
        if get_file_extension(temp_path) in CODE_EXTENSIONS:
            encoding = 'utf-8'
        with codecs.open(temp_path, encoding=encoding) as temp_file:
            try:
                render_result = mfr.render(temp_file, src=public_download_url)
                # Rendered result
                rendered = _build_html(render_result)
            except MFRError as err:
                # Rendered MFR error
                rendered = render_mfr_error(err)

    # Cache rendered content
    with codecs.open(cache_path, 'w', 'utf-8') as render_result_cache:
        render_result_cache.write(rendered)

    # Cleanup when we're done
    os.remove(temp_path)
Exemplo n.º 2
0
def _build_rendered_html(download_url, cache_path, temp_path, public_download_url):
    """
    :param str download_url: The url to download the file to be rendered
    :param str cache_path: Location to cache the rendered file
    :param str temp_path: Where the downloaded file will be cached
    """

    if render_is_done_or_happening(cache_path, temp_path):
        return

    # Ensure our paths exists
    # Note: Ensures that cache directories have the same owner
    # as the files inside them
    ensure_path(os.path.split(temp_path)[0])
    ensure_path(os.path.split(cache_path)[0])

    rendered = None
    try:
        save_to_file_or_error(download_url, temp_path)
    except exceptions.RenderNotPossibleException as e:
        # Write out unavoidable errors
        rendered = e.renderable_error
    else:
        encoding = None
        # Workaround for https://github.com/CenterForOpenScience/osf.io/issues/2389
        # Open text files as utf-8
        # Don't specify an encoding for other filetypes. Otherwise filetypes
        # such as docx will break
        if get_file_extension(temp_path) in CODE_EXTENSIONS:
            encoding = 'utf-8'
        with codecs.open(temp_path, encoding=encoding) as temp_file:
            try:
                render_result = mfr.render(temp_file, src=public_download_url)
                # Rendered result
                rendered = _build_html(render_result)
            except MFRError as err:
                # Rendered MFR error
                rendered = render_mfr_error(err)

    # Cache rendered content
    with codecs.open(cache_path, 'w', 'utf-8') as render_result_cache:
        render_result_cache.write(rendered)

    # Cleanup when we're done
    os.remove(temp_path)
Exemplo n.º 3
0
def populate_data(fp):
    """Determine the appropriate library and use it to populate rows and columns
    :param fp: file pointer
    :return: tuple of column headers and row data
    """

    ext = get_file_extension(fp.name)
    function_preference = config['libs'].get(ext)

    for function in function_preference:
        try:
            imported = function()
            print("Trying " + imported.__name__)
            return imported(fp)
        except ImportError:
            print("Failed to import " + function.__name__)

    raise MissingRequirementsException('Renderer requirements are not met')
Exemplo n.º 4
0
 def detect(self, fp):
     return get_file_extension(fp.name) in EXTENSIONS
Exemplo n.º 5
0
 def detect(self, fp):
     return get_file_extension(fp.name) in ['.markdown', '.md', ]
Exemplo n.º 6
0
 def detect(self, fp):
     return get_file_extension(fp.name) in EXTENSIONS
def test_get_file_extension():
    assert core.get_file_extension('foo.txt') == '.txt'
    assert core.get_file_extension('foo.TXT') == '.txt'
    assert core.get_file_extension('foo/bar/baz.Mp3') == '.mp3'
    assert core.get_file_extension('foo') == ''