mycreole/parameter.py

67 line
1.7 KiB
Python

import config
from django.conf import settings
import importlib
import os
MYCREOLE_ATTACHMENT_ACCESS = "MYCREOLE_ATTACHMENT_ACCESS"
ACCESS_READ = 'read'
ACCESS_MODIFY = 'modify'
MYCREOLE_BAR = "MYCREOLE_BAR"
BAR_MENUBAR = "menubar"
BAR_NAVIBAR = "navibar"
MYCREOLE_ROOT = "MYCREOLE_ROOT"
def no_access(*args, **kwargs):
return False
DEFAULTS = {
MYCREOLE_ATTACHMENT_ACCESS: {
ACCESS_READ: 'mycreole.parameter.no_access',
ACCESS_MODIFY: 'mycreole.parameter.no_access',
},
MYCREOLE_BAR: {
BAR_MENUBAR: None,
BAR_NAVIBAR: None,
},
MYCREOLE_ROOT: os.path.join(settings.BASE_DIR, 'data', 'mycreole'),
}
def __get_object_by_name__(object_name):
class_data = object_name.split(".")
module_path = ".".join(class_data[:-1])
class_str = class_data[-1]
#
module = importlib.import_module(module_path)
return getattr(module, class_str)
def get(key):
# take data from config, settings or defaults
try:
data = getattr(config, key)
except AttributeError:
try:
data = getattr(settings, key)
except AttributeError:
data = DEFAULTS.get(key)
# adapt the data
if key in [MYCREOLE_BAR, MYCREOLE_ATTACHMENT_ACCESS]:
# Change given string to object
rv = {}
for skey in DEFAULTS[key]:
# take the value from data or the default
if skey in data:
value = data[skey]
else:
value = DEFAULTS[key][skey]
# take the object or None
if value is not None:
rv[skey] = __get_object_by_name__(value)
else:
rv[skey] = None
return rv
return data