Completed
Push — master ( 152b55...0bdad9 )
by John
01:22
created

downloader_main()   F

Complexity

Conditions 11

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 11
c 1
b 1
f 0
dl 0
loc 71
rs 3.5526

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 downloader_main() 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
#!/usr/bin/env python3
2
"""Only download OS/radio bar files."""
3
4
import os  # filesystem read
5
import sys  # load arguments
6
from bbarchivist import decorators  # enter to exit
7
from bbarchivist import scriptutils  # script stuff
8
from bbarchivist import utilities  # input validation
9
from bbarchivist import networkutils  # download/lookup
10
11
__author__ = "Thurask"
12
__license__ = "WTFPL v2"
13
__copyright__ = "Copyright 2015-2016 Thurask"
14
15
16
@decorators.timer
17
def grab_args():
18
    """
19
    Parse arguments from argparse/questionnaire.
20
21
    Invoke downloader from :func:`archivist.archivist_main` with arguments.
22
    """
23
    if len(sys.argv) > 1:
24
        parser = scriptutils.default_parser("bb-downloader", "Download bar files",
25
                                            ("folder", "osr"))
26
        parser.add_argument(
27
            "-a",
28
            "--altsw",
29
            dest="altsw",
30
            metavar="SW",
31
            help="Radio software version, if not same as OS",
32
            nargs="?",
33
            default=None)
34
        parser.add_argument(
35
            "-d",
36
            "--debricks",
37
            dest="debricks",
38
            help="Download debricks",
39
            default=False,
40
            action="store_true")
41
        parser.add_argument(
42
            "-c",
43
            "--cores",
44
            dest="cores",
45
            help="Download debricks",
46
            default=False,
47
            action="store_true")
48
        parser.add_argument(
49
            "-r",
50
            "--radios",
51
            dest="radios",
52
            help="Download radios",
53
            default=False,
54
            action="store_true")
55
        parser.set_defaults()
56
        args = parser.parse_args(sys.argv[1:])
57
        if args.folder is None:
58
            args.folder = os.getcwd()
59
        downloader_main(args.os, args.radio, args.swrelease,
60
                        args.folder, args.debricks, args.radios,
61
                        args.cores, args.altsw)
62
    else:
63
        questionnaire()
64
65
66
def questionnaire():
67
    """
68
    Questions to ask if no arguments given.
69
    """
70
    localdir = os.getcwd()
71
    osversion = input("OS VERSION: ")
72
    radioversion = input("RADIO VERSION: ")
73
    softwareversion = input("SOFTWARE RELEASE: ")
74
    debricks = utilities.s2b(input("DOWNLOAD DEBRICKS? Y/N: "))
75
    radios = utilities.s2b(input("DOWNLOAD RADIOS? Y/N: "))
76
    if not radios:
77
        radios = False
78
    cores = utilities.s2b(input("DOWNLOAD CORES? Y/N: "))
79
    if not cores:
80
        cores = False
81
    altsw = None
82
    print(" ")
83
    downloader_main(osversion, radioversion, softwareversion,
84
                    localdir, debricks, radios, cores, altsw)
85
    decorators.enter_to_exit(True)
86
87
88
def downloader_main(osversion, radioversion=None, softwareversion=None,
89
                    localdir=None, debricks=True, radios=True, cores=False, altsw=None):
90
    """
91
    Archivist's download function, abstracted out.
92
93
    :param osversion: OS version, 10.x.y.zzzz.
94
    :type osversion: str
95
96
    :param radioversion: Radio version, 10.x.y.zzzz. Can be guessed.
97
    :type radioversion: str
98
99
    :param softwareversion: Software release, 10.x.y.zzzz. Can be guessed.
100
    :type softwareversion: str
101
102
    :param localdir: Working directory. Local by default.
103
    :type localdir: str
104
105
    :param debricks: Whether to download debrick OS files. True by default.
106
    :type debricks: bool
107
108
    :param radios: Whether to download radio files. True by default.
109
    :type radios: bool
110
111
    :param cores: Whether to download core OS files. False by default.
112
    :type cores: bool
113
114
    :param altsw: Radio software release, if not the same as OS.
115
    :type altsw: str
116
    """
117
    radioversion = scriptutils.return_radio_version(osversion, radioversion)
118
    softwareversion, swchecked = scriptutils.return_sw_checked(softwareversion, osversion)
119
    if altsw:
120
        altsw, altchecked = scriptutils.return_radio_sw_checked(altsw, radioversion)
121
    if localdir is None:
122
        localdir = os.getcwd()
123
    scriptutils.standard_preamble("downloader", osversion, softwareversion, radioversion, altsw)
124
125
    baseurl, alturl = scriptutils.get_baseurls(softwareversion, altsw)
126
    osurls, corurls, radurls = utilities.bulk_urls(baseurl, osversion, radioversion, cores, alturl)
127
128
    # Check availability of software releases
129
    scriptutils.check_sw(baseurl, softwareversion, swchecked)
130
    if altsw:
131
        scriptutils.check_radio_sw(alturl, altsw, altchecked)
132
133
    # Check availability of OS, radio
134
    if debricks:
135
        scriptutils.check_os_bulk(osurls)
136
    if cores:
137
        scriptutils.check_os_bulk(corurls)
138
    if radios:
139
        radurls, radioversion = scriptutils.check_radio_bulk(radurls, radioversion)
140
141
    # Download files
142
    print("BEGIN DOWNLOADING...")
143
    urllist = []
144
    if debricks:
145
        urllist += osurls
146
    if radios:
147
        urllist += radurls
148
    if cores:
149
        urllist += corurls
150
    if urllist:
151
        networkutils.download_bootstrap(urllist, localdir, workers=5)
152
        print("ALL FILES DOWNLOADED")
153
    else:
154
        print("NO FILES TO DOWNLOAD!")
155
        raise SystemExit
156
157
    # Test bar files
158
    scriptutils.test_bar_files(localdir, urllist)
159
160
161
if __name__ == "__main__":
162
    grab_args()
163