8000 Pre-construct frontend index.html by balloob · Pull Request #10520 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Pre-construct frontend index.html #10520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 39 additions & 56 deletions homeassistant/components/frontend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from aiohttp import web
import voluptuous as vol
import jinja2

import homeassistant.helpers.config_validation as cv
from homeassistant.components.http import HomeAssistantView
Expand All @@ -22,7 +23,7 @@
from homeassistant.core import callback
from homeassistant.loader import bind_hass

REQUIREMENTS = ['home-assistant-frontend==20171110.0']
REQUIREMENTS = ['home-assistant-frontend==20171111.0']

DOMAIN = 'frontend'
DEPENDENCIES = ['api', 'websocket_api', 'http']
Expand Down Expand Up @@ -171,20 +172,18 @@ def async_finalize(self, hass, frontend_repository_path):
If frontend_repository_path is set, will be prepended to path of
built-in components.
"""
panel_path = 'panels/ha-panel-{}.html'.format(self.component_name)

if frontend_repository_path is None:
import hass_frontend
import hass_frontend_es5

self.webcomponent_url_latest = \
'/frontend_latest/panels/ha-panel-{}-{}.html'.format(
self.component_name,
hass_frontend.FINGERPRINTS[panel_path])
hass_frontend.FINGERPRINTS[self.component_name])
self.webcomponent_url_es5 = \
'/frontend_es5/panels/ha-panel-{}-{}.html'.format(
self.component_name,
hass_frontend_es5.FINGERPRINTS[panel_path])
hass_frontend_es5.FINGERPRINTS[self.component_name])
else:
# Dev mode
self.webcomponent_url_es5 = self.webcomponent_url_latest = \
Expand Down Expand Up @@ -335,7 +334,7 @@ def async_setup(hass, config):
if os.path.isdir(local):
hass.http.register_static_path("/local", local, not is_dev)

index_view = IndexView(is_dev, js_version)
index_view = IndexView(repo_path, js_version)
hass.http.register_view(index_view)

@asyncio.coroutine
Expand Down Expand Up @@ -435,50 +434,40 @@ class IndexView(HomeAssistantView):
requires_auth = False
extra_urls = ['/states', '/states/{extra}']

def __init__(self, use_repo, js_option):
def __init__(self, repo_path, js_option):
"""Initialize the frontend view."""
from jinja2 import FileSystemLoader, Environment

self.use_repo = use_repo
self.templates = Environment(
autoescape=True,
loader=FileSystemLoader(
os.path.join(os.path.dirname(__file__), 'templates/')
)
)
self.repo_path = repo_path
self.js_option = js_option
self._template_cache = {}

def get_template(self, latest):
"""Get template."""
if self.repo_path is not None:
root = self.repo_path
elif latest:
import hass_frontend
root = hass_frontend.where()
else:
import hass_frontend_es5
root = hass_frontend_es5.where()

tpl = self._template_cache.get(root)

if tpl is None:
with open(os.path.join(root, 'index.html')) as file:
tpl = jinja2.Template(file.read())

# Cache template if not running from repository
if self.repo_path is None:
self._template_cache[root] = tpl

return tpl

@asyncio.coroutine
def get(self, request, extra=None):
"""Serve the index view."""
hass = request.app['hass']
latest = _is_latest(self.js_option, request)
compatibility_url = None

if self.use_repo:
core_url = '/home-assistant-polymer/{}/core.js'.format(
'build' if latest else 'build-es5')
ui_url = '/home-assistant-polymer/src/home-assistant.html'
icons_fp = ''
icons_url = '/static/mdi.html'
else:
if latest:
import hass_frontend
core_url = '/frontend_latest/core-{}.js'.format(
hass_frontend.FINGERPRINTS['core.js'])
ui_url = '/frontend_latest/frontend-{}.html'.format(
hass_frontend.FINGERPRINTS['frontend.html'])
else:
import hass_frontend_es5
core_url = '/frontend_es5/core-{}.js'.format(
hass_frontend_es5.FINGERPRINTS['core.js'])
compatibility_url = '/frontend_es5/compatibility-{}.js'.format(
hass_frontend_es5.FINGERPRINTS['compatibility.js'])
ui_url = '/frontend_es5/frontend-{}.html'.format(
hass_frontend_es5.FINGERPRINTS['frontend.html'])
import hass_frontend
icons_fp = '-{}'.format(hass_frontend.FINGERPRINTS['mdi.html'])
icons_url = '/static/mdi{}.html'.format(icons_fp)

if request.path == '/':
panel = 'states'
Expand All @@ -497,23 +486,17 @@ def get(self, request, extra=None):
# do not try to auto connect on load
no_auth = 'false'

template = yield from hass.async_add_job(
self.templates.get_template, 'index.html')
template = yield from hass.async_add_job(self.get_template, latest)

# pylint is wrong
# pylint: disable=no-member
# This is a jinja2 template, not a HA template so we call 'render'.
resp = template.render(
core_url=core_url, ui_url=ui_url,
compatibility_url=compatibility_url, no_auth=no_auth,
icons_url=icons_url, icons=icons_fp,
panel_url=panel_url, panels=hass.data[DATA_PANELS],
dev_mode=self.use_repo,
no_auth=no_auth,
panel_url=panel_url,
panels=hass.data[DATA_PANELS],
dev_mode=self.repo_path is not None,
theme_color=MANIFEST_JSON['theme_color'],
extra_urls=hass.data[DATA_EXTRA_HTML_URL],
latest=latest,
service_worker_name='/service_worker.js' if latest else
'/service_worker_es5.js')
10000 )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add back service_worker_url

Copy link
Member Author
@balloob balloob Nov 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not needed because I hardcode /service_worker.js in the index.html template. It's replaced by /service_worker_es5.js for the ES5 version.


return web.Response(text=resp, content_type='text/html')

Expand All @@ -528,8 +511,8 @@ class ManifestJSONView(HomeAssistantView):
@asyncio.coroutine
def get(self, request): # pylint: disable=no-self-use
"""Return the manifest.json."""
msg = json.dumps(MANIFEST_JSON, sort_keys=True).encode('UTF-8')
return web.Response(body=msg, content_type="application/manifest+json")
msg = json.dumps(MANIFEST_JSON, sort_keys=True)
return web.Response(text=msg, content_type="application/manifest+json")


class ThemesView(HomeAssistantView):
Expand Down
124 changes: 0 additions & 124 deletions homeassistant/components/frontend/templates/index.html

This file was deleted.

2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ hipnotify==1.0.8
holidays==0.8.1

# homeassistant.components.frontend
home-assistant-frontend==20171110.0
home-assistant-frontend==20171111.0

# homeassistant.components.camera.onvif
http://github.com/tgaugry/suds-passworddigest-py3/archive/86fc50e39b4d2b8997481967d6a7fe1c57118999.zip#suds-passworddigest-py3==0.1.2a
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ hbmqtt==0.8
holidays==0.8.1

# homeassistant.components.frontend
home-assistant-frontend==20171110.0
home-assistant-frontend==20171111.0

# homeassistant.components.influxdb
# homeassistant.components.sensor.influxdb
Expand Down
2 changes: 1 addition & 1 deletion tests/components/test_panel_iframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_wrong_config(self):
})

@patch.dict('hass_frontend_es5.FINGERPRINTS',
{'panels/ha-panel-iframe.html': 'md5md5'})
{'iframe': 'md5md5'})
def test_correct_config(self):
"""Test correct config."""
assert setup.setup_component(
Expand Down
0