diff options
Diffstat (limited to 'grc/python')
-rw-r--r-- | grc/python/FlowGraph.py | 21 | ||||
-rw-r--r-- | grc/python/Platform.py | 53 |
2 files changed, 73 insertions, 1 deletions
diff --git a/grc/python/FlowGraph.py b/grc/python/FlowGraph.py index 49530af8a3..9b55cb6d43 100644 --- a/grc/python/FlowGraph.py +++ b/grc/python/FlowGraph.py @@ -30,9 +30,11 @@ _bussrc_searcher = re.compile('^(bus_source)$') _bus_struct_sink_searcher = re.compile('^(bus_structure_sink)$') _bus_struct_src_searcher = re.compile('^(bus_structure_source)$') + class FlowGraph(_FlowGraph, _GUIFlowGraph): def __init__(self, **kwargs): + self.grc_file_path = '' _FlowGraph.__init__(self, **kwargs) _GUIFlowGraph.__init__(self) self._eval_cache = dict() @@ -232,7 +234,6 @@ class FlowGraph(_FlowGraph, _GUIFlowGraph): return bussrc - def rewrite(self): """ Flag the namespace to be renewed. @@ -299,3 +300,21 @@ class FlowGraph(_FlowGraph, _GUIFlowGraph): #evaluate e = self._eval(expr, self.n, self.n_hash) return e + + def get_new_block(self, key): + """Try to auto-generate the block from file if missing""" + block = _FlowGraph.get_new_block(self, key) + if not block: + platform = self.get_parent() + # we're before the initial fg rewrite(), so no evaluated values! + # --> use raw value instead + path_param = self._options_block.get_param('hier_block_src_path') + file_path = platform.find_file_in_paths( + filename=key + '.' + platform.get_key(), + paths=path_param.get_value(), + cwd=self.grc_file_path + ) + if file_path: # grc file found. load and get block + platform.load_and_generate_flow_graph(file_path) + block = _FlowGraph.get_new_block(self, key) # can be None + return block diff --git a/grc/python/Platform.py b/grc/python/Platform.py index 21fd7ef8d2..1d932761d8 100644 --- a/grc/python/Platform.py +++ b/grc/python/Platform.py @@ -24,6 +24,7 @@ from gnuradio import gr from .. base.Platform import Platform as _Platform from .. gui.Platform import Platform as _GUIPlatform +from .. gui import Messages from . import extract_docs from .FlowGraph import FlowGraph as _FlowGraph @@ -84,6 +85,7 @@ class Platform(_Platform, _GUIPlatform): self, prefs_file=PREFS_FILE ) + self._auto_hier_block_generate_chain = set() @staticmethod def _move_old_pref_file(): @@ -111,6 +113,57 @@ class Platform(_Platform, _GUIPlatform): ) return block + @staticmethod + def find_file_in_paths(filename, paths, cwd): + """Checks the provided paths relative to cwd for a certain filename""" + if not os.path.isdir(cwd): + cwd = os.path.dirname(cwd) + if isinstance(paths, str): + paths = (p for p in paths.split(':') if p) + + for path in paths: + path = os.path.expanduser(path) + if not os.path.isabs(path): + path = os.path.normpath(os.path.join(cwd, path)) + file_path = os.path.join(path, filename) + if os.path.exists(os.path.normpath(file_path)): + return file_path + + def load_and_generate_flow_graph(self, file_path): + """Loads a flowgraph from file and generates it""" + Messages.set_indent(len(self._auto_hier_block_generate_chain)) + Messages.send('>>> Loading: %r\n' % file_path) + if file_path in self._auto_hier_block_generate_chain: + Messages.send(' >>> Warning: cyclic hier_block dependency\n') + return False + self._auto_hier_block_generate_chain.add(file_path) + try: + flow_graph = self.get_new_flow_graph() + flow_graph.grc_file_path = file_path + # other, nested higiter_blocks might be auto-loaded here + flow_graph.import_data(self.parse_flow_graph(file_path)) + flow_graph.rewrite() + flow_graph.validate() + if not flow_graph.is_valid(): + raise Exception('Flowgraph invalid') + except Exception as e: + Messages.send('>>> Load Error: %r: %s\n' % (file_path, str(e))) + return False + finally: + self._auto_hier_block_generate_chain.discard(file_path) + Messages.set_indent(len(self._auto_hier_block_generate_chain)) + + try: + Messages.send('>>> Generating: %r\n' % file_path) + generator = self.get_generator()(flow_graph, file_path) + generator.write() + except Exception as e: + Messages.send('>>> Generate Error: %r: %s\n' % (file_path, str(e))) + return False + + self.load_block_xml(generator.get_file_path_xml()) + return True + ############################################## # Constructors ############################################## |