Passed
Push — develop ( 6810e0...5ae04f )
by Christophe
01:02
created

setup   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 305
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 24
eloc 193
dl 0
loc 305
rs 10
c 0
b 0
f 0

9 Functions

Rating   Name   Duplication   Size   Complexity  
A _latest() 0 10 5
A _post() 0 5 1
A _versions() 0 9 2
A _download() 0 8 3
A _directory() 0 27 3
A _post_fontawesome_5x() 0 27 2
A _post_glyphicons_33() 0 27 4
A _post_material_design_3x() 0 25 1
A _post_fontawesome_47() 0 12 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A BuildPy.run() 0 3 1
A BuildExt.run() 0 3 1
1
"""A setuptools based setup module.
2
3
See:
4
https://packaging.python.org/en/latest/distributing.html
5
https://github.com/chdemko/pandoc-latex-tip
6
"""
7
8
# To use a consistent encoding
9
10
# pylint: disable=no-name-in-module,import-error
11
from distutils.version import LooseVersion
12
13
import os
14
import re
15
import urllib.request
16
import urllib.error
17
import shutil
18
import sys
19
20
import pkg_resources
21
from pkg_resources import get_distribution
22
23
# Always prefer setuptools over distutils
24
from setuptools import setup
25
from setuptools.command.build_ext import build_ext
26
from setuptools.command.build_py import build_py
27
28
HERE = os.path.abspath(os.path.dirname(__file__))
29
30
# Get the long description from the README file
31
with open("README.md", encoding="utf-8") as stream:
32
    LONG_DESCRIPTION = stream.read()
33
34
# pylint: disable=too-many-locals,too-many-branches,too-many-statements
35
def _post():
36
    _post_fontawesome_47()
37
    _post_fontawesome_5x()
38
    _post_glyphicons_33()
39
    _post_material_design_3x()
40
41
42
def _post_fontawesome_47():
43
    # fontawesome 4.7
44
    directory = _directory("fontawesome", "4.7")
45
    _download(
46
        "https://raw.githubusercontent.com/FortAwesome/Font-Awesome/v4.7.0/css/font-awesome.css",
47
        directory,
48
        "font-awesome.css",
49
    )
50
    _download(
51
        "https://github.com/FortAwesome/Font-Awesome/blob/v4.7.0/fonts/fontawesome-webfont.ttf?raw=true",
52
        directory,
53
        "fontawesome-webfont.ttf",
54
    )
55
56
57
def _post_fontawesome_5x():
58
    # fontawesome 5.x
59
    directory = _directory("fontawesome", "5.x")
60
61
    versions = _versions(
62
        "https://api.github.com/repos/FortAwesome/Font-Awesome/tags",
63
        "Unable to get the last version number of the Font-Awesome package on github\n",
64
    )
65
66
    latest = _latest("^5.", versions, "5.14.0")
67
68
    _download(
69
        "https://raw.githubusercontent.com/FortAwesome/Font-Awesome/"
70
        + latest
71
        + "/css/fontawesome.css",
72
        directory,
73
        "fontawesome.css",
74
    )
75
    for ttf in ["fa-brands-400", "fa-regular-400", "fa-solid-900"]:
76
        _download(
77
            "https://github.com/FortAwesome/Font-Awesome/blob/"
78
            + latest
79
            + "/webfonts/"
80
            + ttf
81
            + ".ttf?raw=true",
82
            directory,
83
            ttf + ".ttf",
84
        )
85
86
87
def _post_glyphicons_33():
88
    # glyphicons 3.3
89
    directory = _directory("glyphicons", "3.3")
90
91
    _download(
92
        "https://github.com/twbs/bootstrap/raw/v3.3.7/dist/css/bootstrap.css",
93
        directory,
94
        "bootstrap.css",
95
    )
96
97
    _download(
98
        "https://github.com/twbs/bootstrap/blob/v3.3.7/dist/fonts/glyphicons-halflings-regular.ttf?raw=true",
99
        directory,
100
        "glyphicons-halflings-regular.ttf",
101
    )
102
103
    original = open(os.path.join(directory, "bootstrap.css"), "rt")
104
    modified = open(os.path.join(directory, "bootstrap-modified.css"), "w")
105
    index = 0
106
    for line in original:
107
        if index >= 1067:
108
            break
109
        if index >= 280:
110
            modified.write(line)
111
        index = index + 1
112
    original.close()
113
    modified.close()
114
115
116
def _post_material_design_3x():
117
    # material design 3.x
118
    directory = _directory("materialdesign", "3.x")
119
120
    versions = _versions(
121
        "https://api.github.com/repos/Templarian/MaterialDesign-Webfont/tags",
122
        "Unable to get the last version number of the MaterialDesign-Webfont package on github\n",
123
    )
124
125
    latest = _latest("^v3.", versions, "v5.9.55")
126
127
    _download(
128
        "https://github.com/Templarian/MaterialDesign-Webfont/blob/"
129
        + latest
130
        + "/css/materialdesignicons.css",
131
        directory,
132
        "materialdesignicons.css",
133
    )
134
135
    _download(
136
        "https://github.com/Templarian/MaterialDesign-Webfont/blob/"
137
        + latest
138
        + "/fonts/materialdesignicons-webfont.ttf?raw=true",
139
        directory,
140
        "materialdesignicons-webfont.ttf",
141
    )
142
143
144
def _download(url, directory, filename):
145
    try:
146
        with urllib.request.urlopen(url) as response, open(
147
            os.path.join(directory, filename), "wb"
148
        ) as out_file:
149
            shutil.copyfileobj(response, out_file)
