<commit_before>import re
from functools import lru_cache
from pathlib import Path

from django import template
from django.conf import settings

register = template.Library()


@lru_cache(None)
def path_lookup():
    """
        This function caches every path in the Rails public/ directory (settings.WHITENOISE_ROOT) and returns a
        dictionary mapping the relative paths to the URLs that should be included in templates, stripping out
        hexadecimal hashes from the paths if necessary. This allows the rails_static tag to use
        {% rails_static "packs/css/main.css" %} in the template to include "/packs/css/main-a4a68e68.css" in the HTML.

        Given this set of files in public/ :

        >>> monkeypatch = getfixture('monkeypatch')
        >>> monkeypatch.setattr(Path, 'glob', lambda *args, **kwargs: [
        ...     settings.WHITENOISE_ROOT+'/css/main-a4a68e68.css',
        ...     settings.WHITENOISE_ROOT+'/main.css',
        ...     settings.WHITENOISE_ROOT+'/main-cafe0001.css',
        ... ])

        Return this lookup table:

        >>> assert path_lookup() == {
        ...     'css/main-a4a68e68.css': '/css/main-a4a68e68.css',
        ...     'css/main.css': '/css/main-a4a68e68.css',
        ...     'main.css': '/main.css',
        ...     'main-cafe0001.css': '/main-cafe0001.css',
        ... }

        Note that in the usual case css/main.css can be looked up with or without a hash, but we also make sure that
        main-cafe0001.css does not shadow main.css in the edge case where one file looks like a hashed version of another.
    """
    # get all paths
    paths = Path(settings.WHITENOISE_ROOT).glob('**/*')
    out = {}
    exact_paths = []
<commit_msg>Sort static file links by date to serve newest version<commit_after>import re
from functools import lru_cache
from pathlib import Path

from django import template
from django.conf import settings

register = template.Library()


@lru_cache(None)
def path_lookup():
    """
        This function caches every path in the Rails public/ directory (settings.WHITENOISE_ROOT) and returns a
        dictionary mapping the relative paths to the URLs that should be included in templates, stripping out
        hexadecimal hashes from the paths if necessary. This allows the rails_static tag to use
        {% rails_static "packs/css/main.css" %} in the template to include "/packs/css/main-a4a68e68.css" in the HTML.

        Given this set of files in public/ :

        >>> monkeypatch = getfixture('monkeypatch')
        >>> monkeypatch.setattr(Path, 'glob', lambda *args, **kwargs: [
        ...     settings.WHITENOISE_ROOT+'/css/main-a4a68e68.css',
        ...     settings.WHITENOISE_ROOT+'/main.css',
        ...     settings.WHITENOISE_ROOT+'/main-cafe0001.css',
        ... ])

        Return this lookup table:

        >>> assert path_lookup() == {
        ...     'css/main-a4a68e68.css': '/css/main-a4a68e68.css',
        ...     'css/main.css': '/css/main-a4a68e68.css',
        ...     'main.css': '/main.css',
        ...     'main-cafe0001.css': '/main-cafe0001.css',
        ... }

        Note that in the usual case css/main.css can be looked up with or without a hash, but we also make sure that
        main-cafe0001.css does not shadow main.css in the edge case where one file looks like a hashed version of another.
    """
    # get all paths
    paths = Path(settings.WHITENOISE_ROOT).glob('**/*')
    out = {}
    exact_paths = []

    # sort oldest to newest, so we overwrite older answers with new ones and return the newest version of each file
    paths = sorted(paths, key=lambda p: p.stat().st_mtime)

{'fraction_non_alphanumeric': 0.1148036253776435, 'fraction_numerical': 0.022521285361164517, 'mean_word_length': 2.5222437137330753, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3819866', 'n_tokens_mistral': 1174, 'n_tokens_neox': 1081, 'n_words': 348}