diff --git a/kobin/app.py b/kobin/app.py index 181a14f..07394ec 100644 --- a/kobin/app.py +++ b/kobin/app.py @@ -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): @@ -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: diff --git a/kobin/app.pyi b/kobin/app.pyi index 7a7b9fa..43923f1 100644 --- a/kobin/app.pyi +++ b/kobin/app.pyi @@ -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]: ...