Exemplo n.º 1
0
def translate_module(node, root, namespace, inchannels, outchannels):
    module_type = node["module_type"]
    language = node["language"]
    dependencies = node["dependencies"]

    path = node["path"]
    parent = get_path(root, path[:-1], None, None)
    name = path[-1]
    for c in inchannels + outchannels:
        assert not len(c)  #should have been checked by highlevel
    subcontext = context(toplevel=False)
    setattr(parent, name, subcontext)

    codecell = core_cell("plain")
    subcontext.code = codecell
    if node.get("fingertip_no_recompute"):
        codecell._fingertip_recompute = False
    if node.get("fingertip_no_remote"):
        codecell._fingertip_remote = False
    pathstr = "." + ".".join(path)
    checksum = node.get("checksum")
    if checksum is not None:
        codecell._set_checksum(checksum, initial=True)
    if "mount" in node:
        codecell2 = core_cell("text")
        subcontext.code2 = codecell2
        codecell2.mount(**node["mount"])
        mode = node["mount"].get("mode", "rw")
        if mode == "rw":
            codecell2.bilink(codecell)
        elif mode == "r":
            codecell2.connect(codecell)
        elif mode == "w":
            codecell.connect(codecell2)

    subcontext.module_cell = core_cell("plain")
    subcontext.gen_module_cell = transformer({
        "module_type": ("input", "str"),
        "language": ("input", "str"),
        "module_code": ("input", "plain"),
        "dependencies": ("input", "plain"),
        "result": ("output", "plain")
    })
    subcontext.gen_module_cell.code.cell().set(
        inspect.getsource(gen_module_cell))
    subcontext.gen_module_cell.module_type.cell().set(module_type)
    subcontext.gen_module_cell.language.cell().set(language)
    subcontext.gen_module_cell.dependencies.cell().set(dependencies)
    codecell.connect(subcontext.gen_module_cell.module_code)

    subcontext.gen_module_cell.result.connect(subcontext.module_cell)

    namespace[path, "source"] = subcontext.module_cell, node
    namespace[path, "target"] = codecell, node

    return subcontext
Exemplo n.º 2
0
    def do_connect(source, target):
        if source_is_edit or target_is_edit:
            msg = "Cannot set up an edit link involving a structured cell: %s (with %s)"
            if not isinstance(source, Cell):
                raise Exception(msg % (source.structured_cell(), target))
            if not isinstance(target, Cell):
                raise Exception(msg % (target.structured_cell(), source))
            source.bilink(target)
            return

        if isinstance(source, Cell) or isinstance(target, Cell):
            source.connect(target)
            return

        n = 0
        while 1:
            n += 1
            con_name = "CONNECTION_" + str(n)
            if con_name not in ctx._children:
                break
        hash_pattern = source.hash_pattern
        if isinstance(source, Outchannel):
            if hash_pattern is not None:
                hash_pattern = access_hash_pattern(hash_pattern,
                                                   source.subpath)
        intermediate = core_cell("mixed", hash_pattern=hash_pattern)
        setattr(ctx, con_name, intermediate)
        source.connect(intermediate)
        intermediate.connect(target)
Exemplo n.º 3
0
def build_structured_cell(ctx,
                          name,
                          inchannels,
                          outchannels,
                          *,
                          fingertip_no_remote,
                          fingertip_no_recompute,
                          mount=None,
                          return_context=False,
                          hash_pattern=None):
    #print("build_structured_cell", name)
    name2 = name + STRUC_ID
    c = context(toplevel=False)
    setattr(ctx, name2, c)
    if mount is not None:
        mount.pop("as_directory", None)
        c.mount(**mount)
    c.data = core_cell("mixed")
    c.data._hash_pattern = hash_pattern
    c.auth = core_cell("mixed")
    c.auth._hash_pattern = hash_pattern
    c.schema = core_cell("plain")
    c.buffer = core_cell("mixed")
    c.buffer._hash_pattern = hash_pattern

    sc = StructuredCell(data=c.data,
                        auth=c.auth,
                        schema=c.schema,
                        buffer=c.buffer,
                        inchannels=inchannels,
                        outchannels=outchannels,
                        hash_pattern=hash_pattern)
    c.example_data = core_cell("mixed")
    c.example_buffer = core_cell("mixed")
    c.example = StructuredCell(c.example_data,
                               buffer=c.example_buffer,
                               schema=c.schema)

    for cc in (c.data, c.buffer, c.schema, c.auth, c.example_data,
               c.example_buffer):
        if fingertip_no_recompute:
            cc._fingertip_recompute = False
        if fingertip_no_remote:
            cc._fingertip_remote = False

    if return_context:
        return sc, c
    else:
        return sc
