Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into xmlparser_rework
Conflicts: src/XMLParser.py
- Loading branch information
Showing
5 changed files
with
309 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> | ||
<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> |
Oops, something went wrong.