Ejemplo n.º 1
0
 def add_extension(self, element):
     if isinstance(element, Port):
         PortControl.add_port(element)
     elif isinstance(element, CodeTemplate):
         CodeTemplateControl.add_code_template(element)
     elif isinstance(element, BlockModel):
         BlockControl.add_new_block(element)
         self.update_blocks()
     System.reload()
Ejemplo n.º 2
0
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))
Ejemplo n.º 3
0
 def test_add_port(self):
     PortControl()
     port = self.create_port()
     PortControl.delete_port(port.type)
     System()
     PortControl.add_port(self.create_port())
     System.reload()
     PortControl.delete_port(port.type)
     System.add_port(port)
     PortControl.delete_port(port.type)
Ejemplo n.º 4
0
 def delete_port(self, port_key):
     result = PortControl.delete_port(port_key)
     if not result[0]:
         self.message_dialog = MessageDialog("Error", result[1],
                                             self.main_window)
         self.message_dialog.run()
     System.reload()
Ejemplo n.º 5
0
        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
Ejemplo n.º 6
0
 def delete_extension(self, element, element_type):
     if isinstance(element_type, Port):
         if not PortControl.delete_port(element):
             message = "This port is a python file installed in the System.\n"
             message = message + "Sorry, you can't remove it"
             MessageDialog("Error", message, self.main_window).run()
             return False
         System.reload()
     elif isinstance(element_type, CodeTemplate):
         filename = CodeTemplateControl.delete_code_template(element)
         if not filename:
             message = "This code template does not exist."
             MessageDialog("Error", message, self.main_window).run()
             return False
         if filename is None:
             message = "This code template is a python file installed in the System.\n"
             message = message + "Sorry, you can't remove it"
             MessageDialog("Error", message, self.main_window).run()
             return False
         System.reload()
         return True
     elif isinstance(element_type, BlockModel):
         if not BlockControl.delete_block(element):
             message = "This block is a python file installed in the System.\n"
             message = message + "Sorry, you can't remove it"
             MessageDialog("Error", message, self.main_window).run()
             return False
         self.update_blocks()
     MessageDialog("Info", str(element) + " deleted.", self.main_window).run()
     return True
Ejemplo n.º 7
0
        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
Ejemplo n.º 8
0
 def setUp(self):
     self.port_control = PortControl()
Ejemplo n.º 9
0
 def test_print_port(self):
     PortControl.print_port(self.create_port())
Ejemplo n.º 10
0
 def test_load(self):
     PortControl.load("test.xml")
Ejemplo n.º 11
0
 def test_export_xml(self):
     PortControl.export_xml()
Ejemplo n.º 12
0
 def test_delete_port(self):
     PortControl.delete_port(self.create_port())
Ejemplo n.º 13
0
 def print_ports(cls):
     # This method is used by the launcher class
     ports = System.get_ports()
     for port in ports:
         print "--------------------- "
         PortControl.print_port(ports[port])
Ejemplo n.º 14
0
 def add_port(self, port):
     PortControl.add_port(port)
     System.reload()
Ejemplo n.º 15
0
 def export_xml(cls):
     System()
     BlockControl.export_xml()
     PortControl.export_xml()
     CodeTemplateControl.export_xml()
Ejemplo n.º 16
0
        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)
Ejemplo n.º 17
0
 def export_python(cls):
     System()
     BlockControl.export_python()
     PortControl.export_python()
     CodeTemplateControl.export_python()
Ejemplo n.º 18
0
 def print_ports(cls):
     for port in System.ports:
         print "--------------------- "
         PortControl.print_port(System.ports[port])
Ejemplo n.º 19
0
 def add_port(self, port):
     PortControl.add_port(port)
Ejemplo n.º 20
0
 def test_add_port(self):
     PortControl.add_port(self.create_port())
Ejemplo n.º 21
0
 def delete_port(self, port_key):
     if not PortControl.delete_port(port_key):
         message = "This port is a python file installed in the System.\n"
         message = message + "Sorry, you can't remove it"
         Dialog().message_dialog("Error", message, self.main_window)
     System.reload()
Ejemplo n.º 22
0
 def test_export_xml(self):
     PortControl.export_xml()
     PortControl.load("test.xml")
     PortControl.print_port(self.create_port())
Ejemplo n.º 23
0
 def setUp(self):
     """Do the test basic setup."""
     self.port_control = PortControl()
     self.port = Port()