-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_clacks.py
46 lines (33 loc) · 1 KB
/
flask_clacks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: utf-8 -*-
from functools import wraps
from flask import make_response
__author__ = 'William Mayor'
__version__ = '1.0.1'
class Clacks(object):
def __init__(self, app=None, names=None):
if names is None:
names = []
self.names = list(names) + ['Terry Pratchett']
self.app = app
if app is not None:
self.init_app(app)
def init_app(self, app):
app.after_request(self.add_headers)
def add_headers(self, resp):
for n in self.names:
resp.headers.add('X-Clacks-Overhead', 'GNU ' + n)
return resp
def clacks(names=None):
_names = names
def decorator(f):
c = Clacks(names=_names)
@wraps(f)
def wrapper(*args, **kwargs):
resp = make_response(f(*args, **kwargs))
return c.add_headers(resp)
return wrapper
if callable(names):
_names = None
return decorator(names)
return decorator