From 6d6a4424ce0c61f175223ac143b8b517ee0e94ce Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Sun, 2 Jul 2006 20:10:27 -0800 Subject: [PATCH] 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] --- FSStorage.py | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/FSStorage.py b/FSStorage.py index 207e668..fbf5037 100644 --- a/FSStorage.py +++ b/FSStorage.py @@ -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):