def get_filename(self, basename): """ Returns full path to a file, for example: get_filename('css/one.css') -> '/full/path/to/static/css/one.css' """ filename = None # First try finding the file using the storage class. # This is skipped in DEBUG mode as files might be outdated in # compressor's final destination (COMPRESS_ROOT) during development if not settings.DEBUG: try: # call path first so remote storages don't make it to exists, # which would cause network I/O filename = self.storage.path(basename) if not self.storage.exists(basename): filename = None except NotImplementedError: # remote storages don't implement path, access the file locally if compressor_file_storage.exists(basename): filename = compressor_file_storage.path(basename) # secondly try to find it with staticfiles if not filename and self.finders: filename = self.finders.find(url2pathname(basename)) if filename: return filename # or just raise an exception as the last resort raise UncompressableFileError( "'%s' could not be found in the COMPRESS_ROOT '%s'%s" % (basename, settings.COMPRESS_ROOT, self.finders and " or with staticfiles." or "."))
def get_filecontent(self, filename, charset): """ Reads file contents using given `charset` and returns it as text. """ if charset == 'utf-8': # Removes BOM charset = 'utf-8-sig' with codecs.open(filename, 'r', charset) as fd: try: return fd.read() except IOError as e: raise UncompressableFileError("IOError while processing " "'%s': %s" % (filename, e)) except UnicodeDecodeError as e: raise UncompressableFileError("UnicodeDecodeError while " "processing '%s' with " "charset %s: %s" % (filename, charset, e))
def get_basename(self, url): try: base_url = self.storage.base_url except AttributeError: base_url = settings.COMPRESS_URL if not url.startswith(base_url): raise UncompressableFileError( "'%s' isn't accesible via COMPRESS_URL ('%s') and can't be" " compressed" % (url, base_url)) basename = url.replace(base_url, "", 1) # drop the querystring, which is used for non-compressed cache-busting. return basename.split("?", 1)[0]
def get_filename(self, basename): # first try to find it with staticfiles (in debug mode) filename = None if settings.DEBUG and self.finders: filename = self.finders.find(basename) # secondly try finding the file in the root elif self.storage.exists(basename): filename = self.storage.path(basename) if filename: return filename # or just raise an exception as the last resort raise UncompressableFileError( "'%s' could not be found in the COMPRESS_ROOT '%s'%s" % (basename, settings.COMPRESS_ROOT, self.finders and " or with staticfiles." or "."))
def get_basename(self, url): """ Takes full path to a static file (eg. "/static/css/style.css") and returns path with storage's base url removed (eg. "css/style.css"). """ try: base_url = self.storage.base_url except AttributeError: base_url = settings.COMPRESS_URL if not url.startswith(base_url): raise UncompressableFileError("'%s' isn't accessible via " "COMPRESS_URL ('%s') and can't be " "compressed" % (url, base_url)) basename = url.replace(base_url, "", 1) # drop the querystring, which is used for non-compressed cache-busting. return basename.split("?", 1)[0]
def hunks(self): for kind, value, basename, elem in self.split_contents(): if kind == SOURCE_HUNK: content = self.filter(value, METHOD_INPUT, elem=elem, kind=kind, basename=basename) yield smart_unicode(content) elif kind == SOURCE_FILE: content = "" fd = open(value, 'rb') try: content = fd.read() except IOError, e: raise UncompressableFileError( "IOError while processing '%s': %s" % (value, e)) finally: fd.close()
def hunks(self): for kind, value, elem in self.split_contents(): if kind == "hunk": # Let's cast BeautifulSoup element to unicode here since # it will try to encode using ascii internally later yield unicode( self.filter(value, method="input", elem=elem, kind=kind)) elif kind == "file": content = "" fd = open(value, 'rb') try: content = fd.read() except IOError, e: raise UncompressableFileError( "IOError while processing '%s': %s" % (value, e)) finally: fd.close()
def hunks(self): for kind, value, basename, elem in self.split_contents(): if kind == "hunk": content = self.filter(value, "input", elem=elem, kind=kind, basename=basename) yield unicode(content) elif kind == "file": content = "" fd = open(value, 'rb') try: content = fd.read() except IOError, e: raise UncompressableFileError( "IOError while processing '%s': %s" % (value, e)) finally: fd.close()
def get_filename(self, basename): filename = None # first try finding the file in the root if self.storage.exists(basename): try: filename = self.storage.path(basename) except NotImplementedError: # remote storages don't implement path, access the file locally filename = compressor_file_storage.path(basename) # secondly try to find it with staticfiles (in debug mode) elif self.finders: filename = self.finders.find(urllib.url2pathname(basename)) if filename: return filename # or just raise an exception as the last resort raise UncompressableFileError( "'%s' could not be found in the COMPRESS_ROOT '%s'%s" % (basename, settings.COMPRESS_ROOT, self.finders and " or with staticfiles." or "."))
def hunks(self): for kind, value, elem in self.split_contents(): attribs = self.parser.elem_attribs(elem) if kind == "hunk": # Let's cast BeautifulSoup element to unicode here since # it will try to encode using ascii internally later yield unicode(self.filter(value, "input", elem=elem)) elif kind == "file": content = "" try: fd = open(value, 'rb') try: content = fd.read() finally: fd.close() except IOError, e: raise UncompressableFileError( "IOError while processing '%s': %s" % (value, e)) content = self.filter(content, "input", filename=value, elem=elem) yield unicode(content, attribs.get("charset", self.charset))
def get_basename(self, url): """ Takes full path to a static file (eg. "/static/css/style.css") and returns path with storage's base url removed (eg. "css/style.css"). """ try: base_url = self.storage.base_url except AttributeError: base_url = settings.COMPRESS_URL # Cast ``base_url`` to a string to allow it to be # a string-alike object to e.g. add ``SCRIPT_NAME`` # WSGI param as a *path prefix* to the output URL. # See https://code.djangoproject.com/ticket/25598. base_url = str(base_url) if not url.startswith(base_url): raise UncompressableFileError("'%s' isn't accessible via " "COMPRESS_URL ('%s') and can't be " "compressed" % (url, base_url)) basename = url.replace(base_url, "", 1) # drop the querystring, which is used for non-compressed cache-busting. return basename.split("?", 1)[0]