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.
24 from grc import ParseXML
26 from grc.elements.Element import Element as _Element
27 from grc.elements.FlowGraph import FlowGraph as _FlowGraph
28 from grc.elements.Connection import Connection as _Connection
29 from grc.elements.Block import Block as _Block
30 from grc.elements.Port import Port as _Port
31 from grc.elements.Param import Param as _Param
32 from grc.Constants import DATA_DIR
34 class Platform(_Element):
36 def __init__(self, name, key, block_paths, block_dtd, block_tree, default_flow_graph, generator):
38 Make a platform from the arguments.
39 @param name the platform name
40 @param key the unique platform key
41 @param block_paths the file paths to blocks in this platform
42 @param block_dtd the dtd validator for xml block wrappers
43 @param block_tree the nested tree of block keys and categories
44 @param default_flow_graph the default flow graph file path
45 @param load_one a single file to load into this platform or None
46 @return a platform object
48 _Element.__init__(self)
51 self._block_paths = block_paths
52 self._block_dtd = block_dtd
53 self._block_tree = block_tree
54 self._default_flow_graph = default_flow_graph
55 self._generator = generator
56 #create a dummy flow graph for the blocks
57 self._flow_graph = _Element(self)
60 self._blocks_n = dict()
61 for block_path in self._block_paths:
62 if os.path.isfile(block_path): self._load_block(block_path)
63 elif os.path.isdir(block_path):
64 for dirpath,dirnames,filenames in os.walk(block_path):
65 for filename in filter(lambda f: f.endswith('.xml'), filenames):
66 self._load_block(os.path.join(dirpath, filename))
68 def get_prefs_block(self): return self.get_new_flow_graph().get_new_block('preferences')
70 def _load_block(self, f):
72 Load the block wrapper from the file path.
73 The block wrapper must pass validation, and have a unique block key.
74 If any of the checks fail, exit with error.
75 @param f the file path
77 try: ParseXML.validate_dtd(f, self._block_dtd)
78 except ParseXML.XMLSyntaxError, e: self._exit_with_error('Block definition "%s" failed: \n\t%s'%(f, e))
79 n = ParseXML.from_file(f)['block']
80 block = self.Block(self._flow_graph, n)
82 #test against repeated keys
83 try: assert(key not in self.get_block_keys())
84 except AssertionError: self._exit_with_error('Key "%s" already exists in blocks'%key)
86 self._blocks[key] = block
87 self._blocks_n[key] = n
89 def load_block_tree(self, block_tree):
91 Load a block tree with categories and blocks.
92 Step 1: Load all blocks from the xml specification.
93 Step 2: Load blocks with builtin category specifications.
94 @param block_tree the block tree object
96 #recursive function to load categories and blocks
97 def load_category(cat_n, parent=''):
99 parent = '%s/%s'%(parent, cat_n['name'])
100 block_tree.add_block(parent)
101 #recursive call to load sub categories
102 map(lambda c: load_category(c, parent), Utils.listify(cat_n, 'cat'))
103 #add blocks in this category
104 for block_key in Utils.listify(cat_n, 'block'):
105 block_tree.add_block(parent, self.get_block(block_key))
108 try: ParseXML.validate_dtd(f, os.path.join(DATA_DIR, 'block_tree.dtd'))
109 except ParseXML.XMLSyntaxError, e: self._exit_with_error('Block tree "%s" failed: \n\t%s'%(f, e))
110 #add all blocks in the tree
111 load_category(ParseXML.from_file(f)['cat'])
112 #add all other blocks, use the catgory
113 for block in self.get_blocks():
114 #blocks with empty categories are in the xml block tree or hidden
115 if block.get_category(): block_tree.add_block(block.get_category(), block)
117 def __str__(self): return 'Platform - %s(%s)'%(self.get_key(), self.get_name())
119 def is_platform(self): return True
121 def get_new_flow_graph(self): return self.FlowGraph(self)
123 def get_default_flow_graph(self): return self._default_flow_graph
125 def get_generator(self): return self._generator
127 ##############################################
129 ##############################################
130 def get_block_keys(self): return self._blocks.keys()
131 def get_block(self, key): return self._blocks[key]
132 def get_blocks(self): return self._blocks.values()
133 def get_new_block(self, flow_graph, key): return self.Block(flow_graph, n=self._blocks_n[key])
135 def get_name(self): return self._name
137 def get_key(self): return self._key
139 ##############################################
141 ##############################################
142 FlowGraph = _FlowGraph
143 Connection = _Connection