summaryrefslogtreecommitdiff
path: root/grc/src/platforms/python/utils
diff options
context:
space:
mode:
Diffstat (limited to 'grc/src/platforms/python/utils')
-rw-r--r--grc/src/platforms/python/utils/Makefile.am30
-rw-r--r--grc/src/platforms/python/utils/__init__.py1
-rw-r--r--grc/src/platforms/python/utils/convert_hier.py78
-rw-r--r--grc/src/platforms/python/utils/expr_utils.py137
-rw-r--r--grc/src/platforms/python/utils/extract_docs.py109
5 files changed, 355 insertions, 0 deletions
diff --git a/grc/src/platforms/python/utils/Makefile.am b/grc/src/platforms/python/utils/Makefile.am
new file mode 100644
index 000000000..b12e51d8e
--- /dev/null
+++ b/grc/src/platforms/python/utils/Makefile.am
@@ -0,0 +1,30 @@
+#
+# Copyright 2008 Free Software Foundation, Inc.
+#
+# This file is part of GNU Radio
+#
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GNU Radio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING. If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+#
+
+include $(top_srcdir)/grc/Makefile.inc
+
+ourpythondir = $(grc_src_prefix)/platforms/python/utils
+
+ourpython_PYTHON = \
+ convert_hier.py \
+ expr_utils.py \
+ extract_docs.py \
+ __init__.py
diff --git a/grc/src/platforms/python/utils/__init__.py b/grc/src/platforms/python/utils/__init__.py
new file mode 100644
index 000000000..8b1378917
--- /dev/null
+++ b/grc/src/platforms/python/utils/__init__.py
@@ -0,0 +1 @@
+
diff --git a/grc/src/platforms/python/utils/convert_hier.py b/grc/src/platforms/python/utils/convert_hier.py
new file mode 100644
index 000000000..495358984
--- /dev/null
+++ b/grc/src/platforms/python/utils/convert_hier.py
@@ -0,0 +1,78 @@
+"""
+Copyright 2008 Free Software Foundation, Inc.
+This file is part of GNU Radio
+
+GNU Radio Companion is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+GNU Radio Companion is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+"""
+
+from .. Constants import BLOCK_DTD
+from .... utils import ParseXML
+from .... utils import odict
+
+def convert_hier(flow_graph, python_file):
+ #extract info from the flow graph
+ input_sig = flow_graph.get_input_signature()
+ output_sig = flow_graph.get_output_signature()
+ parameters = flow_graph.get_parameters()
+ block_key = flow_graph.get_option('id')
+ block_name = flow_graph.get_option('title')
+ block_category = flow_graph.get_option('category')
+ block_desc = flow_graph.get_option('description')
+ block_author = flow_graph.get_option('author')
+ #build the nested data
+ block_n = odict()
+ block_n['name'] = block_name
+ block_n['key'] = block_key
+ block_n['category'] = block_category
+ block_n['import'] = 'execfile("%s")'%python_file
+ #make data
+ block_n['make'] = '%s(\n\t%s,\n)'%(
+ block_key,
+ ',\n\t'.join(['%s=$%s'%(param.get_id(), param.get_id()) for param in parameters]),
+ )
+ #callback data
+ block_n['callback'] = ['set_%s($%s)'%(param.get_id(), param.get_id()) for param in parameters]
+ #param data
+ params_n = list()
+ for param in parameters:
+ param_n = odict()
+ param_n['name'] = param.get_param('label').get_value() or param.get_id()
+ param_n['key'] = param.get_id()
+ param_n['value'] = param.get_param('value').get_value()
+ param_n['type'] = 'raw'
+ params_n.append(param_n)
+ block_n['param'] = params_n
+ #sink data
+ if int(input_sig['nports']):
+ sink_n = odict()
+ sink_n['name'] = 'in'
+ sink_n['type'] = input_sig['type']
+ sink_n['vlen'] = input_sig['vlen']
+ sink_n['nports'] = input_sig['nports']
+ block_n['sink'] = sink_n
+ #source data
+ if int(output_sig['nports']):
+ source_n = odict()
+ source_n['name'] = 'out'
+ source_n['type'] = output_sig['type']
+ source_n['vlen'] = output_sig['vlen']
+ source_n['nports'] = output_sig['nports']
+ block_n['source'] = source_n
+ #doc data
+ block_n['doc'] = "%s\n%s\n%s"%(block_author, block_desc, python_file)
+ #write the block_n to file
+ xml_file = python_file + '.xml'
+ ParseXML.to_file({'block': block_n}, xml_file)
+ ParseXML.validate_dtd(xml_file, BLOCK_DTD)
diff --git a/grc/src/platforms/python/utils/expr_utils.py b/grc/src/platforms/python/utils/expr_utils.py
new file mode 100644
index 000000000..40700993d
--- /dev/null
+++ b/grc/src/platforms/python/utils/expr_utils.py
@@ -0,0 +1,137 @@
+"""
+Copyright 2008 Free Software Foundation, Inc.
+This file is part of GNU Radio
+
+GNU Radio Companion is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+GNU Radio Companion is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+"""
+
+import string
+VAR_CHARS = string.letters + string.digits + '_'
+
+class graph(object):
+ """
+ Simple graph structure held in a dictionary.
+ """
+
+ def __init__(self): self._graph = dict()
+
+ def __str__(self): return str(self._graph)
+
+ def add_node(self, node_key):
+ if self._graph.has_key(node_key): return
+ self._graph[node_key] = set()
+
+ def remove_node(self, node_key):
+ if not self._graph.has_key(node_key): return
+ for edges in self._graph.values():
+ if node_key in edges: edges.remove(node_key)
+ self._graph.pop(node_key)
+
+ def add_edge(self, src_node_key, dest_node_key):
+ self._graph[src_node_key].add(dest_node_key)
+
+ def remove_edge(self, src_node_key, dest_node_key):
+ self._graph[src_node_key].remove(dest_node_key)
+
+ def get_nodes(self): return self._graph.keys()
+
+ def get_edges(self, node_key): return self._graph[node_key]
+
+def expr_split(expr):
+ """
+ Split up an expression by non alphanumeric characters, including underscore.
+ Leave strings in-tact.
+ #TODO ignore escaped quotes, use raw strings.
+ @param expr an expression string
+ @return a list of string tokens that form expr
+ """
+ toks = list()
+ tok = ''
+ quote = ''
+ for char in expr:
+ if quote or char in VAR_CHARS:
+ if char == quote: quote = ''
+ tok += char
+ elif char in ("'", '"'):
+ toks.append(tok)
+ tok = char
+ quote = char
+ else:
+ toks.append(tok)
+ toks.append(char)
+ tok = ''
+ toks.append(tok)
+ return filter(lambda t: t, toks)
+
+def expr_prepend(expr, vars, prepend):
+ """
+ Search for vars in the expression and add the prepend.
+ @param expr an expression string
+ @param vars a list of variable names
+ @param prepend the prepend string
+ @return a new expression with the prepend
+ """
+ expr_splits = expr_split(expr)
+ for i, es in enumerate(expr_splits):
+ if es in vars: expr_splits[i] = prepend + es
+ return ''.join(expr_splits)
+
+def get_variable_dependencies(expr, vars):
+ """
+ Return a set of variables used in this expression.
+ @param expr an expression string
+ @param vars a list of variable names
+ @return a subset of vars used in the expression
+ """
+ expr_toks = expr_split(expr)
+ return set(filter(lambda v: v in expr_toks, vars))
+
+def get_graph(exprs):
+ """
+ Get a graph representing the variable dependencies
+ @param exprs a mapping of variable name to expression
+ @return a graph of variable deps
+ """
+ vars = exprs.keys()
+ #get dependencies for each expression, load into graph
+ var_graph = graph()
+ for var in vars: var_graph.add_node(var)
+ for var, expr in exprs.iteritems():
+ for dep in get_variable_dependencies(expr, vars):
+ var_graph.add_edge(dep, var)
+ return var_graph
+
+def sort_variables(exprs):
+ """
+ Get a list of variables in order of dependencies.
+ @param exprs a mapping of variable name to expression
+ @return a list of variable names
+ @throws AssertionError circular dependencies
+ """
+ var_graph = get_graph(exprs)
+ sorted_vars = list()
+ #determine dependency order
+ while var_graph.get_nodes():
+ #get a list of nodes with no edges
+ indep_vars = filter(lambda var: not var_graph.get_edges(var), var_graph.get_nodes())
+ assert indep_vars
+ #add the indep vars to the end of the list
+ sorted_vars.extend(sorted(indep_vars))
+ #remove each edge-less node from the graph
+ for var in indep_vars: var_graph.remove_node(var)
+ return reversed(sorted_vars)
+
+if __name__ == '__main__':
+ for i in sort_variables({'x':'1', 'y':'x+1', 'a':'x+y', 'b':'y+1', 'c':'a+b+x+y'}): print i
diff --git a/grc/src/platforms/python/utils/extract_docs.py b/grc/src/platforms/python/utils/extract_docs.py
new file mode 100644
index 000000000..dfc0b7e97
--- /dev/null
+++ b/grc/src/platforms/python/utils/extract_docs.py
@@ -0,0 +1,109 @@
+"""
+Copyright 2008 Free Software Foundation, Inc.
+This file is part of GNU Radio
+
+GNU Radio Companion is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+GNU Radio Companion is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+"""
+
+from .. Constants import DOCS_DIR
+from lxml import etree
+import os
+
+DOXYGEN_NAME_XPATH = '/doxygen/compounddef/compoundname'
+DOXYGEN_BRIEFDESC_GR_XPATH = '/doxygen/compounddef/briefdescription'
+DOXYGEN_DETAILDESC_GR_XPATH = '/doxygen/compounddef/detaileddescription'
+DOXYGEN_BRIEFDESC_BLKS2_XPATH = '/doxygen/compounddef/sectiondef[@kind="public-func"]/memberdef/briefdescription'
+DOXYGEN_DETAILDESC_BLKS2_XPATH = '/doxygen/compounddef/sectiondef[@kind="public-func"]/memberdef/detaileddescription'
+
+def extract_txt(xml, parent_text=None):
+ """
+ Recursivly pull the text out of an xml tree.
+ @param xml the xml tree
+ @param parent_text the text of the parent element
+ @return a string
+ """
+ text = xml.text or ''
+ tail = parent_text and xml.tail or ''
+ return text + ''.join(
+ map(lambda x: extract_txt(x, text), xml)
+ ) + tail
+
+def is_match(key, file):
+ """
+ Is the block key a match for the given file name?
+ @param key block key
+ @param file the xml file name
+ @return true if matches
+ """
+ if not file.endswith('.xml'): return False
+ file = file.replace('.xml', '') #remove file ext
+ file = file.replace('__', '_') #doxygen xml files have 2 underscores
+ if key.startswith('gr_'):
+ if not file.startswith('classgr_'): return False
+ key = key.replace('gr_', 'classgr_')
+ elif key.startswith('trellis_'):
+ if not file.startswith('classtrellis_'): return False
+ key = key.replace('trellis_', 'classtrellis_')
+ elif key.startswith('blks2_'):
+ if not file.startswith('classgnuradio_'): return False
+ if 'blks2' not in file: return False
+ file = file.replace('_1_1', '_') #weird blks2 doxygen syntax
+ key = key.replace('blks2_', '')
+ else: return False
+ for k, f in zip(*map(reversed, map(lambda x: x.split('_'), [key, file]))):
+ if k == f: continue
+ ks = k.split('x')
+ if len(ks) == 2 and f.startswith(ks[0]) and f.endswith(ks[1]): continue
+ if len(ks) > 2 and all(ki in ('x', fi) for ki, fi in zip(k, f)): continue
+ return False
+ return True
+
+def extract(key):
+ """
+ Extract the documentation from the doxygen generated xml files.
+ If multiple files match, combine the docs.
+ @param key the block key
+ @return a string with documentation
+ """
+ #get potential xml file matches for the key
+ if os.path.exists(DOCS_DIR) and os.path.isdir(DOCS_DIR):
+ matches = filter(lambda f: is_match(key, f), os.listdir(DOCS_DIR))
+ else: matches = list()
+ #combine all matches
+ doc_strs = list()
+ for match in matches:
+ try:
+ xml_file = DOCS_DIR + '/' + match
+ xml = etree.parse(xml_file)
+ #extract descriptions
+ comp_name = extract_txt(xml.xpath(DOXYGEN_NAME_XPATH)[0]).strip('\n')
+ comp_name = ' --- ' + comp_name + ' --- '
+ if key.startswith('gr_') or key.startswith('trellis_'):
+ brief_desc = extract_txt(xml.xpath(DOXYGEN_BRIEFDESC_GR_XPATH)[0]).strip('\n')
+ detailed_desc = extract_txt(xml.xpath(DOXYGEN_DETAILDESC_GR_XPATH)[0]).strip('\n')
+ elif key.startswith('blks2_'):
+ brief_desc = extract_txt(xml.xpath(DOXYGEN_BRIEFDESC_BLKS2_XPATH)[0]).strip('\n')
+ detailed_desc = extract_txt(xml.xpath(DOXYGEN_DETAILDESC_BLKS2_XPATH)[0]).strip('\n')
+ else:
+ brief_desc = ''
+ detailed_desc = ''
+ #combine
+ doc_strs.append('\n'.join([comp_name, brief_desc, detailed_desc]).strip('\n'))
+ except IndexError: pass #bad format
+ return '\n\n'.join(doc_strs)
+
+if __name__ == '__main__':
+ import sys
+ print extract(sys.argv[1])