Exemple #1
0
def createStructTC(id, name, members):
    dlist = [tv_struct, None, id, name]
    mnames = []
    for m in members:
        mnames.append(m.name)
        dlist.append(m.name)
        dlist.append(m.type._d)
    str = omniORB.createUnknownStruct(id, mnames)
    dlist[1] = str
    d = tuple(dlist)
    return createTypeCode(d)
Exemple #2
0
def createStructTC(id, name, members):
    dlist  = [tv_struct, None, id, name]
    mnames = []
    for m in members:
        mnames.append(m.name)
        dlist.append(m.name)
        dlist.append(m.type._d)
    str = omniORB.createUnknownStruct(id, mnames)
    dlist[1] = str
    d = tuple(dlist)
    return createTypeCode(d)
Exemple #3
0
        items = data.items()
        for (k,v) in items:
            if not isinstance(k, StringType):
                raise CORBA.BAD_PARAM(omniORB.BAD_PARAM_WrongPythonType,
                                      CORBA.COMPLETED_NO)
            t, v = _to_tc_value(v)

            if t._k._v in INVALID_MEMBER_KINDS:
                v = CORBA.Any(t,v)
                t = CORBA.TC_any
            
            ms.append(k)
            dl.append(k)
            dl.append(t._d)
            svals.append(v)
        cls   = omniORB.createUnknownStruct(id, ms)
        dl[1] = cls
        tc    = tcInternal.createTypeCode(tuple(dl))
        value = apply(cls, svals)
        return tc, value

    elif isinstance(data, FixedType):
        if data == CORBA.fixed(0):
            tc = tcInternal.createTypeCode((tcInternal.tv_fixed, 1, 0))
        else:
            tc = tcInternal.createTypeCode((tcInternal.tv_fixed,
                                            data.precision(), data.decimals()))
        return tc, data

    elif isinstance(data, CORBA.Any):
        return data._t, data._v
Exemple #4
0
        items = data.items()
        for (k,v) in items:
            if not isinstance(k, str):
                raise CORBA.BAD_PARAM(omniORB.BAD_PARAM_WrongPythonType,
                                      CORBA.COMPLETED_NO)
            t, v = _to_tc_value(v)

            if t._k._v in INVALID_MEMBER_KINDS:
                v = CORBA.Any(t,v)
                t = CORBA.TC_any
            
            ms.append(k)
            dl.append(k)
            dl.append(t._d)
            svals.append(v)
        cls   = omniORB.createUnknownStruct(id, ms)
        dl[1] = cls
        tc    = tcInternal.createTypeCode(tuple(dl))
        value = cls(*svals)
        return tc, value

    elif isinstance(data, FixedType):
        if data == CORBA.fixed(0):
            tc = tcInternal.createTypeCode((tcInternal.tv_fixed, 1, 0))
        else:
            tc = tcInternal.createTypeCode((tcInternal.tv_fixed,
                                            data.precision(), data.decimals()))
        return tc, data

    elif isinstance(data, CORBA.Any):
        return data._t, data._v
