Author: dmeyer
Date: Mon Mar 19 10:02:39 2007
New Revision: 2571

Modified:
   trunk/metadata/src/image/core.py
   trunk/metadata/src/misc/directory.py

Log:
replace kaa.xml in bins parser with xml.sax

Modified: trunk/metadata/src/image/core.py
==============================================================================
--- trunk/metadata/src/image/core.py    (original)
+++ trunk/metadata/src/image/core.py    Mon Mar 19 10:02:39 2007
@@ -34,9 +34,7 @@
 import sys
 import gzip
 import logging
-
-# kaa imports
-from kaa import xml
+import xml.sax
 
 # kaa.metadata imports
 from kaa.metadata.factory import register
@@ -50,6 +48,51 @@
               'thumbnail','software','hardware', 'dpi', 'city', 'rotation', 
'author' ]
 
 
+class BinsParser(xml.sax.ContentHandler):
+    def __init__(self, filename):
+        xml.sax.ContentHandler.__init__(self)
+        self.mode = 0
+        self.var = None
+        self.dict = {}
+
+        parser = xml.sax.make_parser()
+        parser.setContentHandler(self)
+        try:
+            parser.parse(filename)
+        except (KeyboardInterrupt, SystemExit):
+            sys.exit(0)
+        except ParseError:
+            pass
+        except Exception, e:
+            log.exception('bins parser')
+
+    def items(self):
+        return self.dict.items()
+    
+    def startElement(self, name, attr):
+        if self.mode == 0:
+            if name not in ('album', 'image'):
+                raise ParseError
+            self.mode = 1
+        if self.mode == 2 and name == 'field':
+            self.var = attr['name']
+            self.chars = ''
+        if self.mode == 1 and name == 'description':
+            self.mode = 2
+
+    def endElement(self, name):
+        if self.mode == 2 and name == 'description':
+            self.mode = 1
+        if self.var:
+            value = self.chars.strip()
+            if value:
+                self.dict[self.var] = value
+            self.var = None
+            
+    def characters(self, c):
+        if self.var:
+            self.chars += c
+            
 class Image(Media):
     """
     Digital Images, Photos, Pictures.
@@ -72,38 +115,18 @@
         """
         Parse external files like bins and .comments.
         """
-        for func in (self.parse_bins, self.parse_dot_comment):
-            try:
-                func(filename)
-            except (KeyboardInterrupt, SystemExit):
-                sys.exit(0)
-            except:
-                pass
-
-
-    def parse_bins(self, filename):
-        """
-        Parse bins xml files
-        """
+        # Parse bins xml files
         binsxml = filename + '.xml'
-        if not os.path.isfile(binsxml):
-            return
-        doc = xml.Document(binsxml, 'image')
-        for child in doc.get_child('description').children:
-            key = str(child.getattr('name'))
-            if not key or not child.content:
-                continue
-            self._set(key, child.content)
-
-
-    def parse_dot_comment(self, filename):
-        """
-        Parse info in .comments.
-        """
+        if os.path.isfile(binsxml):
+            bins = BinsParser(binsxml)
+            for key, value in bins.items():
+                self._set(key, value)
+        # FIXME: this doesn't work anymore
         comment_file = os.path.join(os.path.dirname(filename), '.comments',
                                     os.path.basename(filename) + '.xml')
-        if not os.path.isfile(comment_file):
+        if not os.path.isfile(comment_file) or 1:
             return
+        # FIXME: replace kaa.xml stuff with sax or minidom
         doc = xml.Document(comment_file, 'Comment')
         for child in doc.children:
             if child.name == 'Place':

Modified: trunk/metadata/src/misc/directory.py
==============================================================================
--- trunk/metadata/src/misc/directory.py        (original)
+++ trunk/metadata/src/misc/directory.py        Mon Mar 19 10:02:39 2007
@@ -36,16 +36,15 @@
 
 # kaa imports
 from kaa.strutils import unicode_to_str
-from kaa import xml
 
 # kaa.metadata imports
 import kaa.metadata.core as core
 import kaa.metadata.factory as factory
+from kaa.metadata.image.core import BinsParser
 
 # get logging object
 log = logging.getLogger('metadata')
 
-
 class Directory(core.Media):
     """
     Simple parser for reading a .directory file.
@@ -54,56 +53,34 @@
 
     def __init__(self, directory):
         core.Media.__init__(self)
-        for func in (self.parse_dot_directory, self.parse_bins):
-            try:
-                func(directory)
-            except (KeyboardInterrupt, SystemExit):
-                sys.exit(0)
-            except:
-                log.exception('%s', func)
-
 
-    def parse_dot_directory(self, directory):
-        """
-        search .directory
-        """
+        # search .directory
         info = os.path.join(directory, '.directory')
-        if not os.path.isfile(info):
-            return
-        f = open(info)
-        for l in f.readlines():
-            if l.startswith('Icon='):
-                image = l[5:].strip()
-                if not image.startswith('/'):
-                    image = os.path.join(directory, image)
-                self._set('image', image)
-            if l.startswith('Name='):
-                self.title = l[5:].strip()
-            if l.startswith('Comment='):
-                self.comment = l[8:].strip()
-        f.close()
-
-
-    def parse_bins(self, directory):
-        """
-        search album.xml (bins)
-        """
-        binsxml = os.path.join(directory, 'album.xml')
-        if not os.path.isfile(binsxml):
-            return
+        if os.path.isfile(info):
+            f = open(info)
+            for l in f.readlines():
+                if l.startswith('Icon='):
+                    image = l[5:].strip()
+                    if not image.startswith('/'):
+                        image = os.path.join(directory, image)
+                    self._set('image', image)
+                if l.startswith('Name='):
+                    self.title = l[5:].strip()
+                if l.startswith('Comment='):
+                    self.comment = l[8:].strip()
+            f.close()
 
-        doc = xml.Document(binsxml, 'album')
-        for child in doc.get_child('description').children:
-            key = str(child.getattr('name'))
-            if not key or not child.content:
-                continue
-            if key == 'sampleimage':
-                image = os.path.join(directory, unicode_to_str(child.content))
-                if not os.path.isfile(image):
+        # search album.xml (bins)
+        binsxml = os.path.join(directory, 'album.xml')
+        if os.path.isfile(binsxml):
+            bins = BinsParser(binsxml)
+            for key, value in bins.items():
+                if key == 'sampleimage':
+                    image = os.path.join(directory, unicode_to_str(value))
+                    if os.path.isfile(image):
+                        self._set('image', image)
                     continue
-                self._set('image', image)
-                continue
-            self._set(key, child.content)
+                self._set(key, value)
 
 # register to kaa.metadata core
 factory.register('directory', core.EXTENSION_DIRECTORY, Directory)

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to