2 Copyright 2008 Free Software Foundation, Inc.
3 This file is part of GNU Radio
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 ##@package grc.elements.Platform
20 #A Platform contains all blocks in platform.
23 from grc import ParseXML
25 from grc.elements.Element import Element as _Element
26 from grc.elements.FlowGraph import FlowGraph as _FlowGraph
27 from grc.elements.Connection import Connection as _Connection
28 from grc.elements.Block import Block as _Block
29 from grc.elements.Port import Port as _Port
30 from grc.elements.Param import Param as _Param
31 from grc.Constants import DATA_DIR
33 class Platform(_Element):
35 def __init__(self, name, key, block_paths, block_dtd, block_tree, default_flow_graph, generator):
37 Make a platform from the arguments.
38 @param name the platform name
39 @param key the unique platform key
40 @param block_paths the file paths to blocks in this platform
41 @param block_dtd the dtd validator for xml block wrappers
42 @param block_tree the nested tree of block keys and categories
43 @param default_flow_graph the default flow graph file path
44 @param load_one a single file to load into this platform or None
45 @return a platform object
47 _Element.__init__(self)
50 self._block_paths = block_paths
51 self._block_dtd = block_dtd
52 self._block_tree = block_tree
53 self._default_flow_graph = default_flow_graph
54 self._generator = generator
55 #create a dummy flow graph for the blocks
56 self._flow_graph = _Element(self)
59 self._blocks_n = dict()
60 for block_path in self._block_paths:
61 if os.path.isfile(block_path): self._load_block(block_path)
62 elif os.path.isdir(block_path):
63 for dirpath,dirnames,filenames in os.walk(block_path):
64 for filename in filter(lambda f: f.endswith('.xml'), filenames):
65 self._load_block(os.path.join(dirpath, filename))
67 def get_prefs_block(self): return self.get_new_flow_graph().get_new_block('preferences')
69 def _load_block(self, f):
71 Load the block wrapper from the file path.
72 The block wrapper must pass validation, and have a unique block key.
73 If any of the checks fail, exit with error.
74 @param f the file path
76 try: ParseXML.validate_dtd(f, self._block_dtd)
77 except ParseXML.XMLSyntaxError, e: self._exit_with_error('Block definition "%s" failed: \n\t%s'%(f, e))
78 n = ParseXML.from_file(f)['block']
79 block = self.Block(self._flow_graph, n)
81 #test against repeated keys
82 try: assert(key not in self.get_block_keys())
83 except AssertionError: self._exit_with_error('Key "%s" already exists in blocks'%key)
85 self._blocks[key] = block
86 self._blocks_n[key] = n
88 def load_block_tree(self, block_tree):
90 Load a block tree with categories and blocks.
91 Step 1: Load all blocks from the xml specification.
92 Step 2: Load blocks with builtin category specifications.
93 @param block_tree the block tree object
95 #recursive function to load categories and blocks
96 def load_category(cat_n, parent=''):
98 parent = '%s/%s'%(parent, cat_n['name'])
99 block_tree.add_block(parent)
100 #recursive call to load sub categories
101 map(lambda c: load_category(c, parent), Utils.listify(cat_n, 'cat'))
102 #add blocks in this category
103 for block_key in Utils.listify(cat_n, 'block'):
104 block_tree.add_block(parent, self.get_block(block_key))
107 try: ParseXML.validate_dtd(f, os.path.join(DATA_DIR, 'block_tree.dtd'))
108 except ParseXML.XMLSyntaxError, e: self._exit_with_error('Block tree "%s" failed: \n\t%s'%(f, e))
109 #add all blocks in the tree
110 load_category(ParseXML.from_file(f)['cat'])
111 #add all other blocks, use the catgory
112 for block in self.get_blocks():
113 #blocks with empty categories are in the xml block tree or hidden
114 if block.get_category(): block_tree.add_block(block.get_category(), block)
116 def __str__(self): return 'Platform - %s(%s)'%(self.get_key(), self.get_name())
118 def is_platform(self): return True
120 def get_new_flow_graph(self): return self.FlowGraph(self)
122 def get_default_flow_graph(self): return self._default_flow_graph
124 def get_generator(self): return self._generator
126 ##############################################
128 ##############################################
129 def get_block_keys(self): return self._blocks.keys()
130 def get_block(self, key): return self._blocks[key]
131 def get_blocks(self): return self._blocks.values()
132 def get_new_block(self, flow_graph, key): return self.Block(flow_graph, n=self._blocks_n[key])
134 def get_name(self): return self._name
136 def get_key(self): return self._key
138 ##############################################
140 ##############################################
141 FlowGraph = _FlowGraph
142 Connection = _Connection