<commit_before>import os.path
import hashlib
import time


_IMPORT_TIME = str(int(time.time()))


_BUST_METHODS = {
    'mtime': lambda src_path: str(int(os.path.getmtime(src_path))),
    'md5': lambda src_path: hashlib.md5(open(src_path).read()).hexdigest(),
    'importtime': lambda src_path: _IMPORT_TIME,
}


# TODO: Cache this.
def get_cache_buster(src_path, method='importtime'):
    """
    Return a string that can be used as a parameter for cache-busting URLs for
    this asset.

    NOTE: The returned value is cached, to avoid doing processing if it's
    called repeatedly with the same parameters.

    :param src_path:
        Filesystem path to the file we're generating a cache-busting value for.

    :param method:
        Method for cache-busting. Supported values: importtime, mtime, md5
        The default is 'importtime', because it requires the least processing.


    Example::

        >>> get_cache_buster('html.py')
        >>> get_cache_buster('html.py', method='mtime')
        >>> get_cache_buster('html.py', method='md5')
    """
    try:
        fn = _BUST_METHODS[method]
    except KeyError:
        raise KeyError('Unsupported busting method value: %s' % method)

    return fn(src_path)
<commit_msg>Add caching to the cache busting.<commit_after>import os.path
import hashlib
import time

from functools_ import memoized_into


_IMPORT_TIME = str(int(time.time()))


_BUST_METHODS = {
    'mtime': lambda src_path: str(int(os.path.getmtime(src_path))),
    'md5': lambda src_path: hashlib.md5(open(src_path).read()).hexdigest(),
    'importtime': lambda src_path: _IMPORT_TIME,
}

_BUST_CACHE = {}


@memoized_into(_BUST_CACHE)
def get_cache_buster(src_path, method='importtime'):
    """
    Return a string that can be used as a parameter for cache-busting URLs for
    this asset.

    NOTE: The returned value is cached, to avoid doing processing if it's
    called repeatedly with the same parameters.

    :param src_path:
        Filesystem path to the file we're generating a cache-busting value for.

    :param method:
        Method for cache-busting. Supported values: importtime, mtime, md5
        The default is 'importtime', because it requires the least processing.


    Example::

        >>> get_cache_buster('html.py') is _IMPORT_TIME
        True
        >>> get_cache_buster('html.py', method='mtime') is not None  # Is there a better doctest example?
        True
        >>> get_cache_buster('html.py', method='md5') is not None  # Is there a better doctest example?
        True
    """
    try:
        fn = _BUST_METHODS[method]
    except KeyError:
        raise KeyError('Unsupported busting method value: %s' % method)

    return fn(src_path)


if __name__ == "__main__":
    import doctest
    doctest.testmod(optionflags=doctest.ELLIPSIS)
{'fraction_non_alphanumeric': 0.10249110320284698, 'fraction_numerical': 0.0028469750889679717, 'mean_word_length': 3.358139534883721, 'pattern_counts': {'":': 1, '<': 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': '7557354', 'n_tokens_mistral': 904, 'n_tokens_neox': 877, 'n_words': 265}