Skip to content

Commit

Permalink
Merge branch 'master' into xmlparser_rework
Browse files Browse the repository at this point in the history
Conflicts:
	src/XMLParser.py
  • Loading branch information
scottferg committed May 28, 2009
2 parents f9fd904 + a18e6ee commit 87ea41f
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 312 deletions.
3 changes: 1 addition & 2 deletions src/MainUI.py
Expand Up @@ -90,9 +90,8 @@ 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:

# Load and instantiate the new plugin
exec "from plugins import " + row[3]
exec "item = " + row[3] + "." + row[3] + "()"
Expand Down
6 changes: 2 additions & 4 deletions src/PluginDatabase.py
Expand Up @@ -25,9 +25,8 @@ 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] ) )
data = XMLParser.parseFile( xml_file )
self.cursor.execute( 'INSERT INTO plugins VALUES (null, ?, ?, ?)', ( data['plugin']['name'], data['description'], data['main_module'] ) )

self.connect.commit( )

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

112 changes: 56 additions & 56 deletions src/XMLParser.py
@@ -1,61 +1,61 @@
'''
Created on Apr 19, 2009
'''Created on Apr 19, 2009
@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( ) )
data_set = {}
current_element = ""

class ParsePlugin( saxutils.DefaultHandler ):

def __init__( self ):
self.plugin_info = [ ]
self.inDescription = 0
self.inMainModule = 0

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 ):
if self.inDescription:
self.plugin_description = self.plugin_description + ch
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 startElement( name, attrs ):
global current_element

def fetchDataSet( self ):
return self.plugin_info

class XMLParser:

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

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 )
current_element = name

if len( attrs ) > 0:
data_set[ name ] = dict( [ ( key, value ) for key, value in attrs.iteritems( ) ] )

def endElement( name ):
global current_element
current_element = ""

def characters( data ):
global current_element

if data:
# Currently this won't read an element with properties and a value
try:
if type( data_set[ current_element ] ) is type( {} ):
return

data_set[ current_element ] = normalizeWhitespace( str( data_set[ current_element ] ) + data )
except KeyError:
data_set[ current_element ] = data

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

def parseStream( uri ):
"""
Parse an XML stream and return a dictionary containing the dataset
"""
parser = expat.ParserCreate( )
parser.StartElementHandler = startElement
parser.EndElementHandler = endElement
parser.CharacterDataHandler = characters
parser.Parse( uri )

return data_set

def parseFile( file ):
"""
Parse an XML file and return a dictionary containing the dataset
"""
parser = expat.ParserCreate( )
parser.StartElementHandler = startElement
parser.EndElementHandler = endElement
parser.CharacterDataHandler = characters
parser.ParseFile( open( file, "r" ) )

return data_set
6 changes: 3 additions & 3 deletions src/plugins/GmailCheck.xml
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin name="Gmail Check 0.1">
<plugin name="Gmail Check 0.2">
<description>
Periodically checks your Gmail account for new messages
Periodically checks your Gmail account for new messages
</description>
<main_module>
GmailCheck
</main_module>
</plugin>
</plugin>

0 comments on commit 87ea41f

Please sign in to comment.