commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] r8432 - in grc/trunk: notes scripts src/grc src/grc/da


From: jblum
Subject: [Commit-gnuradio] r8432 - in grc/trunk: notes scripts src/grc src/grc/data src/grc/elements src/grc/gui src/grc_gnuradio/blocks
Date: Fri, 16 May 2008 00:32:52 -0600 (MDT)

Author: jblum
Date: 2008-05-16 00:32:50 -0600 (Fri, 16 May 2008)
New Revision: 8432

Modified:
   grc/trunk/notes/todo.txt
   grc/trunk/scripts/grc
   grc/trunk/src/grc/ActionHandler.py
   grc/trunk/src/grc/ParseXML.py
   grc/trunk/src/grc/Preferences.py
   grc/trunk/src/grc/converter.py
   grc/trunk/src/grc/data/block_tree.dtd
   grc/trunk/src/grc/data/flow_graph.dtd
   grc/trunk/src/grc/elements/Platform.py
   grc/trunk/src/grc/gui/NotebookPage.py
   grc/trunk/src/grc_gnuradio/blocks/block.dtd
Log:
switched to lxml

Modified: grc/trunk/notes/todo.txt
===================================================================
--- grc/trunk/notes/todo.txt    2008-05-15 17:51:05 UTC (rev 8431)
+++ grc/trunk/notes/todo.txt    2008-05-16 06:32:50 UTC (rev 8432)
@@ -7,14 +7,17 @@
 -pad source/sink
 -hier block
 -optparse block
+-FIR convinence filters
 
 ############   Features to Add:        ####################
--startup tips
 -save working directory after close
 -create sub-flow graphs to be used in larger flow graphs
 -copy and paste blocks
 -disable blocks (grey out, keep in design file, but generate ignores)
--conversion script from previous format
+-eval cache
+-param display, better string truncate
+-param editor, expand entry boxes in focus
+-change param dialog to panel within main window
 
 ############   Suggestions:    ####################
 -scope sink constructor needs average option and triggering option

Modified: grc/trunk/scripts/grc
===================================================================
--- grc/trunk/scripts/grc       2008-05-15 17:51:05 UTC (rev 8431)
+++ grc/trunk/scripts/grc       2008-05-16 06:32:50 UTC (rev 8432)
@@ -38,7 +38,7 @@
        (options, args) = parser.parse_args()   
        #"test" import modules that this program will use
        error = False
-       for module in ('pygtk', 'wx', 'numpy', 'xml.dom.minidom', 'gnuradio', 
'gnuradio.gr.hier_block2', 'Cheetah'):
+       for module in ('pygtk', 'wx', 'numpy', 'lxml', 'gnuradio', 
'gnuradio.gr.hier_block2', 'Cheetah'):
                try: __import__(module)         
                except ImportError: #print error
                        error = True

Modified: grc/trunk/src/grc/ActionHandler.py
===================================================================
--- grc/trunk/src/grc/ActionHandler.py  2008-05-15 17:51:05 UTC (rev 8431)
+++ grc/trunk/src/grc/ActionHandler.py  2008-05-16 06:32:50 UTC (rev 8432)
@@ -280,7 +280,7 @@
                        if not self.get_page().get_file_path(): 
self.handle_states(FLOW_GRAPH_SAVE_AS)
                        else:
                                try:
-                                       
ParseXML.to_file(ParseXML.to_xml(self.get_flow_graph().export_data()), 
self.get_page().get_file_path())
+                                       
ParseXML.to_file(self.get_flow_graph().export_data(), 
self.get_page().get_file_path())
                                        self.get_page().set_saved(True)         
                                except IOError: 
                                        
Messages.send_fail_save(self.get_page().get_file_path())

Modified: grc/trunk/src/grc/ParseXML.py
===================================================================
--- grc/trunk/src/grc/ParseXML.py       2008-05-15 17:51:05 UTC (rev 8431)
+++ grc/trunk/src/grc/ParseXML.py       2008-05-16 06:32:50 UTC (rev 8432)
@@ -20,131 +20,80 @@
 #Parse xml files to nested data and vice-versa.
 address@hidden Josh Blum
 
-import xml.dom.minidom
-from xml.parsers.xmlproc import xmlval
-from xml.parsers.xmlproc import xmlproc
-from xml.parsers.xmlproc import xmldtd
+from lxml import etree
 from Utils import odict
