From 9aa9e5789082e850e7a82b97a961ed4a72e9e788 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 16 May 2016 00:25:47 -0700 Subject: [PATCH] Cleanup --- homeassistant/components/api.py | 17 +------- homeassistant/components/frontend/__init__.py | 2 + homeassistant/components/http.py | 40 ++++++------------- homeassistant/util/eventlet.py | 8 ++-- requirements_all.txt | 3 -- tests/components/test_api.py | 9 ----- 6 files changed, 21 insertions(+), 58 deletions(-) diff --git a/homeassistant/components/api.py b/homeassistant/components/api.py index f2a94d7633c..3819536f666 100644 --- a/homeassistant/components/api.py +++ b/homeassistant/components/api.py @@ -15,7 +15,7 @@ from homeassistant.const import ( HTTP_BAD_REQUEST, HTTP_CREATED, HTTP_NOT_FOUND, HTTP_UNPROCESSABLE_ENTITY, MATCH_ALL, URL_API, URL_API_COMPONENTS, URL_API_CONFIG, URL_API_DISCOVERY_INFO, URL_API_ERROR_LOG, - URL_API_EVENT_FORWARD, URL_API_EVENTS, URL_API_LOG_OUT, URL_API_SERVICES, + URL_API_EVENT_FORWARD, URL_API_EVENTS, URL_API_SERVICES, URL_API_STATES, URL_API_STATES_ENTITY, URL_API_STREAM, URL_API_TEMPLATE, __version__) from homeassistant.exceptions import TemplateError @@ -48,7 +48,6 @@ def setup(hass, config): hass.wsgi.register_view(APIEventForwardingView) hass.wsgi.register_view(APIComponentsView) hass.wsgi.register_view(APIErrorLogView) - hass.wsgi.register_view(APILogOutView) hass.wsgi.register_view(APITemplateView) return True @@ -129,7 +128,7 @@ class APIEventStream(HomeAssistantView): self.hass.bus.listen(MATCH_ALL, thread_forward_events) attached_ping = track_utc_time_change( - self.hass, thread_ping, second=range(0, 60, 3)) #(0, 30)) + self.hass, thread_ping, second=(0, 30)) _LOGGER.debug('STREAM %s ATTACHED', id(stop_obj)) @@ -401,18 +400,6 @@ class APIErrorLogView(HomeAssistantView): return self.file(request, self.hass.config.path(ERROR_LOG_FILENAME)) -class APILogOutView(HomeAssistantView): - """View to handle Log Out requests.""" - - url = URL_API_LOG_OUT - name = "api:log-out" - - def post(self, request): - """Handle log out.""" - # TODO kill session - return {} - - class APITemplateView(HomeAssistantView): """View to handle requests.""" diff --git a/homeassistant/components/frontend/__init__.py b/homeassistant/components/frontend/__init__.py index dab45b6a4b1..ac2fe252b47 100644 --- a/homeassistant/components/frontend/__init__.py +++ b/homeassistant/components/frontend/__init__.py @@ -100,6 +100,8 @@ class IndexView(HomeAssistantView): template = self.templates.get_template('index.html') + # pylint is wrong + # pylint: disable=no-member resp = template.render(app_url=app_url, auth=auth, icons=mdi_version.VERSION) diff --git a/homeassistant/components/http.py b/homeassistant/components/http.py index 59d76a3c8a8..864e517699b 100644 --- a/homeassistant/components/http.py +++ b/homeassistant/components/http.py @@ -70,7 +70,8 @@ def setup(hass, config): def request_class(): """Generate request class. - Done in method because of imports.""" + Done in method because of imports. + """ from werkzeug.exceptions import BadRequest from werkzeug.wrappers import BaseRequest, AcceptMixin from werkzeug.utils import cached_property @@ -100,6 +101,7 @@ def routing_map(hass): class EntityValidator(BaseConverter): """Validate entity_id in urls.""" + regex = r"(\w+)\.(\w+)" def __init__(self, url_map, exist=True, domain=None): @@ -259,17 +261,16 @@ class HomeAssistantWSGI(object): return self.views[endpoint].handle_request(request, **values) except RequestRedirect as ex: return ex - except BadRequest as ex: - return self._handle_error(request, str(ex), 400) - except NotFound as ex: - return self._handle_error(request, str(ex), 404) - except MethodNotAllowed as ex: - return self._handle_error(request, str(ex), 405) - except Unauthorized as ex: - return self._handle_error(request, str(ex), 401) - # TODO This long chain of except blocks is silly. _handle_error should - # just take the exception as an argument and parse the status code - # itself + except (BadRequest, NotFound, MethodNotAllowed, + Unauthorized) as ex: + resp = ex.get_response(request.environ) + if request.accept_mimetypes.accept_json: + resp.data = json.dumps({ + "result": "error", + "message": str(ex), + }) + resp.mimetype = "application/json" + return resp def base_app(self, environ, start_response): """WSGI Handler of requests to base app.""" @@ -288,19 +289,6 @@ class HomeAssistantWSGI(object): environ['PATH_INFO'] = "{}.{}".format(*fingerprinted.groups()) return app(environ, start_response) - def _handle_error(self, request, message, status): - """Handle a WSGI request error.""" - from werkzeug.wrappers import Response - if request.accept_mimetypes.accept_json: - message = json.dumps({ - "result": "error", - "message": message, - }) - mimetype = "application/json" - else: - mimetype = "text/plain" - return Response(message, status=status, mimetype=mimetype) - class HomeAssistantView(object): """Base view for all views.""" @@ -339,8 +327,6 @@ class HomeAssistantView(object): except AttributeError: raise MethodNotAllowed - # TODO: session support + uncomment session test - # Auth code verbose on purpose authenticated = False diff --git a/homeassistant/util/eventlet.py b/homeassistant/util/eventlet.py index 54cb8cfdbe7..6338935b0c9 100644 --- a/homeassistant/util/eventlet.py +++ b/homeassistant/util/eventlet.py @@ -2,8 +2,8 @@ def spawn(hub, func, *args, **kwargs): - """Spawns a function on specified hub.""" + """Spawn a function on specified hub.""" import eventlet - g = eventlet.greenthread.GreenThread(hub.greenlet) - hub.schedule_call_global(0, g.switch, func, args, kwargs) - return g + gthread = eventlet.greenthread.GreenThread(hub.greenlet) + hub.schedule_call_global(0, gthread.switch, func, args, kwargs) + return gthread diff --git a/requirements_all.txt b/requirements_all.txt index 93c5bd36ee4..92c9b735b2c 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -274,9 +274,6 @@ python-nmap==0.6.0 # homeassistant.components.notify.pushover python-pushover==0.2 -# homeassistant.components.socket_io -python-socketio==1.3 - # homeassistant.components.statsd python-statsd==1.7.2 diff --git a/tests/components/test_api.py b/tests/components/test_api.py index 13a2f8746b4..356ee7db4ea 100644 --- a/tests/components/test_api.py +++ b/tests/components/test_api.py @@ -88,15 +88,6 @@ class TestAPI(unittest.TestCase): self.assertEqual(200, req.status_code) - # def test_access_via_session(self): - # """Test access wia session.""" - # session = requests.Session() - # req = session.get(_url(const.URL_API), headers=HA_HEADERS) - # self.assertEqual(200, req.status_code) - - # req = session.get(_url(const.URL_API)) - # self.assertEqual(200, req.status_code) - def test_api_list_state_entities(self): """Test if the debug interface allows us to list state entities.""" req = requests.get(_url(const.URL_API_STATES),