diff options
author | Josh Morman <jmorman@perspectalabs.com> | 2019-08-30 09:47:42 -0400 |
---|---|---|
committer | devnulling <devnulling@users.noreply.github.com> | 2020-02-10 13:33:07 -0800 |
commit | 3232eeb534b5ac1ab503f7f36254227b1cfba1f9 (patch) | |
tree | c950d0e841644ed584737e905e315d778fe26f3e /grc/core/FlowGraph.py | |
parent | 838d081918f8e5ee3f28273237d8b74a8c0b9d9b (diff) |
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
Diffstat (limited to 'grc/core/FlowGraph.py')
-rw-r--r-- | grc/core/FlowGraph.py | 41 |
1 files changed, 41 insertions, 0 deletions
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 |