-
-class ValidationError(Exception):
        
-       def __init__(self, type, message, location):
-               self.type = type
-               self.message = message
-               self.location = location
-       
-       def __str__(self):
-               return "Line: %s, Column: %s\n%s: %s"%(self.location[0], 
self.location[1], self.type, self.message)
-
-class ErrorHandler(xmlproc.ErrorHandler):
-       
-       def location(self):
-               return self.locator.get_line(), self.locator.get_column()
-       
-       def warning(self, msg):
-               raise ValidationError('Warning', msg, self.location())
-       
-       def error(self, msg):
-               raise ValidationError('Error', msg, self.location())
-       
-       def fatal(self, msg):
-               raise ValidationError('Fatal', msg, self.location())
-       
 def validate_dtd(xml_file, dtd_file=None):
        """!
        Validate an xml file against its dtd.
        @param xml_file the xml file
        @param dtd_file the optional dtd file
+       @throws Exception validation fails
        """
        if dtd_file:
-               dtd = xmldtd.load_dtd(dtd_file)
-               parser = xmlproc.XMLProcessor()
-               parser.set_application(xmlval.ValidatingApp(dtd, parser))
+               dtd = etree.DTD(dtd_file)
+               xml = etree.parse(xml_file)
+               if not dtd.validate(xml.getroot()):
+                       raise Exception, '\n'.join(map(str, 
dtd.error_log.filter_from_errors()))
        else:
-               parser = xmlval.XMLValidator()
-       parser.set_error_handler(ErrorHandler(parser))
-       parser.parse_resource(xml_file)
+               parser = etree.XMLParser(dtd_validation=True)
+               xml = etree.parse(xml_file, parser=parser)              
 
-def from_xml(doc):
+def from_file(xml_file):
        """!
-       Create nested data from an xml doc using the from xml helper.
-       @param doc the xml doc
+       Create nested data from an xml file using the from xml helper.
+       @param xml_file the xml file path
        @return the nested data
        """
-       return _from_xml(filter(lambda elem: elem.localName, doc.childNodes)[0])
+       xml = etree.parse(xml_file).getroot()
+       return _from_file(xml)
        
-def _from_xml(doc):
+def _from_file(xml):
        """!
-       Recursivly parse the xml doc into nested data format.   
-       @param doc the xml doc
+       Recursivly parse the xml tree into nested data format.  
+       @param xml the xml tree
        @return the nested data
-       """
-       tag_name = str(doc.localName)
-       if not doc.hasChildNodes(): 
-               return odict({tag_name: ''})
-       elif len(doc.childNodes) == 1 and doc.firstChild.nodeValue:
-               return odict({tag_name: doc.firstChild.nodeValue})
+       """     
+       tag = xml.tag
+       if not len(xml): 
+               return odict({tag: xml.text or ''}) #store empty tags (text is 
None) as empty string
        nested_data = odict()
-       for elem in filter(lambda elem: elem.localName, doc.childNodes):
-               key, value = _from_xml(elem).items()[0]
+       for elem in xml:
+               key, value = _from_file(elem).items()[0]
                if nested_data.has_key(key): nested_data[key].append(value)
                else: nested_data[key] = [value]
+       #delistify if the length of values is 1
        for key, values in nested_data.iteritems():
                if len(values) == 1: nested_data[key] = values[0]
-       return odict({tag_name: nested_data})
+       return odict({tag: nested_data})
        
-def to_xml(nested_data):
+def to_file(nested_data, xml_file):
        """!
-       Create an xml doc and use the to xml helper method to load it.
+       Write an xml file and use the to xml helper method to load it.
        @param nested_data the nested data
-       @return the xml doc
+       @param xml_file the xml file path
        """
-       doc = xml.dom.minidom.Document()
-       doc.appendChild(_to_xml(nested_data, doc, 1))
-       return doc
+       xml = _to_file(nested_data)[0]
+       open(xml_file, 'w').write(etree.tostring(xml, xml_declaration=True, 
pretty_print=True))
        
-def _to_xml(nested_data, doc, level):
+def _to_file(nested_data):
        """!
-       Recursivly parse the nested data into xml doc format.
+       Recursivly parse the nested data into xml tree format.
        @param nested_data the nested data
