|
15 | 15 | # You should have received a copy of the GNU General Public License along with |
16 | 16 | # "GraphvizAnim". If not, see <http://www.gnu.org/licenses/>. |
17 | 17 |
|
| 18 | +import shlex |
| 19 | + |
18 | 20 | import action |
19 | 21 |
|
| 22 | +class ParseException( Exception ): |
| 23 | + pass |
| 24 | + |
20 | 25 | class Step( object ): |
21 | 26 |
|
22 | 27 | def __init__( self, step = None ): |
@@ -86,6 +91,29 @@ def highlight_edge( self, u, v ): |
86 | 91 | def remove_edge( self, u, v ): |
87 | 92 | self._actions.append( action.RemoveEdge( u, v ) ) |
88 | 93 |
|
| 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 | + |
89 | 117 | def steps( self ): |
90 | 118 | steps = [ Step() ] |
91 | 119 | for action in self._actions: |
|
0 commit comments