Exemplo n.º 4
0
def translate_py_transformer(node, root, namespace, inchannels, outchannels, lib_path00, is_lib):
    #TODO: simple translation, without a structured cell
    assert not "code" in node ### node["code"] is an outdated attribute
    inchannels = [ic for ic in inchannels if ic[0] != "code"]

    parent = get_path(root, node["path"][:-1], None, None)
    name = node["path"][-1]
    lib_path0 = lib_path00 + "." + name if lib_path00 is not None else None
    ctx = context(toplevel=False)
    setattr(parent, name, ctx)

    result_name = node["RESULT"]
    if node["language"] == "ipython":
        assert result_name == "result"
    input_name = node["INPUT"]
    for c in inchannels:
        assert (not len(c)) or c[0] != result_name #should have been checked by highlevel

    with_result = node["with_result"]
    buffered = node["buffered"]
    interchannels = [as_tuple(pin) for pin in node["pins"]]
    plain = node["plain"]
    mount = node.get("mount", {})
    silk = (buffered or not plain)
    inp, inp_ctx = build_structured_cell(
      ctx, input_name, silk, plain, buffered, inchannels, interchannels,
      lib_path0,
      return_context=True
    )

    setattr(ctx, input_name, inp)
    if "input_schema" in mount:
        inp_ctx.schema.mount(**mount["input_schema"])
    for inchannel in inchannels:
        path = node["path"] + inchannel
        namespace[path, True] = inp.inchannels[inchannel], node

    assert result_name not in node["pins"] #should have been checked by highlevel
    all_pins = {}
    for pinname, pin in node["pins"].items():
        p = {"io": "input"}
        p.update(pin)
        all_pins[pinname] = p
    all_pins[result_name] = {"io": "output", "transfer_mode": "copy"}
    if node["SCHEMA"]:
        assert with_result
        all_pins[node["SCHEMA"]] = {
            "io": "input", "transfer_mode": "json",
            "access_mode": "json", "content_type": "json"
        }
    ctx.tf = transformer(all_pins)
    if node["debug"]:
        ctx.tf.debug = True
    if lib_path00 is not None:
        lib_path = lib_path00 + "." + name + ".code"
        ctx.code = libcell(lib_path)
    else:
        if node["language"] == "ipython":
            ctx.code = core_cell("ipython")
        else:
            ctx.code = core_cell("transformer")
        if "code" in mount:
            ctx.code.mount(**mount["code"])
        ctx.code._sovereign = True

    ctx.code.connect(ctx.tf.code)
    checksum = node.get("checksum", {})
    if "code" in checksum:
        ctx.code.set_checksum(checksum["code"])
    if "input" in checksum:
        inp.set_checksum(checksum["input"])
    namespace[node["path"] + ("code",), True] = ctx.code, node
    namespace[node["path"] + ("code",), False] = ctx.code, node

    for pin in list(node["pins"].keys()):
        target = getattr(ctx.tf, pin)
        inp.outchannels[(pin,)].connect(target)

    if with_result:
        plain_result = node["plain_result"]
        result, result_ctx = build_structured_cell(
            ctx, result_name, True, plain_result, False, [()],
            outchannels, lib_path0,
            return_context=True
        )
        if "result_schema" in mount:
            result_ctx.schema.mount(**mount["result_schema"])

        setattr(ctx, result_name, result)

        result_pin = getattr(ctx.tf, result_name)
        result_pin.connect(result.inchannels[()])
        if node["SCHEMA"]:
            schema_pin = getattr(ctx.tf, node["SCHEMA"])
            result.schema.connect(schema_pin)
        if "result" in checksum:
            result.set_checksum(checksum["result"])
        if "schema" in checksum:
            result.schema.set_checksum(checksum["schema"])
    else:
        for c in outchannels:
            assert len(c) == 0 #should have been checked by highlevel
        result = getattr(ctx.tf, result_name)
        namespace[node["path"] + (result_name,), False] = result, node

    namespace[node["path"], True] = inp, node
    namespace[node["path"], False] = result, node
