Revision 65769612 commissioning/hlapi/util.py

b/commissioning/hlapi/util.py
3 3
def isstr(s):
4 4
    return issubclass(type(s), basestring)
5 5

  
6
def compatible_type(given_t, expected_t):
7
    if issubclass(expected_t, basestring):
8
        expected_t = basestring
9
    if issubclass(given_t, basestring):
10
        given_t = basestring
11
    return given_t == expected_t
12
        
13

  
6 14
def compatible_input_types(given, expected):
15
    print "given=%s, expected=%s" % (given, expected)
7 16
    if len(given) != len(expected):
8
        return False
17
        return False    
9 18
    for i in xrange(len(given)):
10
        if isstr(given[i]) and not isstr(expected[i]):
11
            return False
12
        if isstr(expected[i]) and not isstr(given[i]):
13
            return False
14
        if type(given[i]) != type(expected[i]):
19
        if not compatible_type(given[i], expected[i]):
15 20
            return False
16 21
    return True
17
        
18 22

  
19
def compatible_return_types(given, expected):
20
    pass
23

  
24
def compatible_return_type(given, expected):
25
    return compatible_type(given, expected)
26

  
21 27

  
22 28
def method_accepts(*types):
23 29
    '''Method decorator. Checks decorated function's arguments are
......
33 39
            def newf(*args):
34 40
                args_to_check = args[1:] # Throw away self (or cls)
35 41
                assert len(args_to_check) == len(types)
42
                if len(args_to_check) != len(types):
43
                    raise TypeError("Wrong number of arguments. Expected is %s, given is %s" % (len(types), len(args_to_check)))
36 44
                argtypes = tuple(map(type, args_to_check))
37
                if argtypes != types:
45
#                if argtypes != types:
46
                if not compatible_input_types(argtypes, types):
38 47
                    msg = info(f.__name__, types, argtypes, 0)
39 48
                    raise TypeError, msg
40 49
                return f(*args)
......
59 68
    try:
60 69
        def decorator(f):
61 70
            def newf(*args):
62
                assert len(args) == len(types)
71
                if len(args) != len(types):
72
                    raise TypeError("Wrong number of arguments. Expected is %s, given is %s" % (len(types), len(args)))
63 73
                argtypes = tuple(map(type, args))
64
                if argtypes != types:
74
#                if argtypes != types:
75
                if not compatible_input_types(argtypes, types):
65 76
                    msg = info(f.__name__, types, argtypes, 0)
66 77
                    raise TypeError, msg
67 78
                return f(*args)
......
88 99
            def newf(*args):
89 100
                result = f(*args)
90 101
                res_type = type(result)
91
                if res_type != ret_type:
102
                print "ret_type=%s, res_type=%s" % (ret_type, res_type)
103
#                if res_type != ret_type:
104
                if not compatible_type(res_type, ret_type):
92 105
                    msg = info(f.__name__, (ret_type,), (res_type,), 1)
93 106
                    raise TypeError, msg
94 107
                return result

Also available in: Unified diff