python:如何用格式插件编写通用文件阅读器

时间:2011-03-30 17:46:46

标签: python image-processing polymorphism filereader

我正在尝试编写各种医学图像格式的通用阅读器 我们来了。我想,让我们从专业人士那里学习并去模仿 PIL generically reads files(“Python Imaging Library”,Formats)。

根据我的理解,PIL有一个开放的功能,循环通过可能的列表 接受功能。当一个工作时,它使用相关的工厂函数来实例化 适当的对象。

所以我去做了这件事,我的(精简的)努力就在这里:


pluginID = []     # list of all registered plugin IDs
OPEN = {}         # plugins have open and (maybe) accept functions as a tuple

_initialized = False

import os, sys

def moduleinit():
    '''Explicitly initializes the library.  This function 
    loads all available file format drivers.

    This routine has been lifted from PIL, the Python Image Library'''

    global _initialized
    global pluginID
    if _initialized:
        return 

    visited = {}

    directories = sys.path

    try:
        directories = directories + [os.path.dirname(__file__)]
    except NameError:
        pass

    # only check directories (including current, if present in the path)
    for directory in filter(isDirectory, directories):
        fullpath = os.path.abspath(directory)
        if visited.has_key(fullpath):
            continue
        for file in os.listdir(directory):
            if file[-19:] == "TestReaderPlugin.py":
                f, e = os.path.splitext(file)
                try:
                    sys.path.insert(0, directory)
                    try: # FIXME: this will not reload and hence pluginID 
                        # will be unpopulated leading to "cannot identify format"
                        __import__(f, globals(), locals(), [])
                    finally:
                        del sys.path[0]
                except ImportError:
                    print f, ":", sys.exc_value
        visited[fullpath] = None

    if OPEN:
        _initialized = True
        return 1

class Reader:
    '''Base class for image file format handlers.'''
    def __init__(self, fp=None, filename=None):

        self.filename = filename

        if isStringType(filename):
            import __builtin__
            self.fp = __builtin__.open(filename) # attempt opening

        # this may fail if not implemented
        self._open() # unimplemented in base class but provided by plugins

    def _open(self):
        raise NotImplementedError(
            "StubImageFile subclass must implement _open"
            )


# this is the generic open that tries to find the appropriate handler
def open(fp):
    '''Probe an image file

    Supposed to attempt all opening methods that are available. Each 
    of them is supposed to fail quickly if the filetype is invalid for its 
    respective format'''

    filename=fp

    moduleinit() # make sure we have access to all the plugins

    for i in pluginID:
        try:
            factory, accept = OPEN[i]
            if accept:
                fp = accept(fp)
                # accept is expected to either return None (if unsuccessful) 
                # or hand back a file handle to be used for opening                                 
                if fp:
                    fp.seek(0)  
                    return factory(fp, filename=filename) 
        except (SyntaxError, IndexError, TypeError): 
                pass # I suppose that factory is allowed to have these 
                # exceptions for problems that weren't caught with accept()
                # hence, they are simply ignored and we try the other plugins

    raise IOError("cannot identify format")

# --------------------------------------------------------------------
# Plugin registry

def register_open(id, factory, accept=None):
    pluginID.append(id)
    OPEN[id] = factory, accept

# --------------------------------------------------------------------
# Internal:

# type stuff
from types import  StringType

def isStringType(t):
    return isinstance(t, StringType)

def isDirectory(f):
    '''Checks if an object is a string, and that it points to a directory'''
    return isStringType(f) and os.path.isdir(f)

幕后的重要一点是所有格式插件的注册 第一次尝试打开文件(moduleinit)。每个合格的 插件必须位于可访问的路径中并命名为* TestReaderPlugin.py。它会 获取(动态)导入。每个插件模块都必须调用register_open 提供ID,创建文件的方法和要测试的接受函数 候选人档案。

示例插件将如下所示:


import TestReader

def _accept(filename):
    fp=open(filename,"r")
    # we made it here, so let's just accept this format
    return fp

class exampleTestReader(TestReader.Reader):
    format='example'

    def _open(self):
        self.data = self.fp.read()

TestReader.register_open('example', exampleTestReader, accept=_accept)

TestReader.open()是用户将使用的函数:

import TestReader
a=TestReader.open(filename) # easy

那么问题出在哪里?首先,我还在寻找pythonic 办法。是这个吗?我怀疑它的理由是模块中的魔力 舞台看起来凌乱。它是直接从PIL复制的。主要问题:如果你 重新加载(TestReader),它将全部停止工作,因为ID被初始化为[], 但插件不会重新加载。

是否有更好的方法来设置通用阅读器 1.允许对所有格式进行简单的打开(文件名)调用 2.只需要为您想要的任何格式提供封装良好的插件 3.重装?

1 个答案:

答案 0 :(得分:0)

一些指导原则:

  1. 将“peek”的概念用于缓冲区,以测试是否有您能理解的数据数据。
  2. 了解进口商的名称是用户不想知道的(如果您有100个进口商,该怎么办) 使用“facade”界面 medicimage.open(filepath)
  3. 要重新加载你需要实现一点逻辑,那里有关于如何实现这一点的说法