Exemplo n.º 1
0
def _make_reference_for_native(kernel, d):
    # Ugly delayed import here because I can't solve the cyclic
    # package dependency right now :(.
    from jsii._runtime import python_jsii_mapping

    if isinstance(d, dict):
        return {k: _make_reference_for_native(kernel, v) for k, v in d.items()}
    elif isinstance(d, list):
        return [_make_reference_for_native(kernel, i) for i in d]

    mapping = python_jsii_mapping(d)
    if mapping:
        struct_data = {
            jsii_name: getattr(d, python_name)
            for python_name, jsii_name in mapping.items()
        }
        return _make_reference_for_native(kernel, struct_data)
    elif hasattr(d, "__jsii_type__"):
        return d
    elif isinstance(d, (int, type(None), str, float, bool, datetime.datetime)):
        return d
    elif isinstance(
            d, (FunctionType, MethodType, BuiltinFunctionType, LambdaType)):
        # Whether a given object is a function-like object.
        # We won't use iscallable() since objects may implement __call__()
        # but we still want to serialize them as normal.
        raise JSIIError(
            "Cannot pass function as argument here (did you mean to call this function?): %r"
            % d)
    else:
        d.__jsii__type__ = "Object"
        kernel.create(Object, d)
        _reference_map.register_reference(d)
        return d
Exemplo n.º 2
0
    def __call__(cls, *args, **kwargs):
        inst = super().__call__(*args, **kwargs)

        # Register this instance with our reference map.
        _reference_map.register_reference(inst)

        return inst
Exemplo n.º 3
0
def _make_reference_for_native(kernel, d):
    # Ugly delayed import here because I can't solve the cyclic
    # package dependency right now :(.
    from jsii._runtime import python_jsii_mapping

    if isinstance(d, dict):
        return {k: _make_reference_for_native(kernel, v) for k, v in d.items()}
    elif isinstance(d, list):
        return [_make_reference_for_native(kernel, i) for i in d]

    mapping = python_jsii_mapping(d)
    if mapping:
        struct_data = {
            jsii_name: getattr(d, python_name)
            for python_name, jsii_name in mapping.items()
        }
        return _make_reference_for_native(kernel, struct_data)
    elif hasattr(d, "__jsii_type__"):
        return d
    elif isinstance(d, (int, type(None), str, float, bool, datetime.datetime)):
        return d
    else:
        d.__jsii__type__ = "Object"
        kernel.create(Object, d)
        _reference_map.register_reference(d)
        return d
def _make_reference_for_native(kernel, d):
    if isinstance(d, dict):
        return {"$jsii.map": {k: _make_reference_for_native(kernel, v) for k, v in d.items()}}
    elif isinstance(d, list):
        return [_make_reference_for_native(kernel, i) for i in d]

    if getattr(d, "__jsii_type__", None) is not None:
        # Ugly delayed import here because I can't solve the cyclic
        # package dependency right now :(.
        from jsii._runtime import python_jsii_mapping

        typeFqn = getattr(d, "__jsii_type__")
        mapping = python_jsii_mapping(d)
        if mapping: # This means we are handling a data_type (aka Struct)
            return {
                "$jsii.struct": {
                    "fqn": typeFqn,
                    "data": {
                        jsii_name: _make_reference_for_native(kernel, getattr(d, python_name)) for python_name, jsii_name in mapping.items()
                    }
                }
            }
        return d
    elif isinstance(d, (int, type(None), str, float, bool, datetime.datetime)):
        return d
    elif isinstance(d, (FunctionType, MethodType, BuiltinFunctionType, LambdaType)):
        # Whether a given object is a function-like object.
        # We won't use iscallable() since objects may implement __call__()
        # but we still want to serialize them as normal.
        raise JSIIError("Cannot pass function as argument here (did you mean to call this function?): %r" % d)
    else:
        kernel.create(d.__class__, d)
        _reference_map.register_reference(d)
        return d
    def create(
        self, klass: JSClass, obj: Any, args: Optional[List[Any]] = None
    ) -> ObjRef:
        if args is None:
            args = []

        response = self.provider.create(
            CreateRequest(
                fqn=klass.__jsii_type__ or "Object",
                args=_make_reference_for_native(self, args),
                overrides=_get_overides(klass, obj),
                interfaces=[iface.__jsii_type__ for iface in getattr(klass, "__jsii_ifaces__", [])],
            )
        )
        if isinstance(response, Callback):
            obj.__jsii_ref__ =  _callback_till_result(self, response, CreateResponse)
        else:
            obj.__jsii_ref__ = response

        # Register this to the reference map already (so it's available within the rest of the __init__)
        _reference_map.register_reference(obj)

        return obj.__jsii_ref__