root / snf-common / synnefo / util / entry_points.py @ cfcedbb1
History | View | Annotate | Download (1.2 kB)
1 |
import sys |
---|---|
2 |
import pkg_resources |
3 |
import inspect |
4 |
|
5 |
|
6 |
def get_entry_ponts(ns, name): |
7 |
for entry_point in pkg_resources.iter_entry_points(group=ns): |
8 |
if entry_point.name == name:
|
9 |
yield entry_point
|
10 |
|
11 |
|
12 |
def extend_settings(ns, module_name): |
13 |
"""
|
14 |
Extend module from entry_point hooks
|
15 |
"""
|
16 |
settings = sys.modules[module_name] |
17 |
# apps hook
|
18 |
app_entry_points = get_entry_ponts(ns, 'apps')
|
19 |
# settings hook
|
20 |
settings_entry_points = get_entry_ponts(ns, 'settings')
|
21 |
|
22 |
# extend INSTALLED_APPS setting
|
23 |
NEW_INSTALLED_APPS = list(getattr(settings, 'INSTALLED_APPS', [])); |
24 |
|
25 |
# if failed to execute app hook as function parse it as string
|
26 |
# synnefo.app:get_additional_apps or app1,app2,app3
|
27 |
for e in app_entry_points: |
28 |
try:
|
29 |
NEW_INSTALLED_APPS = list(e.load()())
|
30 |
except Exception, ex: |
31 |
NEW_INSTALLED_APPS = NEW_INSTALLED_APPS + \ |
32 |
e.module_name.split(",")
|
33 |
|
34 |
# extend additional settings
|
35 |
# TODO: existing settings logic ??
|
36 |
for e in settings_entry_points: |
37 |
module = e.load() |
38 |
for k in dir(module): |
39 |
if k == k.upper():
|
40 |
setattr(settings, k, getattr(module, k)) |
41 |
|
42 |
setattr(settings, 'INSTALLED_APPS', NEW_INSTALLED_APPS) |
43 |
|