Completed
Push — master ( fac099...37b9e9 )
by Christophe
02:15
created

_post()   F

Complexity

Conditions 16

Size

Total Lines 119

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 16
dl 0
loc 119
rs 2
c 4
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like _post() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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
# Always prefer setuptools over distutils
9
from setuptools import setup, find_packages
10
11
# To use a consistent encoding
12
from codecs import open
13
from os import path, makedirs
14
15
here = path.abspath(path.dirname(__file__))
16
17
# Get the long description from the README file
18
try:
19
    import pypandoc
20
    long_description = pypandoc.convert('README.md', 'rst')
21
except (IOError, ImportError):
22
    with open(path.join(here, 'README.md'), encoding='utf-8') as f:
23
        long_description = f.read()
24
25
from distutils.command.build_py import build_py as _build_py
26
from distutils.command.build_ext import build_ext as _build_ext
27
28
def _post():
29
    import icon_font_to_png
30
    from pkg_resources import get_distribution
31
    from appdirs import AppDirs
32
    import requests
33
    import re
34
    from distutils.version import LooseVersion
35
    import os
36
37
    # fontawesome 4.7
38
    dirs = AppDirs(
39
        os.path.join(
40
            'pandoc_latex_tip',
41
            get_distribution('pandoc_latex_tip').version,
42
            'fontawesome',
43
            '4.7'
44
        )
45
    )
46
    directory = dirs.user_data_dir
47
    if not path.exists(directory):
48
        makedirs(directory)
49
        downloader = icon_font_to_png.FontAwesomeDownloader(directory)
50
        downloader.css_url = 'https://cdn.rawgit.com/FortAwesome/Font-Awesome/v4.7.0/css/font-awesome.css'
51
        downloader.ttf_url = 'https://cdn.rawgit.com/FortAwesome/Font-Awesome/v4.7.0/fonts/fontawesome-webfont.ttf'
52
        downloader.download_files()
53
54
    # fontawesome 5.0
55
    dirs = AppDirs(
56
        os.path.join(
57
            'pandoc_latex_tip',
58
            get_distribution('pandoc_latex_tip').version,
59
            'fontawesome',
60
            '5.0'
61
        )
62
    )
63
    try:
64
        versions = requests.get('https://api.github.com/repos/FortAwesome/Font-Awesome/tags').json()
65
    except ValueError:
66
        import sys
67
        sys.stderr.write('Unable to get the last version number of the Font-Awesome package on github\n')
68
        sys.exit(1)
69
70
    latest = '5.0'
71
    for version in versions:
72
        if re.match('^5.0', version['name']) and LooseVersion(version['name']) > LooseVersion(latest):
73
            latest = version['name']
74
75
    directory = dirs.user_data_dir
76
    if not path.exists(directory):
77
        makedirs(directory)
78
79
    downloader = icon_font_to_png.FontAwesomeDownloader(directory)
80
    downloader.css_url = 'https://cdn.rawgit.com/FortAwesome/Font-Awesome/' + latest + '/web-fonts-with-css/css/fontawesome.css'
81
    # brands
82
    downloader.ttf_url = 'https://cdn.rawgit.com/FortAwesome/Font-Awesome/' + latest + '/web-fonts-with-css/webfonts/fa-brands-400.ttf'
83
    downloader.download_files()
84
    # regular
85
    downloader.ttf_url = 'https://cdn.rawgit.com/FortAwesome/Font-Awesome/' + latest + '/web-fonts-with-css/webfonts/fa-regular-400.ttf'
86
    downloader.download_files()
87
    # solid
88
    downloader.ttf_url = 'https://cdn.rawgit.com/FortAwesome/Font-Awesome/' + latest + '/web-fonts-with-css/webfonts/fa-solid-900.ttf'
89
    downloader.download_files()
90
91
    # glyphicons 3.3
92
    dirs = AppDirs(
93
        os.path.join(
94
            'pandoc_latex_tip',
95
            get_distribution('pandoc_latex_tip').version,
96
            'glyphicons',
97
            '3.3'
98
        )
99
    )
100
    directory = dirs.user_data_dir
101
    if not path.exists(directory):
102
        makedirs(directory)
103
        downloader = icon_font_to_png.FontAwesomeDownloader(directory)
104
        downloader.css_url = 'https://cdn.rawgit.com/twbs/bootstrap/v3.3.7/dist/css/bootstrap.css'
105
        downloader.ttf_url = 'https://cdn.rawgit.com/twbs/bootstrap/v3.3.7/dist/fonts/glyphicons-halflings-regular.ttf'
106
        downloader.download_files()
107
        original = open(os.path.join(directory, "bootstrap.css"), "rt")
108
        modified = open(os.path.join(directory, "bootstrap-modified.css"), "w")
109
        index = 0
110
        for line in original:
111
            if index >= 1067:
112
                break
113
            elif index >= 280:
114
                modified.write(line)
115
            index = index + 1
116
        original.close()
117
        modified.close()
118
119
    # material design 2.4
120
    dirs = AppDirs(
121
        os.path.join(
122
            'pandoc_latex_tip',
123
            get_distribution('pandoc_latex_tip').version,
124
            'materialdesign',
125
            '2.4'
126
        )
127
    )
