diff options
author | Josh Morman <jmorman@gnuradio.org> | 2021-11-24 12:48:20 -0500 |
---|---|---|
committer | mormj <34754695+mormj@users.noreply.github.com> | 2021-11-24 14:41:53 -0500 |
commit | 817fc3ce9cdc819a291e76ec324c4e748381f035 (patch) | |
tree | ed00faf5ea2c0f5a8caaba0ce41cd816dd6ca958 /grc/core/blocks | |
parent | e776d673aa51b5ef19e16cfb6d22098c0b14a679 (diff) |
grc: pep8 formatting
Signed-off-by: Josh Morman <jmorman@gnuradio.org>
Diffstat (limited to 'grc/core/blocks')
-rw-r--r-- | grc/core/blocks/__init__.py | 1 | ||||
-rw-r--r-- | grc/core/blocks/_build.py | 12 | ||||
-rw-r--r-- | grc/core/blocks/_templates.py | 3 | ||||
-rw-r--r-- | grc/core/blocks/block.py | 94 | ||||
-rw-r--r-- | grc/core/blocks/dummy.py | 7 | ||||
-rw-r--r-- | grc/core/blocks/embedded_python.py | 11 | ||||
-rw-r--r-- | grc/core/blocks/virtual.py | 9 |
7 files changed, 85 insertions, 52 deletions
diff --git a/grc/core/blocks/__init__.py b/grc/core/blocks/__init__.py index a80121e8bf..cc5297ece4 100644 --- a/grc/core/blocks/__init__.py +++ b/grc/core/blocks/__init__.py @@ -21,6 +21,7 @@ def register_build_in(cls): build_ins[cls.key] = cls return cls + from .dummy import DummyBlock from .embedded_python import EPyBlock, EPyModule from .virtual import VirtualSink, VirtualSource diff --git a/grc/core/blocks/_build.py b/grc/core/blocks/_build.py index 40155fc84f..91ebef1a42 100644 --- a/grc/core/blocks/_build.py +++ b/grc/core/blocks/_build.py @@ -79,7 +79,8 @@ def build_ports(ports_raw, direction): port_id = port.setdefault('id', str(next(stream_port_ids))) if port_id in port_ids: - raise Exception('Port id "{}" already exists in {}s'.format(port_id, direction)) + raise Exception( + 'Port id "{}" already exists in {}s'.format(port_id, direction)) port_ids.add(port_id) ports.append(port) @@ -137,16 +138,19 @@ def _single_mako_expr(value, block_id): return None value = value.strip() if not (value.startswith('${') and value.endswith('}')): - raise ValueError('{} is not a mako substitution in {}'.format(value, block_id)) + raise ValueError( + '{} is not a mako substitution in {}'.format(value, block_id)) return value[2:-1].strip() def _validate_option_attributes(param_data, block_id): if param_data['dtype'] != 'enum': - send_warning('{} - option_attributes are for enums only, ignoring'.format(block_id)) + send_warning( + '{} - option_attributes are for enums only, ignoring'.format(block_id)) del param_data['option_attributes'] else: for key in list(param_data['option_attributes'].keys()): if key in dir(str): del param_data['option_attributes'][key] - send_warning('{} - option_attribute "{}" overrides str, ignoring'.format(block_id, key)) + send_warning( + '{} - option_attribute "{}" overrides str, ignoring'.format(block_id, key)) diff --git a/grc/core/blocks/_templates.py b/grc/core/blocks/_templates.py index 37775cea1d..8ed69c6eac 100644 --- a/grc/core/blocks/_templates.py +++ b/grc/core/blocks/_templates.py @@ -16,12 +16,15 @@ from ..errors import TemplateError # The utils dict contains convenience functions # that can be called from any template + + def no_quotes(string, fallback=None): if len(string) > 2: if str(string)[0] + str(string)[-1] in ("''", '""'): return str(string)[1:-1] return str(fallback if fallback else string) + utils = {'no_quotes': no_quotes} diff --git a/grc/core/blocks/block.py b/grc/core/blocks/block.py index 86c408f617..89f96f96c8 100644 --- a/grc/core/blocks/block.py +++ b/grc/core/blocks/block.py @@ -22,6 +22,7 @@ from ._flags import Flags from ..base import Element from ..utils.descriptors import lazy_property + def _get_elem(iterable, key): items = list(iterable) for item in items: @@ -39,7 +40,7 @@ class Block(Element): key = '' label = '' category = [] - vtype = '' # This is only used for variables when we want C++ output + vtype = '' # This is only used for variables when we want C++ output flags = Flags('') documentation = {'': ''} @@ -67,13 +68,16 @@ class Block(Element): if self.key == 'options': self.params['id'].hide = 'part' - self.sinks = [port_factory(parent=self, **params) for params in self.inputs_data] - self.sources = [port_factory(parent=self, **params) for params in self.outputs_data] + self.sinks = [port_factory(parent=self, **params) + for params in self.inputs_data] + self.sources = [port_factory(parent=self, **params) + for params in self.outputs_data] self.active_sources = [] # on rewrite self.active_sinks = [] # on rewrite - self.states = {'state': True, 'bus_source': False, 'bus_sink': False, 'bus_structure': None} + self.states = {'state': True, 'bus_source': False, + 'bus_sink': False, 'bus_structure': None} self.block_namespace = {} self.deprecated = self.is_deprecated() @@ -81,7 +85,8 @@ class Block(Element): # This is a workaround to allow embedded python blocks/modules to load as there is # currently 'cpp' in the flags by default caused by the other built-in blocks if hasattr(self, 'cpp_templates'): - self.orig_cpp_templates = self.cpp_templates # The original template, in case we have to edit it when transpiling to C++ + # The original template, in case we have to edit it when transpiling to C++ + self.orig_cpp_templates = self.cpp_templates self.current_bus_structure = {'source': None, 'sink': None} @@ -100,8 +105,8 @@ class Block(Element): except: return None - # region Rewrite_and_Validation + def rewrite(self): """ Add and remove ports to adjust for the nports. @@ -125,7 +130,8 @@ class Block(Element): self.update_bus_logic() # disconnect hidden ports - self.parent_flowgraph.disconnect(*[p for p in self.ports() if p.hidden]) + self.parent_flowgraph.disconnect( + *[p for p in self.ports() if p.hidden]) self.active_sources = [p for p in self.sources if not p.hidden] self.active_sinks = [p for p in self.sinks if not p.hidden] @@ -142,11 +148,12 @@ class Block(Element): # this is ok behavior, unfortunately we could be hiding other import bugs pass except Exception: - self.add_error_message(f'Failed to evaluate import expression {imports!r}') + self.add_error_message( + f'Failed to evaluate import expression {imports!r}') def update_bus_logic(self): ############################### - ## Bus Logic + # Bus Logic ############################### for direc in {'source', 'sink'}: @@ -170,13 +177,12 @@ class Block(Element): removed_bus_connections.append(c) ports.remove(port) - if (bus_state): struct = self.form_bus_structure(direc) self.current_bus_structure[direc] = struct # Hide ports that are not part of the bus structure - #TODO: Blocks where it is desired to only have a subset + # TODO: Blocks where it is desired to only have a subset # of ports included in the bus still has some issues for idx, port in enumerate(ports): if any([idx in bus for bus in self.current_bus_structure[direc]]): @@ -187,19 +193,20 @@ class Block(Element): # Add the Bus Ports to the list of ports for i in range(len(struct)): # self.sinks = [port_factory(parent=self, **params) for params in self.inputs_data] - port = self.parent.parent.make_port(self, direction=direc, id=str(len(ports)), label='bus', dtype='bus', bus_struct=struct[i]) + port = self.parent.parent.make_port(self, direction=direc, id=str( + len(ports)), label='bus', dtype='bus', bus_struct=struct[i]) ports.append(port) for (saved_port, connection) in zip(removed_bus_ports, removed_bus_connections): if port.key == saved_port.key: - self.parent_flowgraph.connections.remove(connection) + self.parent_flowgraph.connections.remove( + connection) if saved_port.is_source: connection.source_port = port if saved_port.is_sink: connection.sink_port = port self.parent_flowgraph.connections.add(connection) - else: self.current_bus_structure[direc] = None @@ -209,8 +216,6 @@ class Block(Element): port.hidden = port.stored_hidden_state port.stored_hidden_state = None - - def _rewrite_nports(self, ports): for port in ports: if hasattr(port, 'master_port'): # Not a master port and no left-over clones @@ -218,7 +223,7 @@ class Block(Element): port.vlen = port.master_port.vlen continue nports = port.multiplicity - for clone in port.clones[nports-1:]: + for clone in port.clones[nports - 1:]: # Remove excess connections self.parent_flowgraph.disconnect(clone) port.remove_clone(clone) @@ -245,9 +250,11 @@ class Block(Element): for expr in self.asserts: try: if not self.evaluate(expr): - self.add_error_message('Assertion "{}" failed.'.format(expr)) + self.add_error_message( + 'Assertion "{}" failed.'.format(expr)) except Exception: - self.add_error_message('Assertion "{}" did not evaluate.'.format(expr)) + self.add_error_message( + 'Assertion "{}" did not evaluate.'.format(expr)) def _validate_generate_mode_compat(self): """check if this is a GUI block and matches the selected generate option""" @@ -261,7 +268,8 @@ class Block(Element): self.add_error_message("Can't generate this block in mode: {} ".format( repr(current_generate_option))) - check_generate_mode('QT GUI', Flags.NEED_QT_GUI, ('qt_gui', 'hb_qt_gui')) + check_generate_mode('QT GUI', Flags.NEED_QT_GUI, + ('qt_gui', 'hb_qt_gui')) def _validate_output_language_compat(self): """check if this block supports the selected output language""" @@ -269,19 +277,23 @@ class Block(Element): if current_output_language == 'cpp': if 'cpp' not in self.flags: - self.add_error_message("This block does not support C++ output.") + self.add_error_message( + "This block does not support C++ output.") if self.key == 'parameter': if not self.params['type'].value: - self.add_error_message("C++ output requires you to choose a parameter type.") + self.add_error_message( + "C++ output requires you to choose a parameter type.") def _validate_var_value(self): """or variables check the value (only if var_value is used)""" if self.is_variable and self.value != 'value': try: - self.parent_flowgraph.evaluate(self.value, local_namespace=self.namespace) + self.parent_flowgraph.evaluate( + self.value, local_namespace=self.namespace) except Exception as err: - self.add_error_message('Value "{}" cannot be evaluated:\n{}'.format(self.value, err)) + self.add_error_message( + 'Value "{}" cannot be evaluated:\n{}'.format(self.value, err)) # endregion # region Properties @@ -458,7 +470,7 @@ class Block(Element): # For container types we must also determine the type of the template parameter(s) return 'std::vector<' + get_type(str(evaluated[0]), type(evaluated[0])) + '>' - except IndexError: # empty list + except IndexError: # empty list return 'std::vector<std::string>' if _vtype == dict: @@ -466,9 +478,9 @@ class Block(Element): # For container types we must also determine the type of the template parameter(s) key = list(evaluated)[0] val = list(evaluated.values())[0] - return 'std::map<' + get_type(str(key), type(key)) + ', ' + get_type(str(val), type(val)) +'>' + return 'std::map<' + get_type(str(key), type(key)) + ', ' + get_type(str(val), type(val)) + '>' - except IndexError: # empty dict + except IndexError: # empty dict return 'std::map<std::string, std::string>' else: @@ -480,12 +492,12 @@ class Block(Element): # The r-value for these types must be transformed to create legal C++ syntax. if self.vtype in ['bool', 'gr_complex'] or 'std::map' in self.vtype or 'std::vector' in self.vtype: evaluated = ast.literal_eval(value) - self.cpp_templates['var_make'] = self.cpp_templates['var_make'].replace('${value}', self.get_cpp_value(evaluated)) + self.cpp_templates['var_make'] = self.cpp_templates['var_make'].replace( + '${value}', self.get_cpp_value(evaluated)) if 'string' in self.vtype: self.cpp_templates['includes'].append('#include <string>') - def get_cpp_value(self, pyval): if type(pyval) == int or type(pyval) == float: @@ -494,8 +506,10 @@ class Block(Element): # Check for PI and replace with C++ constant pi_re = r'^(math|numpy|np|scipy|sp)\.pi$' if re.match(pi_re, str(pyval)): - val_str = re.sub(pi_re, 'boost::math::constants::pi<double>()', val_str) - self.cpp_templates['includes'].append('#include <boost/math/constants/constants.hpp>') + val_str = re.sub( + pi_re, 'boost::math::constants::pi<double>()', val_str) + self.cpp_templates['includes'].append( + '#include <boost/math/constants/constants.hpp>') return str(pyval) @@ -503,7 +517,8 @@ class Block(Element): return str(pyval)[0].lower() + str(pyval)[1:] elif type(pyval) == complex: - self.cpp_templates['includes'].append('#include <gnuradio/gr_complex.h>') + self.cpp_templates['includes'].append( + '#include <gnuradio/gr_complex.h>') evaluated = ast.literal_eval(str(pyval).strip()) return '{' + str(evaluated.real) + ', ' + str(evaluated.imag) + '}' @@ -523,7 +538,8 @@ class Block(Element): self.cpp_templates['includes'].append('#include <map>') val_str = '{' for key in pyval: - val_str += '{' + self.get_cpp_value(key) + ', ' + self.get_cpp_value(pyval[key]) + '}, ' + val_str += '{' + self.get_cpp_value(key) + \ + ', ' + self.get_cpp_value(pyval[key]) + '}, ' if len(val_str) > 1: # truncate to trim superfluous ', ' from the end @@ -535,7 +551,6 @@ class Block(Element): self.cpp_templates['includes'].append('#include <string>') return '"' + pyval + '"' - def is_virtual_sink(self): return self.key == 'virtual_sink' @@ -561,8 +576,8 @@ class Block(Element): print(exception.message) return False - # Block bypassing + def get_bypassed(self): """ Check if the block is bypassed @@ -618,7 +633,8 @@ class Block(Element): @property def namespace(self): # update block namespace - self.block_namespace.update({key:param.get_evaluated() for key, param in self.params.items()}) + self.block_namespace.update( + {key: param.get_evaluated() for key, param in self.params.items()}) return self.block_namespace @property @@ -721,7 +737,7 @@ class Block(Element): struct = [range(len(ports))] # struct = list(range(len(ports))) - #TODO for more complicated port structures, this code is needed but not working yet + # TODO for more complicated port structures, this code is needed but not working yet if any([p.multiplicity for p in ports]): structlet = [] last = 0 @@ -736,8 +752,8 @@ class Block(Element): continue if p.multiplicity > 1: - cnt = p.multiplicity-1 - structlet.append([idx+j for j in range(p.multiplicity)]) + cnt = p.multiplicity - 1 + structlet.append([idx + j for j in range(p.multiplicity)]) else: structlet.append([idx]) diff --git a/grc/core/blocks/dummy.py b/grc/core/blocks/dummy.py index 5cdc05a9df..8147eededb 100644 --- a/grc/core/blocks/dummy.py +++ b/grc/core/blocks/dummy.py @@ -9,6 +9,7 @@ from . import Block, register_build_in from ._build import build_params + @register_build_in class DummyBlock(Block): @@ -19,12 +20,14 @@ class DummyBlock(Block): def __init__(self, parent, missing_block_id, parameters, **_): self.key = missing_block_id - self.parameters_data = build_params([], False, False, self.flags, self.key) + self.parameters_data = build_params( + [], False, False, self.flags, self.key) super(DummyBlock, self).__init__(parent=parent) param_factory = self.parent_platform.make_param for param_id in parameters: - self.params.setdefault(param_id, param_factory(parent=self, id=param_id, dtype='string')) + self.params.setdefault(param_id, param_factory( + parent=self, id=param_id, dtype='string')) def is_valid(self): return False diff --git a/grc/core/blocks/embedded_python.py b/grc/core/blocks/embedded_python.py index 051ba70317..893dcda948 100644 --- a/grc/core/blocks/embedded_python.py +++ b/grc/core/blocks/embedded_python.py @@ -122,7 +122,8 @@ class EPyBlock(Block): self.label = blk_io.name or blk_io.cls self.documentation = {'': blk_io.doc} - self.module_name = "{}_{}".format(self.parent_flowgraph.get_option("id"), self.name) + self.module_name = "{}_{}".format( + self.parent_flowgraph.get_option("id"), self.name) self.templates['imports'] = 'import {} as {} # embedded python block'.format( self.module_name, self.name) self.templates['make'] = '{mod}.{cls}({args})'.format( @@ -131,7 +132,7 @@ class EPyBlock(Block): args=', '.join('{0}=${{ {0} }}'.format(key) for key, _ in blk_io.params)) self.templates['callbacks'] = [ '{0} = ${{ {0} }}'.format(attr) for attr in blk_io.callbacks - ] + ] self._update_params(blk_io.params) self._update_ports('in', self.sinks, blk_io.sinks, 'sink') @@ -195,7 +196,8 @@ class EPyBlock(Block): def validate(self): super(EPyBlock, self).validate() if self._epy_reload_error: - self.params['_source_code'].add_error_message(str(self._epy_reload_error)) + self.params['_source_code'].add_error_message( + str(self._epy_reload_error)) @register_build_in @@ -240,6 +242,7 @@ class EPyModule(Block): def rewrite(self): super(EPyModule, self).rewrite() - self.module_name = "{}_{}".format(self.parent_flowgraph.get_option("id"), self.name) + self.module_name = "{}_{}".format( + self.parent_flowgraph.get_option("id"), self.name) self.templates['imports'] = 'import {} as {} # embedded python module'.format( self.module_name, self.name) diff --git a/grc/core/blocks/virtual.py b/grc/core/blocks/virtual.py index 9d1387dbbb..34e0feb34b 100644 --- a/grc/core/blocks/virtual.py +++ b/grc/core/blocks/virtual.py @@ -21,7 +21,8 @@ class VirtualSink(Block): flags.set('cpp') parameters_data = build_params( - params_raw=[dict(label='Stream ID', id='stream_id', dtype='stream_id')], + params_raw=[ + dict(label='Stream ID', id='stream_id', dtype='stream_id')], have_inputs=False, have_outputs=False, flags=flags, block_id=key ) inputs_data = [dict(domain='stream', dtype='', direction='sink', id="0")] @@ -45,10 +46,12 @@ class VirtualSource(Block): flags.set('cpp') parameters_data = build_params( - params_raw=[dict(label='Stream ID', id='stream_id', dtype='stream_id')], + params_raw=[ + dict(label='Stream ID', id='stream_id', dtype='stream_id')], have_inputs=False, have_outputs=False, flags=flags, block_id=key ) - outputs_data = [dict(domain='stream', dtype='', direction='source', id="0")] + outputs_data = [dict(domain='stream', dtype='', + direction='source', id="0")] def __init__(self, parent, **kwargs): super(VirtualSource, self).__init__(parent, **kwargs) |