1 ### -*- coding: utf-8 -*- #################################################### |
|
2 ############################################################################## |
|
3 # |
|
4 # Copyright (c) 2008 Thierry Florac <tflorac AT ulthar.net> |
|
5 # All Rights Reserved. |
|
6 # |
|
7 # This software is subject to the provisions of the Zope Public License, |
|
8 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
9 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
10 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
11 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
12 # FOR A PARTICULAR PURPOSE. |
|
13 # |
|
14 ############################################################################## |
|
15 |
|
16 |
|
17 # import standard packages |
|
18 |
|
19 # import Zope3 interfaces |
|
20 |
|
21 # import local interfaces |
|
22 |
|
23 # import Zope3 packages |
|
24 from zope.interface import Interface |
|
25 |
|
26 # import local packages |
|
27 |
|
28 |
|
29 def getParent(context, interface=Interface, allow_context=True): |
|
30 """Get first parent of the given context that implements given interface""" |
|
31 if allow_context: |
|
32 parent = context |
|
33 else: |
|
34 parent = getattr(context, '__parent__', None) |
|
35 while parent is not None: |
|
36 if interface.providedBy(parent): |
|
37 return interface(parent) |
|
38 parent = getattr(parent, '__parent__', None) |
|
39 return None |
|
40 |
|
41 |
|
42 # copied from fanstatic (which copied it from zope.dottedname !) |
|
43 def resolve(name, module=None): |
|
44 name = name.split('.') |
|
45 if not name[0]: |
|
46 if module is None: |
|
47 raise ValueError("relative name without base module") |
|
48 module = module.split('.') |
|
49 name.pop(0) |
|
50 while not name[0]: |
|
51 module.pop() |
|
52 name.pop(0) |
|
53 name = module + name |
|
54 |
|
55 used = name.pop(0) |
|
56 found = __import__(used) |
|
57 for n in name: |
|
58 used += '.' + n |
|
59 try: |
|
60 found = getattr(found, n) |
|
61 except AttributeError: |
|
62 __import__(used) |
|
63 found = getattr(found, n) |
|
64 |
|
65 return found |
|