Exemplo n.º 5
0
def translate_cell(node, root, namespace, inchannels, outchannels):
    path = node["path"]
    parent = get_path(root, path[:-1], None, None)
    name = path[-1]
    ct = node["celltype"]
    if ct == "structured":
        datatype = node["datatype"]
        ### TODO: harmonize datatype with schema type
        hash_pattern = node["hash_pattern"]
        mount = node.get("mount")
        child = build_structured_cell(
            parent,
            name,
            inchannels,
            outchannels,
            fingertip_no_remote=node.get("fingertip_no_remote", False),
            fingertip_no_recompute=node.get("fingertip_no_recompute", False),
            hash_pattern=hash_pattern,
            mount=mount)
        for inchannel in inchannels:
            cname = child.inchannels[inchannel].subpath
            if cname == "self":
                cpath = path
            else:
                if isinstance(cname, str):
                    cname = (cname, )
                cpath = path + cname
            namespace[cpath, "target"] = child.inchannels[inchannel], node
        for outchannel in outchannels:
            cpath = path + outchannel
            namespace[cpath, "source"] = child.outchannels[outchannel], node
    else:  #not structured
        for c in inchannels + outchannels:
            assert not len(c)  #should have been checked by highlevel
        if ct == "code":
            if node["language"] in ("python", "ipython"):
                if node.get("transformer"):
                    child = core_cell("transformer")
                else:
                    child = core_cell(node["language"])
            else:
                child = core_cell("text")
                child.set_file_extension(node["file_extension"])

        elif ct in direct_celltypes:
            child = core_cell(ct)
            if ct == "mixed":
                child._hash_pattern = node.get("hash_pattern")
        else:
            raise ValueError(
                ct)  #unknown celltype; should have been caught by high level
        if node.get("fingertip_no_recompute"):
            child._fingertip_recompute = False
        if node.get("fingertip_no_remote"):
            child._fingertip_remote = False
    setattr(parent, name, child)
    pathstr = "." + ".".join(path)
    checksum = node.get("checksum")
    if checksum is not None:
        if ct == "structured":
            set_structured_cell_from_checksum(child, checksum)
        else:
            if "value" in checksum and not len(inchannels):
                child._set_checksum(checksum["value"], initial=True)
            """
            if "temp" in checksum:
                assert len(checksum) == 1, checksum.keys()
                child._set_checksum(checksum["temp"], initial=True)
            """
    if ct != "structured":
        if "file_extension" in node:
            child.set_file_extension(node["file_extension"])
        if "mount" in node:
            child.mount(**node["mount"])

    return child