Exemple #5
0
def _to_tc_value(data):
    """_to_tc_value(data) -- return TypeCode and value for Any insertion"""

    if data is None:
        return CORBA.TC_null, None

    elif isinstance(data, str):
        return CORBA.TC_string, data

    elif isinstance(data, str):
        return CORBA.TC_wstring, data

    elif isinstance(data, bytes):
        return CORBA._tc_OctetSeq, data

    elif isinstance(data, bool):
        return CORBA.TC_boolean, data

    elif isinstance(data, int):
        if -2147483648 <= data <= 2147483647:
            return CORBA.TC_long, int(data)
        elif 0 <= data <= 4294967295:
            return CORBA.TC_ulong, data
        elif -9223372036854775808 <= data <= 9223372036854775807:
            return CORBA.TC_longlong, data
        elif 0 <= data <= 18446744073709551615:
            return CORBA.TC_ulonglong, data
        else:
            raise CORBA.BAD_PARAM(omniORB.BAD_PARAM_PythonValueOutOfRange,
                                  CORBA.COMPLETED_NO)

    elif isinstance(data, float):
        return CORBA.TC_double, data

    elif isinstance(data, list):
        if data == []:
            tc = tcInternal.createTypeCode(
                (tcInternal.tv_sequence, tcInternal.tv_any, 0))
            return tc, data

        d0 = data[0]

        if isinstance(d0, str):
            for d in data:
                if not isinstance(d, str):
                    break
            else:
                # List of strings
                tc = tcInternal.createTypeCode(
                    (tcInternal.tv_sequence, CORBA.TC_string._d, 0))
                return tc, data

        elif isinstance(d0, str):
            for d in data:
                if not isinstance(d, str):
                    break
            else:
                # List of wstrings
                tc = tcInternal.createTypeCode(
                    (tcInternal.tv_sequence, CORBA.TC_wstring._d, 0))
                return tc, data

        elif isinstance(d0, bytes):
            for d in data:
                if not isinstance(d, bytes):
                    break
            else:
                # List of bytes
                tc = tcInternal.createTypeCode(
                    (tcInternal.tv_sequence, CORBA._tc_OctetSeq, 0))
                return tc, data

        elif isinstance(d0, bool):
            for d in data:
                if not isinstance(d, bool):
                    break
            else:
                tc = tcInternal.createTypeCode(
                    (tcInternal.tv_sequence, tcInternal.tv_boolean, 0))
                return tc, data

        elif isinstance(d0, int):
            # Numeric. Try to find a numeric type suitable for the whole list
            min_v = max_v = 0
            for d in data:
                if (not (isinstance(d, int) or isinstance(d, bool))):
                    break
                if d < min_v: min_v = d
                if d > max_v: max_v = d
            else:
                if min_v >= -2147483648 and max_v <= 2147483647:
                    tc = tcInternal.createTypeCode(
                        (tcInternal.tv_sequence, tcInternal.tv_long, 0))
                    return tc, list(map(int, data))
                elif min_v >= 0 and max_v <= 4294967295:
                    tc = tcInternal.createTypeCode(
                        (tcInternal.tv_sequence, tcInternal.tv_ulong, 0))
                    return tc, list(map(int, data))
                elif (min_v >= -9223372036854775808
                      and max_v <= 9223372036854775807):
                    tc = tcInternal.createTypeCode(
                        (tcInternal.tv_sequence, tcInternal.tv_longlong, 0))
                    return tc, list(map(int, data))
                elif min_v >= 0 and max_v <= 18446744073709551615:
                    tc = tcInternal.createTypeCode(
                        (tcInternal.tv_sequence, tcInternal.tv_ulonglong, 0))
                    return tc, list(map(int, data))
                else:
                    raise CORBA.BAD_PARAM(
                        omniORB.BAD_PARAM_PythonValueOutOfRange,
                        CORBA.COMPLETED_NO)

        elif isinstance(d0, float):
            for d in data:
                if not isinstance(d, float):
                    break
            else:
                # List of doubles
                tc = tcInternal.createTypeCode(
                    (tcInternal.tv_sequence, tcInternal.tv_double, 0))
                return tc, data

        elif isinstance(d0, CORBA.Any):
            for d in data:
                if not isinstance(d, CORBA.Any):
                    break
            else:
                # List of anys
                tc = tcInternal.createTypeCode(
                    (tcInternal.tv_sequence, tcInternal.tv_any, 0))
                return tc, data

        # Generic list
        tc = tcInternal.createTypeCode(
            (tcInternal.tv_sequence, tcInternal.tv_any, 0))
        any_list = list(map(to_any, data))

        atc = any_list[0]._t

        if atc._k._v not in INVALID_MEMBER_KINDS:
            for a in any_list:
                if not a._t.equivalent(atc):
                    break
            else:
                tc = tcInternal.createTypeCode(
                    (tcInternal.tv_sequence, atc._d, 0))
                for i in range(len(any_list)):
                    any_list[i] = any_list[i]._v

        return tc, any_list

    elif isinstance(data, tuple):
        return _to_tc_value(list(data))

    elif isinstance(data, dict):
        # Represent dictionaries as structs
        global _idcount

        _idlock.acquire()
        try:
            _idcount = _idcount + 1
            id = "omni:%s:%08x" % (_idbase, _idcount)
        finally:
            _idlock.release()

        dl = [tcInternal.tv_struct, None, id, ""]
        ms = []
        svals = []
        items = list(data.items())
        for (k, v) in items:
            if not isinstance(k, str):
                raise CORBA.BAD_PARAM(omniORB.BAD_PARAM_WrongPythonType,
                                      CORBA.COMPLETED_NO)
            t, v = _to_tc_value(v)

            if t._k._v in INVALID_MEMBER_KINDS:
                v = CORBA.Any(t, v)
                t = CORBA.TC_any

            ms.append(k)
            dl.append(k)
            dl.append(t._d)
            svals.append(v)
        cls = omniORB.createUnknownStruct(id, ms)
        dl[1] = cls
        tc = tcInternal.createTypeCode(tuple(dl))
        value = cls(*svals)
        return tc, value

    elif isinstance(data, FixedType):
        if data == CORBA.fixed(0):
            tc = tcInternal.createTypeCode((tcInternal.tv_fixed, 1, 0))
        else:
            tc = tcInternal.createTypeCode(
                (tcInternal.tv_fixed, data.precision(), data.decimals()))
        return tc, data

    elif isinstance(data, CORBA.Any):
        return data._t, data._v

    elif isinstance(data, omniORB.EnumItem):
        return omniORB.findTypeCode(data._parent_id), data

    elif hasattr(data, "_NP_RepositoryId"):
        return omniORB.findTypeCode(data._NP_RepositoryId), data

    raise CORBA.BAD_PARAM(omniORB.BAD_PARAM_WrongPythonType,
                          CORBA.COMPLETED_NO)
