Browse Source

add a function to register a function to be called to detect if the

filesystem object has a better class to handle it than the default
mimetype one...   This means we can handle items in a zip archive
or an iso image..

[git-p4: depot-paths = "//depot/": change = 802]
v0.3
John-Mark Gurney 18 years ago
parent
commit
6d6a4424ce
1 changed files with 33 additions and 6 deletions
  1. +33
    -6
      FSStorage.py

+ 33
- 6
FSStorage.py View File

@@ -1,6 +1,7 @@
#!/usr/bin/env python

import errno
import itertools
import os
import sets
import stat
@@ -8,8 +9,18 @@ from DIDLLite import StorageFolder, Item, VideoItem, AudioItem, TextItem, ImageI
from twisted.web import static
from twisted.python import log

__all__ = [ 'registerklassfun', 'FSObject', 'FSItem', 'FSVideoItem',
'FSAudioItem', 'FSTextItem', 'FSImageItem', 'mimetoklass',
'FSDirectory',
]

mimedict = static.loadMimeTypes()

klassfuns = []

def registerklassfun(fun):
klassfuns.append(fun)

def statcmp(a, b, cmpattrs = [ 'st_ino', 'st_dev', 'st_size', 'st_mtime', ]):
if a is None or b is None:
return False
@@ -84,14 +95,14 @@ mimetoklass = {
'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()
try:
mt = mimedict[ext]
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]
if mimetoklass.has_key(mt):
@@ -100,10 +111,26 @@ def dofileadd(cd, parent, path, name):
klass = mimetoklass[ty]
else:
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 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):
def __init__(self, *args, **kwargs):


Loading…
Cancel
Save