Skip to content

Script Reference

scripts.stubs

Usage: stubs.py <path to project directory>

stubs(dir: Path)

Collect library stubs and symlink, then ignore missing import errors.

This should be run after adding any library stubs e.g types-requests.

To work around this issue.

Original code by pawamoy:

https://github.com/python/mypy/issues/10633#issuecomment-974840203

and extended by jaynewey.

Source code in scripts/stubs.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def stubs(dir: Path):
    """Collect library stubs and symlink, then ignore missing import errors.

    This should be run after adding any library stubs e.g `types-requests`.

    To work around [this issue](https://github.com/python/mypy/issues/10633).

    Original code by `pawamoy`:

    [https://github.com/python/mypy/issues/10633#issuecomment-974840203]()

    and extended by `jaynewey`.
    """

    os.chdir(str(dir))

    # compute packages directory path
    py = f"{sys.version_info.major}.{sys.version_info.minor}"
    pkgs_dir = Path("__pypackages__", py, "lib").resolve()

    # build the list of available packages
    packages = {}
    for package in pkgs_dir.glob("*"):
        if (
            package.suffix not in {".dist-info", ".pth"}
            and package.name != "__pycache__"
        ):
            packages[package.name] = package

    # handle .pth files
    for pth in pkgs_dir.glob("*.pth"):
        with suppress(OSError):
            for package in Path(Path(pth).read_text().splitlines()[0]).glob("*"):
                if package.suffix != ".dist-info":
                    packages[package.name] = package

    stub_dir = Path(".stubs/")
    rmtree(stub_dir, ignore_errors=True)
    stub_dir.mkdir(parents=True, exist_ok=True)

    # symlink the stubs
    ignore = set()
    for stubs in (path for name, path in packages.items() if name.endswith("-stubs")):
        Path(stub_dir, stubs.name).symlink_to(stubs, target_is_directory=True)
        # try to symlink the corresponding package
        # see https://www.python.org/dev/peps/pep-0561/#stub-only-packages
        pkg_name = stubs.name.replace("-stubs", "")
        if pkg_name in packages:
            ignore.add(pkg_name)
            Path(stub_dir, pkg_name).symlink_to(
                packages[pkg_name], target_is_directory=True
            )

    # create temporary mypy config to ignore stubbed packages
    mypy_config = Path("mypy.ini")
    config_contents = mypy_config.read_text() if mypy_config.exists() else ""
    config_contents += "\n[mypy]\nmypy_path = $MYPY_CONFIG_FILE_DIR\n"
    config_contents += "\n" + "\n\n".join(
        f"[mypy-{pkg}.*]\nignore_errors=true" for pkg in ignore
    )
    Path(stub_dir, "mypy.ini").write_text(config_contents)

scripts.fetch_charm

Usage: fetch_charm.py <path to assets folder>

fetch_charm(dir: Path)

Fetches all Charm icons and puts in specified folder.

Fetches from latest GitHub release.

See Charm Icons.

Parameters:

Name Type Description Default
dir Path

The directory to store the icons in. Will store at '{dir}/charm/'.

required
Source code in scripts/fetch_charm.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def fetch_charm(dir: Path):
    """Fetches all Charm icons and puts in specified folder.

    Fetches from latest GitHub release.

    See [Charm Icons](https://github.com/jaynewey/charm-icons).

    Args:
        dir: The directory to store the icons in. Will store at '{dir}/charm/'.
    """

    # first do some checks - if we don't have to fetch, don't
    # if the directory {dir}/charm exists and is not empty, do not fetch
    charm_dir = dir / "charm"
    if charm_dir.is_dir() and any(charm_dir.iterdir()):
        return

    # else, begin our procedure
    os.chdir(str(dir))
    version = (requests.get(_LATEST_RELEASE_URL).url).split("/")[-1]

    tar = requests.get(f"{_RELEASES_URL}/{version}.tar.gz", stream=True)
    file = tarfile.open(fileobj=tar.raw, mode="r|gz")
    file.extractall()
    shutil.rmtree("charm/", ignore_errors=True)
    shutil.copytree(f"charm-icons-{version[1:]}/icons/", "charm/")
    shutil.rmtree(f"charm-icons-{version[1:]}/")