Skip to content

Commit c335ddc

Browse files
committed
Moved parse code to Antimation, now uses shlex.split (closes #1)
1 parent 3c64cfb commit c335ddc

3 files changed

Lines changed: 33 additions & 28 deletions

File tree

examples/simple.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ae 1 2
22
ae 1 3
3-
ae 2 3
3+
ae 2 "a node"
44
ns
55
hn 1
66
ns
77
hn 2
8-
ln 2 mammamia
8+
ln 2 "foo bar"
99
ns
10-
hn 3
10+
hn "a node"
1111
ae 1 4
1212
ns
1313
un 2

gvanim/__main__.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# "GraphvizAnim". If not, see <http://www.gnu.org/licenses/>.
1717

1818
from argparse import ArgumentParser, FileType
19-
from sys import stdin, argv, stderr
19+
from sys import stdin
2020

2121
import animation, render
2222

@@ -29,30 +29,7 @@ def main():
2929
args = parser.parse_args()
3030

3131
ga = animation.Animation()
32-
cmd2method = {
33-
'ns' : ga.next_step,
34-
'an' : ga.add_node,
35-
'hn' : ga.highlight_node,
36-
'ln' : ga.label_node,
37-
'un' : ga.unlabel_node,
38-
'rn' : ga.remove_node,
39-
'ae' : ga.add_edge,
40-
'he' : ga.highlight_edge,
41-
're' : ga.remove_edge,
42-
}
43-
44-
for line in args.animation:
45-
parts = line.strip().split()
46-
cmd, params = parts[ 0 ], parts[ 1: ]
47-
try:
48-
cmd2method[ cmd ]( *params )
49-
except KeyError:
50-
print >>stderr, 'gvanim: unrecognized command: {}'.format( cmd )
51-
return
52-
except TypeError:
53-
print >>stderr, 'gvanim: wrong number of parameters: {}'.format( line.strip() )
54-
return
55-
32+
ga.parse( args.animation )
5633
render.gif( render.render( ga.graphs(), args.basename, 'png' ), args.basename, args.delay )
5734

5835
if __name__ == '__main__':

gvanim/animation.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
# You should have received a copy of the GNU General Public License along with
1616
# "GraphvizAnim". If not, see <http://www.gnu.org/licenses/>.
1717

18+
import shlex
19+
1820
import action
1921

22+
class ParseException( Exception ):
23+
pass
24+
2025
class Step( object ):
2126

2227
def __init__( self, step = None ):
@@ -86,6 +91,29 @@ def highlight_edge( self, u, v ):
8691
def remove_edge( self, u, v ):
8792
self._actions.append( action.RemoveEdge( u, v ) )
8893

94+
def parse( self, lines ):
95+
action2method = {
96+
'ns' : self.next_step,
97+
'an' : self.add_node,
98+
'hn' : self.highlight_node,
99+
'ln' : self.label_node,
100+
'un' : self.unlabel_node,
101+
'rn' : self.remove_node,
102+
'ae' : self.add_edge,
103+
'he' : self.highlight_edge,
104+
're' : self.remove_edge,
105+
}
106+
for line in lines:
107+
parts = shlex.split( line.strip(), True )
108+
action, params = parts[ 0 ], parts[ 1: ]
109+
try:
110+
action2method[ action ]( *params )
111+
except KeyError:
112+
raise ParseException( 'unrecognized command: {}'.format( action ) )
113+
except TypeError:
114+
raise ParseException( 'wrong number of parameters: {}'.format( line.strip() ) )
115+
return
116+
89117
def steps( self ):
90118
steps = [ Step() ]
91119
for action in self._actions:

0 commit comments

Comments
 (0)