예제 #1
0
 def dot_to_png(self, dot_content, png_file, **kwargs):
     logger.info("dot_to_png from {} to {}".format(dot_content, png_file))
     basename = png_file[:-4]
     with open(basename + '.txt', "w") as fp:
         fp.write(dot_content)
     src = Source(dot_content)
     file_path = src.render(filename=basename, format='png')
     return os.path.basename(file_path)
예제 #2
0
def paint():
    upload_form = UploadForm()
    script_form = ScriptForm()
    if script_form.validate_on_submit():
        logger.info("submit: {} or save {}".format(
            script_form.submit_button.data, script_form.save_button.data))
        script_content = script_form.script_content.data
        image_path = "{}/../static".format(dir_path)
        image_name = "uml_{}.txt".format(uuid.uuid4())

        if script_form.diagram_name.data:
            image_name = script_form.diagram_name.data

        painter = Painter(image_name)
        painter.path = image_path
        if script_form.diagram_type.data == 1:
            script_form.diagram_path.data = painter.draw_graph(script_content)
        elif script_form.diagram_type.data == 2:
            painter.set_directed(False)
            script_form.diagram_path.data = painter.draw_graph(script_content)
        else:
            script_form.diagram_path.data = painter.draw_uml(script_content)

        script_form.diagram_content.data = painter.content

        if script_form.save_button.data:

            diagram_id = script_form.diagram_id.data
            if diagram_id:
                diagram = Diagram.query.filter_by(
                    diagram_id=diagram_id).first()
            else:
                diagram = Diagram()
            logger.info("save {}, {}".format(script_form.diagram_id.data,
                                             script_form.diagram_name.data))
            diagram.diagram_name = script_form.diagram_name.data
            diagram.diagram_script = script_form.script_content.data
            diagram.diagram_type = script_form.diagram_type.data
            diagram.image_path = script_form.diagram_path.data
            diagram.author_id = current_user.id
            tagName = script_form.diagram_tag.data
            tag = Tag.query.filter_by(name=tagName).first()
            if not tag:
                tag = Tag(name=tagName)
                db.session.add(tag)
                db.session.commit()

            diagram.tag_id = tag.id

            logger.info("save diagram {}".format(diagram))
            db.session.add(diagram)
            db.session.commit()

    else:
        logger.info(script_form.errors)
        #flash(script_form.errors, 'error')

    return render_template('diagram.html',
                           form=script_form,
                           upload_form=upload_form)
예제 #3
0
    def draw_graph(self, script):

        logger.info("script: {}, sep: {}".format(script, self.separator))
        title = ""

        node_set = set()
        node_list = []
        for line in script.strip().split('\n'):
            line = line.strip("; \t\r\n")
            if len(line) == 0:
                continue

            if line.startswith('title'):
                title = line.replace('title', '').strip(': ')

            if line.startswith('node'):
                node_set.add(line.replace('node', '').strip(': '))

            if self.separator not in line:
                continue
            logger.info("process line: {}".format(line))
            arr = line.split(self.separator)
            for node_desc in arr:
                pos = node_desc.find('[')
                if pos > 0:
                    node_desc = node_desc[:pos]

                node_set.add(node_desc.strip("; "))
            line = re.sub(r'\([^-]*\)', '', line)
            line = line.replace('cond=', 'label=')
            line = line.replace('\'', '"')
            node_list.append('\t{};'.format(line))

        parameters = {
            'diagram_name': title or "",
            'diagram_desc': node_set_desc(node_set),
            'diagram_content': '\n'.join(node_list)
        }

        with open(self.tplName, "r") as tpl_file:
            template = Template(tpl_file.read())
            dot_content = template.render(parameters)
            if self.name and self.name.endswith(".png"):
                png_file = "{}/{}".format(self.path, self.name)
                return self.dot_to_png(dot_content, png_file)
            else:
                self.content = dot_content.replace('\n', '')
                return ""
예제 #4
0
    def draw_uml(self, script_content):

        script_file = "{}/{}".format(self.path, self.name)
        if not script_file.endswith(".txt"):
            script_file = script_file[:-4] + ".txt"

        with open(script_file, "w") as fp:
            if not script_content.startswith("@startuml"):
                script_content = "@startuml\n{}\n@enduml\n".format(
                    script_content.replace('\r\n', '\n'))
            fp.write(script_content)
        logger.info("draw %s" % script_file)
        cmd = "java -jar plantuml.jar %s" % script_file
        logger.info("execute {}".format(cmd))
        os.system(cmd)
        return os.path.basename(script_file)[:-4] + ".png"
예제 #5
0
def upload_diagram():
    form = UploadForm()
    input_content = ""
    if form.validate_on_submit():
        file = form.script_file.data
        logger.info('filename={}'.format(file.filename))
        if file and allowed_file(file.filename):

            file_content = file.read()
            if file_content:
                input_content = file_content.decode('UTF-8').strip()
            file.close()

            logger.info('File successfully uploaded: {}'.format(input_content))
    else:
        logger.info(form.errors)
    upload_form = UploadForm()
    script_form = ScriptForm()
    script_form.script_content.data = input_content
    return render_template('diagram.html',
                           form=script_form,
                           upload_form=upload_form)