Author: dmeyer
Date: Sat Feb 11 17:53:15 2006
New Revision: 1169
Removed:
trunk/metadata/src/image/bins.py
Modified:
trunk/metadata/TODO
trunk/metadata/src/image/core.py
trunk/metadata/src/misc/dirinfo.py
trunk/metadata/src/misc/xmlinfo.py
Log:
move all xml code to use libxml2, simplify bins support
Modified: trunk/metadata/TODO
==============================================================================
--- trunk/metadata/TODO (original)
+++ trunk/metadata/TODO Sat Feb 11 17:53:15 2006
@@ -4,9 +4,6 @@
Add more types and information.
-Move the XML parser from Python XML to libxml Python bindings. This is
-faster and this is also what other kaa modules use for XML.
-
Create a REAME
Every file needs a header and inline docs. Also correct the header to
Modified: trunk/metadata/src/image/core.py
==============================================================================
--- trunk/metadata/src/image/core.py (original)
+++ trunk/metadata/src/image/core.py Sat Feb 11 17:53:15 2006
@@ -33,12 +33,11 @@
import os
import gzip
import logging
-from xml.utils import qp_xml
+import libxml2
# kaa imports
from kaa.metadata import factory
from kaa.metadata import mediainfo
-import bins
# get logging object
log = logging.getLogger('metadata')
@@ -58,39 +57,61 @@
setattr(self,k,None)
self.keys.append(k)
+ def correct_data(self):
+ """
+ Add additional information and correct data.
+ FIXME: parse_external_files here is very wrong
+ """
+ if self.url and self.url.startswith('file://'):
+ self.parse_external_files(self.url[7:])
+ mediainfo.MediaInfo.correct_data(self)
+
+
def parse_external_files(self, filename):
"""
Parse external files like bins and .comments.
"""
- if os.path.isfile(filename + '.xml'):
- try:
- binsinfo = bins.get_bins_desc(filename)
- # get needed keys from exif infos
- for key in ATTRIBUTES + mediainfo.MEDIACORE:
- if not self[key] and binsinfo['exif'].has_key(key):
- self[key] = binsinfo['exif'][key]
- # get _all_ infos from description
- for key in binsinfo['desc']:
- self[key] = binsinfo['desc'][key]
+ self.parse_bins(filename)
+ self.parse_dot_comment(filename)
+
+ def parse_bins(self, filename):
+ """
+ Parse bins xml files
+ """
+ binsxml = filename + '.xml'
+ if not os.path.isfile(binsxml):
+ return
+ for node in libxml2.parseFile(binsxml).children:
+ if not node.name == 'description':
+ continue
+ for child in node.children:
+ if not child.name == 'field':
+ continue
+ value = unicode(child.getContent(), 'utf-8').strip()
+ key = child.prop('name')
+ if key and value:
+ self[key] = value
if not key in ATTRIBUTES + mediainfo.MEDIACORE:
# if it's in desc it must be important
self.keys.append(key)
- except Exception, e:
- log.exception('problem reading the image information')
- pass
+
+ def parse_dot_comment(self, filename):
+ """
+ Parse info in .comments.
+ """
comment_file = os.path.join(os.path.dirname(filename), '.comments',
os.path.basename(filename) + '.xml')
- if os.path.isfile(comment_file):
- try:
- f = gzip.open(comment_file)
- p = qp_xml.Parser()
- tree = p.parse(f)
- f.close()
- for c in tree.children:
- if c.name == 'Place':
- self.location = c.textof()
- if c.name == 'Note':
- self.description = c.textof()
- except:
- pass
+ if not os.path.isfile(comment_file):
+ return
+ for node in libxml2.parseFile(comment_file).children:
+ if not node.name == 'Comment':
+ continue
+ for child in node.children:
+ value = unicode(child.getContent(), 'utf-8')
+ if not value or value == '0':
+ continue
+ if child.name == 'Place':
+ self.location = value
+ if child.name == 'Note':
+ self.description = value
Modified: trunk/metadata/src/misc/dirinfo.py
==============================================================================
--- trunk/metadata/src/misc/dirinfo.py (original)
+++ trunk/metadata/src/misc/dirinfo.py Sat Feb 11 17:53:15 2006
@@ -34,56 +34,73 @@
# python imports
import os
import logging
+import libxml2
# kaa imports
-from kaa.metadata import mediainfo
-from kaa.metadata import factory
-from kaa.metadata.image import bins
+from kaa.base.strutils import unicode_to_str
+from kaa.metadata.mediainfo import MediaInfo, MEDIACORE, EXTENSION_DIRECTORY,
TYPE_MISC
+from kaa.metadata.factory import register
# get logging object
log = logging.getLogger('metadata')
-class DirInfo(mediainfo.MediaInfo):
+class DirInfo(MediaInfo):
"""
Simple parser for reading a .directory file.
"""
def __init__(self, directory):
- mediainfo.MediaInfo.__init__(self)
+ MediaInfo.__init__(self)
self.media = 'directory'
-
- # search .directory
- info = os.path.join(directory, '.directory')
- if os.path.isfile(info):
- f = open(info)
- for l in f.readlines():
- if l.startswith('Icon='):
- self.image = l[5:]
- if self.image.startswith('./'):
- self.image = self.image[2:]
- self.keys.append('image')
- f.close()
-
- # search album.xml (bins)
- info = os.path.join(directory, 'album.xml')
- if os.path.isfile(info):
- info = bins.get_bins_desc(directory)
- if info.has_key('desc'):
- info = info['desc']
- if info.has_key('sampleimage') and info['sampleimage']:
- # album.xml defines a sampleimage, use it as image for the
- # directory
- image = os.path.join(directory, info['sampleimage'])
- if os.path.isfile(image):
- self.image = image
- self.keys.append('image')
-
- if info.has_key('title') and info['title']:
- # album.xml defines a title
- self.title = info['title']
+ self.parse_dot_directory(directory)
+ self.parse_bins(directory)
+ def parse_dot_directory(self, 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='):
+ self.image = l[5:]
+ if self.image.startswith('./'):
+ self.image = self.image[2:]
+ self.keys.append('image')
+ 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
+
+ for node in libxml2.parseFile(binsxml).children:
+ if not node.name == 'description':
+ continue
+ for child in node.children:
+ if not child.name == 'field':
+ continue
+ value = unicode(child.getContent(), 'utf-8').strip()
+ key = child.prop('name')
+ if key and value:
+ if key == 'sampleimage':
+ image = os.path.join(directory, unicode_to_str(value))
+ if os.path.isfile(image):
+ self.image = image
+ self.keys.append('image')
+ else:
+ self[key] = value
+ if not key in MEDIACORE:
+ # if it's in desc it must be important
+ self.keys.append(key)
-factory.register('directory', mediainfo.EXTENSION_DIRECTORY,
- mediainfo.TYPE_MISC, DirInfo)
+# register to kaa.metadata core
+register('directory', EXTENSION_DIRECTORY, TYPE_MISC, DirInfo)
Modified: trunk/metadata/src/misc/xmlinfo.py
==============================================================================
--- trunk/metadata/src/misc/xmlinfo.py (original)
+++ trunk/metadata/src/misc/xmlinfo.py Sat Feb 11 17:53:15 2006
@@ -32,6 +32,7 @@
# python imports
import os
import logging
+import libxml2
# kaa imports
from kaa.metadata import factory
@@ -56,22 +57,20 @@
self.mime = 'text/xml'
self.type = ''
- try:
- parser = qp_xml.Parser()
- tree = parser.parse(file)
- except:
- raise mediainfo.KaaMetadataParseError()
+ libxml2.SAXParseFile(self, file.name, 10)
+
+
+ def startElement(self, tag, attrs):
+ """
+ Callback for <tag>
+ """
+ if self.type:
+ return
- if tree.name in XML_TAG_INFO:
- self.type = XML_TAG_INFO[tree.name]
+ if tag in XML_TAG_INFO:
+ self.type = XML_TAG_INFO[tag]
else:
self.type = 'XML file'
-try:
- # XML support
- from xml.utils import qp_xml
- factory.register( 'text/xml', ('xml', 'fxd'), mediainfo.TYPE_MISC,
- XMLInfo )
-except:
- log.warning('Python XML not found')
+factory.register( 'text/xml', ('xml', 'fxd'), mediainfo.TYPE_MISC, XMLInfo )
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog