예제 #1
0
    def test_load(self):
        file_name = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                 "assets", "block.xml")
        block = BlockControl.load(file_name)
        assert isinstance(block, BlockModel)

        file_name = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                 "assets", "block_error.xml")

        block = BlockControl.load(file_name)
        assert not isinstance(block, BlockModel)
예제 #2
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
예제 #3
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
예제 #4
0
class TestBlockControl(TestCase):
    def setUp(self):
        """Do the test basic setup."""
        #data = {"label": ("Type"), "name":"type", "value": "text"}
        self.blockmodel = BlockModel()

        self.blockmodel.id = 1
        self.blockmodel.x = 2
        self.blockmodel.y = 2

        self.blockmodel.language = "c"
        self.blockmodel.framework = "opencv"
        self.blockmodel.help = "Adiciona bordas na imagem."
        self.blockmodel.label = "Teste BlockModel"
        self.blockmodel.color = "0:180:210:150"
        self.blockmodel.in_ports = [{
            "type": "mosaicode_c_opencv.extensions.ports.image",
            "name": "input_image",
            "label": "Input Image"
        }, {
            "type": "mosaicode_c_opencv.extensions.ports.int",
            "name": "border_size",
            "label": "Border Size"
        }]
        self.blockmodel.out_ports = [{
            "type": "mosaicode_c_opencv.extensions.ports.image",
            "name": "output_image",
            "label": "Output Image"
        }]
        self.blockmodel.group = "Experimental"

        self.blockmodel.properties = [{
            "label": "Color",
            "name": "color",
            "type": MOSAICODE_COLOR,
            "value": "#FF0000"
        }, {
            "name":
            "type",
            "label":
            "Type",
            "type":
            MOSAICODE_COMBO,
            "value":
            "IPL_BORDER_CONSTANT",
            "values": [
                "IPL_BORDER_CONSTANT", "IPL_BORDER_REPLICATE",
                "IPL_BORDER_REFLECT", "IPL_BORDER_WRAP"
            ]
        }, {
            "label": "Border Size",
            "name": "border_size",
            "type": MOSAICODE_INT,
            "value": "50"
        }]

        self.blockmodel.codes[0] = \
            "CvScalar get_scalar_color(const char * rgbColor){\n" + \
            "   if (strlen(rgbColor) < 13 || rgbColor[0] != '#')\n" + \
            "       return cvScalar(0,0,0,0);\n" + \
            "   char r[4], g[4], b[4];\n" + \
            "   strncpy(r, rgbColor+1, 4);\n" + \
            "   strncpy(g, rgbColor+5, 4);\n" + \
            "   strncpy(b, rgbColor+9, 4);\n" + \
            "\n" + \
            "   int ri, gi, bi = 0;\n" + \
            "   ri = (int)strtol(r, NULL, 16);\n" + \
            "   gi = (int)strtol(g, NULL, 16);\n" + \
            "   bi = (int)strtol(b, NULL, 16);\n" + \
            "\n" + \
            "   ri /= 257;\n" + \
            "   gi /= 257;\n" + \
            "   bi /= 257;\n" + \
            "   \n" + \
            "   return cvScalar(bi, gi, ri, 0);\n" + \
            "}\n"

        self.blockmodel.codes[1] = \
            "IplImage * block$id$_img_i0 = NULL;\n" + \
            "int block$id$_int_i1 = $prop[border_size]$;\n" + \
            "IplImage * block$id$_img_o0 = NULL;\n"

        self.blockmodel.codes[2] = \
            'if(block$id$_img_i0){\n' + \
            '\tCvSize size$id$ = cvSize(block$id$_img_i0->width +' + \
            ' block$id$_int_i1 * 2, block$id$_img_i0->height' + \
            ' + block$id$_int_i1 * 2);\n' + \
            '\tblock$id$_img_o0 = cvCreateImage(size$id$,' + \
            ' block$id$_img_i0->depth,block$id$_img_i0->nChannels);\n' + \
            '\tCvPoint point$id$ = cvPoint' + \
            '(block$id$_int_i1, block$id$_int_i1);\n' + \
            '\tCvScalar color = get_scalar_color("$prop[color]$");\n' + \
            '\tcvCopyMakeBorder(block$id$_img_i0, block$id$_img_o0,' + \
            ' point$id$, $prop[type]$, color);\n' + \
            '}\n'

        self.blockcontrol = BlockControl()

    # ----------------------------------------------------------------------
    def test_export_xml(self):
        self.assertIsNone(self.blockcontrol.export_xml())

    # ----------------------------------------------------------------------
    def test_export_python(self):
        self.assertIsNone(self.blockcontrol.export_python())

    # ----------------------------------------------------------------------
    def test_load(self):
        file_name = "Aa"
        self.assertIsNone(self.blockcontrol.load(file_name))
        file_name = ""
        self.assertIsNone(self.blockcontrol.load(file_name))
        file_name = "Teste.xml"
        self.assertIsNone(self.blockcontrol.load(file_name))
        file_name = "Teste.py"
        self.assertIsNone(self.blockcontrol.load(file_name))
        # file_name = None
        # self.assertIsNone(self.blockcontrol.load(file_name))

    # ----------------------------------------------------------------------
    def test_add_new_block(self):
        self.assertIsNone(self.blockcontrol.add_new_block(self.blockmodel))

        # NÃO TRATA None
        # self.assertIsNone(self.blockcontrol.add_new_block(None))

    # ----------------------------------------------------------------------
    def test_delete_block(self):
        #self.assertFalse(self.blockcontrol.delete_block("test_codegenerator.py"))
        #self.assertFalse(self.blockcontrol.delete_block("test_codegenerator.py"))

        #Apresenta um erro de System nao tem blockmodels:
        #self.assertFalse(self.blockcontrol.delete_block(self.blockmodel))
        #self.assertTrue(self.blockcontrol.delete_block(self.blockmodel))
        self.assertIsNotNone(self.blockcontrol.delete_block(self.blockmodel))

        self.blockmodel.id = 1
        self.blockmodel.x = 2
        self.blockmodel.y = 2
        self.blockmodel.type = "c"
        self.blockmodel.source = "/home/lucas/mosaicode/extensions/c/opencv/mosaicode.model.blockmodel"

        self.blockmodel.language = "c"
        self.blockmodel.framework = "opencv"
        self.blockmodel.help = "Adiciona bordas na imagem."
        self.blockmodel.label = "Testing A"
        self.blockmodel.color = "0:180:210:150"
        self.blockmodel.in_ports = [{
            "type": "mosaicode_c_opencv.extensions.ports.image",
            "name": "input_image",
            "label": "Input Image"
        }, {
            "type": "mosaicode_c_opencv.extensions.ports.int",
            "name": "border_size",
            "label": "Border Size"
        }]
        self.blockmodel.out_ports = [{
            "type": "mosaicode_c_opencv.extensions.ports.image",
            "name": "output_image",
            "label": "Output Image"
        }]
        self.blockmodel.group = "Experimental"

        self.blockmodel.properties = [{
            "label": "Color",
            "name": "color",
            "type": MOSAICODE_COLOR,
            "value": "#FF0000"
        }, {
            "name":
            "type",
            "label":
            "Type",
            "type":
            MOSAICODE_COMBO,
            "value":
            "IPL_BORDER_CONSTANT",
            "values": [
                "IPL_BORDER_CONSTANT", "IPL_BORDER_REPLICATE",
                "IPL_BORDER_REFLECT", "IPL_BORDER_WRAP"
            ]
        }, {
            "label": "Border Size",
            "name": "border_size",
            "type": MOSAICODE_INT,
            "value": "50"
        }]

        self.blockmodel.codes[0] = \
            "CvScalar get_scalar_color(const char * rgbColor){\n" + \
            "   if (strlen(rgbColor) < 13 || rgbColor[0] != '#')\n" + \
            "       return cvScalar(0,0,0,0);\n" + \
            "   char r[4], g[4], b[4];\n" + \
            "   strncpy(r, rgbColor+1, 4);\n" + \
            "   strncpy(g, rgbColor+5, 4);\n" + \
            "   strncpy(b, rgbColor+9, 4);\n" + \
            "\n" + \
            "   int ri, gi, bi = 0;\n" + \
            "   ri = (int)strtol(r, NULL, 16);\n" + \
            "   gi = (int)strtol(g, NULL, 16);\n" + \
            "   bi = (int)strtol(b, NULL, 16);\n" + \
            "\n" + \
            "   ri /= 257;\n" + \
            "   gi /= 257;\n" + \
            "   bi /= 257;\n" + \
            "   \n" + \
            "   return cvScalar(bi, gi, ri, 0);\n" + \
            "}\n"

        self.blockmodel.codes[1] = \
            "IplImage * block$id$_img_i0 = NULL;\n" + \
            "int block$id$_int_i1 = $prop[border_size]$;\n" + \
            "IplImage * block$id$_img_o0 = NULL;\n"

        self.blockmodel.codes[2] = \
            'if(block$id$_img_i0){\n' + \
            '\tCvSize size$id$ = cvSize(block$id$_img_i0->width +' + \
            ' block$id$_int_i1 * 2, block$id$_img_i0->height' + \
            ' + block$id$_int_i1 * 2);\n' + \
            '\tblock$id$_img_o0 = cvCreateImage(size$id$,' + \
            ' block$id$_img_i0->depth,block$id$_img_i0->nChannels);\n' + \
            '\tCvPoint point$id$ = cvPoint' + \
            '(block$id$_int_i1, block$id$_int_i1);\n' + \
            '\tCvScalar color = get_scalar_color("$prop[color]$");\n' + \
            '\tcvCopyMakeBorder(block$id$_img_i0, block$id$_img_o0,' + \
            ' point$id$, $prop[type]$, color);\n' + \
            '}\n'

        #self.assertFalse(self.blockcontrol.delete_block(self.blockmodel))
        #self.assertTrue(self.blockcontrol.delete_block(self.blockmodel))
        self.assertIsNotNone(self.blockcontrol.delete_block(self.blockmodel))

        # PARA QUE O TESTE ABAIXO SEJA EXECUTADO,
        # DEVE-SE CRIAR UM ARQUIVO c.xml DENTRO
        # DA PASTA mosaicode/extensions, QUE SE
        # ENCONTRA NA home do usuário. ASSIM,
        # O TESTE IRÁ ABRANGER 100% DA CLASSE.

        # LEMBRANDO QUE, ISTO É ERRADO, POIS NÃO
        # EXCLUIRÁ O PLUGIN EM SI, MAS, UM ARQUIVO
        # QUALQUER XML. CASO NÃO SE DELETE, NÃO APRESENTA
        # NENHUMA MENSAGEM DE ERRO.
        self.blockmodel.type = "c"
        self.blockmodel.source = "xml"

        #self.assertFalse(self.blockcontrol.delete_block(self.blockmodel))
        #self.assertFalse(self.blockcontrol.delete_block(self.blockmodel))
        self.assertIsNotNone(self.blockcontrol.delete_block(self.blockmodel))

    # ----------------------------------------------------------------------
    def test_print_block(self):

        #self.assertIsNone(self.blockcontrol.print_block("test_codegenerator.py"))
        self.assertIsNone(self.blockcontrol.print_block(self.blockmodel))
예제 #5
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)