128
    try:
129
        versions = requests.get('https://api.github.com/repos/Templarian/MaterialDesign-Webfont/tags').json()
130
    except ValueError:
131
        import sys
132
        sys.stderr.write('Unable to get the last version number of the MaterialDesign-Webfont package on github\n')
133
        sys.exit(1)
134
135
    latest = 'v2.4'
136
    for version in versions:
137
        if re.match('^v2.4', version['name']) and LooseVersion(version['name']) > LooseVersion(latest):
138
            latest = version['name']
139
140
    directory = dirs.user_data_dir
141
    if not path.exists(directory):
142
        makedirs(directory)
143
    downloader = icon_font_to_png.FontAwesomeDownloader(directory)
144
    downloader.css_url = 'https://cdn.rawgit.com/Templarian/MaterialDesign-Webfont/' + latest + '/css/materialdesignicons.css'
145
    downloader.ttf_url = 'https://cdn.rawgit.com/Templarian/MaterialDesign-Webfont/' + latest + '/fonts/materialdesignicons-webfont.ttf'
146
    downloader.download_files()
147
148
149
class build_py(_build_py):
150
    def run(self):
151
        _build_py.run(self)
152
        self.execute(_post, (), msg="Running post build task")
153
154
class build_ext(_build_ext):
155
    def run(self):
156
        _build_ext.run(self)
157
        self.execute(_post, (), msg="Running post build task")
158
159
setup(
160
    cmdclass={'build_py': build_py, 'build_ext': build_ext},
161
    name='pandoc-latex-tip',
162
163
    # Versions should comply with PEP440.  For a discussion on single-sourcing
164
    # the version across setup.py and the project code, see
165
    # https://packaging.python.org/en/latest/single_source_version.html
166
    version='1.5.0',
167
168
    # The project's description
169
    description='A pandoc filter for adding tip in LaTeX',
170
    long_description=long_description,
171
172
    # The project's main homepage.
173
    url='https://github.com/chdemko/pandoc-latex-tip',
174
175
    # The project's download page
176
    download_url = 'https://github.com/chdemko/pandoc-latex-tip/archive/master.zip',
177
178
    # Author details
179
    author='Christophe Demko',
180
    author_email='[email protected]',
181
182
    # Maintainer details
183
    maintainer='Christophe Demko',
184
    maintainer_email='[email protected]',
185
186
    # Choose your license
187
    license='BSD-3-Clause',
188
189
    # See https://pypi.python.org/pypi?%3Aaction=list_classifiers
190
    classifiers=[
191
        # How mature is this project? Common values are
192
        #   3 - Alpha
193
        #   4 - Beta
194
        #   5 - Production/Stable
195
        'Development Status :: 5 - Production/Stable',
196
197
        # Specify the OS
198
        'Operating System :: OS Independent',
199
200
        # Indicate who your project is intended for
201
        'Environment :: Console',
202
        'Intended Audience :: End Users/Desktop',
203
        'Intended Audience :: Developers',
204
        'Topic :: Software Development :: Build Tools',
205
        'Topic :: Software Development :: Documentation',
206
        'Topic :: Text Processing :: Filters',
207
208
        # Specify the Python versions you support here. In particular, ensure
209
        # that you indicate whether you support Python 2, Python 3 or both.
210
        'Programming Language :: Python :: 2.7',
211
        'Programming Language :: Python :: 3.4',
212
        'Programming Language :: Python :: 3.5',
213
    ],
214
215
    # What does your project relate to?
216
    keywords='pandoc filters latex tip Font-Awesome icon',
217
218
    # Alternatively, if you want to distribute just a my_module.py, uncomment
219
    # this:
220
    py_modules=["pandoc_latex_tip"],
221
222
    # To provide executable scripts, use entry points in preference to the
223
    # "scripts" keyword. Entry points provide cross-platform support and allow
224
    # pip to create the appropriate form of executable for the target platform.
225
    entry_points={
226
        'console_scripts': [
227
            'pandoc-latex-tip = pandoc_latex_tip:main',
228
        ],
229
    },
230
231
    # List run-time dependencies here.  These will be installed by pip when
232
    # your project is installed. For an analysis of "install_requires" vs pip's
233
    # requirements files see:
234
    # https://packaging.python.org/en/latest/requirements.html
235
    install_requires=[
236
        'panflute>=1.10',
237
        'icon_font_to_png>=0.4',
238
        'pillow>=5.1',
239
        'appdirs>=1.4',
240
        'pypandoc>=1.4'
241
    ],
242
243
    # List additional groups of dependencies here (e.g. development
244
    # dependencies). You can install these using the following syntax,
245
    # for example:
246
    # $ pip install -e .[dev,test]
247
    extras_require={
248
        'dev': ['check-manifest'],
249
        'test': ['coverage'],
250
    },
251
252
    # packages=find_packages(),
253
    # include_package_data = True,
254
255
    setup_requires=[
256
        'pytest-runner',
257
        'icon_font_to_png>=0.4',
258
        'appdirs>=1.4'
259
    ],
260
    tests_require=['pytest', 'coverage'],
261
)
262