def translate_bash_transformer(node, root, namespace, inchannels, outchannels, lib_path00, is_lib):
    #TODO: simple translation, without a structured cell
    #TODO: there is a lot of common code with py transformer
    assert not "code" in node ### node["code"] is an outdated attribute

    # Just to register the "bash_transformer" lib
    from seamless.core.macro_mode import get_macro_mode, curr_macro    
    from seamless.lib.bash_transformer import bash_transformer as _

    inchannels = [ic for ic in inchannels if ic[0] != "code"]

    parent = get_path(root, node["path"][:-1], None, None)
    name = node["path"][-1]
    lib_path0 = lib_path00 + "." + name if lib_path00 is not None else None
    ctx = context(toplevel=False)
    setattr(parent, name, ctx)

    result_name = node["RESULT"]
    input_name = node["INPUT"]
    for c in inchannels:
        assert (not len(c)) or c[0] != result_name #should have been checked by highlevel

    with_result = node["with_result"]
    buffered = node["buffered"]
    pins = node["pins"].copy()
    for extrapin in ("bashcode", "pins"):
        assert extrapin not in node["pins"], extrapin
        pins[extrapin] =  {
            "transfer_mode": "ref",
            "access_mode": "plain",
            "content_type": None,
        }
    ctx.pins = core_cell("plain").set(list(pins.keys()))

    interchannels = [as_tuple(pin) for pin in pins]
    plain = node["plain"]
    mount = node.get("mount", {})
    inp, inp_ctx = build_structured_cell(
      ctx, input_name, True, plain, buffered, inchannels, interchannels,
      lib_path0,
      return_context=True
    )
    setattr(ctx, input_name, inp)
    if "input_schema" in mount:
        inp_ctx.schema.mount(**mount["input_schema"])
    for inchannel in inchannels:
        path = node["path"] + inchannel
        namespace[path, True] = inp.inchannels[inchannel], node

    assert result_name not in pins #should have been checked by highlevel
    all_pins = {}
    for pinname, pin in pins.items():
        p = {"io": "input"}
        p.update(pin)
        all_pins[pinname] = p
    all_pins[result_name] = {"io": "output", "transfer_mode": "copy"}    
    if node["SCHEMA"]:
        assert with_result
        all_pins[node["SCHEMA"]] = {
            "io": "input", "transfer_mode": "json",
            "access_mode": "json", "content_type": "json"
        }
    ctx.tf = transformer(all_pins)
    if node["debug"]:
        ctx.tf.debug = True
    if lib_path00 is not None:
        lib_path = lib_path00 + "." + name + ".code"
        ctx.code = libcell(lib_path)
    else:
        ctx.code = core_cell("text")
        if "code" in mount:
            ctx.code.mount(**mount["code"])
        ctx.code._sovereign = True

    ctx.pins.connect(ctx.tf.pins)
    ctx.code.connect(ctx.tf.bashcode)
    checksum = node.get("checksum", {})
    if "code" in checksum:
        ctx.code.set_checksum(checksum["code"])
    if "input" in checksum:
        inp.set_checksum(checksum["input"])

    with library.bind("bash_transformer"):
        ctx.executor_code = libcell(".executor_code")    
    ctx.executor_code.connect(ctx.tf.code)

    namespace[node["path"] + ("code",), True] = ctx.code, node
    namespace[node["path"] + ("code",), False] = ctx.code, node

    for pin in list(node["pins"].keys()):
        target = getattr(ctx.tf, pin)
        inp.outchannels[(pin,)].connect(target)

    if with_result:
        plain_result = node["plain_result"]
        result, result_ctx = build_structured_cell(
            ctx, result_name, True, plain_result, False, [()],
            outchannels, lib_path0,
            return_context=True
        )
        if "result_schema" in mount:
            result_ctx.schema.mount(**mount["result_schema"])

        setattr(ctx, result_name, result)

        result_pin = getattr(ctx.tf, result_name)
        result_pin.connect(result.inchannels[()])
        if node["SCHEMA"]:
            schema_pin = getattr(ctx.tf, node["SCHEMA"])
            result.schema.connect(schema_pin)
        if "result" in checksum:
            result.set_checksum(checksum["result"])
        if "schema" in checksum:
            result.schema.set_checksum(checksum["schema"])
    else:
        for c in outchannels:
            assert len(c) == 0 #should have been checked by highlevel
        result = getattr(ctx.tf, result_name)
        namespace[node["path"] + (result_name,), False] = result, node

    namespace[node["path"], True] = inp, node
    namespace[node["path"], False] = result, node
