[kupfer] plugin.xml_support: Move XMLEntryHandler to own file



commit bf4c1a0fa9a4559611d0da3315c8b87c4cbe230b
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Mon Aug 24 22:17:48 2009 +0200

    plugin.xml_support: Move XMLEntryHandler to own file
    
    The XML parser was generalized and moves to an own file.
    
    I also regard the original code as purely inspirational now, and the
    xml_support is reasonably under my copyright alone. Please compare to
    verify:
    
    git diff c235450664c6ef58e77e..8cc66160a6499f710dd -- kupfer/plugin/rhythmbox_support.py

 kupfer/plugin/rhythmbox_support.py |   82 ++---------------------------------
 kupfer/plugin/xml_support.py       |   52 +++++++++++++++++++++++
 2 files changed, 57 insertions(+), 77 deletions(-)
---
diff --git a/kupfer/plugin/rhythmbox_support.py b/kupfer/plugin/rhythmbox_support.py
index d197d65..7b78b2b 100644
--- a/kupfer/plugin/rhythmbox_support.py
+++ b/kupfer/plugin/rhythmbox_support.py
@@ -1,90 +1,18 @@
-#!/usr/bin/python
-
-"""
-Copyright (c) 2007 Tyler G. Hicks-Wright
-              2009 Ulrik Sverdrup <ulrik sverdrup gmail com>
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
-"""
 
 import os
-from xml.sax import make_parser
-from xml.sax.handler import ContentHandler
-
-class XMLEntryHandler(ContentHandler):
-    """Parse XML input into a list of dictionaries.
-    Get only entries of type @entry_type are taken.
-    and only record keys in the iterable wanted_keys.
+import xml.sax
 
-    This ContentHandler keeps a big string mapping open, to
-    make sure that equal strings are using equal instances to save memory.
-    """
-    def __init__(self, entries, entry_name, entry_attributes, wanted_keys):
-        ContentHandler.__init__(self)
-        self.all_entries = entries
-        self.entry_name = entry_name
-        self.entry_attributes = entry_attributes.items()
-        self.wanted_keys = dict((k, k) for k in wanted_keys)
-        self.is_parsing_tag = False
-        self.is_wanted_element = False
-        self.song_entry = None
-        self.string_map = {}
-        self.element_content = ''
-
-    def startElement(self, sName, attributes):
-        if (sName == self.entry_name and
-                all(attributes.get(k) == v for k, v in self.entry_attributes)):
-            self.song_entry = {}
-            self.is_parsing_tag = True
-            self.is_wanted_element = True
-        else:
-            self.is_wanted_element = (sName in self.wanted_keys)
-        self.element_content = ''
-
-    def characters(self, sData):
-        if self.is_wanted_element:
-            self.element_content += sData
-
-    def _get_or_internalize(self, string):
-        if string not in self.string_map:
-            self.string_map[string] = string
-            return string
-        return self.string_map[string]
-
-    def endElement(self,sName):
-        if sName == self.entry_name:
-            if self.song_entry:
-                self.all_entries.append(self.song_entry)
-            self.song_entry = None
-            self.is_parsing_tag = False
-        elif self.is_parsing_tag and self.is_wanted_element:
-            sName = self.wanted_keys[sName]
-            self.song_entry[sName] = self._get_or_internalize(self.element_content)
+from . import xml_support
 
 NEEDED_KEYS= ("title", "artist", "album", "track-number", "location", )
+
 def get_rhythmbox_songs(typ="song", keys=NEEDED_KEYS,
         dbfile='~/.local/share/rhythmbox/rhythmdb.xml'):
     rhythmbox_dbfile = os.path.expanduser(dbfile)
-    rbParser = make_parser()
+    rbParser = xml.sax.make_parser()
     lSongs = []
     attributes = {"type": typ}
-    rbHandler = XMLEntryHandler(lSongs, "entry", attributes, keys)
+    rbHandler = xml_support.XMLEntryHandler(lSongs, "entry", attributes, keys)
     rbParser.setContentHandler(rbHandler)
     rbParser.parse(rhythmbox_dbfile)
     return lSongs
diff --git a/kupfer/plugin/xml_support.py b/kupfer/plugin/xml_support.py
new file mode 100644
index 0000000..fbc4f99
--- /dev/null
+++ b/kupfer/plugin/xml_support.py
@@ -0,0 +1,52 @@
+
+from xml.sax.handler import ContentHandler
+
+class XMLEntryHandler(ContentHandler):
+    """Parse XML input into a list of dictionaries.
+    Get only entries of type @entry_type are taken.
+    and only record keys in the iterable wanted_keys.
+
+    This ContentHandler keeps a big string mapping open, to
+    make sure that equal strings are using equal instances to save memory.
+    """
+    def __init__(self, entries, entry_name, entry_attributes, wanted_keys):
+        ContentHandler.__init__(self)
+        self.all_entries = entries
+        self.entry_name = entry_name
+        self.entry_attributes = entry_attributes.items()
+        self.wanted_keys = dict((k, k) for k in wanted_keys)
+        self.is_parsing_tag = False
+        self.is_wanted_element = False
+        self.song_entry = None
+        self.string_map = {}
+        self.element_content = ''
+
+    def startElement(self, sName, attributes):
+        if (sName == self.entry_name and
+                all(attributes.get(k) == v for k, v in self.entry_attributes)):
+            self.song_entry = {}
+            self.is_parsing_tag = True
+            self.is_wanted_element = True
+        else:
+            self.is_wanted_element = (sName in self.wanted_keys)
+        self.element_content = ''
+
+    def characters(self, sData):
+        if self.is_wanted_element:
+            self.element_content += sData
+
+    def _get_or_internalize(self, string):
+        if string not in self.string_map:
+            self.string_map[string] = string
+            return string
+        return self.string_map[string]
+
+    def endElement(self,sName):
+        if sName == self.entry_name:
+            if self.song_entry:
+                self.all_entries.append(self.song_entry)
+            self.song_entry = None
+            self.is_parsing_tag = False
+        elif self.is_parsing_tag and self.is_wanted_element:
+            sName = self.wanted_keys[sName]
+            self.song_entry[sName] = self._get_or_internalize(self.element_content)



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