Issues (227)

awips/UsageArgumentParser.py (2 issues)

1
#
2
# SOFTWARE HISTORY
3
#
4
# Date          Ticket#  Engineer  Description
5
# ------------- -------- --------- ---------------------------------------------
6
# Feb 13, 2017  6092     randerso  Added StoreTimeAction
7
#
8
9
import argparse
10
import sys
11
import time
12
13
from dynamicserialize.dstypes.com.raytheon.uf.common.dataplugin.gfe.db.objects import DatabaseID
14
from dynamicserialize.dstypes.com.raytheon.uf.common.dataplugin.gfe.db.objects import ParmID
15
16
TIME_FORMAT = "%Y%m%d_%H%M"
17
18
19
class UsageArgumentParser(argparse.ArgumentParser):
20
    """
21
    A subclass of ArgumentParser that overrides error() to print the
22
    whole help text, rather than just the usage string.
23
    """
24
    def error(self, message):
25
        sys.stderr.write('%s: error: %s\n' % (self.prog, message))
26
        self.print_help()
27
        sys.exit(2)
28
29
30
# Custom actions for ArgumentParser objects
31
class StoreDatabaseIDAction(argparse.Action):
32
    def __call__(self, parser, namespace, values, option_string=None):
33
        did = DatabaseID(values)
34
        if did.isValid():
35
            setattr(namespace, self.dest, did)
36
        else:
37
            parser.error("DatabaseID [" + values + "] not a valid identifier")
38
39
40 View Code Duplication
class AppendParmNameAndLevelAction(argparse.Action):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
41
    def __call__(self, parser, namespace, values, option_string=None):
42
        tx = ParmID.parmNameAndLevel(values)
43
        comp = tx[0] + '_' + tx[1]
44
        if (hasattr(namespace, self.dest)) and (getattr(namespace, self.dest) is not None):
45
                currentValues = getattr(namespace, self.dest)
46
                currentValues.append(comp)
47
                setattr(namespace, self.dest, currentValues)
48
        else:
49
            setattr(namespace, self.dest, [comp])
50
51
52 View Code Duplication
class StoreTimeAction(argparse.Action):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
53
    """
54
    argparse.Action subclass to validate GFE formatted time strings
55
    and parse them to time.struct_time
56
    """
57
    def __call__(self, parser, namespace, values, option_string=None):
58
        try:
59
            timeStruct = time.strptime(values, TIME_FORMAT)
60
            setattr(namespace, self.dest, timeStruct)
61
        except ValueError:
62
            parser.error(str(values) + " is not a valid time string of the format YYYYMMDD_hhmm")
63