From 8864d153b0c8f2ddaceabcffde478091499e975d Mon Sep 17 00:00:00 2001 From: JezerM Date: Sat, 21 Aug 2021 18:45:50 -0600 Subject: [PATCH] Battery bugs fixed and custom path to theme kind of added --- web-greeter/globals.py | 3 +-- web-greeter/main.py | 15 ++------------ web-greeter/utils/battery.py | 38 ++++++++++++++++++++---------------- web-greeter/utils/theme.py | 27 ++++++++++++++++--------- 4 files changed, 42 insertions(+), 41 deletions(-) diff --git a/web-greeter/globals.py b/web-greeter/globals.py index 7c59e44..3ed597c 100644 --- a/web-greeter/globals.py +++ b/web-greeter/globals.py @@ -206,8 +206,7 @@ class WebGreeter(App): def load_theme(self): self.logger.debug('Loading theme...') - theme.checkTheme(self) - theme_url = '/{0}/{1}/index.html'.format(self.config.themes_dir, self.config.greeter.theme) + theme_url = theme.checkTheme(self) self._web_container.load(theme_url) diff --git a/web-greeter/main.py b/web-greeter/main.py index 12c28e4..064dd33 100644 --- a/web-greeter/main.py +++ b/web-greeter/main.py @@ -31,12 +31,7 @@ import sys import ruamel.yaml as yaml import pkg_resources import os -from typing import ( - ClassVar, - Type, - List, - Tuple, -) +from typing import ( List ) # 3rd-Party Libs @@ -104,13 +99,7 @@ def debugMode(value: bool): def changeTheme(theme: str): - dirlist = listThemes(True) - - if theme in dirlist: - custom_config["app"]["greeter"]["theme"] = theme - else: - logger.error("Theme not found. Going with config theme") - return + custom_config["app"]["greeter"]["theme"] = theme def listThemes(quiet = False): diff --git a/web-greeter/utils/battery.py b/web-greeter/utils/battery.py index a7e6e35..2abb44e 100644 --- a/web-greeter/utils/battery.py +++ b/web-greeter/utils/battery.py @@ -143,23 +143,27 @@ class Battery: self.watt = 0 elif self.status != "Full": rate_time = 0 - if (sum_rate_power > 0 or sum_rate_current > 0): - div = (sum_rate_power > 0 and sum_rate_power) or sum_rate_current - - if self.status == "Charging": - rate_time = (sum_energy_full - sum_energy_now) / div - else: - rate_time = sum_energy_now / div - - if 0 < rate_time and rate_time < 0.01: - rate_time_magnitude = tonumber(abs(math.floor(math.log10(rate_time)))) - rate_time = rate_time * 10 ^ (rate_time_magnitude - 2) - - hours = math.floor(rate_time) - minutes = math.floor((rate_time - hours) * 60) - self.perc = math.floor(min(100, (sum_energy_now / sum_energy_full) * 100) + 0.5) - self.time = "{:02d}:{:02d}".format(hours, minutes) - self.watt = "{:.2f}".format(sum_rate_energy / 1e6) + if (sum_rate_power > 0 or sum_rate_current > 0): + div = (sum_rate_power > 0 and sum_rate_power) or sum_rate_current + + if self.status == "Charging": + rate_time = (sum_energy_full - sum_energy_now) / div + else: + rate_time = sum_energy_now / div + + if 0 < rate_time and rate_time < 0.01: + rate_time_magnitude = tonumber(abs(math.floor(math.log10(rate_time)))) + rate_time = rate_time * 10 ^ (rate_time_magnitude - 2) + + hours = math.floor(rate_time) + minutes = math.floor((rate_time - hours) * 60) + self.perc = math.floor(min(100, (sum_energy_now / sum_energy_full) * 100) + 0.5) + self.time = "{:02d}:{:02d}".format(hours, minutes) + self.watt = "{:.2f}".format(sum_rate_energy / 1e6) + elif self.status == "Full": + self.perc = 100 + self.time = "00:00" + self.watt = 0 self.perc = self.perc == None and 0 or self.perc diff --git a/web-greeter/utils/theme.py b/web-greeter/utils/theme.py index 2c0bebf..e431441 100644 --- a/web-greeter/utils/theme.py +++ b/web-greeter/utils/theme.py @@ -29,6 +29,7 @@ # Standard Lib import os +from os.path import abspath # This Application from .pkg_json import PackageJSON @@ -55,15 +56,23 @@ logger.addHandler(stream_handler) def checkTheme(self): - themes = listThemes(self) - config_theme = self.config.greeter.theme - default = "gruvbox" - if self.config.greeter.theme in themes: - pass - else: - logger.error("Config theme not valid: \"{0}\". Going with \"{1}\" theme".format( - config_theme, default)) - self.config.greeter.theme = default + theme: str = self.config.greeter.theme + dir = self.config.themes_dir + path_to_theme: str = os.path.join(dir, theme, "index.html") + def_theme = "gruvbox" + + if theme.startswith("/"): path_to_theme = theme; + elif "." in theme or "/" in theme: + path_to_theme = os.path.abspath(theme) + + if not path_to_theme.endswith(".html"): + path_to_theme = os.path.join(path_to_theme, "index.html") + + if not os.path.exists(path_to_theme): + logger.error("\"{0}\" theme does not exists. Using \"{1}\"".format(theme, def_theme)) + path_to_theme = os.path.join(dir, def_theme, "index.html") + + return path_to_theme def listThemes(self):