diff --git a/changelog.txt b/changelog.txt index cf377b0..fe04623 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,4 +1,5 @@ Version 0.3.8 () +- Some dependencies are now included within the package (Issue #177) Version 0.3.7 (2019-11-23) - Rename files via UI (Issue #162) diff --git a/hass_configurator/configurator.py b/hass_configurator/configurator.py index 7c2c1af..1dcb396 100644 --- a/hass_configurator/configurator.py +++ b/hass_configurator/configurator.py @@ -120,3484 +120,17 @@ FAIL2BAN_IPS = {} REPO = None -INDEX = Template(r""" +ERROR_HTML = """ - HASS Configurator - - - - - - - + HASS Configurator - Error -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
-
-
- - -
-
-
- -
- -
- -
- -
- -
-
- - - - - - - - - - - +

Error loading html. Please check the logs.

-""") +""" # pylint: disable=unused-argument def signal_handler(sig, frame): @@ -3619,10 +152,9 @@ def load_settings(args): if settingsfile: try: if os.path.isfile(settingsfile): - with open(settingsfile) as fptr: - settings = json.loads(fptr.read()) - LOG.debug("Settings from file:") - LOG.debug(settings) + settings = json.loads(load_file(settingsfile).decode("utf-8")) + LOG.debug("Settings from file:") + LOG.debug(settings) else: LOG.warning("File not found: %s", settingsfile) except Exception as err: @@ -3843,18 +375,17 @@ def sorted_file_list(): return dircontent -def get_html(): - """Load the HTML from file in dev-mode, otherwise embedded.""" - if DEV: - try: - with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), - "dev.html")) as fptr: - html = Template(fptr.read()) - return html - except Exception as err: - LOG.warning(err) - LOG.warning("Delivering embedded HTML") - return INDEX +def load_file(filename, static=False): + """Load files. If static is True, set to path of configurator.""" + if static: + filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), filename) + try: + with open(filename, 'rb') as fptr: + content = fptr.read() + return content + except Exception as err: + LOG.critical(err) + return None def password_problems(password, name="UNKNOWN"): """Rudimentary checks for password strength.""" @@ -3991,12 +522,10 @@ def do_GET(self): if mimetype[0].split('/')[0] == 'image': is_raw = True if is_raw: - with open(filepath, 'rb') as fptr: - content = fptr.read() + content = load_file(filepath) self.send_header('Content-type', mimetype[0]) else: - with open(filepath, 'rb') as fptr: - content += fptr.read().decode('utf-8') + content = load_file(filepath).decode("utf-8") self.send_header('Content-type', 'text/text') else: self.send_header('Content-type', 'text/text') @@ -4020,9 +549,9 @@ def do_GET(self): if ENFORCE_BASEPATH and not is_safe_path(BASEPATH, filename): raise OSError('Access denied.') LOG.info(filename) - if os.path.isfile(os.path.join(BASEDIR.encode('utf-8'), filename)): - with open(os.path.join(BASEDIR.encode('utf-8'), filename), 'rb') as fptr: - filecontent = fptr.read() + filepath = os.path.join(BASEDIR.encode('utf-8'), filename) + if os.path.isfile(filepath): + filecontent = load_file(filepath) self.send_header( 'Content-Disposition', 'attachment; filename=%s' % filename.decode('utf-8').split(os.sep)[-1]) @@ -4324,25 +853,90 @@ def do_GET(self): standalone = "" if not HASS_API: standalone = "toggle_hass_panels();" - html = get_html().safe_substitute( - services=services, - events=events, - states=states, - loadfile=loadfile, - current=VERSION, - versionclass=color, - githidden="" if GIT else "hiddendiv", - # pylint: disable=anomalous-backslash-in-string - separator="\%s" % os.sep if os.sep == "\\" else os.sep, - your_address=self.client_address[0], - listening_address="%s://%s:%i" % ( - 'https' if SSL_CERTIFICATE else 'http', LISTENIP, PORT), - hass_api_address="%s" % (HASS_API, ), - hass_ws_address=ws_api, - api_password=HASS_API_PASSWORD if HASS_API_PASSWORD else "", - standalone=standalone) + try: + html = Template(load_file("dev.html", static=True).decode('utf-8')) + html = html.safe_substitute( + services=services, + events=events, + states=states, + loadfile=loadfile, + current=VERSION, + versionclass=color, + githidden="" if GIT else "hiddendiv", + # pylint: disable=anomalous-backslash-in-string + separator="\%s" % os.sep if os.sep == "\\" else os.sep, + your_address=self.client_address[0], + listening_address="%s://%s:%i" % ( + 'https' if SSL_CERTIFICATE else 'http', LISTENIP, PORT), + hass_api_address="%s" % (HASS_API, ), + hass_ws_address=ws_api, + api_password=HASS_API_PASSWORD if HASS_API_PASSWORD else "", + standalone=standalone) + except Exception as err: + LOG.warning("Error getting html: %s", err) + html = ERROR_HTML self.wfile.write(bytes(html, "utf8")) return + elif req.path.endswith('/jquery-3.4.1.min.js'): + try: + data = load_file("jquery-3.4.1.min.js", static=True) + except Exception as err: + self.send_response(404) + self.end_headers() + self.wfile.write(bytes("File not found", "utf8")) + self.send_header('Content-type', 'application/javascript') + self.end_headers() + self.wfile.write(data) + elif req.path.endswith('/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2'): + try: + data = load_file("flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2", static=True) + except Exception as err: + self.send_response(404) + self.end_headers() + self.wfile.write(bytes("File not found", "utf8")) + self.send_header('Content-type', 'font/woff') + self.end_headers() + self.wfile.write(data) + elif req.path.endswith('/js-yaml.min.js'): + try: + data = load_file("js-yaml.min.js", static=True) + except Exception as err: + self.send_response(404) + self.end_headers() + self.wfile.write(bytes("File not found", "utf8")) + self.send_header('Content-type', 'application/javascript') + self.end_headers() + self.wfile.write(data) + elif req.path.endswith('/materialize.min.js'): + try: + data = load_file("materialize.min.js", static=True) + except Exception as err: + self.send_response(404) + self.end_headers() + self.wfile.write(bytes("File not found", "utf8")) + self.send_header('Content-type', 'application/javascript') + self.end_headers() + self.wfile.write(data) + elif req.path.endswith('/material-icons.fallback.css'): + try: + data = load_file("material-icons.fallback.css", static=True) + except Exception as err: + self.send_response(404) + self.end_headers() + self.wfile.write(bytes("File not found", "utf8")) + self.send_header('Content-type', 'text/css') + self.end_headers() + self.wfile.write(data) + elif req.path.endswith('/style.css'): + try: + data = load_file("style.css", static=True) + except Exception as err: + self.send_response(404) + self.end_headers() + self.wfile.write(bytes("File not found", "utf8")) + self.send_header('Content-type', 'text/css') + self.end_headers() + self.wfile.write(data) else: self.send_response(404) self.end_headers() diff --git a/hass_configurator/dev.html b/hass_configurator/dev.html index 47ec904..7c23df2 100644 --- a/hass_configurator/dev.html +++ b/hass_configurator/dev.html @@ -4,495 +4,10 @@ HASS Configurator - + - - + + @@ -614,16 +129,16 @@
  • Toggle HASS panel