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

sqlexport_main()   F

Complexity

Conditions 10

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
dl 0
loc 45
rs 3.1304
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like sqlexport_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
"""Export SQL database to CSV."""
3
4
import sys  # load arguments
5
from bbarchivist import sqlutils  # the export function
6
from bbarchivist import scriptutils  # default parser
7
8
__author__ = "Thurask"
9
__license__ = "WTFPL v2"
10
__copyright__ = "Copyright 2015-2016 Thurask"
11
12
13
def sqlexport_main():
14
    """
15
    Wrapper around CSV export function.
16
17
    Invoke :func:`bbarchivist.sqlutils.export_sql_db`.
18
    """
19
    parser = scriptutils.default_parser("bb-sqlexport", "SQL-related tools")
20
    parser.add_argument(
21
        "-p",
22
        "--pop",
23
        dest="popsw",
24
        help="Pop this OS and SW from the database",
25
        nargs=2,
26
        metavar=("OS", "SW"),
27
        default=False)
28
    parser.add_argument(
29
        "-l",
30
        "--list",
31
        dest="list",
32
        help="List entries in database",
33
        action="store_true",
34
        default=False)
35
    parser.add_argument(
36
        "-a",
37
        "--available",
38
        dest="avail",
39
        help="List only available entries in database (implies -l)",
40
        action="store_true",
41
        default=False)
42
    parser.set_defaults()
43
    args = parser.parse_args(sys.argv[1:])
44
    if args.list or args.avail:
45
        args.popsw = False
46
    if not args.popsw and not args.list and not args.avail:
47
        sqlutils.export_sql_db()
48
    elif args.popsw:
49
        sqlutils.pop_sw_release(*args.popsw)
50
        print("POPPED: OS {0} - SW {1}".format(*args.popsw))
51
    else:
52
        rellist = sqlutils.list_sw_releases(args.avail)
53
        if rellist is not None:
54
            for rel in rellist:
55
                affix = "  " if rel[2] == "available" else ""
56
                print("OS {0} - SR {1} - {2} - {3}".format(
57
                    rel[0], rel[1], (rel[2] + affix), rel[3]))
58
59
60
if __name__ == "__main__":
61
    sqlexport_main()
62