class TestPortControl(TestCase): def setUp(self): """Do the test basic setup.""" self.port_control = PortControl() self.port = Port() # ---------------------------------------------------------------------- def test_load(self): file_name = "TESTE" self.assertIsNone(self.port_control.load(file_name)) file_name = "AAA.xml" self.assertIsNone(self.port_control.load(file_name)) # ---------------------------------------------------------------------- def test_export_xml(self): self.assertIsNone(self.port_control.export_xml()) # ---------------------------------------------------------------------- def test_export_python(self): self.assertIsNone(self.port_control.export_python()) # ---------------------------------------------------------------------- def test_print_port(self): #port = Port() #port.type = "c" #port.language = "c" #port.source = "xml" #port.code = "xxxx" self.assertIsNone(self.port_control.print_port(self.port)) # ---------------------------------------------------------------------- def test_add_port(self): #port = Port() #port.type = "c" #port.language = "c" #port.source = "xml" #port.code = "xxxx" self.assertIsNone(self.port_control.add_port(self.port)) # ---------------------------------------------------------------------- def test_delete_port(self): port_key = -1 self.assertIsNone(self.port_control.delete_port(port_key)) port_key = 0 self.assertIsNone(self.port_control.delete_port(port_key)) self.assertIsNone(self.port_control.delete_port(self.port))
def __get_extensions_xml(self, data_dir): if not os.path.exists(data_dir): return for file_name in os.listdir(data_dir): full_file_path = data_dir + "/" + file_name # Recursion to make it more interesting... if os.path.isdir(full_file_path): self.__get_extensions_xml(full_file_path) if not file_name.endswith(".xml"): continue code_template = CodeTemplateControl.load(full_file_path) if code_template is not None: code_template.file = full_file_path self.__code_templates[code_template.type] = code_template port = PortControl.load(full_file_path) if port is not None: port.file = full_file_path self.__ports[port.type] = port block = BlockControl.load(full_file_path) if block is not None: block.file = full_file_path self.__blocks[block.type] = block
def __load_xml(self, data_dir): if not os.path.exists(data_dir): return for file_name in os.listdir(data_dir): full_file_path = data_dir + "/" + file_name # Recursion to make it more interesting... if os.path.isdir(full_file_path): self.__load_xml(full_file_path) if not file_name.endswith(".xml"): continue code_template = CodeTemplateControl.load(full_file_path) if code_template is not None: code_template.source = "xml" self.code_templates[code_template.type] = code_template port = PortControl.load(full_file_path) if port is not None: port.source = "xml" self.ports[port.type] = port plugin = BlockControl.load(full_file_path) if plugin is not None: plugin.source = "xml" self.plugins[plugin.type] = plugin
def test_load(self): PortControl.load("test.xml")
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)
def test_export_xml(self): PortControl.export_xml() PortControl.load("test.xml") PortControl.print_port(self.create_port())