File: Synopsis/import_processor.py
 1#
 2# Copyright (C) 2003 Stefan Seefeld
 3# All rights reserved.
 4# Licensed to the public under the terms of the GNU LGPL (>= 2),
 5# see the file COPYING for details.
 6#
 7
 8def import_processor(name, verbose=False):
 9    """Import a named processor and return it.
10    Throws ImportError on failure."""
11
12
13    i = name.rfind('.')
14    if i == -1:
15        raise ImportError, '%s does not name a valid processor'%name
16
17    module, processor = name[:i], name.split('.')
18    mod = __import__(module)
19
20    for c in processor[1:]:
21        try:
22            mod = getattr(mod, c)
23        except AttributeError, msg:
24            raise ImportError, "Unable to find %s in %s"%(c, repr(mod))
25
26    return mod
27