-
Notifications
You must be signed in to change notification settings - Fork 0
/
uml_stm_text_export.py
83 lines (75 loc) · 3.83 KB
/
uml_stm_text_export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Export finite state machines into a text format.
#
# Based on a script by Unai Estebanez Sevilla:
#
# From: Unai Estebanez <unai unainet net>
# To: dia-list gnome org
# Subject: python script to export finite state machines
# Date: Tue, 7 Jul 2009 15:05:10 +0200
#
# https://mail.gnome.org/archives/dia-list/2009-July/msg00005.html
#
# Changes by Tomas Pospisek <tpo_deb@sourcepole.ch>
#
# Copyright (c) 2009-2012, Unai Estebanez Sevilla, Tomas Pospisek
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
import dia
import uml_stm_export
class TxtDiagramRenderer(uml_stm_export.SimpleSTM):
CODE_PREAMBLE = \
"# Machine generated file, do not edit!!!\n" + \
"# Generated by uml_stm_text_export.py script for DIA\n" + \
"# Format is:\n" + \
"# <comments zone>\n" + \
"# [STATES]\n" + \
"# <state>\n" + \
"# ...\n" + \
"# <state>\n" + \
"# [TRANSITIONS]\n" + \
"# <transition>\n" + \
"# ...\n" + \
"# <transition>\n" + \
"# \n" + \
"# Where:\n" + \
"# <comments zone> is a line that begins with \'#\'\n" + \
"# <state> is a line with comma separated values:\n" + \
"# state_name,input_action,output_action\n" + \
"# <transition> is a line with comma separated values:\n" + \
"# source_state,target_state,trigger,action\n" + \
"# [STATES] is states zone begin tag\n" + \
"# [TRANSITIONS] is transitions zone begin tag\n" + \
"#---------------------------------------------------------\n"
def __init__(self):
uml_stm_export.SimpleSTM.__init__(self)
self.filename = ""
def begin_render (self, data, filename):
self.filename = filename
uml_stm_export.SimpleSTM.parse(self, data)
def end_render(self) :
f = open(self.filename, "w")
f.write(self.CODE_PREAMBLE)
f.write("[STATES]\n")
for key in self.states.keys():
state = self.states[key]
f.write("%s, %s, %s\n" % (state.name, state.iaction, state.oaction))
f.write("[TRANSITIONS]\n")
for transition in self.transitions:
f.write("%s, %s, %s, %s\n" % (transition.source, transition.target, transition.trigger, transition.action))
f.close()
self.states = {}
self.transitions = []
# dia-python keeps a reference to the renderer class and uses it on demand
dia.register_export("State Machine Textual Dump", "txt", TxtDiagramRenderer())