Passed
Push — master ( 816317...cc1553 )
by John
03:01
created

bbarchivist.scripts.sqlexport.pprint()   A

Complexity

Conditions 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python3
2
"""Export SQL database to CSV."""
3
4
import sys  # load arguments
5
6
from bbarchivist import argutils  # arguments
7
from bbarchivist import sqlutils  # the export function
8
9
__author__ = "Thurask"
10
__license__ = "WTFPL v2"
11
__copyright__ = "2015-2018 Thurask"
12
13
14
def grab_args():
15
    """
16
    Parse arguments from argparse/questionnaire.
17
    """
18
    parser = argutils.default_parser("bb-sqlexport", "SQL-related tools")
19
    parser.add_argument(
20
        "-p",
21
        "--pop",
22
        dest="popsw",
23
        help="Pop this OS and SW from the database",
24
        nargs=2,
25
        metavar=("OS", "SW"),
26
        default=False)
27
    parser.add_argument(
28
        "-l",
29
        "--list",
30
        dest="list",
31
        help="List entries in database",
32
        action="store_true",
33
        default=False)
34
    parser.add_argument(
35
        "-a",
36
        "--available",
37
        dest="avail",
38
        help="List only available entries in database (implies -l)",
39
        action="store_true",
40
        default=False)
41
    parser.set_defaults()
42
    args = parser.parse_args(sys.argv[1:])
43
    if args.list or args.avail:
44
        args.popsw = False
45
    sqlexport_main(args.list, args.avail, args.popsw)
46
47
48
def pprint(avail):
49
    """
50
    Pretty print the release list.
51
52
    :param avail: List only available entries in database (implies listing in the first place).
53
    :type avail: bool
54
    """
55
    rellist = sqlutils.list_sw_releases(avail)
56
    if rellist is not None:
57
        for rel in rellist:
58
            affix = "  " if rel[2] == "available" else ""
59
            print("OS {0} - SR {1} - {2} - {3}".format(
60
                rel[0], rel[1], (rel[2] + affix), rel[3]))
61
62
63
def sqlexport_main(listed, avail, popsw):
64
    """
65
    Wrapper around CSV export function/other SQL-related stuff.
66
67
    :param listed: List entries in database.
68
    :type listed: bool
69
70
    :param avail: List only available entries in database (implies listing in the first place).
71
    :type avail: bool
72
73
    :param popsw: If we're popping a software release: False if not, ("OS", "SW") tuple if we are.
74
    :type popsw: tuple
75
    """
76
    if not popsw and not listed and not avail:
77
        sqlutils.export_sql_db()
78
    elif popsw:
79
        sqlutils.pop_sw_release(*popsw)
80
        print("POPPED: OS {0} - SW {1}".format(*popsw))
81
    else:
82
        pprint(avail)
83
84
85
if __name__ == "__main__":
86
    grab_args()
87