MCPcopy
hub / github.com/django/django / _post_process

Method _post_process

django/contrib/staticfiles/storage.py:380–461  ·  view source on GitHub ↗
(self, paths, adjustable_paths, hashed_files)

Source from the content-addressed store, hash-verified

378 yield from processed_adjustable_paths.values()
379
380 def _post_process(self, paths, adjustable_paths, hashed_files):
381 # Sort the files by directory level
382 def path_level(name):
383 return len(name.split(os.sep))
384
385 for name in sorted(paths, key=path_level, reverse=True):
386 substitutions = True
387 # use the original, local file, not the copied-but-unprocessed
388 # file, which might be somewhere far away, like S3
389 storage, path = paths[name]
390 with storage.open(path) as original_file:
391 cleaned_name = self.clean_name(name)
392 hash_key = self.hash_key(cleaned_name)
393
394 # generate the hash with the original content, even for
395 # adjustable files.
396 if hash_key not in hashed_files:
397 hashed_name = self.hashed_name(name, original_file)
398 else:
399 hashed_name = hashed_files[hash_key]
400
401 # then get the original's file content..
402 if hasattr(original_file, "seek"):
403 original_file.seek(0)
404
405 hashed_file_exists = self.exists(hashed_name)
406 processed = False
407
408 # ..to apply each replacement pattern to the content
409 if name in adjustable_paths:
410 old_hashed_name = hashed_name
411 try:
412 content = original_file.read().decode("utf-8")
413 except UnicodeDecodeError as exc:
414 yield name, None, exc, False
415 for extension, patterns in self._patterns.items():
416 if matches_patterns(path, (extension,)):
417 for pattern, template in patterns:
418 converter = self.url_converter(
419 name,
420 hashed_files,
421 template,
422 self.get_comment_blocks(
423 content,
424 include_line_comments=path.endswith(".js"),
425 ),
426 )
427 try:
428 content = pattern.sub(converter, content)
429 except ValueError as exc:
430 yield name, None, exc, False
431 if hashed_file_exists:
432 self.delete(hashed_name)
433 # then save the processed result
434 content_file = ContentFile(content.encode())
435 if self.keep_intermediate_files:
436 # Save intermediate file for reference
437 self._save(hashed_name, content_file)

Callers 1

post_processMethod · 0.95

Calls 15

clean_nameMethod · 0.95
hash_keyMethod · 0.95
hashed_nameMethod · 0.95
url_converterMethod · 0.95
get_comment_blocksMethod · 0.95
matches_patternsFunction · 0.90
ContentFileClass · 0.90
seekMethod · 0.80
openMethod · 0.45
existsMethod · 0.45
decodeMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected