Skip to content

Commit

Permalink
Begin rework of XML parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Ferguson committed May 22, 2009
1 parent 9e74b38 commit 3d7df62
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 64 deletions.
3 changes: 2 additions & 1 deletion src/MainUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ def col_toggled_cb( self, cell, path, model ):

if model[path][2]:
list = self.plugin_db.fetch_plugin( str( model[path][0] ) )

for row in list:
print row

# Load and instantiate the new plugin
exec "from plugins import " + row[3]
Expand Down
8 changes: 4 additions & 4 deletions src/PluginDatabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def write_available_plugins( self ):
xml_fileset = glob.glob( 'plugins/*.xml' )

for xml_file in xml_fileset:
parser = XMLParser.XMLParser( xml_file )
data = parser.return_result( )
self.cursor.execute( 'INSERT INTO plugins VALUES (null, ?, ?, ?)', ( data[0], data[1], data[2] ) )
parser = XMLParser.Parser( xml_file )
data = parser.fetchSet( )
self.cursor.execute( 'INSERT INTO plugins VALUES (null, ?, ?, ?)', ( data['name'], data['description'], data['main_module'] ) )

self.connect.commit( )

Expand All @@ -42,4 +42,4 @@ def __init__( self ):
self.create_plugin_table( )
self.write_available_plugins( )
self.fetch_available_plugins( )


94 changes: 42 additions & 52 deletions src/XMLParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,50 @@
@author: scott
'''
from xml.sax import saxutils
from xml.sax import make_parser
from xml.sax.handler import feature_namespaces
from xml.parsers import expat

def normalize_whitespace( text ):
"Remove redundant whitespace from a string"
return ' '.join( text.split( ) )

class ParsePlugin( saxutils.DefaultHandler ):

def __init__( self ):
# Initialize the flag to false
self.plugin_info = [ ]
self.inDescription = 0
self.inMainModule = 0
class Parser( ):

def startElement( self, root, attrs ):
if root == 'plugin':
name = attrs.get( 'name', None )
self.plugin_info.append( normalize_whitespace( name ) )

elif root == 'description':
self.inDescription = 1
self.plugin_description = ""

elif root == 'main_module':
self.inMainModule = 1
self.plugin_module = ""

def characters( self, ch ):
def startElement( self, name, attrs ):
if name == 'plugin':
self.name = attrs['name']
elif name == 'description':
self.inDescription = True
elif name == 'main_module':
self.inMainModule = True

def endElement( self, name ):
if name == 'description':
self.inDescription = False
elif name == 'main':
self.inMainModule = False

def characters( self, data ):
print "Description: " + self.normalizeWhitespace( data )

if self.inDescription:
self.plugin_description = self.plugin_description + ch
self.description = self.normalizeWhitespace( data )
self.inDescription = False
elif self.inMainModule:
self.plugin_module = self.plugin_module + ch

def endElement( self, name ):
if name =='description':
self.inDescription = 0
self.plugin_info.append( normalize_whitespace( self.plugin_description ) )
elif name =='main_module':
self.inMainModule = 0
self.plugin_info.append( normalize_whitespace( self.plugin_module ) )

def fetchDataSet( self ):
return self.plugin_info

class XMLParser:

def return_result( self ):
return self.default_handler.fetchDataSet( )

self.main_module = self.normalizeWhitespace( data )
self.inMainModule = False

def normalizeWhitespace( self, text ):
"""Remove redundant whitespace from a string"""
return " ".join( text.split( ) )

def fetchSet( self ):

print self.name

return { 'name' : self.name, 'description' : self.description, 'main_module' : self.main_module }

def __init__( self, file ):
self.parser = make_parser( )
self.parser.setFeature( feature_namespaces, 0 )
self.default_handler = ParsePlugin( )
self.parser.setContentHandler( self.default_handler )
self.parser.parse( file )
self.inDescription = False
self.inMainModule = False

parser = expat.ParserCreate( )
parser.StartElementHandler = self.startElement
parser.EndElementHandler = self.endElement
parser.CharacterDataHandler = self.characters
parser.ParseFile( open( file, "r" ) )
14 changes: 7 additions & 7 deletions src/plugins/GmailCheck.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin name="Gmail Check 0.1">
<description>
Periodically checks your Gmail account for new messages
</description>
<main_module>
GmailCheck
</main_module>
</plugin>
<description>
Periodically checks your Gmail account for new messages
</description>
<main_module>
GmailCheck
</main_module>
</plugin>

0 comments on commit 3d7df62

Please sign in to comment.