Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / util / entry_points.py @ 88506db0

History | View | Annotate | Download (4.8 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
import os
39

    
40
from collections import defaultdict
41
import inspect
42

    
43
# List of python distribution names which entry points will get excluded
44
# from snf-common settings extension mechanism
45
EXCLUDED_PACKAGES = os.environ.get('SYNNEFO_EXCLUDE_PACKAGES', '').split(":")
46

    
47

    
48
def get_entry_points(ns, name):
49
    for entry_point in pkg_resources.iter_entry_points(group=ns):
50
        if entry_point.name == name and \
51
                not entry_point.dist.project_name in EXCLUDED_PACKAGES:
52
            yield entry_point
53

    
54

    
55
def extend_module(module_name, attrs):
56
    module = sys.modules[module_name]
57
    for k,v in attrs.iteritems():
58
        setattr(module, k, v)
59

    
60

    
61
def entry_point_to_object(ep):
62
    object_or_func = ep.load()
63

    
64
    # user defined entry point is a function
65
    # get the return value
66
    obj = object_or_func
67
    if hasattr(object_or_func, '__call__'):
68
        obj = object_or_func()
69

    
70
    if type(obj) == types.ModuleType:
71
        dct = {}
72
        for k in dir(obj):
73
            if k.startswith("__"):
74
                continue
75
            dct[k] = getattr(obj, k)
76

    
77
        obj = dct
78

    
79
    return obj
80

    
81

    
82
def extend_module_from_entry_point(module_name, ns):
83
    """
84
    Extend a settings module from entry_point hooks
85
    """
86
    for e in get_entry_points(ns, 'default_settings'):
87
        extend_module(module_name, entry_point_to_object(e))
88

    
89

    
90
def extend_dict_from_entry_point(settings_object, ns, entry_point_name):
91
    for e in get_entry_points(ns, entry_point_name):
92
        settings_object.update(entry_point_to_object(e))
93

    
94
    return settings_object
95

    
96

    
97
def extend_list_from_entry_point(settings_object, ns, entry_point_name,
98
        unique=True):
99
    settings_object = list(settings_object)
100
    for e in get_entry_points(ns, entry_point_name):
101
        obj = entry_point_to_object(e)
102
        for row in obj:
103
            # skip duplicate entries
104
            if row in settings_object:
105
                continue
106

    
107
            if type(row) == dict and (row.get('before', False) or \
108
                    row.get('after', False)):
109
                if row.get('before', False):
110
                    position = settings_object.index(row.get('before'))
111
                    insert_at = position - 1
112
                else:
113
                    position = settings_object.index(row.get('after'))
114
                    insert_at = position + 1
115

    
116
                if insert_at < 0:
117
                    insert_at = 0
118

    
119
                inserts = row.get('insert', [])
120
                if not type(inserts) == list:
121
                    inserts  = [inserts]
122

    
123
                for entry in inserts:
124
                    if not entry in settings_object:
125
                        settings_object.insert(insert_at, entry)
126
                        insert_at = insert_at + 1
127
            else:
128
                settings_object.append(row)
129

    
130
    return settings_object
131

    
132

    
133
def collect_defaults(ns):
134
    settings = defaultdict(lambda: [])
135
    for e in get_entry_points('synnefo', 'default_settings'):
136
        attrs = dir(e.load())
137
        settings[e.dist.key] = settings[e.dist.key] + attrs
138

    
139
    return settings
140

    
141

    
142
def extend_settings(mname, ns):
143
    extend_module_from_entry_point(mname, ns)
144

    
145

    
146
def extend_urls(patterns, ns):
147
    for e in get_entry_points(ns, 'urls'):
148
        patterns += e.load()
149

    
150
    return patterns
151