MCPcopy
hub / github.com/django/django / converter

Method converter

django/contrib/staticfiles/storage.py:245–312  ·  view source on GitHub ↗

Convert the matched URL to a normalized and hashed URL. This requires figuring out which files the matched URL resolves to and calling the url() method of the storage.

(matchobj)

Source from the content-addressed store, hash-verified

243 return msg
244
245 def converter(matchobj):
246 """
247 Convert the matched URL to a normalized and hashed URL.
248
249 This requires figuring out which files the matched URL resolves
250 to and calling the url() method of the storage.
251 """
252 matches = matchobj.groupdict()
253 matched = matches["matched"]
254 url = matches["url"]
255
256 # Ignore URLs in comments.
257 if comment_blocks and self.is_in_comment(matchobj.start(), comment_blocks):
258 return matched
259
260 # Ignore absolute/protocol-relative and data-uri URLs.
261 if re.match(r"^[a-z]+:", url) or url.startswith("//"):
262 return matched
263
264 # Ignore absolute URLs that don't point to a static file (dynamic
265 # CSS / JS?). Note that STATIC_URL cannot be empty.
266 if url.startswith("/") and not url.startswith(settings.STATIC_URL):
267 return matched
268
269 # Strip off the fragment so a path-like fragment won't interfere.
270 url_path, fragment = urldefrag(url)
271
272 # Ignore URLs without a path
273 if not url_path:
274 return matched
275
276 if url_path.startswith("/"):
277 # Otherwise the condition above would have returned
278 # prematurely.
279 assert url_path.startswith(settings.STATIC_URL)
280 target_name = url_path.removeprefix(settings.STATIC_URL)
281 else:
282 # We're using the posixpath module to mix paths and URLs
283 # conveniently.
284 source_name = name if os.sep == "/" else name.replace(os.sep, "/")
285 target_name = posixpath.join(posixpath.dirname(source_name), url_path)
286
287 # Determine the hashed name of the target file with the storage
288 # backend.
289 try:
290 hashed_url = self._url(
291 self._stored_name,
292 unquote(target_name),
293 force=True,
294 hashed_files=hashed_files,
295 )
296 except ValueError as exc:
297 line = _line_at_position(matchobj.string, matchobj.start())
298 note = f"{name!r} contains this reference {matched!r} on line {line}"
299 exc.add_note(note)
300 raise exc
301
302 transformed_url = "/".join(

Callers

nothing calls this directly

Calls 6

is_in_commentMethod · 0.95
_urlMethod · 0.95
unquoteFunction · 0.85
matchMethod · 0.45
joinMethod · 0.45
splitMethod · 0.45

Tested by

no test coverage detected