2017-07-15 01:23:19 -04:00
|
|
|
def is_callable_csp_dict(data):
|
|
|
|
if callable(data):
|
|
|
|
return True
|
|
|
|
if not isinstance(data, dict):
|
|
|
|
return False
|
2022-10-30 18:10:02 -04:00
|
|
|
return any(callable(value) for value in data.values())
|
2017-07-15 01:23:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
def call_csp_dict(data, request, response):
|
|
|
|
if callable(data):
|
|
|
|
return data(request, response)
|
|
|
|
|
|
|
|
result = {}
|
2022-10-30 18:10:02 -04:00
|
|
|
for key, value in data.items():
|
2017-07-15 01:23:19 -04:00
|
|
|
if callable(value):
|
|
|
|
result[key] = value(request, response)
|
|
|
|
else:
|
|
|
|
result[key] = value
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def merge_csp_dict(template, override):
|
|
|
|
result = template.copy()
|
2022-10-30 18:10:02 -04:00
|
|
|
for key, value in override.items():
|
2017-07-15 01:23:19 -04:00
|
|
|
if key not in result:
|
|
|
|
result[key] = value
|
|
|
|
continue
|
|
|
|
orig = result[key]
|
|
|
|
if isinstance(orig, list):
|
2017-07-15 02:36:31 -04:00
|
|
|
result[key] = orig + list(value)
|
2017-07-15 01:23:19 -04:00
|
|
|
elif isinstance(orig, set):
|
2017-07-15 02:36:31 -04:00
|
|
|
result[key] = orig.union(value)
|
2017-07-15 01:23:19 -04:00
|
|
|
elif isinstance(orig, tuple):
|
|
|
|
result[key] = orig + tuple(value)
|
|
|
|
else:
|
|
|
|
result[key] = value
|
|
|
|
return result
|