@@ -1,6 +1,7 @@
#!/usr/bin/env python
#!/usr/bin/env python
import errno
import errno
import itertools
import os
import os
import sets
import sets
import stat
import stat
@@ -8,8 +9,18 @@ from DIDLLite import StorageFolder, Item, VideoItem, AudioItem, TextItem, ImageI
from twisted.web import static
from twisted.web import static
from twisted.python import log
from twisted.python import log
__all__ = [ 'registerklassfun', 'FSObject', 'FSItem', 'FSVideoItem',
'FSAudioItem', 'FSTextItem', 'FSImageItem', 'mimetoklass',
'FSDirectory',
]
mimedict = static.loadMimeTypes()
mimedict = static.loadMimeTypes()
klassfuns = []
def registerklassfun(fun):
klassfuns.append(fun)
def statcmp(a, b, cmpattrs = [ 'st_ino', 'st_dev', 'st_size', 'st_mtime', ]):
def statcmp(a, b, cmpattrs = [ 'st_ino', 'st_dev', 'st_size', 'st_mtime', ]):
if a is None or b is None:
if a is None or b is None:
return False
return False
@@ -84,14 +95,14 @@ mimetoklass = {
'image': FSImageItem,
'image': FSImageItem,
}
}
def dofileadd(cd, parent, path, name ):
fn, ext = os.path.splitext(name )
def defFS(path ):
fn, ext = os.path.splitext(path )
ext = ext.lower()
ext = ext.lower()
try:
try:
mt = mimedict[ext]
mt = mimedict[ext]
except KeyError:
except KeyError:
log.msg('no mime-type for: %s' % name )
return
log.msg('no mime-type for: %s' % path )
return None, None
ty = mt.split('/')[0]
ty = mt.split('/')[0]
if mimetoklass.has_key(mt):
if mimetoklass.has_key(mt):
@@ -100,10 +111,26 @@ def dofileadd(cd, parent, path, name):
klass = mimetoklass[ty]
klass = mimetoklass[ty]
else:
else:
log.msg('no item for mt: %s' % mt)
log.msg('no item for mt: %s' % mt)
return None, None
return klass, { 'path': path, 'mimetype': mt }
def dofileadd(cd, parent, path, name):
klass = None
for i in itertools.chain(klassfuns, ( defFS, )):
try:
klass, kwargs = i(os.path.join(path, name))
if klass is not None:
break
except:
import traceback
traceback.print_exc()
if klass is None:
return
return
return cd.addItem(parent, klass, name,
path = os.path.join(path, name), mimetype = mt)
log.msg('matched:', os.path.join(path, name), `i`)
return cd.addItem(parent, klass, name, **kwargs )
class FSDirectory(FSObject, StorageFolder):
class FSDirectory(FSObject, StorageFolder):
def __init__(self, *args, **kwargs):
def __init__(self, *args, **kwargs):