150
    except urllib.error.HTTPError as exception:
151
        sys.stderr.write(str(exception))
152
153
154
def _latest(match, versions, latest):
155
    try:
156
        for version in versions:
157
            if re.match(match, version["name"]) and LooseVersion(
158
                version["name"]
159
            ) > LooseVersion(latest):
160
                latest = version["name"]
161
    except TypeError:
162
        pass
163
    return latest
164
165
166
def _directory(collection, icon_version):
167
    # pylint: disable=import-outside-toplevel
168
    import appdirs
169
170
    try:
171
        dirs = appdirs.AppDirs(
172
            os.path.join(
173
                "pandoc_latex_tip",
174
                get_distribution("pandoc_latex_tip").version,
175
                collection,
176
                icon_version,
177
            )
178
        )
179
    except pkg_resources.DistributionNotFound:
180
        dirs = appdirs.AppDirs(
181
            os.path.join(
182
                "pandoc_latex_tip",
183
                pkg_resources.require("pandoc_latex_tip")[0].version,
184
                collection,
185
                icon_version,
186
            )
187
        )
188
189
    directory = dirs.user_data_dir
190
    if not os.path.exists(directory):
191
        os.makedirs(directory)
192
    return directory
193
194
195
def _versions(url, message):
196
    # pylint: disable=import-outside-toplevel
197
    import requests
198
199
    try:
200
        return requests.get(url).json()
201
    except ValueError:
202
        sys.stderr.write(message)
203
        return []
204
205
206
class BuildPy(build_py):
207
    def run(self):
208
        super().run()
209
        self.execute(_post, (), msg="Running post build task")
210
211
212
class BuildExt(build_ext):
213
    def run(self):
214
        super().run()
215
        self.execute(_post, (), msg="Running post build task")
216
217
218
setup(
219
    cmdclass={"build_py": BuildPy, "build_ext": BuildExt},
220
    name="pandoc-latex-tip",
221
    # Versions should comply with PEP440.  For a discussion on single-sourcing
222
    # the version across setup.py and the project code, see
223
    # https://packaging.python.org/en/latest/single_source_version.html
224
    # The project's description
225
    description="A pandoc filter for adding tip in LaTeX",
226
    long_description=LONG_DESCRIPTION,
227
    long_description_content_type="text/markdown",
228
    # The project's main homepage.
229
    url="https://github.com/chdemko/pandoc-latex-tip",
230
    # The project's download page
231
    download_url="https://github.com/chdemko/pandoc-latex-tip/archive/develop.zip",
232
    # Author details
233
    author="Christophe Demko",
234
    author_email="[email protected]",
235
    # Maintainer details
236
    maintainer="Christophe Demko",
237
    maintainer_email="[email protected]",
238
    # Choose your license
239
    license="BSD-3-Clause",
240
    # See https://pypi.python.org/pypi?%3Aaction=list_classifiers
241
    classifiers=[
242
        # How mature is this project? Common values are
243
        #   3 - Alpha
244
        #   4 - Beta
245
        #   5 - Production/Stable
246
        "Development Status :: 5 - Production/Stable",
247
        # Specify the OS
248
        "Operating System :: OS Independent",
249
        # Indicate who your project is intended for
250
        "Environment :: Console",
251
        "Intended Audience :: End Users/Desktop",
252
        "Intended Audience :: Developers",
253
        "Topic :: Software Development :: Build Tools",
254
        "Topic :: Software Development :: Documentation",
255
        "Topic :: Text Processing :: Filters",
256
        # Specify the Python versions you support here. In particular, ensure
257
        # that you indicate whether you support Python 2, Python 3 or both.
258
        "Programming Language :: Python :: 3.6",
259
        "Programming Language :: Python :: 3.7",
260
        "Programming Language :: Python :: 3.8",
261
        "Programming Language :: Python :: 3.9",
262
    ],
263
    # What does your project relate to?
264
    keywords="pandoc filters latex tip Font-Awesome icon",
265
    # Alternatively, if you want to distribute just a my_module.py, uncomment
266
    # this:
267
    py_modules=["pandoc_latex_tip"],
268
    # To provide executable scripts, use entry points in preference to the
269
    # "scripts" keyword. Entry points provide cross-platform support and allow
270
    # pip to create the appropriate form of executable for the target platform.
271
    entry_points={"console_scripts": ["pandoc-latex-tip = pandoc_latex_tip:main"]},
272
    # List run-time dependencies here.  These will be installed by pip when
273
    # your project is installed. For an analysis of "install_requires" vs pip's
274
    # requirements files see:
275
    # https://packaging.python.org/en/latest/requirements.html
276
    install_requires=[
277
        "panflute>=2.0",
278
        "icon_font_to_png>=0.4",
279
        "Pillow>=8.1",
280
        "appdirs>=1.4",
281
        "requests>=2",
282
    ],
283
    # List additional groups of dependencies here (e.g. development
284
    # dependencies). You can install these using the following syntax,
285
    # for example:
286
    # $ pip install -e .[dev,test]
287
    extras_require={
288
        "dev": ["check-manifest"],
289
        "test": [
290
            "black",
291
            "tox",
292
            "pytest-runner",
293
            "coverage",
294
            "pylint",
295
            "Pygments",
296
            "radon",
297
            "mypy",
298
            "pytest-cov",
299
        ],
300
        "docs": ["Sphinx>=3.5", "sphinx_rtd_theme>=0.5"]
301
    },
302
    # packages=find_packages(),
303
    # include_package_data = True,
304
    setup_requires=["icon_font_to_png>=0.4", "appdirs>=1.4"],
305
)
306