Skip to content
This repository has been archived by the owner on May 31, 2019. It is now read-only.

Commit

Permalink
Implement __delattr__ for freezing the state
Browse files Browse the repository at this point in the history
  • Loading branch information
c-bata committed Mar 20, 2017
1 parent 772d61d commit 3e4aa2c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
38 changes: 24 additions & 14 deletions kobin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,31 @@ def __init__(self, config=None):
self.logger = self.config.get('LOGGER')
self._frozen = False

def __setattr__(self, *args, **kwargs):
if '_frozen' in dir(self):
if self._frozen:
warnings.warn("Cannot Change the state of started application!",
stacklevel=2)
def __call__(self, environ, start_response):
"""It is called when receive http request."""
if not self._frozen:
self._frozen = True
response = self._handle(environ)
start_response(response.status, response.headerlist)
return response.body

def __setattr__(self, key, value):
if self.frozen:
warnings.warn("Cannot Change the state of started application!", stacklevel=2)
else:
super().__setattr__(key, value)

def __delattr__(self, item):
if self.frozen:
warnings.warn("Cannot Delete the state of started application!", stacklevel=2)
else:
super().__setattr__(*args, **kwargs)
super().__setattr__(item)

@property
def frozen(self):
if '_frozen' not in dir(self):
return False
return self._frozen

def route(self, rule=None, method='GET', name=None, callback=None):
def decorator(callback_func):
Expand Down Expand Up @@ -91,14 +109,6 @@ def _handle(self, environ):
response = HTTPError(error_message, 500)
return response

def __call__(self, environ, start_response):
"""It is called when receive http request."""
if not self._frozen:
self._frozen = True
response = self._handle(environ)
start_response(response.status, response.headerlist)
return response.body


def _get_exception_message(e, debug):
if debug:
Expand Down
2 changes: 2 additions & 0 deletions kobin/app.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Kobin:
_frozen: bool

def __init__(self, config: Dict[str, Any] = ...) -> None: ...
@property
def frozen(self) -> bool: ...
def route(self, rule: str = ..., method: str = ..., name: str = ...,
callback: ViewFunction = ...) -> ViewFunction: ...
def before_request(self, callback: Callable[[], None]) -> Callable[[], None]: ...
Expand Down

0 comments on commit 3e4aa2c

Please sign in to comment.