|
- #!/usr/bin/env python
- # Copyright 2009 John-Mark Gurney <jmg@funkthat.com>
-
- __version__ = '$Change$'
- # $Id$
-
- import FileDIDL
- import xml.dom.minidom
- from DIDLLite import StorageFolder, Item, Resource, ResourceList
- from FSStorage import FSObject, registerklassfun
-
- def getElementText(elm, strip=True):
- s = ''.join(x.nodeValue for x in elm.childNodes)
- if strip:
- s = s.strip()
-
- return s
-
- class ItemObject(FSObject, Item):
- def __init__(self, *args, **kwargs):
- path = kwargs.pop('path')
- dom = kwargs.pop('dom')
-
- FSObject.__init__(self, path)
- Item.__init__(self, *args, **kwargs)
-
- self.checkUpdate(dom=dom)
-
- def doUpdate(self, dom=None):
- if dom is None:
- dom = xml.dom.minidom.parse(open(self.FSpath))
- obj = dom.getElementsByTagName('Object')[0]
- mt = getElementText(obj.getElementsByTagName('mimetype')[0])
- self.res = ResourceList()
- for i in obj.getElementsByTagName('res'):
- r = Resource(getElementText(i.getElementsByTagName(
- 'url')[0]), 'http-get:*:%s:*' % mt)
- size = i.getElementsByTagName('size')
- if size:
- r.size = int(getElementText(size[0]))
- self.res.append(r)
-
- super(ItemObject, self).doUpdate()
-
- def canHandle(fobj):
- # XXX - create schema and validate
- dom = xml.dom.minidom.parse(fobj)
- #print 'ch:', `dom`
- obj = dom.getElementsByTagName('Object')[0]
- mt = getElementText(obj.getElementsByTagName('mimetype')[0])
-
- #print 'ch:', `obj`, `mt`
-
- return dom, mt
-
- def detectitem(path, fobj):
- #print 'di:', `path`, `fobj`
- try:
- dom, mt = canHandle(fobj)
- except:
- #import traceback
- #traceback.print_exc()
- return None, None
-
- klass, mt = FileDIDL.buildClassMT(ItemObject, path, mimetype=mt)
-
- return klass, { 'path': path, 'dom': dom }
-
- registerklassfun(detectitem)
|