Exemplo n.º 7
0
def translate_py_reactor(node, root, namespace, inchannels, outchannels,
                         editchannels, lib_path00, is_lib):
    #TODO: simple-mode translation, without a structured cell
    skip_channels = ("code_start", "code_update", "code_stop")
    inchannels = [ic for ic in inchannels if ic[0] not in skip_channels]
    editchannels = [ec for ec in editchannels if ec[0] not in skip_channels]
    parent = get_path(root, node["path"][:-1], None, None)
    name = node["path"][-1]
    lib_path0 = lib_path00 + "." + name if lib_path00 is not None else None
    ctx = context(context=parent, name=name)
    setattr(parent, name, ctx)

    io_name = node["IO"]
    if len(inchannels):
        lib_path0 = None  #partial authority or no authority; no library update in either case

    buffered = node["buffered"]
    interchannels_in = [
        as_tuple(p) for p, pin in node["pins"].items() if pin["io"] == "output"
    ]
    interchannels_out = [
        as_tuple(p) for p, pin in node["pins"].items() if pin["io"] == "input"
    ]
    interchannels_edit = [
        as_tuple(p) for p, pin in node["pins"].items() if pin["io"] == "edit"
    ]

    all_inchannels = interchannels_in + inchannels  #highlevel must check that there are no duplicates
    all_outchannels = interchannels_out + [
        p for p in outchannels if p not in interchannels_out
    ]
    all_editchannels = interchannels_edit + [
        p for p in editchannels if p not in interchannels_edit
    ]

    plain = node["plain"]
    io_state = node.get("stored_state_io", None)
    if io_state is None:
        io_state = node.get("cached_state_io", None)
    io = build_structured_cell(ctx,
                               io_name,
                               True,
                               plain,
                               buffered,
                               all_inchannels,
                               all_outchannels,
                               io_state,
                               lib_path0,
                               editchannels=all_editchannels)
    setattr(ctx, io_name, io)
    for inchannel in inchannels:
        path = node["path"] + inchannel
        namespace[path, True] = io.inchannels[inchannel], node
    for outchannel in outchannels:
        path = node["path"] + outchannel
        namespace[path, False] = io.outchannels[outchannel], node
    for channel in editchannels:
        path = node["path"] + channel
        namespace[path, True] = io.editchannels[channel], node
        namespace[path, False] = io.editchannels[channel], node

    ctx.rc = reactor(node["pins"])
    for attr in ("code_start", "code_stop", "code_update"):
        if lib_path00 is not None:
            lib_path = lib_path00 + "." + name + "." + attr
            c = libcell(lib_path)
            setattr(ctx, attr, c)
        else:
            c = core_cell(node["language"])
            c._sovereign = True
            setattr(ctx, attr, c)
            if "mount" in node and attr in node["mount"]:
                c.mount(**node["mount"][attr])
        c.connect(getattr(ctx.rc, attr))
        code = node.get(attr)
        if code is None:
            code = node.get("cached_" + attr)
        try_set(c, code)
        namespace[node["path"] + (attr, ), True] = c, node
        namespace[node["path"] + (attr, ), False] = c, node

    for pinname, pin in node["pins"].items():
        target = getattr(ctx.rc, pinname)
        iomode = pin["io"]
        if iomode == "input":
            io.connect_outchannel((pinname, ), target)
        elif iomode == "edit":
            io.connect_editchannel((pinname, ), target)
        elif iomode == "output":
            io.connect_inchannel(target, (pinname, ))

    temp = node.get("TEMP")
    if temp is None:
        temp = {}
    for attr in ("code_start", "code_stop", "code_update"):
        if attr in temp:
            try_set(getattr(ctx, attr), temp[attr])
    iohandle = io.handle
    for k, v in temp.items():
        if k in ("code_start", "code_stop", "code_update"):
            continue
        setattr(iohandle, k, v)

    if not is_lib:  #clean up cached state and in_equilibrium, unless a library context
        node.pop("cached_state_io", None)

    namespace[node["path"], True] = io, node
    namespace[node["path"], False] = io, node
    node.pop("TEMP", None)
