File: bundle.py

package info (click to toggle)
python-fonticon-fontawesome6 6.4.0%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 224 kB
  • sloc: python: 2,117; makefile: 9
file content (108 lines) | stat: -rw-r--r-- 3,128 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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import ast
import io
import json
import keyword
import shutil
import subprocess
import urllib.request
from pathlib import Path
from zipfile import ZipFile

VERSION = "6.4.0"
PKG_DIR = Path(__file__).parent.parent / "src" / "fonticon_fa6"
URL = "https://github.com/FortAwesome/Font-Awesome/releases/download/{0}/fontawesome-free-{0}-desktop.zip"  # noqa


def get_data(version, pkg_dir):
    font_dir = Path(pkg_dir) / "fonts"
    font_dir.mkdir(exist_ok=True)
    metadata = None
    otfs = []

    url = URL.format(version)
    with urllib.request.urlopen(url) as response:
        with ZipFile(io.BytesIO(response.read())) as thezip:
            for zipinfo in thezip.infolist():
                if zipinfo.filename.endswith(".otf") or "LICENSE" in zipinfo.filename:
                    dest = font_dir / Path(zipinfo.filename).name
                    with thezip.open(zipinfo) as source, open(dest, "wb") as target:
                        shutil.copyfileobj(source, target)
                        print("writing", dest)
                    if str(dest).endswith("otf"):
                        otfs.append(dest)
                elif zipinfo.filename.endswith("metadata/icons.json"):
                    with thezip.open(zipinfo) as f:
                        metadata = json.loads(f.read())
    out = []
    for otf in otfs:
        if "Brands" in str(otf):
            style = "brands"
        elif "Solid" in str(otf):
            style = "solid"
        else:
            style = "regular"

        charmap = {k: v["unicode"] for k, v in metadata.items() if style in v["styles"]}
        name = f"FA{version[0]}{style[0].upper()}"
        out.append((charmap, otf, name))
    return out


def normkey(key: str):
    if key[0].isdigit():
        key = "_" + key
    if keyword.iskeyword(key):
        key += "_"
    key = key.replace("-", "_")
    if not key.isidentifier():
        raise ValueError(f"not identifier: {key}")
    return key


TEMPLATE = """
from pathlib import Path

from ._iconfont import IconFont

FONTS = Path(__file__).parent / "fonts"


class {name}(IconFont):
    '''{doc}.'''
    __font_file__ = str(FONTS / "{file}")
""".strip()


def build(data, version, pkg):
    init = f"__version__ = {version!r}\n\n"
    _all = []

    for charmap, otf, name in data:
        code = TEMPLATE.format(name=name, doc=otf.stem, file=otf.name) + "\n\n"
        for key, glpyh in charmap.items():
            line = f"{normkey(key)} = '\\u{glpyh}'"
            try:
                ast.parse(line)
            except SyntaxError:
                continue
            code += f"    {line}\n"

        dest = Path(pkg) / f"{name.lower()}.py"
        dest.write_text(code)
        print("writing", dest)

        init += f"from .{name.lower()} import {name}\n"
        _all.append(name)

    init = f"__all__ = {_all!r}\n" + init
    (Path(pkg) / "__init__.py").write_text(init)
    print("writing __init__.py")


def main(version, root):
    build(get_data(version, root), version, root)
    subprocess.run(["pre-commit", "run", "--all-files"])


if __name__ == "__main__":
    main(version=VERSION, root=PKG_DIR)