Beispiel #1
0
    def _add(self, fullpath, mtime, content, minify_html=True):
        """
        Add given content to the cache and store the content as the given
        full path.
        """
        # create intermediate folders if required
        path = os.path.dirname(fullpath)
        if not os.path.exists(path):
            os.makedirs(path)

        # minify content
        if minify_html:
            try:
                content = html_minify(content)

                # Fix for Source Element
                # W3 Spec: "The source element is a void element. A source element must have a start tag but must not have an end tag."
                # https://github.com/cobrateam/django-htmlmin/issues/37
                content = content.replace('</source>', '')
            except:
                pass

        # write content
        with codecs.open(fullpath, 'w', 'utf-8') as f:
            f.write(content)

        # update file's modification time to the given one
        file_set_mtime(fullpath, mtime)
Beispiel #2
0
    def test_should_set_file_mod_time(self):
        tmp = tempfile.gettempdir()
        filename = os.path.join(tmp, 'mtime_test')
        file_put_contents(filename, 'test')
        ts = datetime(2016, 10, 8, 13, 40, 13)
        file_set_mtime(filename, ts)

        ts_read = file_get_mtime(filename)
        self.assertEqual(ts, ts_read)
Beispiel #3
0
 def test_get_mtime_should_return_last_mod_time_of_file(self):
     base = tempfile.gettempdir()
     filename = os.path.join(base, 'foo')
     file_put_contents(filename, 'test')
     try:
         ts = datetime(2016, 11, 18)
         file_set_mtime(filename, ts)
         self.assertEqual(ts, self.cache.get_mtime(filename))
     finally:
         if os.path.isfile(filename):
             os.remove(filename)
Beispiel #4
0
    def _download_font_file(self, url, path, mtime):
        """
        Download the given font file to the given local path.
        """
        # download file
        try:
            response = requests.get(url, timeout=3000)
            if response == None: return False
            if response.status_code != 200: return False
        except ConnectionError: # pragma: no cover
            # hard fail in debug
            if settings.DEBUG:
                raise

            return False

        # write file to disk
        with open(path, 'wb') as f:
            f.write(response.content)

        # update file's modification time
        file_set_mtime(path, mtime)

        return True