Ejemplo n.º 1
0
def _old_build_rendered_html(file_path, cache_dir, cache_file_name, download_url):
    """
    :param str file_path: Full path to raw file on disk
    :param str cache_dir: Folder to store cached file in
    :param str cache_file_name: Name of cached file
    :param str download_url: External download URL
    """
    with codecs.open(file_path) as file_pointer:

        # Build path to cached content
        # Note: Ensures that cache directories have the same owner as the files
        # inside them
        ensure_path(cache_dir)
        cache_file_path = os.path.join(cache_dir, cache_file_name)

        with codecs.open(cache_file_path, 'w', 'utf-8') as write_file_pointer:
            # Render file
            try:
                render_result = mfr.render(file_pointer, src=download_url)
            except MFRError as err:
                rendered = render_mfr_error(err).format(download_path=download_url)
            else:
                rendered = _build_html(render_result)

            # Cache rendered content
            write_file_pointer.write(rendered)

    os.remove(file_path)
    return True
Ejemplo n.º 2
0
def _build_rendered_html(file_path, cache_dir, cache_file_name, download_url):
    """

    :param str file_path: Full path to raw file on disk
    :param str cache_dir: Folder to store cached file in
    :param str cache_file_name: Name of cached file
    :param str download_url: External download URL
    """
    file_pointer = codecs.open(file_path)

    # Build path to cached content
    # Note: Ensures that cache directories have the same owner as the files
    # inside them
    ensure_path(cache_dir)
    cache_file_path = os.path.join(cache_dir, cache_file_name)

    # Render file
    try:
        rendered = mfr.render(file_pointer, url=download_url)
    except MFRError as err:
        rendered = render_mfr_error(err).format(download_path=download_url)

    # Close read pointer
    file_pointer.close()

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

    os.remove(file_path)
 def test_render_return_type(self):
     for filename in os.listdir(self.FILES_DIR):
         fp = open(os.path.join(self.FILES_DIR, filename))
         renderer = mfr.detect(fp, many=False)
         if renderer:
             # src = url_for('render.serve_file', filename=filename)
             result = mfr.render(fp)
             assert type(result) == mfr.RenderResult
Ejemplo 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:
        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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
def _build_rendered_html(file_name, file_content, cache_dir, cache_file_name,
                         download_path):
    """

    :param str file_name:
    :param str file_content:
    :param str cache_dir:
    :param str cache_file_name:
    :param str download_path:

    """

    # Open file pointer if no content provided
    if file_content is None:
        file_pointer = codecs.open(file_name)
    # Else create temporary file with content
    else:
        file_pointer = tempfile.NamedTemporaryFile(
            suffix=os.path.splitext(file_name)[1],
        )
        file_pointer.write(file_content)
        file_pointer.seek(0)

    # Build path to cached content
    # Note: Ensures that cache directories have the same owner as the files
    # inside them
    ensure_path(cache_dir)
    cache_file_path = os.path.join(cache_dir, cache_file_name)

    # Render file
    try:
        rendered = mfr.render(file_pointer, url=download_path)
    except MFRError as err:
        rendered = render_mfr_error(err).format(download_path=download_path)

    # Close read pointer
    file_pointer.close()

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

    return True
Ejemplo n.º 7
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)
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
def render(filename, renderer_name=None):
    """Make a file viewable in HTML

    :param filename: file to be rendered
    :param renderer_name: optional name of the specific renderer module
    :return: html representation of the file
    """

    try:
        fp = open(os.path.join(current_app.config['FILES_DIR'], filename))
    except IOError as err:
        flash(err, 'error')
        abort(404)

    renderer = None
    if renderer_name is None:
        renderer = mfr.detect(fp, many=False)  # return the first valid filehandler
    else:
        handlers = get_registry()

        for available_handler in handlers:
            if mfr.get_namespace(available_handler) == renderer_name:
                renderer = available_handler()
                break
        if renderer is None:
            raise IOError('Could not load a matching renderer')

    src = url_for('render.serve_file', filename=filename)

    if renderer is None:
        return ("A matching renderer cannot be found", 400)

    return render_template(
        "main/view_file.html",
        render_result=mfr.render(fp, handler=renderer, src=src)
    )