File: typecheck.py

package info (click to toggle)
raritan-json-rpc-sdk 4.0.20%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 57,236 kB
  • sloc: cs: 223,121; perl: 117,786; python: 26,872; javascript: 6,544; makefile: 27
file content (101 lines) | stat: -rw-r--r-- 2,748 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# SPDX-License-Identifier: BSD-3-Clause
#
# Copyright 2010 Raritan Inc. All rights reserved.

from __future__ import absolute_import
import sys

import raritan.rpc
from raritan.rpc import Interface, Structure, Enumeration, ValueObject
import inspect

#
# simple types
#
def is_bool(x, exception):
    if not isinstance(x, bool):
        raise exception("'%s' is not a bool" % x)

def _is_int(x):
    if sys.version_info.major < 3:
        return isinstance(x, (int, long))
    else:
        return isinstance(x, int)

def _is_float(x):
    return _is_int(x) or isinstance(x, float)

def is_byte(x, exception):
    if not _is_int(x) or int(x) < 0 or int(x) > 255:
        raise exception("'%s' is not a byte" % x)

def is_int(x, exception):
    if not _is_int(x):
        raise exception("'%s' is not an integer" % x)

def is_long(x, exception):
    if not _is_int(x):
        raise exception("'%s' is not a long" % x)

def is_float(x, exception):
    if not _is_float(x):
        raise exception("'%s' is not a float" % x)

def is_double(x, exception):
    if not _is_float(x):
        raise exception("'%s' is not a double" % x)

def is_string(x, exception):
    if sys.version_info.major < 3:
        if not isinstance(x, basestring):
            raise exception("'%s' is not a string" % x)
    else:
        if not isinstance(x, str):
            raise exception("'%s' is not a string" % x)

def is_time(x, exception):
    if not isinstance(x, raritan.rpc.Time):
        raise exception("'%s' is not a Time" % x)

def is_remote_obj(x, exception):
    is_interface(x, Interface, exception)

def is_typeinfo(x, exception):
    if not inspect.isclass(x) or (not issubclass(x, Interface) and not issubclass(x, ValueObject)):
        raise exception("'%s' is not a TypeInfo" % x)

#
# complex types
#
def is_interface(x, cls, exception):
    # allow to pass None as interface
    if x is None:
        return True

    if not isinstance(x, Interface):
        raise exception("'%s' is not an interface reference" % (x))
    is_class(x, cls, exception)

def is_struct(x, cls, exception):
    if not isinstance(x, Structure):
        raise exception("'%s' is not a structure" % (x))
    is_class(x, cls, exception)

def is_enum(x, cls, exception):
    if not isinstance(x, Enumeration):
        raise exception("'%s' is not an enumeration" % (x))
    is_class(x, cls, exception)

def is_valobj(x, cls, exception):
    if x == None:
        return True # ValueObject may be null / 'None'
    if not isinstance(x, ValueObject):
        raise exception("'%s' is not a value object" % (x))
    is_class(x, cls, exception)

#
# helpers
#
def is_class(x, cls, exception):
    if not isinstance(x, cls):
        raise exception("'%s' is not a %s" % (x, cls))