Exemplo n.º 8
0
def translate_cell(node,
                   root,
                   namespace,
                   inchannels,
                   outchannels,
                   editchannels,
                   lib_path0,
                   is_lib,
                   link_target=None):
    path = node["path"]
    parent = get_path(root, path[:-1], None, None)
    name = path[-1]
    ct = node["celltype"]
    if len(inchannels):
        lib_path0 = None  #partial authority or no authority; no library update in either case
    if ct == "structured":
        assert not link_target
        buffered = node["buffered"]
        datatype = node["datatype"]
        ### TODO: harmonize datatype with schema type
        if datatype in ("mixed", "array"):
            plain = False
        else:  #unknown datatype must be text
            plain = True
        silk = node["silk"]
        state = node.get("stored_state")
        if state is None:
            state = node.get("cached_state")
        mount = node.get("mount")
        child = build_structured_cell(parent,
                                      name,
                                      silk,
                                      plain,
                                      buffered,
                                      inchannels,
                                      outchannels,
                                      state,
                                      lib_path0,
                                      mount=mount,
                                      editchannels=editchannels)
        for inchannel in inchannels:
            cname = child.inchannels[inchannel].name
            if cname == "self":
                cpath = path
            else:
                if isinstance(cname, str):
                    cname = (cname, )
                cpath = path + cname
            namespace[cpath, True] = child.inchannels[inchannel], node
        for outchannel in outchannels:
            cpath = path + outchannel
            namespace[cpath, False] = child.outchannels[outchannel], node
    else:  #not structured
        for c in inchannels + outchannels + editchannels:
            assert not len(c)  #should have been checked by highlevel
        if link_target:
            child = core_link(link_target)
        elif lib_path0:
            lib_path + lib_path0 + "." + name
            if ct == "mixed":
                raise NotImplementedError  #libmixedcell + cell args
            child = libcell(lib_path)
            #TODO: allow fork to be set
        else:
            if ct == "code":
                if node["language"] in ("python", "ipython"):
                    if node["transformer"]:
                        child = core_cell("transformer")
                    else:
                        child = core_cell(node["language"])
                else:
                    child = core_cell("text")
            elif ct in ("text", "json"):
                child = core_cell(ct)
            elif ct in ("mixed", "array", "signal"):
                raise NotImplementedError(ct)
            else:
                raise ValueError(
                    ct
                )  #unknown celltype; should have been caught by high level
            child._sovereign = True
    setattr(parent, name, child)
    pathstr = "." + ".".join(path)
    if node.get("TEMP") is not None:
        if link_target is not None:
            warn(
                "Cell %s has a link target, cannot set construction constant" %
                pathstr)
        else:
            try_set(child, node["TEMP"])
    if ct != "structured":
        if link_target is not None:
            if "mount" in node:
                warn("Cell %s has a link target, cannot mount" % pathstr)
            stored_value = node.get("stored_value")
            if stored_value is not None:
                warn("Cell %s has a link target, cannot set stored value" %
                     pathstr)
            cached_value = node.get("cached_value")
            if cached_value is not None:
                warn("Cell %s has a link target, cannot set cached value" %
                     pathstr)
        else:
            if "file_extension" in node:
                child.set_file_extension(node["file_extension"])
            if "mount" in node:
                child.mount(**node["mount"])
            stored_value = node.get("stored_value")
            if stored_value is not None:
                assert child.authoritative
                try_set(child, stored_value)
            else:
                cached_value = node.get("cached_value")
                if cached_value is not None:
                    ###assert not child.authoritative
                    if not child.authoritative:
                        manager = child._get_manager()
                        try_set2(child, manager, cached_value, from_pin=True)

    if not is_lib:
        node.pop("cached_state", None)
    node.pop("TEMP", None)
    return child
