<commit_before>import os
import uuid
import asyncio
import logging

import waterbutler.core.streams
import waterbutler.core.exceptions

from mfr.server import settings
from mfr.core import utils as utils
from mfr.server.handlers import core

logger = logging.getLogger(__name__)


class RenderHandler(core.BaseHandler):

    ALLOWED_METHODS = ['GET']

    async def prepare(self):
        if self.request.method not in self.ALLOWED_METHODS:
            return

        await super().prepare()

        self.cache_file_path = await self.cache_provider.validate_path('/render/' + self.metadata.unique_key)
        self.source_file_path = await self.local_cache_provider.validate_path('/render/' + str(uuid.uuid4()))

    async def get(self):
        """Return HTML that will display the given file."""
        renderer = utils.make_renderer(
            self.metadata.ext,
            self.metadata,
            self.source_file_path.full_path,
            self.url,
            '{}://{}/assets'.format(self.request.protocol, self.request.host),
            self.request.uri.replace('/render?', '/export?', 1)
        )

        if renderer.cache_result and settings.CACHE_ENABLED:
            try:
                cached_stream = await self.cache_provider.download(self.cache_file_path)
            except waterbutler.core.exceptions.DownloadError as e:
                assert e.code == 404, 'Non-404 DownloadError {!r}'.format(e)
                logger.info('No cached file found; Starting render [{}]'.format(self.cache_file_path))
            else:
                logger.info('Cached file found; Sending downstream [{}]'.format(self.cache_file_path))
                return await self.write_stream(cached_stream)

        if renderer.file_required:
            await self.local_cache_provider.upload(
                await self.provider.download(),
                self.source_file_path
            )

        loop = asyncio.get_event_loop()
        rendition = await loop.run_in_executor(None, renderer.render)

        # Spin off upload into non-blocking operation
        if renderer.cache_result and settings.CACHE_ENABLED:
            asyncio.ensure_future(
                self.cache_provider.upload(
                    waterbutler.core.streams.StringStream(rendition),
                    self.cache_file_path
                )
            )

        await self.write_stream(waterbutler.core.streams.StringStream(rendition))

    async def _cache_and_clean(self):
        try:
            os.remove(self.source_file_path.full_path)
        except FileNotFoundError:
            pass<commit_msg>don't try to clean render source unless present<commit_after>import os
import uuid
import asyncio
import logging

import waterbutler.core.streams
import waterbutler.core.exceptions

from mfr.server import settings
from mfr.core import utils as utils
from mfr.server.handlers import core

logger = logging.getLogger(__name__)


class RenderHandler(core.BaseHandler):

    ALLOWED_METHODS = ['GET']

    async def prepare(self):
        if self.request.method not in self.ALLOWED_METHODS:
            return

        await super().prepare()

        self.cache_file_path = await self.cache_provider.validate_path('/render/' + self.metadata.unique_key)
        self.source_file_path = await self.local_cache_provider.validate_path('/render/' + str(uuid.uuid4()))

    async def get(self):
        """Return HTML that will display the given file."""
        renderer = utils.make_renderer(
            self.metadata.ext,
            self.metadata,
            self.source_file_path.full_path,
            self.url,
            '{}://{}/assets'.format(self.request.protocol, self.request.host),
            self.request.uri.replace('/render?', '/export?', 1)
        )

        if renderer.cache_result and settings.CACHE_ENABLED:
            try:
                cached_stream = await self.cache_provider.download(self.cache_file_path)
            except waterbutler.core.exceptions.DownloadError as e:
                assert e.code == 404, 'Non-404 DownloadError {!r}'.format(e)
                logger.info('No cached file found; Starting render [{}]'.format(self.cache_file_path))
            else:
                logger.info('Cached file found; Sending downstream [{}]'.format(self.cache_file_path))
                return await self.write_stream(cached_stream)

        if renderer.file_required:
            await self.local_cache_provider.upload(
                await self.provider.download(),
                self.source_file_path
            )

        loop = asyncio.get_event_loop()
        rendition = await loop.run_in_executor(None, renderer.render)

        # Spin off upload into non-blocking operation
        if renderer.cache_result and settings.CACHE_ENABLED:
            asyncio.ensure_future(
                self.cache_provider.upload(
                    waterbutler.core.streams.StringStream(rendition),
                    self.cache_file_path
                )
            )

        await self.write_stream(waterbutler.core.streams.StringStream(rendition))

    async def _cache_and_clean(self):
        if hasattr(self, 'source_file_path'):
            try:
                os.remove(self.source_file_path.full_path)
            except FileNotFoundError:
                pass
{'fraction_non_alphanumeric': 0.09215981907274784, 'fraction_numerical': 0.003015454202789295, 'mean_word_length': 2.2942271880819365, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5911674', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1447, 'n_words': 299}