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
|
# SPDX-License-Identifier: BSD-3-Clause
#
# Copyright 2010 Raritan Inc. All rights reserved.
#
# Decodes IDL "typecode" to python class and vice versa.
#
import re
import importlib
import functools
# TODO: generate prefix from "base-package" in config
prefix = "raritan.rpc"
class TypeInfo(object):
@staticmethod
def typeBaseName(typeId):
b = typeId.split(":")[0] # remove version
b = re.sub(r'_[0-9]*_[0-9]*_[0-9]*', r'', b) # remove version
return b
@classmethod
def idlTypeIdToPyClass(cls, typeId):
"""Returns python class for given typeId.
The module defining this class will be auto-imported.
"""
pyName = "%s.%s" % (prefix, TypeInfo.typeBaseName(typeId))
comps = pyName.split(".")
className = [comps.pop()]
module = None
# remove components from end until import succeeds, import must be inside raritan.rpc
while len(comps) >= 2:
modName = ".".join(comps)
try:
module = importlib.import_module(modName)
except ImportError:
className.insert(0, comps.pop())
continue
cls = functools.reduce(getattr, className, module)
return cls
raise ImportError("Unable to find package for %s." % typeId)
@classmethod
def pyClassToIdlName(cls, pyClass):
return pyClass.idlType
@classmethod
def decode(cls, json):
typeId = json
return cls.idlTypeIdToPyClass(typeId)
@classmethod
def encode(cls, pyClass):
return cls.pyClassToIdlName(pyClass)
|