-       @param the xml document class for creating nodes
-       @param level the nesting level
-       @return the xml doc filled with child nodes
+       @return the xml tree filled with child nodes
        """     
-       assert(len(nested_data) == 1)
-       tag_name, value = nested_data.items()[0]
-       child = doc.createElementNS(None, tag_name)
-       if type(value) == type(odict()):        
-               child.appendChild(doc.createTextNode("\n"))
-               for key, elems in value.iteritems():
-                       if type(elems) != type(list()): elems = [elems]
-                       for elem in elems:                              
-                               
child.appendChild(doc.createTextNode("\t"*level))
-                               child.appendChild(_to_xml(odict({key: elem}), 
doc, level + 1))
-                               child.appendChild(doc.createTextNode("\n"))
-               child.appendChild(doc.createTextNode("\t"*(level-1)))
-       elif value: child.appendChild(doc.createTextNode(str(value)))
-       return child
+       nodes = list()
+       for key,values in nested_data.iteritems():
+               #listify the values if not a list
+               if not isinstance(values, (list, set, tuple)):
+                       values = [values]
+               for value in values:
+                       node = etree.Element(key)
+                       if isinstance(value, str): node.text = value
+                       else: node.extend(_to_file(value))
+                       nodes.append(node)
+       return nodes
 
-def to_file(doc, file_path):
-       """!
-       Save the xml doc to the file path.
-       @param doc the xml doc
-       @param file_path the destination xml-file
-       """ 
-       open(file_path, 'w').write(doc.toxml())
-       
-def from_file(file_path):
-       """!
-       Load an xml doc from the file path.
-       @param file_path the source xml-file
-       @return the xml doc
-       """
-       return xml.dom.minidom.parse(open(file_path, 'r'))
-
 if __name__ == '__main__':     
        """Use the main method to test parse xml's functions."""
-       import sys
-       n = from_xml(from_file(sys.argv[1]))
-       to_file(to_xml(n), sys.argv[2])
        pass    

Modified: grc/trunk/src/grc/Preferences.py
===================================================================
--- grc/trunk/src/grc/Preferences.py    2008-05-15 17:51:05 UTC (rev 8431)
+++ grc/trunk/src/grc/Preferences.py    2008-05-16 06:32:50 UTC (rev 8432)
@@ -20,7 +20,7 @@
 #Holds global paramerences
 address@hidden Josh Blum
 
-from Constants import PREFERENCES_FILE_PATH
+from grc.Constants import PREFERENCES_FILE_PATH, DATA_DIR
 import ParseXML
 import Messages
 