Exemplo n.º 9
0
def translate_bash_transformer(node, root, namespace, inchannels, outchannels, lib_path00, is_lib):
    #TODO: simple translation, without a structured cell
    #TODO: there is a lot of common code with py transformer

    # Just to register the "bash_transformer" lib
    from seamless.lib.bash_transformer import bash_transformer as _

    inchannels = [ic for ic in inchannels if ic[0] != "code"]

    parent = get_path(root, node["path"][:-1], None, None)
    name = node["path"][-1]
    lib_path0 = lib_path00 + "." + name if lib_path00 is not None else None
    ctx = context(context=parent, name=name)
    setattr(parent, name, ctx)

    result_name = node["RESULT"]
    input_name = node["INPUT"]
    if len(inchannels):
        lib_path0 = None #partial authority or no authority; no library update in either case
    for c in inchannels:
        assert (not len(c)) or c[0] != result_name #should have been checked by highlevel

    with_result = node["with_result"]
    buffered = node["buffered"]
    pins = node["pins"].copy()
    for extrapin in ("bashcode", "pins"):
        assert extrapin not in node["pins"], extrapin
        pins[extrapin] =  {
            "transfer_mode": "ref",
            "access_mode": "default",
            "content_type": None,
        }
    ctx.pins = core_cell("json").set(list(pins.keys()))    

    interchannels = [as_tuple(pin) for pin in pins]
    plain = node["plain"]
    input_state = node.get("stored_state_input", None)
    mount = node.get("mount", {})
    if input_state is None:
        input_state = node.get("cached_state_input", None)
    inp, inp_ctx = build_structured_cell(
      ctx, input_name, True, plain, buffered, inchannels, interchannels,
      input_state, lib_path0,
      return_context=True
    )
    setattr(ctx, input_name, inp)
    if "input_schema" in mount:
        inp_ctx.schema.mount(**mount["input_schema"])
    for inchannel in inchannels:
        path = node["path"] + inchannel
        namespace[path, True] = inp.inchannels[inchannel], node

    assert result_name not in pins #should have been checked by highlevel
    all_pins = {}
    for pinname, pin in pins.items():
        p = {"io": "input"}
        p.update(pin)
        all_pins[pinname] = p
    all_pins[result_name] = {"io": "output", "transfer_mode": "copy"}
    if node["SCHEMA"]:
        assert with_result
        all_pins[node["SCHEMA"]] = {
            "io": "input", "transfer_mode": "json",
            "access_mode": "json", "content_type": "json"
        }
    in_equilibrium = node.get("in_equilibrium", False)
    ctx.tf = transformer(all_pins, in_equilibrium=in_equilibrium)
    if node["debug"]:
        ctx.tf.debug = True
    if lib_path00 is not None:
        lib_path = lib_path00 + "." + name + ".code"
        ctx.code = libcell(lib_path)
    else:
        ctx.code = core_cell("json")
        if "code" in mount:
            ctx.code.mount(**mount["code"])
        ctx.code._sovereign = True

    ctx.pins.connect(ctx.tf.pins)
    ctx.code.connect(ctx.tf.bashcode)
    code = node.get("code")
    if code is None:
        code = node.get("cached_code")
    ctx.code.set(code)
    temp = node.get("TEMP")
    if temp is None:
        temp = {}
    if "code" in temp:
        ctx.code.set(temp["code"])

    with library.bind("bash_transformer"):
        ctx.executor_code = libcell(".executor_code")    
    ctx.executor_code.connect(ctx.tf.code)

    inphandle = inp.handle
    for k,v in temp.items():
        if k == "code":
            continue
        setattr(inphandle, k, v)
    namespace[node["path"] + ("code",), True] = ctx.code, node
    namespace[node["path"] + ("code",), False] = ctx.code, node

    for pin in list(node["pins"].keys()):
        target = getattr(ctx.tf, pin)
        inp.connect_outchannel( (pin,) ,  target )

    if with_result:
        plain_result = node["plain_result"]
        result_state = node.get("cached_state_result", None)
        result, result_ctx = build_structured_cell(
            ctx, result_name, True, plain_result, False, [()],
            outchannels, result_state, lib_path0,
            return_context=True
        )
        if "result_schema" in mount:
            result_ctx.schema.mount(**mount["result_schema"])

        setattr(ctx, result_name, result)

        result_pin = getattr(ctx.tf, result_name)
        result.connect_inchannel(result_pin, ())
        if node["SCHEMA"]:
            schema_pin = getattr(ctx.tf, node["SCHEMA"])
            result.schema.connect(schema_pin)
    else:
        for c in outchannels:
            assert len(c) == 0 #should have been checked by highlevel
        result = getattr(ctx.tf, result_name)
        namespace[node["path"] + (result_name,), False] = result, node

    if not is_lib: #clean up cached state and in_equilibrium, unless a library context
        node.pop("cached_state_input", None)
        if not in_equilibrium:
            node.pop("cached_state_result", None)
        node.pop("in_equilibrium", None)

    namespace[node["path"], True] = inp, node
    namespace[node["path"], False] = result, node
    node.pop("TEMP", None)
