Esempio 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)
Esempio 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)
Esempio n. 3
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:
        with codecs.open(temp_path) as temp_file:
            # Try to render 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)
Esempio n. 4
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:
        with codecs.open(temp_path) as temp_file:
            # Try to render 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)
Esempio n. 5
0
    def test_temp_newish(self, mockmtime, mock_isfile, mock_remove):
        mockmtime.return_value = time.time()
        mock_isfile.side_effect = [False, True]

        assert_true(core.render_is_done_or_happening('path', 'path'))
Esempio n. 6
0
    def test_old_temp(self, mockmtime, mock_isfile, mock_remove):
        mockmtime.return_value = 0
        mock_isfile.side_effect = [False, True]

        assert_false(core.render_is_done_or_happening('path', 'path'))
Esempio n. 7
0
 def test_nothing_exists(self, mock_isfile):
     mock_isfile.return_value = False
     assert_false(core.render_is_done_or_happening('path', 'path'))
Esempio n. 8
0
 def test_cache_exists_is_true(self, mock_isfile):
     mock_isfile.return_value = True
     assert_true(core.render_is_done_or_happening('path', 'path'))
Esempio n. 9
0
    def test_temp_newish(self, mockmtime, mock_isfile, mock_remove):
        mockmtime.return_value = time.time()
        mock_isfile.side_effect = [False, True]

        assert_true(core.render_is_done_or_happening('path', 'path'))
Esempio n. 10
0
    def test_old_temp(self, mockmtime, mock_isfile, mock_remove):
        mockmtime.return_value = 0
        mock_isfile.side_effect = [False, True]

        assert_false(core.render_is_done_or_happening('path', 'path'))
Esempio n. 11
0
 def test_nothing_exists(self, mock_isfile):
     mock_isfile.return_value = False
     assert_false(core.render_is_done_or_happening('path', 'path'))
Esempio n. 12
0
 def test_cache_exists_is_true(self, mock_isfile):
     mock_isfile.return_value = True
     assert_true(core.render_is_done_or_happening('path', 'path'))