From 3232eeb534b5ac1ab503f7f36254227b1cfba1f9 Mon Sep 17 00:00:00 2001 From: Josh Morman <jmorman@perspectalabs.com> Date: Fri, 30 Aug 2019 09:47:42 -0400 Subject: grc: add python snippets to GRC This feature adds the ability to insert arbitrary code into the python flowgraph. It gives a little more low-level flexibility for quickly modifying flowgraphs and adding custom bits of code rather than having to go and edit the generated py file One example is synchronizing multiple USRP objects - sometimes you want different sync than what is offered in the multi-usrp object, so you can put a bit of code in the snippet block to do the custom synchronization --- grc/core/FlowGraph.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'grc/core/FlowGraph.py') diff --git a/grc/core/FlowGraph.py b/grc/core/FlowGraph.py index 21c3cdb59d..b0141f8705 100644 --- a/grc/core/FlowGraph.py +++ b/grc/core/FlowGraph.py @@ -81,6 +81,47 @@ class FlowGraph(Element): parameters = [b for b in self.iter_enabled_blocks() if b.key == 'parameter'] return parameters + def get_snippets(self): + """ + Get a set of all code snippets (Python) in this flow graph namespace. + + Returns: + a list of code snippets + """ + return [b for b in self.iter_enabled_blocks() if b.key == 'snippet'] + + def get_snippets_dict(self, section=None): + """ + Get a dictionary of code snippet information for a particular section. + + Args: + section: string specifier of section of snippets to return, section=None returns all + + Returns: + a list of code snippets dicts + """ + snippets = self.get_snippets() + if not snippets: + return [] + + output = [] + for snip in snippets: + d ={} + sect = snip.params['section'].value + d['section'] = sect + d['priority'] = snip.params['priority'].value + d['lines'] = snip.params['code'].value.splitlines() + d['def'] = 'def snipfcn_{}(self):'.format(snip.name) + d['call'] = 'snipfcn_{}(tb)'.format(snip.name) + if not section or sect == section: + output.append(d) + + # Sort by descending priority + if section: + output = sorted(output, key=lambda x: x['priority'], reverse=True) + + return output + def get_monitors(self): """ Get a list of all ControlPort monitors -- cgit v1.2.3