@@ -80,8 +80,9 @@
        @param window optional flow graph window
        """
        try: 
-               n = 
ParseXML.from_xml(ParseXML.from_file(PREFERENCES_FILE_PATH))                
-               block.import_data(n['preferences'])
+               ParseXML.validate_dtd(PREFERENCES_FILE_PATH, DATA_DIR + 
'/flow_graph.dtd')
+               n = ParseXML.from_file(PREFERENCES_FILE_PATH)
+               block.import_data(n['block'])
                #set window size
                try:
                        size = window_size_param.evaluate()
@@ -97,7 +98,7 @@
        if window:
                size = str(window.get_size())
                window_size_param.set_value(size)
-       try: ParseXML.to_file(ParseXML.to_xml({'preferences': 
block.export_data()}), PREFERENCES_FILE_PATH)             
+       try: ParseXML.to_file({'block': block.export_data()}, 
PREFERENCES_FILE_PATH)    
        except IOError: Messages.send_fail_save_preferences()
                
 ###########################################################################

Modified: grc/trunk/src/grc/converter.py
===================================================================
--- grc/trunk/src/grc/converter.py      2008-05-15 17:51:05 UTC (rev 8431)
+++ grc/trunk/src/grc/converter.py      2008-05-16 06:32:50 UTC (rev 8432)
@@ -82,7 +82,7 @@
        ############################################################
        #       extract window size, variables, blocks, and connections
        ############################################################
-       old_n = ParseXML.from_xml(ParseXML.from_file(file_path))['flow_graph']
+       old_n = ParseXML.from_file(file_path)['flow_graph']
        window_size = '%s, %s'%(
                Utils.exists_or_else(old_n, 'window_width', '2048'), 
                Utils.exists_or_else(old_n, 'window_height', '2048'),
@@ -232,5 +232,5 @@
        #backup after successful conversion
        os.rename(file_path, file_path+'.bak')
        #save new flow graph to file path
-       ParseXML.to_file(ParseXML.to_xml({'flow_graph': new_n}), file_path)
+       ParseXML.to_file({'flow_graph': new_n}, file_path)
        

Modified: grc/trunk/src/grc/data/block_tree.dtd
===================================================================
--- grc/trunk/src/grc/data/block_tree.dtd       2008-05-15 17:51:05 UTC (rev 
8431)
+++ grc/trunk/src/grc/data/block_tree.dtd       2008-05-16 06:32:50 UTC (rev 
8432)
@@ -1,4 +1,3 @@
-<?xml version="1.0"?>
 <!-- 
 Copyright 2008 Free Software Foundation, Inc.
 This file is part of GNU Radio

Modified: grc/trunk/src/grc/data/flow_graph.dtd
===================================================================
--- grc/trunk/src/grc/data/flow_graph.dtd       2008-05-15 17:51:05 UTC (rev 
8431)
+++ grc/trunk/src/grc/data/flow_graph.dtd       2008-05-16 06:32:50 UTC (rev 
8432)
@@ -1,4 +1,3 @@
-<?xml version="1.0"?>
 <!-- 
 Copyright 2008 Free Software Foundation, Inc.
 This file is part of GNU Radio

Modified: grc/trunk/src/grc/elements/Platform.py
===================================================================
--- grc/trunk/src/grc/elements/Platform.py      2008-05-15 17:51:05 UTC (rev 
8431)
+++ grc/trunk/src/grc/elements/Platform.py      2008-05-16 06:32:50 UTC (rev 
8432)
@@ -64,9 +64,8 @@
        
        def _load_block(self, f):
                try: ParseXML.validate_dtd(f)
-               except ParseXML.ValidationError, e: 
self._exit_with_error('Block definition "%s" failed: \n\t%s'%(f, e))
-               x = ParseXML.from_file(f)
-               n = ParseXML.from_xml(x)['block']
+               except Exception, e: self._exit_with_error('Block definition 
"%s" failed: \n\t%s'%(f, e))
+               n = ParseXML.from_file(f)['block']
                block = self.Block(self.flow_graph, n)
                key = block.get_key()
                #test against repeated keys
@@ -85,9 +84,8 @@
        def get_block_tree(self):
                f = self._block_tree
                try: ParseXML.validate_dtd(f, DATA_DIR + '/block_tree.dtd')
-               except ParseXML.ValidationError, e: 
self._exit_with_error('Block tree "%s" failed: \n\t%s'%(f, e))
-               x = ParseXML.from_file(f)
-               n = ParseXML.from_xml(x)['block_tree']
+               except Exception, e: self._exit_with_error('Block tree "%s" 
failed: \n\t%s'%(f, e))
+               n = ParseXML.from_file(f)['block_tree']
                return n
        
        def get_default_flow_graph(self): return self._default_flow_graph

Modified: grc/trunk/src/grc/gui/NotebookPage.py
===================================================================
--- grc/trunk/src/grc/gui/NotebookPage.py       2008-05-15 17:51:05 UTC (rev 
8431)
+++ grc/trunk/src/grc/gui/NotebookPage.py       2008-05-16 06:32:50 UTC (rev 
8432)
@@ -53,7 +53,7 @@
                converter.convert(file_path, flow_graph.get_parent())
                ############################################################
                ParseXML.validate_dtd(file_path, DATA_DIR + '/flow_graph.dtd')
-               initial_state = ParseXML.from_xml(ParseXML.from_file(file_path))
+               initial_state = ParseXML.from_file(file_path)
                self.state_cache = StateCache(initial_state)                    
        
                self.set_saved(True)    
                #parse xml success, initialize page             

Modified: grc/trunk/src/grc_gnuradio/blocks/block.dtd
===================================================================
--- grc/trunk/src/grc_gnuradio/blocks/block.dtd 2008-05-15 17:51:05 UTC (rev 
8431)
+++ grc/trunk/src/grc_gnuradio/blocks/block.dtd 2008-05-16 06:32:50 UTC (rev 
8432)
@@ -1,4 +1,3 @@
-<?xml version="1.0"?>
 <!-- 
 Copyright 2008 Free Software Foundation, Inc.
 This file is part of GNU Radio





reply via email to

[Prev in Thread] Current Thread [Next in Thread]