Advertisement
kastielspb

Static hesher

Jan 16th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. # settings/default.py
  2. STATICFILES_STORAGE = 'markup.storages.DjsManifestStaticFilesStorage'
  3.  
  4. # markup.storages.py
  5. import os
  6. from urllib.parse import urlsplit, unquote, urlunsplit
  7.  
  8. from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
  9.  
  10.  
  11. class DjsManifestStaticFilesStorage(ManifestStaticFilesStorage):
  12.     manifest_strict = False
  13.  
  14.     def hashed_name(self, name, content=None, filename=None):
  15.         # `filename` is the name of file to hash if `content` isn't given.
  16.         # `name` is the base name to construct the new hashed filename from.
  17.         parsed_name = urlsplit(unquote(name))
  18.         clean_name = parsed_name.path.strip()
  19.         if filename:
  20.             filename = urlsplit(unquote(filename)).path.strip()
  21.         filename = filename or clean_name
  22.         opened = False
  23.         if content is None:
  24.             try:
  25.                 content = self.open(filename)
  26.             except IOError:
  27.                 # Handle directory paths and fragments
  28.                 return name
  29.             opened = True
  30.         try:
  31.             file_hash = self.file_hash(clean_name, content)
  32.         finally:
  33.             if opened:
  34.                 content.close()
  35.         path, filename = os.path.split(clean_name)
  36.         root, ext = os.path.splitext(filename)
  37.         if file_hash is not None:
  38.             file_hash = ".%s" % file_hash
  39.         hashed_name = os.path.join(path, "%s%s%s" %
  40.                                    (root, file_hash, ext))
  41.         unparsed_name = list(parsed_name)
  42.         unparsed_name[2] = hashed_name
  43.         # Special casing for a @font-face hack, like url(myfont.eot?#iefix")
  44.         # http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
  45.         if '?#' in name and not unparsed_name[3]:
  46.             unparsed_name[2] += '?'
  47.         return urlunsplit(unparsed_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement