Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / util / entry_points.py @ 2ce0636e

History | View | Annotate | Download (4.1 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import sys
35
import pkg_resources
36
import inspect
37
import types
38

    
39

    
40
def get_entry_points(ns, name):
41
    for entry_point in pkg_resources.iter_entry_points(group=ns):
42
        if entry_point.name == name:
43
            yield entry_point
44

    
45

    
46
def extend_module(module_name, attrs):
47
    module = sys.modules[module_name]
48
    for k,v in attrs.iteritems():
49
        setattr(module, k, v)
50

    
51

    
52
def entry_point_to_object(ep):
53
    object_or_func = ep.load()
54

    
55
    # user defined entry point is a function
56
    # get the return value
57
    obj = object_or_func
58
    if hasattr(object_or_func, '__call__'):
59
        obj = object_or_func()
60

    
61
    if type(obj) == types.ModuleType:
62
        dct = {}
63
        for k in dir(obj):
64
            if k.startswith("__"):
65
                continue
66
            dct[k] = getattr(obj, k)
67

    
68
        obj = dct
69

    
70
    return obj
71

    
72

    
73
def extend_module_from_entry_point(module_name, ns):
74
    """
75
    Extend a settings module from entry_point hooks
76
    """
77
    for e in get_entry_points(ns, 'default_settings'):
78
        extend_module(module_name, entry_point_to_object(e))
79

    
80

    
81
def extend_dict_from_entry_point(settings_object, ns, entry_point_name):
82
    for e in get_entry_points(ns, entry_point_name):
83
        settings_object.update(entry_point_to_object(e))
84

    
85
    return settings_object
86

    
87

    
88
def extend_list_from_entry_point(settings_object, ns, entry_point_name,
89
        unique=True):
90
    settings_object = list(settings_object)
91
    for e in get_entry_points(ns, entry_point_name):
92
        obj = entry_point_to_object(e)
93
        for row in obj:
94
            if type(row) == dict and (row.get('before', False) or \
95
                    row.get('after', False)):
96
                if row.get('before', False):
97
                    position = settings_object.index(row.get('before'))
98
                    insert_at = position - 1
99
                else:
100
                    position = settings_object.index(row.get('after'))
101
                    insert_at = position + 1
102

    
103
                if insert_at < 0:
104
                    insert_at = 0
105

    
106
                inserts = row.get('insert', [])
107
                if not type(inserts) == list:
108
                    inserts  = [inserts]
109

    
110
                for entry in inserts:
111
                    if not entry in settings_object:
112
                        settings_object.insert(insert_at, entry)
113
                        insert_at = insert_at + 1
114
            else:
115
                settings_object.append(row)
116

    
117
    return settings_object
118

    
119
def extend_settings(mname, ns):
120
    extend_module_from_entry_point(mname, ns)
121

    
122
def extend_urls(patterns, ns):
123
    for e in get_entry_points(ns, 'urls'):
124
        patterns += e.load()
125

    
126
    return patterns
127