def export_xml(cls):
     System()
     BlockControl.export_xml()
     PortControl.export_xml()
     CodeTemplateControl.export_xml()
예제 #2
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)
 def export_python(cls):
     System()
     BlockControl.export_python()
     PortControl.export_python()
     CodeTemplateControl.export_python()
예제 #4
0
 def test_print_template(self):
     CodeTemplateControl.print_template(self.create_code_template())
예제 #5
0
 def test_init(self):
     CodeTemplateControl()
예제 #6
0
 def test_add_code_template(self):
     System()
     System.reload()
     code_template = self.create_code_template()
     CodeTemplateControl.add_code_template(code_template)
예제 #7
0
 def test_load(self):
     file_name = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                              "assets", "codetemplate.xml")
     CodeTemplateControl.load(file_name)
예제 #8
0
 def test_export_xml(self):
     System()
     System.reload()
     CodeTemplateControl.export_xml()
예제 #9
0
 def print_templates(cls):
     # This method is used by the launcher class
     code_templates = System.get_code_templates()
     for template in code_templates:
         print "--------------------- "
         CodeTemplateControl.print_template(code_templates[template])
예제 #10
0
 def add_code_template(self, code_template):
     CodeTemplateControl.add_code_template(code_template)
     System.reload()