Completed
Push — master ( fe128a...71463b )
by John
01:12
created

sqlexport_main()   B

Complexity

Conditions 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 0
loc 20
rs 8.5454
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 grab_args():
14
    """
15
    Parse arguments from argparse/questionnaire.
16
    """
17
    parser = scriptutils.default_parser("bb-sqlexport", "SQL-related tools")
18
    parser.add_argument(
19
        "-p",
20
        "--pop",
21
        dest="popsw",
22
        help="Pop this OS and SW from the database",
23
        nargs=2,
24
        metavar=("OS", "SW"),
25
        default=False)
26
    parser.add_argument(
27
        "-l",
28
        "--list",
29
        dest="list",
30
        help="List entries in database",
31
        action="store_true",
32
        default=False)
33
    parser.add_argument(
34
        "-a",
35
        "--available",
36
        dest="avail",
37
        help="List only available entries in database (implies -l)",
38
        action="store_true",
39
        default=False)
40
    parser.set_defaults()
41
    args = parser.parse_args(sys.argv[1:])
42
    if args.list or args.avail:
43
        args.popsw = False
44
    sqlexport_main(args.list, args.avail, args.popsw)
45
46
47
def pprint():
48
    """
49
    Pretty print the release list.
50
    """
51
    rellist = sqlutils.list_sw_releases(avail)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'avail'
Loading history...
52
    if rellist is not None:
53
        for rel in rellist:
54
            affix = "  " if rel[2] == "available" else ""
55
            print("OS {0} - SR {1} - {2} - {3}".format(
56
                rel[0], rel[1], (rel[2] + affix), rel[3]))
57
58
59
def sqlexport_main(list, avail, popsw):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in list.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
60
    """
61
    Wrapper around CSV export function/other SQL-related stuff.
62
63
    :param list: List entries in database.
64
    :type list: bool
65
66
    :param avail: List only available entries in database (implies listing in the first place).
67
    :type avail: bool
68
69
    :param popsw: If we're popping a software release: False if not, ("OS", "SW") tuple if we are.
70
    :type popsw: tuple
71
    """
72
    if not popsw and not list and not avail:
73
        sqlutils.export_sql_db()
74
    elif popsw:
75
        sqlutils.pop_sw_release(*popsw)
1 ignored issue
show
Coding Style introduced by
Usage of * or ** arguments should usually be done with care.

Generally, there is nothing wrong with usage of * or ** arguments. For readability of the code base, we suggest to not over-use these language constructs though.

For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.

Loading history...
76
        print("POPPED: OS {0} - SW {1}".format(*popsw))
1 ignored issue
show
Coding Style introduced by
Usage of * or ** arguments should usually be done with care.

Generally, there is nothing wrong with usage of * or ** arguments. For readability of the code base, we suggest to not over-use these language constructs though.

For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.

Loading history...
77
    else:
78
        pprint()
79
80
81
if __name__ == "__main__":
82
    grab_args()
83