awips.UsageArgumentParser   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 34.92 %

Importance

Changes 0
Metric Value
wmc 8
eloc 34
dl 22
loc 63
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A StoreDatabaseIDAction.__call__() 0 6 2
A UsageArgumentParser.error() 0 4 1
A StoreTimeAction.__call__() 6 6 2
A AppendParmNameAndLevelAction.__call__() 9 9 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
Duplication introduced by
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
Duplication introduced by
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