~reldan/nova/l-m-n-fix-network-info

« back to all changes in this revision

Viewing changes to nova/validate.py

  • Committer: Eric Day
  • Date: 2010-10-21 18:49:51 UTC
  • mto: This revision was merged to the branch mainline in revision 377.
  • Revision ID: eday@oddments.org-20101021184951-x0vs3s8y7mc0aeyy
PEP8 and pylint cleanup. There should be no functional changes here, just style changes to get violations down.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#    License for the specific language governing permissions and limitations
17
17
#    under the License.
18
18
 
19
 
"""
20
 
  Decorators for argument validation, courtesy of 
21
 
  http://rmi.net/~lutz/rangetest.html
22
 
"""
23
 
 
24
 
def rangetest(**argchecks):                 # validate ranges for both+defaults
25
 
    def onDecorator(func):                  # onCall remembers func and argchecks
 
19
"""Decorators for argument validation, courtesy of
 
20
http://rmi.net/~lutz/rangetest.html"""
 
21
 
 
22
 
 
23
def rangetest(**argchecks):
 
24
    """Validate ranges for both + defaults"""
 
25
 
 
26
    def onDecorator(func):
 
27
        """onCall remembers func and argchecks"""
26
28
        import sys
27
29
        code = func.__code__ if sys.version_info[0] == 3 else func.func_code
28
 
        allargs  = code.co_varnames[:code.co_argcount]
 
30
        allargs = code.co_varnames[:code.co_argcount]
29
31
        funcname = func.__name__
30
 
        
 
32
 
31
33
        def onCall(*pargs, **kargs):
32
34
            # all pargs match first N args by position
33
35
            # the rest must be in kargs or omitted defaults
38
40
                # for all args to be checked
39
41
                if argname in kargs:
40
42
                    # was passed by name
41
 
                    if float(kargs[argname]) < low or float(kargs[argname]) > high:
 
43
                    if float(kargs[argname]) < low or \
 
44
                       float(kargs[argname]) > high:
42
45
                        errmsg = '{0} argument "{1}" not in {2}..{3}'
43
46
                        errmsg = errmsg.format(funcname, argname, low, high)
44
47
                        raise TypeError(errmsg)
46
49
                elif argname in positionals:
47
50
                    # was passed by position
48
51
                    position = positionals.index(argname)
49
 
                    if float(pargs[position]) < low or float(pargs[position]) > high:
50
 
                        errmsg = '{0} argument "{1}" with value of {4} not in {2}..{3}'
51
 
                        errmsg = errmsg.format(funcname, argname, low, high, pargs[position])
 
52
                    if float(pargs[position]) < low or \
 
53
                       float(pargs[position]) > high:
 
54
                        errmsg = '{0} argument "{1}" with value of {4} ' \
 
55
                                 'not in {2}..{3}'
 
56
                        errmsg = errmsg.format(funcname, argname, low, high,
 
57
                                               pargs[position])
52
58
                        raise TypeError(errmsg)
53
59
                else:
54
60
                    pass
62
68
    def onDecorator(func):
63
69
        import sys
64
70
        code = func.__code__ if sys.version_info[0] == 3 else func.func_code
65
 
        allargs  = code.co_varnames[:code.co_argcount]
 
71
        allargs = code.co_varnames[:code.co_argcount]
66
72
        funcname = func.__name__
67
 
    
 
73
 
68
74
        def onCall(*pargs, **kargs):
69
75
            positionals = list(allargs)[:len(pargs)]
70
76
            for (argname, typeof) in argchecks.items():
76
82
                elif argname in positionals:
77
83
                    position = positionals.index(argname)
78
84
                    if not isinstance(pargs[position], typeof):
79
 
                        errmsg = '{0} argument "{1}" with value of {2} not of type {3}'
80
 
                        errmsg = errmsg.format(funcname, argname, pargs[position], typeof)
 
85
                        errmsg = '{0} argument "{1}" with value of {2} ' \
 
86
                                 'not of type {3}'
 
87
                        errmsg = errmsg.format(funcname, argname,
 
88
                                               pargs[position], typeof)
81
89
                        raise TypeError(errmsg)
82
90
                else:
83
91
                    pass
84
92
            return func(*pargs, **kargs)
85
93
        return onCall
86
94
    return onDecorator
87