bf09bef0c2998c23c515baf2887a1480689389da
[debian/gnuradio] / grc / src / grc / elements / Platform.py
1 """
2 Copyright 2008 Free Software Foundation, Inc.
3 This file is part of GNU Radio
4
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.
9
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.
14
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
18 """
19 ##@package grc.elements.Platform
20 #A Platform contains all blocks in platform.
21
22 import os
23 from grc import ParseXML
24 from grc import Utils
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
32
33 class Platform(_Element):
34
35         def __init__(self, name, key, block_paths, block_dtd, block_tree, default_flow_graph, generator):
36                 """!
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
46                 """
47                 _Element.__init__(self)
48                 self._name = name
49                 self._key = key
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)
57                 #load the blocks
58                 self._blocks = dict()
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))
66
67         def get_prefs_block(self): return self.get_new_flow_graph().get_new_block('preferences')
68
69         def _load_block(self, f):
70                 """!
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
75                 """
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)
80                 key = block.get_key()
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)
84                 #store the block
85                 self._blocks[key] = block
86                 self._blocks_n[key] = n
87
88         def load_block_tree(self, block_tree):
89                 """!
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
94                 """
95                 #recursive function to load categories and blocks
96                 def load_category(cat_n, parent=''):
97                         #add this category
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))
105                 #load the block tree
106                 f = self._block_tree
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)
115
116         def __str__(self): return 'Platform - %s(%s)'%(self.get_key(), self.get_name())
117
118         def is_platform(self): return True
119
120         def get_new_flow_graph(self): return self.FlowGraph(self)
121
122         def get_default_flow_graph(self): return self._default_flow_graph
123
124         def get_generator(self): return self._generator
125
126         ##############################################
127         # Access Blocks
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])
133
134         def get_name(self): return self._name
135
136         def get_key(self): return self._key
137
138         ##############################################
139         # Constructors
140         ##############################################
141         FlowGraph = _FlowGraph
142         Connection = _Connection
143         Block = _Block
144         Source = _Port
145         Sink = _Port
146         Param = _Param