~jaap.karssenberg/zim/pyzim-gtk3

« back to all changes in this revision

Viewing changes to zim/plugins/sequencediagrameditor.py

  • Committer: Jaap Karssenberg
  • Date: 2014-03-08 11:47:43 UTC
  • mfrom: (668.1.49 pyzim-refactor)
  • Revision ID: jaap.karssenberg@gmail.com-20140308114743-fero6uvy9zirbb4o
Merge branch with refactoring

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
# Copyright 2011 Greg Warner <gdwarner@gmail.com>
 
4
# (Pretty much copied from diagrameditor.py)
 
5
 
 
6
 
 
7
from zim.plugins import PluginClass
 
8
from zim.plugins.base.imagegenerator import ImageGeneratorPlugin, ImageGeneratorClass
 
9
 
 
10
from zim.fs import File, TmpFile
 
11
from zim.applications import Application, ApplicationError
 
12
 
 
13
 
 
14
# TODO put these commands in preferences
 
15
diagcmd = ('seqdiag', '-o')
 
16
 
 
17
 
 
18
class InsertSequenceDiagramPlugin(ImageGeneratorPlugin):
 
19
 
 
20
        plugin_info = {
 
21
                'name': _('Insert Sequence Diagram'), # T: plugin name
 
22
                'description': _('''\
 
23
This plugin provides a sequence diagram editor for zim based on seqdiag.
 
24
It allows easy editing of sequence diagrams.
 
25
'''), # T: plugin description
 
26
                'help': 'Plugins:Sequence Diagram Editor',
 
27
                'author': 'Greg Warner',
 
28
        }
 
29
 
 
30
        object_type = 'seqdiagram'
 
31
        short_label = _('Sequence Diagram')
 
32
        insert_label = _('Insert Sequence Diagram')
 
33
        edit_label = _('_Edit Sequence Diagram')
 
34
        syntax = None
 
35
 
 
36
        @classmethod
 
37
        def check_dependencies(klass):
 
38
                has_diagcmd = Application(diagcmd).tryexec()
 
39
                return has_diagcmd, [("seqdiag", has_diagcmd, True)]
 
40
 
 
41
 
 
42
class SequenceDiagramGenerator(ImageGeneratorClass):
 
43
 
 
44
        uses_log_file = False
 
45
 
 
46
        object_type = 'seqdiagram'
 
47
        scriptname = 'seqdiagram.diag'
 
48
        imagename = 'seqdiagram.png'
 
49
 
 
50
        def __init__(self, plugin):
 
51
                ImageGeneratorClass.__init__(self, plugin)
 
52
                self.diagfile = TmpFile(self.scriptname)
 
53
                self.diagfile.touch()
 
54
                self.pngfile = File(self.diagfile.path[:-5] + '.png') # len('.diag') == 5
 
55
 
 
56
        def generate_image(self, text):
 
57
                if isinstance(text, basestring):
 
58
                        text = text.splitlines(True)
 
59
 
 
60
                # Write to tmp file
 
61
                self.diagfile.writelines(text)
 
62
 
 
63
                # Call seqdiag
 
64
                try:
 
65
                        diag = Application(diagcmd)
 
66
                        diag.run((self.pngfile, self.diagfile))
 
67
                except ApplicationError:
 
68
                        return None, None # Sorry, no log
 
69
                else:
 
70
                        return self.pngfile, None
 
71
 
 
72
        def cleanup(self):
 
73
                self.diagfile.remove()
 
74
                self.pngfile.remove()