def create_block(self, diagram_control=None): if diagram_control is None: diagram_control = self.create_diagram_control() block_model = BlockModel() block_model.maxIO = 2 block = Block(diagram_control.diagram, block_model) block.language = "language" block.properties = [{"name": "test", "label": "Test", "type": "Test"}] block.ports = [{ "type": "Test", "label": "Input", "conn_type": "Input", "name": "input" }, { "type": "Test", "label": "Output", "conn_type": "Outpu", "name": "output" }] BlockControl.load_ports(block, System.get_ports()) DiagramControl.add_block(diagram_control.diagram, block) return block
def test_load_ports(self): block = self.create_block() # if it is not a dictionary block.ports.append("x") # if "type" not in port: block.ports.append({ "label": "Output", "conn_type": "Output", "name": "output" }) # if "conn_type" not in port block.ports.append({ "type": "Output", "label": "Output", "name": "output" }) # Port ok block.ports.append({ "type": "Output", "label": "Output", "conn_type": "Output", "name": "output" }) BlockControl.load_ports(block, System.get_ports())
def __load_libs(self): # Create user directory if does not exist if not os.path.isdir(System.get_user_dir() + "/extensions/"): try: os.makedirs(System.get_user_dir() + "/extensions/") except: pass # Load CodeTemplates, Blocks and Ports self.__code_templates.clear() self.__ports.clear() self.__blocks.clear() # First load ports on python classes. # They are installed with mosaicode as root def walk_lib_packages(path=None, name_par=""): for importer, name, ispkg in pkgutil.iter_modules(path, name_par + "."): if path is None and name.startswith("." + System.APP): name = name.replace('.', '', 1) if not name.startswith(System.APP+"_lib") and not name_par.startswith(System.APP+"_lib"): continue if ispkg: if name_par is not "" and not name.startswith(System.APP): name = name_par + "." + name __import__(name) path = getattr(sys.modules[name], '__path__', None) or [] walk_lib_packages(path, name) else: module = __import__(name, fromlist="dummy") for class_name, obj in inspect.getmembers(module): if not inspect.isclass(obj): continue modname = inspect.getmodule(obj).__name__ if not modname.startswith(System.APP+"_lib"): continue instance = obj() if isinstance(instance, CodeTemplate): self.__code_templates[instance.type] = instance if isinstance(instance, Port): self.__ports[instance.type] = instance if isinstance(instance, BlockModel): if instance.label != "": self.__blocks[instance.type] = instance walk_lib_packages(None, "") # Load XML files in application space self.__load_xml(System.DATA_DIR + "extensions") # Load XML files in user space self.__load_xml(System.get_user_dir() + "/extensions") for key in self.__blocks: try: block = self.__blocks[key] BlockControl.load_ports(block, self.__ports) except: print("Error in loading plugin " + key)
def test_load_ports(self): block = self.create_block() block.ports.append("x") block.ports.append({ "type": "ERRO!", "label": "Output", "conn_type": "Output", "name": "output" }) block.ports.append({ "label": "Output", "conn_type": "Output", "name": "output" }) BlockControl.load_ports(block, System.get_ports())
def create_block(self, diagram_control=None): if diagram_control is None: diagram_control = self.create_diagram_control() System() block_model = BlockModel() System.add_port(self.create_port()) block_model.ports = [{ "type": "Test", "label": "Click", "conn_type": "Input", "name": "0" }, { "type": "Test", "label": "Click", "conn_type": "Output", "name": "1" }, { "type": "Test", "label": "Click", "conn_type": "Input", "name": "2" }, { "type": "Test", "label": "Click", "conn_type": "Output", "name": "3" }] block_model.help = "Test" block_model.label = "Test" block_model.color = "200:200:25:150" block_model.group = "Test" block_model.codes = {"code0": "Test", "Code1": "Test", "Code2": "Test"} block_model.type = "Test" block_model.language = "Test" block_model.properties = [{ "name": "test", "label": "Test", "value": "0", "type": MOSAICODE_FLOAT }] block_model.extension = "Test" block_model.file = None result = BlockControl.load_ports(block_model, System.get_ports()) System.add_block(block_model) self.assertEquals(result[1], "Success") self.assertEquals(result[0], True) self.assertEquals(len(block_model.ports), 4) block = Block(diagram_control.diagram, block_model) self.assertEquals(len(block.ports), 4) return block
def __load_extensions(self): # Load CodeTemplates, Blocks and Ports self.__code_templates.clear() self.__ports.clear() self.__blocks.clear() # First load ports on python classes. # They are installed with mosaicode as root def walk_lib_packages(path=None, name_par=""): for importer, name, ispkg in pkgutil.iter_modules( path, name_par + "."): if path is None and name.startswith("." + System.APP): name = name.replace('.', '', 1) if not name.startswith(System.APP + "_lib") and not name_par.startswith( System.APP + "_lib"): continue if ispkg: if name_par != "" and not name.startswith(System.APP): name = name_par + "." + name __import__(name) path = getattr(sys.modules[name], '__path__', None) or [] walk_lib_packages(path, name) else: module = __import__(name, fromlist="dummy") for class_name, obj in inspect.getmembers(module): if not inspect.isclass(obj): continue modname = inspect.getmodule(obj).__name__ if not modname.startswith(System.APP + "_lib"): continue try: instance = obj() except Exception as error: continue if isinstance(instance, BlockModel): if instance.label != "": self.__blocks[instance.type] = instance continue elif isinstance(instance, Port): self.__ports[instance.type] = instance continue elif isinstance(instance, CodeTemplate): self.__code_templates[instance.type] = instance continue walk_lib_packages(None, "") # Load XML files in user space data_dir = System.get_user_dir() + "/extensions" if not os.path.exists(data_dir): return # List of languages for languages in os.listdir(data_dir): lang_path = os.path.join(data_dir, languages) # Load Code Templates for file_name in os.listdir( os.path.join(lang_path, "codetemplates")): if not file_name.endswith(".json"): continue file_path = os.path.join(lang_path, "codetemplates") file_path = os.path.join(file_path, file_name) code_template = CodeTemplateControl.load(file_path) if code_template is not None: code_template.file = file_path self.__code_templates[ code_template.type] = code_template # Load Ports for file_name in os.listdir(os.path.join(lang_path, "ports")): if not file_name.endswith(".json"): continue file_path = os.path.join(lang_path, "ports") file_path = os.path.join(file_path, file_name) port = PortControl.load(file_path) if port is not None: port.file = file_path self.__ports[port.type] = port # Load Blocks for extension_name in os.listdir( os.path.join(lang_path, "blocks")): extension_path = os.path.join(lang_path, "blocks") extension_path = os.path.join(extension_path, extension_name) for group_name in os.listdir(extension_path): group_path = os.path.join(extension_path, group_name) for file_name in os.listdir(group_path): if not file_name.endswith(".json"): continue file_path = os.path.join(group_path, file_name) block = BlockControl.load(file_path) if block is not None: block.file = file_path self.__blocks[block.type] = block for key in self.__blocks: try: block = self.__blocks[key] BlockControl.load_ports(block, self.__ports) except: print("Error in loading block " + key)