def __get_code_generator(self, diagram): if diagram.language is None: message = "You shall not generate the code of an empty diagram!" Dialog().message_dialog("Error", message, self.main_window) return None if diagram.code_template is not None: return CodeGenerator(diagram) template_list = [] code_templates = System.get_code_templates() for key in code_templates: if code_templates[key].language == diagram.language: template_list.append(code_templates[key]) if len(template_list) == 0: message = "Generator not available for the language " + diagram.language + "." Dialog().message_dialog("Error", message, self.main_window) return None if len(template_list) == 1: diagram.code_template = deepcopy(template_list[0]) return CodeGenerator(diagram) select = SelectCodeTemplate(self.main_window, template_list) diagram.code_template = deepcopy(select.get_value()) return CodeGenerator(diagram)
def test_generate_code(self): diagram = self.create_diagram() self.code_generator = CodeGenerator(diagram) self.code_generator.generate_code() diagram.code_template = None self.code_generator.generate_code() diagram = self.create_full_diagram() self.code_generator = CodeGenerator(diagram) self.code_generator.generate_code()
def run(self, codes=None): """ This method runs the code. """ diagram = self.get_current_diagram() generator, message = CodeGenerator.get_code_generator(diagram) if generator is None: return False self.save_source(codes=codes, generator=generator) command = diagram.code_template.command command = command.replace("$dir_name$", System.get_dir_name(diagram)) def __run(self): process = subprocess.Popen(command, cwd=System.get_dir_name(diagram), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid) self.threads[thread] = diagram, process self.main_window.toolbar.update_threads(self.threads) (stdout_data, stderr_data) = process.communicate() System.log(stdout_data + "\n") System.log(stderr_data + "\n") del self.threads[thread] self.main_window.toolbar.update_threads(self.threads) System.log("Executing Code:\n" + command) thread = Thread(target=__run, args=(self, )) thread.start() return True
def get_code_template(self): code_template = CodeTemplate() for key in System.code_templates: if System.code_templates[key].language == self.diagram.language: code_template = System.code_templates[key] break generator = CodeGenerator(self.diagram, code_template) return generator
def test_constructor(self): self.code_generator = CodeGenerator(None) diagram = self.create_diagram() self.code_generator = CodeGenerator(diagram) diagram = self.create_diagram() diagram.code_template = None self.code_generator = CodeGenerator(diagram) diagram = self.create_diagram() diagram.language = None self.code_generator = CodeGenerator(diagram)
def save_source(self, codes=None, generator=None): """ This method saves the source codes. """ diagram = self.get_current_diagram() result, msg = CodeGenerator.save_source(diagram=diagram, codes=codes, generator=generator) if not result: self.message_dialog = MessageDialog("Error", msg, self.main_window) self.message_dialog.run()
class TestCodeGenerator(TestBase): def test_constructor(self): self.code_generator = CodeGenerator(None) diagram = self.create_diagram() self.code_generator = CodeGenerator(diagram) diagram = self.create_diagram() diagram.code_template = None self.code_generator = CodeGenerator(diagram) diagram = self.create_diagram() diagram.language = None self.code_generator = CodeGenerator(diagram) def test_generate_code(self): diagram = self.create_diagram() self.code_generator = CodeGenerator(diagram) self.code_generator.generate_code() diagram.code_template = None self.code_generator.generate_code() diagram = self.create_full_diagram() self.code_generator = CodeGenerator(diagram) self.code_generator.generate_code()
def view_source(self): """ This method view the source code. """ diagram = self.get_current_diagram() generator, msg = CodeGenerator.get_code_generator(diagram) if generator is not None: codes = generator.generate_code() self.code_window = CodeWindow(self.main_window, codes) self.code_window.run() self.code_window.close() self.code_window.destroy() else: self.message_dialog = MessageDialog("Error", msg, self.main_window) self.message_dialog.run()
def setUp(self): self.code_generator = CodeGenerator(self.create_diagram())
class TestCodeGenerator(TestBase): def setUp(self): self.code_generator = CodeGenerator(self.create_diagram()) def test_generate_code(self): self.code_generator.generate_code()
def setUp(self): """Do the test basic setup.""" 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 = "Add Border" 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' win = MainWindow() self.diagram = Diagram(win) self.codetemplate = CodeTemplate() #block = Block(diagram, blockmodel) #diagram.language = None self.code_generator = CodeGenerator(None, None) self.diagram.language = None self.code_generator = CodeGenerator(self.diagram, None) self.diagram.language = "" self.code_generator = CodeGenerator(self.diagram, None) self.code_generator = CodeGenerator(self.diagram, self.codetemplate)
class TestCodeGenerator(TestCase): def setUp(self): """Do the test basic setup.""" 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 = "Add Border" 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' win = MainWindow() self.diagram = Diagram(win) self.codetemplate = CodeTemplate() #block = Block(diagram, blockmodel) #diagram.language = None self.code_generator = CodeGenerator(None, None) self.diagram.language = None self.code_generator = CodeGenerator(self.diagram, None) self.diagram.language = "" self.code_generator = CodeGenerator(self.diagram, None) self.code_generator = CodeGenerator(self.diagram, self.codetemplate) # ---------------------------------------------------------------------- #def test_replace_wildcards(self): # self.code_generator.replace_wildcards("Teste") # ---------------------------------------------------------------------- def test_get_dir_name(self): self.assertIsNotNone(self.code_generator.get_dir_name()) #self.code_generator.get_dir_name() # ---------------------------------------------------------------------- def test_get_filename(self): #from mosaicode.system import System as System #print "Ad: " + System.properties.default_directory self.assertIsNotNone(self.code_generator.get_filename()) #self.code_generator.get_filename() # ---------------------------------------------------------------------- #def test_change_directory(self): # self.code_generator.change_directory() # ---------------------------------------------------------------------- #def test_sort_blocks(self): # self.code_generator.sort_blocks() # ---------------------------------------------------------------------- #def test_get_max_weight(self): # self.code_generator.get_max_weight() # ---------------------------------------------------------------------- #def test_generate_parts(self): # self.code_generator.generate_parts() # ---------------------------------------------------------------------- def test_replace_key_value(self): key = "2" value = "3" self.assertIsNone( self.code_generator.replace_key_value(self.blockmodel, key, value)) key = "1" value = "1" self.assertIsNone( self.code_generator.replace_key_value(self.blockmodel, key, value)) #self.code_generator.replace_key_value() # ---------------------------------------------------------------------- def test_generate_block_code(self): #self.code_generator.generate_block_code() self.assertIsNone( self.code_generator.generate_block_code(self.blockmodel)) # ---------------------------------------------------------------------- def test_generate_code(self): #self.code_generator.generate_code() self.assertIsNotNone(self.code_generator.generate_code()) # ---------------------------------------------------------------------- def test_save_code(self): name = "None" code = "None" self.assertIsNone(self.code_generator.save_code(name, code)) name = "None" code = None self.assertIsNone(self.code_generator.save_code(name, code)) # ESTE TESTE E NECESSARIO PARA VERIFICAR # SE PODE ACEITAR NONE. EXISTE UM if..else # COM name E code = None #name = None #code = None #self.assertIsNone(self.code_generator.save_code(name, code)) # ---------------------------------------------------------------------- def test_run(self): self.code = None self.assertIsNone(self.code_generator.run(self.code)) self.code = CodeTemplate() self.assertIsNone(self.code_generator.run(self.code)) self.code = "Teste()" self.assertIsNone(self.code_generator.run(self.code))
class TestCodeGenerator(TestCase): def setUp(self): """Do the test basic setup.""" win = MainWindow() diagram = Diagram(win) block = Block() self.code_generator = CodeGenerator(diagram) # ---------------------------------------------------------------------- def test_replace_wildcards(self): self.code_generator.replace_wildcards("Teste") # ---------------------------------------------------------------------- def test_get_dir_name(self): self.code_generator.get_dir_name() # ---------------------------------------------------------------------- def test_get_filename(self): self.code_generator.get_filename() # ---------------------------------------------------------------------- def test_change_directory(self): self.code_generator.change_directory() # ---------------------------------------------------------------------- def test_sort_blocks(self): self.code_generator.sort_blocks() # ---------------------------------------------------------------------- def test_get_max_weight(self): self.code_generator.get_max_weight() # ---------------------------------------------------------------------- def test_generate_parts(self): self.code_generator.generate_parts() # ---------------------------------------------------------------------- def test_generate_block_code(self): self.code_generator.generate_block_code() # ---------------------------------------------------------------------- def test_generate_code(self): self.code_generator.generate_code() # ---------------------------------------------------------------------- def test_save_code(self): self.code_generator.save_code() # ---------------------------------------------------------------------- def test_compile(self): self.code_generator.compile() # ---------------------------------------------------------------------- def test_execute(self): self.code_generator.execute()
def setUp(self): """Do the test basic setup.""" win = MainWindow() diagram = Diagram(win) block = Block() self.code_generator = CodeGenerator(diagram)