[Python-Dev] Zipping Zope3

Fredrik Lundh fredrik@pythonware.com
Wed, 18 Dec 2002 14:48:15 +0100


Guido van Rossum wrote:

> I like get_data().  The caller should always be prepared for it to
> return an error (how does it do that? raise what? or return None? that
> should be specified!).  An importer that can't do this should
> implement a version that always reports an error.  This is much easier
> on the client than calling hasattr(importer, 'get_data') first or
> catching AttributeError.

IOError makes a certain kind of sense.

on the other hand, I'm not sure that requiring the use of hasattr/try-
except is that much of a pain, in practice.

:::

And if we're making get_data part of the protocol, can we please have
an optional get_module_names/list_modules/listdir method too?

Quoting myself from an earlier post (update the examples as necessary):

    as I might have mentioned before, it would be really nice if custom
    importers supported optional "load_data" and "get_module_names"
    hooks.

    the former can be used by code that looks for data files on the path
    (Zope, PIL, most likely hundreds of other tools/programs):

        for path in sys.path:
            if hasattr(path, "load_data"):
                try:
                    data =3D path.load_data(name)
                except IOError:
                    pass
                else:
                    return data # got it
            if isinstance(path, types.StringTypes) and =
os.path.isdir(path):
                try:
                    data =3D open(os.path.join(path, file), "rb").read()
                except IOError:
                    pass
                else:
                    return data
            # ...

    the latter can be used by code that looks for plugins using a
    name pattern (PIL's *ImagePlugins, for example):

        for path in sys.path:
            if hasattr(path, "get_module_names"):
                names =3D path.get_module_names()
            elif isinstance(path, types.StringTypes) and =
os.path.isdir(path):
                names =3D os.listdir(path)
            else:
                continue
            for name in names:
                if name.endswith("ImagePlugin"):
                    __import__(name)

</F>