[kupfer] plugin.rhythmbox_support: Parse library with .iterparse()



commit 7c000d138710bb7258affabfefa0f1667d7cd17d
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Mon Sep 14 13:42:22 2009 +0200

    plugin.rhythmbox_support: Parse library with .iterparse()
    
    .iterparse uses less memory since we are allowed to discard the
    elements directly after they are used; it is also slightly faster.

 kupfer/plugin/rhythmbox_support.py |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
---
diff --git a/kupfer/plugin/rhythmbox_support.py b/kupfer/plugin/rhythmbox_support.py
index b210f85..cbc5ad9 100644
--- a/kupfer/plugin/rhythmbox_support.py
+++ b/kupfer/plugin/rhythmbox_support.py
@@ -27,10 +27,11 @@ def get_rhythmbox_songs(dbfile, typ="song", keys=NEEDED_KEYS):
 	lSongs = []
 	strmap = {}
 
-	etree = ElementTree.parse(rhythmbox_dbfile)
+	# Parse with iterparse; we get the elements when
+	# they are finished, and can remove them directly after use.
 
-	for entry in etree.getroot():
-		if not entry.get("type") == typ:
+	for event, entry in ElementTree.iterparse(rhythmbox_dbfile):
+		if not (entry.tag == ("entry") and entry.get("type") == typ):
 			continue
 		info = {}
 		for child in entry.getchildren():
@@ -39,6 +40,7 @@ def get_rhythmbox_songs(dbfile, typ="song", keys=NEEDED_KEYS):
 				text = _lookup_string(child.text, strmap)
 				info[tag] = text
 		lSongs.append(info)
+		entry.clear()
 	return lSongs
 
 def sort_album(album):



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