Exemplo n.º 10
0
def build_structured_cell(ctx,
                          name,
                          silk,
                          plain,
                          buffered,
                          inchannels,
                          outchannels,
                          state,
                          lib_path0,
                          *,
                          editchannels=[],
                          mount=None,
                          return_context=False):
    #print("build_structured_cell", name, lib_path)
    name2 = name + STRUC_ID
    c = context(name=name2, context=ctx)
    setattr(ctx, name2, c)
    if mount is not None:
        c.mount(**mount)
    lib_path = lib_path0 + "." + name2 if lib_path0 is not None else None
    sovereign = True
    if lib_path:
        path = lib_path + ".form"
        cc = libcell(path)
    else:
        cc = core_cell("json")
        cc._sovereign = sovereign
    c.form = cc
    if plain:
        if lib_path:
            path = lib_path + ".data"
            cc = libcell(path)
        else:
            cc = core_cell("json")
            cc._sovereign = sovereign
        c.data = cc
        storage = None
    else:
        if lib_path:
            path = lib_path + ".storage"
            storage = libcell(path)
        else:
            storage = core_cell("text")
            storage._sovereign = sovereign
        c.storage = storage
        if lib_path:
            path = lib_path + ".data"
            c.data = libmixedcell(path,
                                  form_cell=c.form,
                                  storage_cell=c.storage)
        else:
            c.data = core_cell("mixed",
                               form_cell=c.form,
                               storage_cell=c.storage)
            c.data._sovereign = sovereign
    if silk:
        if lib_path:
            path = lib_path + ".schema"
            schema = libcell(path)
        else:
            schema = core_cell("json")
        c.schema = schema
    else:
        schema = None
    if buffered:
        if lib_path:
            path = lib_path + ".buffer_form"
            cc = libcell(path)
        else:
            cc = core_cell("json")
            cc._sovereign = sovereign
        c.buffer_form = cc
        if plain:
            if lib_path:
                path = lib_path + ".buffer_data"
                cc = libcell(path)
            else:
                cc = core_cell("json")
                cc._sovereign = sovereign
            c.buffer_data = cc
            buffer_storage = None
        else:
            if lib_path:
                path = lib_path + ".buffer_storage"
                buffer_storage = libcell(path)
            else:
                buffer_storage = core_cell("text")
                buffer_storage._sovereign = sovereign
            c.buffer_storage = buffer_storage
            if lib_path:
                path = lib_path + ".buffer_data"
                c.buffer_data = libmixedcell(
                    path,
                    form_cell=c.buffer_form,
                    storage_cell=c.buffer_storage,
                )
            else:
                c.buffer_data = core_cell(
                    "mixed",
                    form_cell=c.buffer_form,
                    storage_cell=c.buffer_storage,
                )
                c.buffer_data._sovereign = sovereign
        bufferwrapper = BufferWrapper(c.buffer_data, buffer_storage,
                                      c.buffer_form)
    else:
        bufferwrapper = None

    sc = StructuredCell(name,
                        c.data,
                        storage=storage,
                        form=c.form,
                        schema=schema,
                        buffer=bufferwrapper,
                        inchannels=inchannels,
                        outchannels=outchannels,
                        state=state,
                        editchannels=editchannels)
    if return_context:
        return sc, c
    else:
        return sc
Exemplo n.º 11
0
def build_structured_cell(
  ctx, name, silk, plain, buffered,
  inchannels, outchannels, lib_path0,
  *, editchannels=[], mount=None, return_context=False
):
    #print("build_structured_cell", name, lib_path)
    name2 = name + STRUC_ID
    c = context(toplevel=False)
    setattr(ctx, name2, c)
    if mount is not None:
        c.mount(**mount)
    lib_path = lib_path0 + "." + name2 if lib_path0 is not None else None
    sovereign = True
    if plain:
        if lib_path:
            path = lib_path + ".data"
            cc = libcell(path)
        else:
            cc = core_cell("mixed")
            cc._sovereign = sovereign
        c.data = cc
        storage = None
    else:
        if lib_path:
            path = lib_path + ".data"
            c.data = libcell(path, "mixed")
        else:
            c.data = core_cell("mixed")
            c.data._sovereign = sovereign
    if silk:
        if lib_path:
            path = lib_path + ".schema"
            schema = libcell(path)
        else:
            schema = core_cell("plain")
        c.schema = schema
    else:
        schema = None
        
    if buffered:
        if plain:
            if lib_path:
                path = lib_path + ".buffer"
                cc = libcell(path)
            else:
                cc = core_cell("mixed")
                cc._sovereign = sovereign
            c.buffer = cc
            storage = None
        else:
            if lib_path:
                path = lib_path + ".buffer"
                c.buffer = libcell(path, "mixed")
            else:
                c.buffer = core_cell("mixed")
                c.buffer._sovereign = sovereign
        buffer = c.buffer
    else:
        buffer = None
        
    sc = StructuredCell(
        name,
        c.data,
        schema=schema,
        buffer=buffer,
        plain=plain,
        inchannels=inchannels,
        outchannels=outchannels,
        editchannels=editchannels
    )
    if return_context:
        return sc, c
    else:
        return sc