def load(ctx): ctx.readme = libcell(".readme") ctx.tf = transformer({ "iterations": "input", "pi": "output" }) ctx.code = libcell(".code") ctx.code.connect(ctx.tf.code) ctx.iterations = link(ctx.tf.iterations) ctx.result = link(ctx.tf.pi)
def select(ctx, which): assert which in ("pi", "e") ctx.readme = libcell(".readme") ctx.loader = macro({}) if which == "pi": ctx.load = libcell(".pi.load") else: ctx.load = libcell(".e.load") ctx.load.connect(ctx.loader.code) compute = ctx.loader.gen_context ctx.iterations = cell() ctx.iterations.connect(compute.iterations) ctx.result = cell() compute.result.connect(ctx.result)
def select(ctx, which): assert which in ("pi", "e") ctx.readme = libcell(".readme") ctx.loader = macro({}) if which == "pi": ctx.load = libcell(".pi.load") else: ctx.load = libcell(".e.load") ctx.load.connect(ctx.loader.code) compute = ctx.loader.ctx ctx.iterations = cell() ctx.iterations.connect(compute.iterations) ctx.result = cell() compute.result.connect(ctx.result)
def load_e(ctx): ctx.tf = transformer({ "iterations": "input", "e": "output" }) ctx.code = libcell(".e.code") ctx.code.connect(ctx.tf.code) ctx.iterations = cell() ctx.iterations.connect(ctx.tf.iterations) ctx.result = cell() ctx.tf.e.connect(ctx.result)
def main(): global ctx, compute ctx = context(toplevel=True) ctx.select_compute = libcell("compute.select") ctx.compute = macro(select_params, lib="compute") ctx.select_compute.connect(ctx.compute.code) ctx.compute.which.cell().set("pi") compute = ctx.compute.gen_context ctx.iterations = cell().set(10000) ctx.iterations.connect(compute.iterations) ctx.result = cell() compute.result.connect(ctx.result)
def load(ctx): ctx.readme = libcell(".readme") def recreate(ctx, name0, with_buffer, with_schema, inchannels): name = "." + name0 ctx.struc = context(name="struc",context=ctx) ctx.struc.storage = libcell(name+".storage") ctx.struc.form = libcell(name+".form") ctx.struc.data = libmixedcell(name+".data", form_cell = ctx.struc.form, storage_cell = ctx.struc.storage, ) schema = None if with_schema: ctx.struc.schema = libcell(name+".schema") schema = ctx.struc.schema bufferwrapper = None if with_buffer: ctx.struc.buffer_storage = libcell(name+".buffer_storage") ctx.struc.buffer_form = libcell(name+".buffer_form") ctx.struc.buffer_data = libmixedcell(name+".buffer_data", form_cell = ctx.struc.buffer_form, storage_cell = ctx.struc.buffer_storage, ) bufferwrapper = BufferWrapper( ctx.struc.buffer_data, ctx.struc.buffer_storage, ctx.struc.buffer_form ) ctx.hub = StructuredCell( "hub", ctx.struc.data, storage = ctx.struc.storage, form = ctx.struc.form, schema = schema, buffer = bufferwrapper, inchannels = inchannels, outchannels = [], ) ctx.auth_json = context(name="auth_json", context=ctx) recreate(ctx.auth_json, "auth_json.struc", with_buffer=False, with_schema=False, inchannels=[]) ctx.auth = context(name="auth", context=ctx) recreate(ctx.auth, "auth.struc", with_buffer=False, with_schema=True, inchannels=[]) ctx.err = context(name="err", context=ctx) recreate(ctx.err, "err.struc", with_buffer=True, with_schema=True, inchannels=[]) ctx.nauth = context(name="nauth", context=ctx) try: recreate(ctx.nauth, "nauth.struc", with_buffer=False, with_schema=False, inchannels=[("z",)]) except: import traceback; traceback.print_exc()
def recreate(ctx, name0, with_buffer, with_schema, inchannels): name = "." + name0 ctx.struc = context(name="struc",context=ctx) ctx.struc.storage = libcell(name+".storage") ctx.struc.form = libcell(name+".form") ctx.struc.data = libmixedcell(name+".data", form_cell = ctx.struc.form, storage_cell = ctx.struc.storage, ) schema = None if with_schema: ctx.struc.schema = libcell(name+".schema") schema = ctx.struc.schema bufferwrapper = None if with_buffer: ctx.struc.buffer_storage = libcell(name+".buffer_storage") ctx.struc.buffer_form = libcell(name+".buffer_form") ctx.struc.buffer_data = libmixedcell(name+".buffer_data", form_cell = ctx.struc.buffer_form, storage_cell = ctx.struc.buffer_storage, ) bufferwrapper = BufferWrapper( ctx.struc.buffer_data, ctx.struc.buffer_storage, ctx.struc.buffer_form ) ctx.hub = StructuredCell( "hub", ctx.struc.data, storage = ctx.struc.storage, form = ctx.struc.form, schema = schema, buffer = bufferwrapper, inchannels = inchannels, outchannels = [], )
def recreate(ctx, name0, with_buffer, with_schema, inchannels): name = "." + name0 ctx.struc = context(name="struc", context=ctx) ctx.struc.storage = libcell(name + ".storage") ctx.struc.form = libcell(name + ".form") ctx.struc.data = libmixedcell( name + ".data", form_cell=ctx.struc.form, storage_cell=ctx.struc.storage, ) schema = None if with_schema: ctx.struc.schema = libcell(name + ".schema") schema = ctx.struc.schema bufferwrapper = None if with_buffer: ctx.struc.buffer_storage = libcell(name + ".buffer_storage") ctx.struc.buffer_form = libcell(name + ".buffer_form") ctx.struc.buffer_data = libmixedcell( name + ".buffer_data", form_cell=ctx.struc.buffer_form, storage_cell=ctx.struc.buffer_storage, ) bufferwrapper = BufferWrapper(ctx.struc.buffer_data, ctx.struc.buffer_storage, ctx.struc.buffer_form) ctx.hub = StructuredCell( "hub", ctx.struc.data, storage=ctx.struc.storage, form=ctx.struc.form, schema=schema, buffer=bufferwrapper, inchannels=inchannels, outchannels=[], )
def main(): global ctx, compute ctx = context(toplevel=True) ctx.select_compute = libcell("compute.select") ctx.compute = macro(select_params, lib="compute") ctx.select_compute.connect(ctx.compute.code) ###ctx.compute.which.cell().set("pi") # TODO: malfunctioning ### KLUDGE ctx.which_cell = cell().set("pi") ctx.which_cell.connect(ctx.compute.which) ### /KLUDGE compute = ctx.compute.ctx ctx.iterations = cell().set(10000) ctx.iterations.connect(compute.iterations) ctx.result = cell() compute.result.connect(ctx.result)
def _init_from_library(ctf, debug): # Just to register the "compiled_transformer" lib from seamless.lib.compiled_transformer import compiled_transformer as _ with library.bind("compiled_transformer"): ctf.gen_header_code = libcell(".gen_header.code") ctf.gen_header_params = libcell(".gen_header_params") ctf.gen_header = transformer(ctf.gen_header_params.value) ctf.gen_header_code.connect(ctf.gen_header.code) ctf.compiler_code = libcell(".compiler.code") ctf.compiler_params = libcell(".compiler_params") ctf.compiler = transformer(ctf.compiler_params.value) ctf.compiler_code.connect(ctf.compiler.code) ctf.translator_code = libcell(".translator.code") ctf.translator_params = libcell(".translator_params") ctf.translator = transformer(ctf.translator_params.value) if debug: ctf.translator.debug = True ctf.translator_code.connect(ctf.translator.code)
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
ctx.nauth = context(name="nauth", context=ctx) create(ctx.nauth, with_buffer=False, with_schema=False, inchannels=[("z",)]) nauth = ctx.nauth.hub nauth.handle["a"] = "value of nauth.a" nauth.handle["b"] = "value of nauth.b" print(nauth.value) lib = library.build(ctx) library.register("test", lib) print() print("!" * 80) print("LOAD") print("!" * 80) ctx2 = context(toplevel=True) ctx2.load_test = libcell("test.load") ctx2.test = macro({}, lib="test") ctx2.load_test.connect(ctx2.test.code) test = ctx2.test.gen_context print(test.readme.value) print("!" * 80) print("INIT") print("!" * 80) auth_json = test.auth_json.hub print(auth_json.value) auth = test.auth.hub print(auth.value, auth.schema.value) err = test.err.hub print("VALUE", err.value, "HANDLE", err.handle, err.schema.value)
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)
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
with_schema=False, inchannels=[("z", )]) nauth = ctx.nauth.hub nauth.handle["a"] = "value of nauth.a" nauth.handle["b"] = "value of nauth.b" print(nauth.value) lib = library.build(ctx) library.register("test", lib) print() print("!" * 80) print("LOAD") print("!" * 80) ctx2 = context(toplevel=True) ctx2.load_test = libcell("test.load") ctx2.test = macro({}, lib="test") ctx2.load_test.connect(ctx2.test.code) test = ctx2.test.gen_context print(test.readme.value) print("!" * 80) print("INIT") print("!" * 80) auth_json = test.auth_json.hub print(auth_json.value) auth = test.auth.hub print(auth.value, auth.schema.value) err = test.err.hub print("VALUE", err.value, "HANDLE", err.handle, err.schema.value)
def load(ctx): ctx.readme = libcell(".readme") def recreate(ctx, name0, with_buffer, with_schema, inchannels): name = "." + name0 ctx.struc = context(name="struc", context=ctx) ctx.struc.storage = libcell(name + ".storage") ctx.struc.form = libcell(name + ".form") ctx.struc.data = libmixedcell( name + ".data", form_cell=ctx.struc.form, storage_cell=ctx.struc.storage, ) schema = None if with_schema: ctx.struc.schema = libcell(name + ".schema") schema = ctx.struc.schema bufferwrapper = None if with_buffer: ctx.struc.buffer_storage = libcell(name + ".buffer_storage") ctx.struc.buffer_form = libcell(name + ".buffer_form") ctx.struc.buffer_data = libmixedcell( name + ".buffer_data", form_cell=ctx.struc.buffer_form, storage_cell=ctx.struc.buffer_storage, ) bufferwrapper = BufferWrapper(ctx.struc.buffer_data, ctx.struc.buffer_storage, ctx.struc.buffer_form) ctx.hub = StructuredCell( "hub", ctx.struc.data, storage=ctx.struc.storage, form=ctx.struc.form, schema=schema, buffer=bufferwrapper, inchannels=inchannels, outchannels=[], ) ctx.auth_json = context(name="auth_json", context=ctx) recreate(ctx.auth_json, "auth_json.struc", with_buffer=False, with_schema=False, inchannels=[]) ctx.auth = context(name="auth", context=ctx) recreate(ctx.auth, "auth.struc", with_buffer=False, with_schema=True, inchannels=[]) ctx.err = context(name="err", context=ctx) recreate(ctx.err, "err.struc", with_buffer=True, with_schema=True, inchannels=[]) ctx.nauth = context(name="nauth", context=ctx) try: recreate(ctx.nauth, "nauth.struc", with_buffer=False, with_schema=False, inchannels=[("z", )]) except: import traceback traceback.print_exc()
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
def load_e(ctx): ctx.tf = transformer({"iterations": "input", "e": "output"}) ctx.code = libcell(".e.code") ctx.code.connect(ctx.tf.code) ctx.iterations = link(ctx.tf.iterations) ctx.result = link(ctx.tf.e)
def translate_compiled_transformer(node, root, namespace, inchannels, outchannels, lib_path00, is_lib): #TODO: still a lot of common code with translate_py_transformer, put in functions inchannels = [ic for ic in inchannels if ic[0] != "code"] main_module_inchannels = [("objects", ) + ic[1:] for ic in inchannels if ic[0] == "_main_module"] inchannels = [ic for ic in inchannels if ic[0] != "_main_module"] 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) input_name = node["INPUT"] result_name = node["RESULT"] 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"] assert with_result #compiled transformers must have with_result buffered = node["buffered"] mount = node.get("mount", {}) plain = node["plain"] input_state = node.get("stored_state_input", None) 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, [()], 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 node[ "pins"] #should have been checked by highlevel assert "translator_result_" not in node[ "pins"] #should have been checked by highlevel all_pins = {} inputpins = [] for pinname, pin in node["pins"].items(): p = {"io": "input"} p.update(pin) if p["io"] == "input": inputpins.append(pinname) all_pins[pinname] = p all_pins[result_name] = "output" 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) temp = node.get("TEMP") if temp is None: temp = {} # Compiler ctx.language = cell("text").set(node["language"]) main_module_state = node.get("stored_state_main_module", None) if main_module_state is None: main_module_state = node.get("cached_state_main_module", None) ctx.main_module = build_structured_cell( ctx, "main_module", False, True, False, main_module_inchannels, [()], main_module_state, lib_path00, ) if "_main_module" in temp and len(temp["_main_module"]): temp_main_module = temp["_main_module"] main_module_handle = ctx.main_module.handle main_module_data = ctx.main_module.data.value if main_module_data is None: ctx.main_module.monitor.set_path((), {"objects": {}}, forced=True) main_module_data = ctx.main_module.data.value elif "objects" not in main_module_data: main_module_handle["objects"] = {} for objname, obj in temp_main_module.items(): for key, value in obj.items(): if objname in main_module_data["objects"] and \ key in main_module_data["objects"][objname]: msg = "WARNING: %s main module object '%s': %s already defined" print(msg % (node["path"], objname, key)) continue if objname not in main_module_data["objects"]: ctx.main_module.monitor.set_path(("objects", objname), {}, forced=True) main_module_handle["objects"][objname][key] = value elif main_module_state is None: ctx.main_module.monitor.set_path((), {}, forced=True) for ic in main_module_inchannels: icpath = node["path"] + ("_main_module", ) + ic[1:] namespace[icpath, True] = ctx.main_module.inchannels[ic], node compiler_verbose = node["main_module"]["compiler_verbose"] ctx.compiler_verbose = cell("json").set(compiler_verbose) target = node["main_module"].get("target") if target is not None: ctx.main_module.monitor.set_path(("target", ), target, forced=True) # Transformer itself ctf = ctx.tf = context(name="tf", context=ctx) debug = node["debug"] _init_from_library(ctf, debug) if lib_path00 is not None: lib_path = lib_path00 + "." + name + ".code" ctx.code = libcell(lib_path) else: ctx.code = cell("text") ctx.code.set_file_extension(node["file_extension"]) if "code" in mount: ctx.code.mount(**mount["code"]) 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) assert not node["SCHEMA"] ctx.pins = cell("json").set(all_pins) ctx.inputpins = cell("json").set(inputpins) c_inp = getattr(ctx, input_name + STRUC_ID) c_result = getattr(ctx, result_name + STRUC_ID) _finalize(ctx, ctf, inp, c_inp, result, c_result, input_name, result_name) if "header" in mount: ctx.header.mount(**mount["header"]) code = node.get("code") if code is None: code = node.get("cached_code") if code is not None: ctx.code.set(code) if "code" in temp: ctx.code.set(temp["code"]) inphandle = inp.handle for k, v in temp.items(): if k in ("code", "_main_module"): continue setattr(inphandle, k, v) namespace[node["path"] + ("code", ), True] = ctx.code, node namespace[node["path"] + ("code", ), False] = ctx.code, 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)
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
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)
}) ctx.code = libcell(".code") ctx.code.connect(ctx.tf.code) ctx.iterations = link(ctx.tf.iterations) ctx.result = link(ctx.tf.pi) lctx = context(toplevel=True) lctx.readme = cell("text").set("Compute pi iteratively") lctx.code = cell("python").set(compute_pi) lctx.load = cell("macro").set(load) lctx.equilibrate() lib = library.build(lctx) library.register("compute", lib) ctx = context(toplevel=True) ctx.load_compute = libcell("compute.load") ctx.compute = macro({}, lib="compute") ctx.load_compute.connect(ctx.compute.code) compute = ctx.compute.gen_context print(compute.readme.value) ctx.iterations = cell().set(10000) ctx.iterations.connect(compute.iterations) ctx.result = cell() print("START") print() compute.result.connect(ctx.result) ctx.equilibrate() print(ctx.status()) print(ctx.result.value)
'b': {'io': 'input', 'transfer_mode': 'copy', 'access_mode': 'object'}, 'result': "output" }) ctx.inputpins = cell("json").set(["a", "b"]) # Just to register the "compiled_transformer" lib from seamless.lib.compiled_transformer import compiled_transformer as _ libdict = library._lib["compiled_transformer"] libcells = [k for k in libdict.keys() if k.find("_STRUC") == -1] print(libcells) # 2: set up library with macro_mode_on(), library.bind("compiled_transformer"): ctf.gen_header_code = libcell(".gen_header.code") ctf.gen_header_params = libcell(".gen_header_params") ctf.gen_header = transformer(ctf.gen_header_params.value) ctf.gen_header_code.connect(ctf.gen_header.code) ctf.gen_header.result_name.cell().set("result") ctf.gen_header.input_name.cell().set("input") ctf.compiler_code = libcell(".compiler.code") ctf.compiler_params = libcell(".compiler_params") ctf.compiler = transformer(ctf.compiler_params.value) ctf.compiler_code.connect(ctf.compiler.code) ctf.translator_code = libcell(".translator.code") ctf.translator_params = libcell(".translator_params") ctf.translator = transformer(ctf.translator_params.value) ctf.translator_code.connect(ctf.translator.code)
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
'access_mode': 'object' }, 'result': "output" }) ctx.inputpins = cell("json").set(["a", "b"]) # Just to register the "compiled_transformer" lib from seamless.lib.compiled_transformer import compiled_transformer as _ libdict = library._lib["compiled_transformer"] libcells = [k for k in libdict.keys() if k.find("_STRUC") == -1] print(libcells) # 2: set up library with macro_mode_on(), library.bind("compiled_transformer"): ctf.gen_header_code = libcell(".gen_header.code") ctf.gen_header_params = libcell(".gen_header_params") ctf.gen_header = transformer(ctf.gen_header_params.value) ctf.gen_header_code.connect(ctf.gen_header.code) ctf.gen_header.result_name.cell().set("result") ctf.gen_header.input_name.cell().set("input") ctf.compiler_code = libcell(".compiler.code") ctf.compiler_params = libcell(".compiler_params") ctf.compiler = transformer(ctf.compiler_params.value) ctf.compiler_code.connect(ctf.compiler.code) ctf.translator_code = libcell(".translator.code") ctf.translator_params = libcell(".translator_params") ctf.translator = transformer(ctf.translator_params.value) ctf.translator_code.connect(ctf.translator.code)