Passed
Push — develop ( 874d1e...76289b )
by Christophe
04:05
created

download.get_latest()   A

Complexity

Conditions 5

Size

Total Lines 24
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nop 3
dl 0
loc 24
rs 9.3333
c 0
b 0
f 0
1
"""
2
Download the icons.
3
"""
4
5
import pathlib
6
import shutil
7
import sys
8
import urllib.error
9
import urllib.request
10
11
12
def get_icons() -> None:
13
    """
14
    Download the icons.
15
    """
16
    get_fontawesome()
17
18
19
def get_fontawesome() -> None:
20
    """
21
    Get the fontawesome icons.
22
    """
23
    folder = get_folder("fontawesome")
24
    download(
25
        "https://raw.githubusercontent.com/FortAwesome/Font-Awesome/6.6.0/"
26
        "css/fontawesome.css",
27
        folder,
28
        "fontawesome.css",
29
    )
30
    download(
31
        "https://raw.githubusercontent.com/FortAwesome/Font-Awesome/6.6.0/"
32
        "css/brands.css",
33
        folder,
34
        "brands.css",
35
    )
36
    download(
37
        "https://raw.githubusercontent.com/FortAwesome/Font-Awesome/6.6.0/"
38
        "webfonts/fa-brands-400.ttf",
39
        folder,
40
        "fa-brands-400.ttf",
41
    )
42
    download(
43
        "https://raw.githubusercontent.com/FortAwesome/Font-Awesome/6.6.0/"
44
        "webfonts/fa-regular-400.ttf",
45
        folder,
46
        "fa-regular-400.ttf",
47
    )
48
    download(
49
        "https://raw.githubusercontent.com/FortAwesome/Font-Awesome/6.6.0/"
50
        "webfonts/fa-solid-900.ttf",
51
        folder,
52
        "fa-solid-900.ttf",
53
    )
54
55
56
def download(url: str, folder: pathlib.Path, filename: str) -> None:
57
    """
58
    Download an url to a folder/filename.
59
60
    Arguments
61
    ---------
62
    url
63
        An url
64
    folder
65
        A folder
66
    filename
67
        A filename
68
    """
69
    print(f"Download '{url}' to {folder}/{filename}")
70
    try:
71
        with urllib.request.urlopen(url) as response, pathlib.Path(
72
            folder, filename
73
        ).open("wb") as out_file:
74
            shutil.copyfileobj(response, out_file)
75
    except urllib.error.HTTPError as exception:
76
        sys.stderr.write(str(exception))
77
78
79
def get_folder(collection: str) -> pathlib.Path:
80
    """
81
    Get a folder.
82
83
    Arguments
84
    ---------
85
    collection
86
        The collection name
87
88
    Returns
89
    -------
90
    pathlib.Path
91
        The folder
92
    """
93
    folder = pathlib.Path(
94
        "share",
95
        "pandoc_latex_tip",
96
        collection,
97
    )
98
99
    if not folder.exists():
100
        folder.mkdir(parents=True)
101
102
    return folder
103
104
105
if __name__ == "__main__":
106
    get_icons()
107