File: cache.py

package info (click to toggle)
python-mkdocs 1.6.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,812 kB
  • sloc: python: 14,346; javascript: 10,535; perl: 143; sh: 57; makefile: 30; xml: 11
file content (36 lines) | stat: -rw-r--r-- 1,127 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import datetime
import urllib.request
from typing import Callable

import mkdocs_get_deps.cache

import mkdocs


def download_url(url: str) -> bytes:
    req = urllib.request.Request(url, headers={"User-Agent": f"mkdocs/{mkdocs.__version__}"})
    with urllib.request.urlopen(req) as resp:
        return resp.read()


def download_and_cache_url(
    url: str,
    cache_duration: datetime.timedelta,
    *,
    download: Callable[[str], bytes] = download_url,
    comment: bytes = b"# ",
) -> bytes:
    """
    Downloads a file from the URL, stores it under ~/.cache/, and returns its content.

    For tracking the age of the content, a prefix is inserted into the stored file, rather than relying on mtime.

    Args:
        url: URL to use.
        download: Callback that will accept the URL and actually perform the download.
        cache_duration: How long to consider the URL content cached.
        comment: The appropriate comment prefix for this file format.
    """
    return mkdocs_get_deps.cache.download_and_cache_url(
        url=url, cache_duration=cache_duration, download=download, comment=comment
    )