diff options
author | Sebastian Koslowski <koslowski@kit.edu> | 2016-06-19 20:39:22 -0700 |
---|---|---|
committer | Sebastian Koslowski <koslowski@kit.edu> | 2016-07-13 16:35:49 +0200 |
commit | 020878e8468d848cebe1bfe5b0dc7c9b557214f7 (patch) | |
tree | 3d576c9bc0320fa15367a889758c7cf54c282592 /grc | |
parent | 4f29b9ae0b518bcc41038d6d300429e5d656d8e0 (diff) |
grc: refactor: block states are no longer hidden params
Diffstat (limited to 'grc')
-rw-r--r-- | grc/core/Block.py | 49 | ||||
-rw-r--r-- | grc/gui/Block.py | 31 | ||||
-rw-r--r-- | grc/gui/FlowGraph.py | 14 |
3 files changed, 49 insertions, 45 deletions
diff --git a/grc/core/Block.py b/grc/core/Block.py index ec65a1a494..013f2cdbd8 100644 --- a/grc/core/Block.py +++ b/grc/core/Block.py @@ -21,6 +21,7 @@ from __future__ import absolute_import import collections import itertools +import ast import six from six.moves import map, range @@ -31,7 +32,7 @@ from . import utils from . Constants import ( BLOCK_FLAG_NEED_QT_GUI, BLOCK_FLAG_NEED_WX_GUI, - ADVANCED_PARAM_TAB, DEFAULT_PARAM_TAB, + ADVANCED_PARAM_TAB, BLOCK_FLAG_THROTTLE, BLOCK_FLAG_DISABLE_BYPASS, BLOCK_FLAG_DEPRECATED, ) @@ -101,6 +102,8 @@ class Block(Element): self.sources = self._init_ports(sources_n, direction='source') self.sinks = self._init_ports(sinks_n, direction='sink') + self.states = {'_enabled': True} + self._epy_source_hash = -1 # for epy blocks self._epy_reload_error = None @@ -113,7 +116,6 @@ class Block(Element): def _init_params(self, params_n, has_sources, has_sinks): self._add_param(key='id', name='ID', type='id') - self._add_param(key='_enabled', name='Enabled', value='True', type='raw', hide='all') # Virtual source/sink and pad source/sink blocks are # indistinguishable from normal GR blocks. Make explicit @@ -409,7 +411,7 @@ class Block(Element): DISABLED - 2 """ try: - return self.STATE_LABELS[int(self.params['_enabled'].get_value())] + return self.STATE_LABELS[int(self.states['_enabled'])] except ValueError: return 'enabled' @@ -424,10 +426,10 @@ class Block(Element): DISABLED - 2 """ try: - encoded = str(self.STATE_LABELS.index(value)) + encoded = self.STATE_LABELS.index(value) except ValueError: - encoded = str(self.STATE_LABELS.index('enabled')) - self.params['_enabled'].set_value(encoded) + encoded = 1 + self.states['_enabled'] = encoded # Enable/Disable Aliases def get_enabled(self): @@ -643,11 +645,16 @@ class Block(Element): """ n = collections.OrderedDict() n['key'] = self.key - n['param'] = [param.export_data() for _, param in sorted(six.iteritems(self.params))] - if 'bus' in [a.get_type() for a in self.sinks]: - n['bus_sink'] = str(1) - if 'bus' in [a.get_type() for a in self.sources]: - n['bus_source'] = str(1) + + params = (param.export_data() for param in six.itervalues(self.params)) + states = (collections.OrderedDict([('key', key), ('value', repr(value))]) + for key, value in six.iteritems(self.states)) + n['param'] = sorted(itertools.chain(states, params), key=lambda p: p['key']) + + if any('bus' in a.get_type() for a in self.sinks): + n['bus_sink'] = '1' + if any('bus' in a.get_type() for a in self.sources): + n['bus_source'] = '1' return n def import_data(self, n): @@ -662,22 +669,26 @@ class Block(Element): Args: n: the nested data odict """ - params_n = n.get('param', []) + param_data = {p['key']: p['value'] for p in n.get('param', [])} + + for key in self.states: + try: + self.states[key] = ast.literal_eval(param_data.pop(key)) + except (KeyError, SyntaxError, ValueError): + pass def get_hash(): - return hash(tuple(map(hash, self.params.values()))) + return hash(tuple(hash(v) for v in self.params.values())) - my_hash = 0 - while get_hash() != my_hash: - for param_n in params_n: - key = param_n['key'] - value = param_n['value'] + pre_rewrite_hash = -1 + while pre_rewrite_hash != get_hash(): + for key, value in six.iteritems(param_data): try: self.params[key].set_value(value) except KeyError: continue # Store hash and call rewrite - my_hash = get_hash() + pre_rewrite_hash = get_hash() self.rewrite() self._import_bus_stuff(n) diff --git a/grc/gui/Block.py b/grc/gui/Block.py index a38e4cc59a..94887eeb89 100644 --- a/grc/gui/Block.py +++ b/grc/gui/Block.py @@ -45,13 +45,11 @@ class Block(Element, _Block): Add graphics related params to the block. """ _Block.__init__(self, flow_graph, n) - self.W = self.H = 0 - self._add_param(key='_coordinate', name='GUI Coordinate', value='(0, 0)', - hide='all') - self._add_param(key='_rotation', name='GUI Rotation', value='0', - hide='all') - Element.__init__(self) # needs the params + self.states.update(_coordinate=(0, 0), _rotation=0) + Element.__init__(self) # needs the states + + self.W = self.H = 0 self._param_layouts = [] self._comment_layout = None self._bg_color = Colors.BLOCK_ENABLED_COLOR @@ -64,13 +62,7 @@ class Block(Element, _Block): Returns: the coordinate tuple (x, y) or (0, 0) if failure """ - try: - coor = self.params['_coordinate'].get_value() # should evaluate to tuple - coor = tuple(int(x) for x in coor[1:-1].split(',')) - except: - coor = 0, 0 - self.set_coordinate(coor) - return coor + return self.states['_coordinate'] def set_coordinate(self, coor): """ @@ -85,7 +77,7 @@ class Block(Element, _Block): Utils.align_to_grid(coor[0] + offset_x) - offset_x, Utils.align_to_grid(coor[1] + offset_y) - offset_y ) - self.get_param('_coordinate').set_value(repr(coor)) + self.states['_coordinate'] = coor def get_rotation(self): """ @@ -94,21 +86,16 @@ class Block(Element, _Block): Returns: the rotation in degrees or 0 if failure """ - try: #should evaluate to dict - rotation = int(self.get_param('_rotation').get_value()) - except: - rotation = POSSIBLE_ROTATIONS[0] - self.set_rotation(rotation) - return rotation + return self.states['_rotation'] def set_rotation(self, rot): """ Set the rotation into the position param. - +q Args: rot: the rotation in degrees """ - self.get_param('_rotation').set_value(repr(int(rot))) + self.states['_rotation'] = rot def create_shapes(self): """Update the block, parameters, and ports when a change occurs.""" diff --git a/grc/gui/FlowGraph.py b/grc/gui/FlowGraph.py index 87bd91d880..d592242e2e 100644 --- a/grc/gui/FlowGraph.py +++ b/grc/gui/FlowGraph.py @@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA from __future__ import absolute_import import functools +import ast import random from distutils.spawn import find_executable from itertools import count @@ -219,12 +220,17 @@ class FlowGraph(Element, _Flowgraph): continue # unknown block was pasted (e.g. dummy block) selected.add(block) #set params - params = {n['key']: n['value'] for n in block_n.get('param', [])} + param_data = {n['key']: n['value'] for n in block_n.get('param', [])} + for key in block.states: + try: + block.states[key] = ast.literal_eval(param_data.pop(key)) + except (KeyError, SyntaxError, ValueError): + pass if block_key == 'epy_block': - block.get_param('_io_cache').set_value(params.pop('_io_cache')) - block.get_param('_source_code').set_value(params.pop('_source_code')) + block.get_param('_io_cache').set_value(param_data.pop('_io_cache')) + block.get_param('_source_code').set_value(param_data.pop('_source_code')) block.rewrite() # this creates the other params - for param_key, param_value in six.iteritems(params): + for param_key, param_value in six.iteritems(param_data): #setup id parameter if param_key == 'id': old_id2block[param_value] = block |