root / snf-common / synnefo / util / entry_points.py @ bed8ed73
History | View | Annotate | Download (4.4 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 |
from collections import defaultdict |
40 |
import inspect |
41 |
|
42 |
def get_entry_points(ns, name): |
43 |
for entry_point in pkg_resources.iter_entry_points(group=ns): |
44 |
if entry_point.name == name:
|
45 |
yield entry_point
|
46 |
|
47 |
|
48 |
def extend_module(module_name, attrs): |
49 |
module = sys.modules[module_name] |
50 |
for k,v in attrs.iteritems(): |
51 |
setattr(module, k, v)
|
52 |
|
53 |
|
54 |
def entry_point_to_object(ep): |
55 |
object_or_func = ep.load() |
56 |
|
57 |
# user defined entry point is a function
|
58 |
# get the return value
|
59 |
obj = object_or_func |
60 |
if hasattr(object_or_func, '__call__'): |
61 |
obj = object_or_func() |
62 |
|
63 |
if type(obj) == types.ModuleType: |
64 |
dct = {} |
65 |
for k in dir(obj): |
66 |
if k.startswith("__"): |
67 |
continue
|
68 |
dct[k] = getattr(obj, k)
|
69 |
|
70 |
obj = dct |
71 |
|
72 |
return obj
|
73 |
|
74 |
|
75 |
def extend_module_from_entry_point(module_name, ns): |
76 |
"""
|
77 |
Extend a settings module from entry_point hooks
|
78 |
"""
|
79 |
for e in get_entry_points(ns, 'default_settings'): |
80 |
extend_module(module_name, entry_point_to_object(e)) |
81 |
|
82 |
|
83 |
def extend_dict_from_entry_point(settings_object, ns, entry_point_name): |
84 |
for e in get_entry_points(ns, entry_point_name): |
85 |
settings_object.update(entry_point_to_object(e)) |
86 |
|
87 |
return settings_object
|
88 |
|
89 |
|
90 |
def extend_list_from_entry_point(settings_object, ns, entry_point_name, |
91 |
unique=True):
|
92 |
settings_object = list(settings_object)
|
93 |
for e in get_entry_points(ns, entry_point_name): |
94 |
obj = entry_point_to_object(e) |
95 |
for row in obj: |
96 |
if type(row) == dict and (row.get('before', False) or \ |
97 |
row.get('after', False)): |
98 |
if row.get('before', False): |
99 |
position = settings_object.index(row.get('before'))
|
100 |
insert_at = position - 1
|
101 |
else:
|
102 |
position = settings_object.index(row.get('after'))
|
103 |
insert_at = position + 1
|
104 |
|
105 |
if insert_at < 0: |
106 |
insert_at = 0
|
107 |
|
108 |
inserts = row.get('insert', [])
|
109 |
if not type(inserts) == list: |
110 |
inserts = [inserts] |
111 |
|
112 |
for entry in inserts: |
113 |
if not entry in settings_object: |
114 |
settings_object.insert(insert_at, entry) |
115 |
insert_at = insert_at + 1
|
116 |
else:
|
117 |
settings_object.append(row) |
118 |
|
119 |
return settings_object
|
120 |
|
121 |
|
122 |
def collect_defaults(ns): |
123 |
settings = defaultdict(lambda: [])
|
124 |
for e in get_entry_points('synnefo', 'default_settings'): |
125 |
attrs = dir(e.load())
|
126 |
settings[e.dist.key] = settings[e.dist.key] + attrs |
127 |
|
128 |
return settings
|
129 |
|
130 |
def extend_settings(mname, ns): |
131 |
extend_module_from_entry_point(mname, ns) |
132 |
|
133 |
|
134 |
def extend_urls(patterns, ns): |
135 |
for e in get_entry_points(ns, 'urls'): |
136 |
patterns += e.load() |
137 |
|
138 |
return patterns
|
139 |
|