Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / util / entry_points.py @ 5c285c17

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
def get_entry_points(ns, name):
48
    for entry_point in pkg_resources.iter_entry_points(group=ns):
49
        if entry_point.name == name and \
50
                not entry_point.dist.project_name in EXCLUDED_PACKAGES:
51
            yield entry_point
52

    
53

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

    
59

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

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

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

    
76
        obj = dct
77

    
78
    return obj
79

    
80

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

    
88

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

    
93
    return settings_object
94

    
95

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

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

    
115
                if insert_at < 0:
116
                    insert_at = 0
117

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

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

    
129
    return settings_object
130

    
131

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

    
138
    return settings
139

    
140
def extend_settings(mname, ns):
141
    extend_module_from_entry_point(mname, ns)
142

    
143

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

    
148
    return patterns
149