Exemple #6
0
def _to_tc_value(data):
    """_to_tc_value(data) -- return TypeCode and value for Any insertion"""

    if data is None:
        return CORBA.TC_null, None

    elif isinstance(data, str):
        return CORBA.TC_string, data

    elif isinstance(data, str):
        return CORBA.TC_wstring, data

    elif isinstance(data, bytes):
        return CORBA._tc_OctetSeq, data

    elif isinstance(data, bool):
        return CORBA.TC_boolean, data

    elif isinstance(data, int):
        if -2147483648 <= data <= 2147483647:
            return CORBA.TC_long, int(data)
        elif 0 <= data <= 4294967295:
            return CORBA.TC_ulong, data
        elif -9223372036854775808 <= data <= 9223372036854775807:
            return CORBA.TC_longlong, data
        elif 0 <= data <= 18446744073709551615:
            return CORBA.TC_ulonglong, data
        else:
            raise CORBA.BAD_PARAM(omniORB.BAD_PARAM_PythonValueOutOfRange,
                                  CORBA.COMPLETED_NO)

    elif isinstance(data, float):
        return CORBA.TC_double, data

    elif isinstance(data, list):
        if data == []:
            tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
                                            tcInternal.tv_any, 0))
            return tc, data

        d0 = data[0]

        if isinstance(d0, str):
            for d in data:
                if not isinstance(d, str):
                    break
            else:
                # List of strings
                tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
                                                CORBA.TC_string._d, 0))
                return tc, data

        elif isinstance(d0, str):
            for d in data:
                if not isinstance(d, str):
                    break
            else:
                # List of wstrings
                tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
                                                CORBA.TC_wstring._d, 0))
                return tc, data

        elif isinstance(d0, bytes):
            for d in data:
                if not isinstance(d, bytes):
                    break
            else:
                # List of bytes
                tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
                                                CORBA._tc_OctetSeq, 0))
                return tc, data

        elif isinstance(d0, bool):
            for d in data:
                if not isinstance(d, bool):
                    break
            else:
                tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
                                                tcInternal.tv_boolean, 0))
                return tc, data

        elif isinstance(d0, int):
            # Numeric. Try to find a numeric type suitable for the whole list
            min_v = max_v = 0
            for d in data:
                if (not (isinstance(d, int) or isinstance(d, bool))):
                    break
                if d < min_v: min_v = d
                if d > max_v: max_v = d
            else:
                if min_v >= -2147483648 and max_v <= 2147483647:
                    tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
                                                    tcInternal.tv_long, 0))
                    return tc, list(map(int, data))
                elif min_v >= 0 and max_v <= 4294967295:
                    tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
                                                    tcInternal.tv_ulong, 0))
                    return tc, list(map(int, data))
                elif (min_v >= -9223372036854775808 and
                      max_v <= 9223372036854775807):
                    tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
                                                    tcInternal.tv_longlong, 0))
                    return tc, list(map(int, data))
                elif min_v >= 0 and max_v <= 18446744073709551615:
                    tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
                                                    tcInternal.tv_ulonglong,0))
                    return tc, list(map(int, data))
                else:
                    raise CORBA.BAD_PARAM(
                        omniORB.BAD_PARAM_PythonValueOutOfRange,
                        CORBA.COMPLETED_NO)

        elif isinstance(d0, float):
            for d in data:
                if not isinstance(d, float):
                    break
            else:
                # List of doubles
                tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
                                                tcInternal.tv_double, 0))
                return tc, data

        elif isinstance(d0, CORBA.Any):
            for d in data:
                if not isinstance(d, CORBA.Any):
                    break
            else:
                # List of anys
                tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
                                                tcInternal.tv_any, 0))
                return tc, data

        # Generic list
        tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
                                        tcInternal.tv_any, 0))
        any_list = list(map(to_any, data))

        atc = any_list[0]._t

        if atc._k._v not in INVALID_MEMBER_KINDS:
            for a in any_list:
                if not a._t.equivalent(atc):
                    break
            else:
                tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
                                                atc._d, 0))
                for i in range(len(any_list)):
                    any_list[i] = any_list[i]._v
            
        return tc, any_list

    elif isinstance(data, tuple):
        return _to_tc_value(list(data))

    elif isinstance(data, dict):
        # Represent dictionaries as structs
        global _idcount

        _idlock.acquire()
        try:
            _idcount = _idcount + 1
            id = "omni:%s:%08x" % (_idbase, _idcount)
        finally:
            _idlock.release()

        dl = [tcInternal.tv_struct, None, id, ""]
        ms = []
        svals = []
        items = list(data.items())
        for (k,v) in items:
            if not isinstance(k, str):
                raise CORBA.BAD_PARAM(omniORB.BAD_PARAM_WrongPythonType,
                                      CORBA.COMPLETED_NO)
            t, v = _to_tc_value(v)

            if t._k._v in INVALID_MEMBER_KINDS:
                v = CORBA.Any(t,v)
                t = CORBA.TC_any
            
            ms.append(k)
            dl.append(k)
            dl.append(t._d)
            svals.append(v)
        cls   = omniORB.createUnknownStruct(id, ms)
        dl[1] = cls
        tc    = tcInternal.createTypeCode(tuple(dl))
        value = cls(*svals)
        return tc, value

    elif isinstance(data, FixedType):
        if data == CORBA.fixed(0):
            tc = tcInternal.createTypeCode((tcInternal.tv_fixed, 1, 0))
        else:
            tc = tcInternal.createTypeCode((tcInternal.tv_fixed,
                                            data.precision(), data.decimals()))
        return tc, data

    elif isinstance(data, CORBA.Any):
        return data._t, data._v

    elif isinstance(data, omniORB.EnumItem):
        return omniORB.findTypeCode(data._parent_id), data

    elif hasattr(data, "_NP_RepositoryId"):
        return omniORB.findTypeCode(data._NP_RepositoryId), data

    raise CORBA.BAD_PARAM(omniORB.BAD_PARAM_WrongPythonType,
                          CORBA.COMPLETED_NO)