diff --git a/web-greeter/bridge/Greeter.py b/web-greeter/bridge/Greeter.py index 2e3a819..ec02875 100644 --- a/web-greeter/bridge/Greeter.py +++ b/web-greeter/bridge/Greeter.py @@ -43,12 +43,13 @@ from PyQt5.QtCore import QTimer # This Application from . import ( + Battery, language_to_dict, layout_to_dict, session_to_dict, user_to_dict, battery_to_dict, - debugLog + logger ) LightDMGreeter = LightDM.Greeter() @@ -63,7 +64,7 @@ def changeBrightness(self, method: str, quantity: int): if child.returncode == 1: raise ChildProcessError("xbacklight returned 1") except Exception as err: - debugLog("Brightness: {}".format(err), 4) + logger.error("Brightness: {}".format(err)) else: self.brightness_update.emit() pass @@ -75,7 +76,7 @@ def getBrightness(self): level = subprocess.run(["xbacklight", "-get"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True) return int(level.stdout) except Exception as err: - debugLog("Battery: {}".format(err), 4) + logger.error("Brightness: {}".format(err)) return -1 def updateBattery(self): @@ -84,14 +85,9 @@ def updateBattery(self): try: acpi = subprocess.run(["acpi", "-b"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True) - battery = acpi.stdout.split(": ") - data = re.sub("%|,", "", battery[1]) - level = data.split(" ")[1] - - self._battery = int(level) - self._acpi = acpi.stdout + self._battery.update(acpi.stdout) except Exception as err: - debugLog("Battery: {}".format(err), 4) + logger.error("Battery: {}".format(err)) else: self.property_changed.emit() @@ -110,8 +106,7 @@ class Greeter(BridgeObject): noop_signal = bridge.signal() property_changed = bridge.signal() - _battery = -1 - _acpi = "" + _battery = Battery() def __init__(self, config, *args, **kwargs): super().__init__(name='LightDMGreeter', *args, **kwargs) @@ -175,7 +170,7 @@ class Greeter(BridgeObject): @bridge.prop(Variant, notify=property_changed) def batteryData(self): - return battery_to_dict(self._acpi) + return battery_to_dict(self._battery) @bridge.prop(int, notify=brightness_update) def brightness(self): diff --git a/web-greeter/bridge/__init__.py b/web-greeter/bridge/__init__.py index 8660913..d81d20f 100644 --- a/web-greeter/bridge/__init__.py +++ b/web-greeter/bridge/__init__.py @@ -27,6 +27,8 @@ # along with Web Greeter; If not, see . import re +import threading, time + from logging import ( getLogger, DEBUG, @@ -37,7 +39,7 @@ from logging import ( log_format = ''.join([ '%(asctime)s [ %(levelname)s ] %(filename)s %(', - 'lineno)d: %(message)s' + 'lineno)d : %(funcName)s | %(message)s' ]) formatter = Formatter(fmt=log_format, datefmt="%Y-%m-%d %H:%M:%S") logger = getLogger("greeter") @@ -48,18 +50,45 @@ stream_handler.setFormatter(formatter) logger.setLevel(DEBUG) logger.addHandler(stream_handler) -def debugLog(txt: str, level: int = 1): - if (level == 1): - logger.debug(txt) - elif (level == 2): - logger.info(txt) - elif (level == 3): - logger.warn(txt) - elif (level == 4): - logger.error(txt) - else: - logger.debug(txt) +class setInterval: + def __init__(self, interval, action): + self.interval = interval + self.action = action + self.stopEvent = threading.Event() + thread = threading.Thread(target=self.__setInterval) + thread.start() + + def __setInterval(self): + nextTime = time.time() + self.interval + while not self.stopEvent.wait(nextTime - time.time()): + nextTime += self.interval + self.action() + +class Battery: + _name = "" + _level = -1 + _state = "" + + def __init__(self): + pass + + def update(self, acpi: str): + formatted = re.sub("%|,|\n", "", acpi) + colon = formatted.split(": ") + splitted = colon[1].split(" ") + + self._name = colon[0] + self._level = int(splitted[1]) + self._state = splitted[0] + + def get_name(self): + return self._name + + def get_level(self): + return self._level + def get_state(self): + return self._state def language_to_dict(lang): return dict(code=lang.get_code(), name=lang.get_name(), territory=lang.get_territory()) @@ -94,16 +123,11 @@ def user_to_dict(user): username=user.get_name(), ) -def battery_to_dict(batt): - if batt == "": - return dict() - formatted = re.sub("%|,|\n", "", batt) - colon = formatted.split(": ") - splitted = colon[1].split(" ") +def battery_to_dict(battery): return dict( - name = colon[0], - level = int(splitted[1]), - state = splitted[0] + name = battery.get_name(), + level = battery.get_level(), + state = battery.get_state() ) diff --git a/web-greeter/globals.py b/web-greeter/globals.py index fa72a8e..3625c6d 100644 --- a/web-greeter/globals.py +++ b/web-greeter/globals.py @@ -78,18 +78,6 @@ logger.propagate = False logger.setLevel(DEBUG) logger.addHandler(stream_handler) -def debugLog(txt: str, level: int = 1): - if (level == 1): - logger.debug(txt) - elif (level == 2): - logger.info(txt) - elif (level == 3): - logger.warn(txt) - elif (level == 4): - logger.error(txt) - else: - logger.debug(txt) - initial_timeout = 0 def setScreenSaver(timeout: int): @@ -104,17 +92,17 @@ def setScreenSaver(timeout: int): subprocess.run(["xset", "s", str(timeout)], check=True) except Exception as err: - debugLog("Screensaver timeout couldn't be set", 4) + logger.error("Screensaver timeout couldn't be set") else: - debugLog("Screensaver timeout set") + logger.debug("Screensaver timeout set") def resetScreenSaver(): try: subprocess.run(["xset", "s", str(initial_timeout)]) except Exception as err: - debugLog("Screensaver reset failed", 4) + logger.error("Screensaver reset failed") else: - debugLog("Screensaver reset") + logger.debug("Screensaver reset") BASE_DIR = os.path.dirname(os.path.realpath(__file__)) diff --git a/web-greeter/greeter.py b/web-greeter/greeter.py index 093d594..2b982db 100644 --- a/web-greeter/greeter.py +++ b/web-greeter/greeter.py @@ -41,10 +41,10 @@ from typing import ( # 3rd-Party Libs # This Application -from utils import config +from utils import errorPrompt, keyboard import globals -from globals import WebGreeter +from globals import WebGreeter, logger def loadWhitherConf(): global whither_yaml @@ -101,7 +101,7 @@ def changeTheme(theme: str): if theme in dirlist: custom_config["theme"] = theme else: - globals.debugLog("Theme not found. Going with config theme", 4) + logger.error("Theme not found. Going with config theme") return def listThemes(quiet = False): diff --git a/web-greeter/utils/config.py b/web-greeter/utils/errorPrompt.py similarity index 84% rename from web-greeter/utils/config.py rename to web-greeter/utils/errorPrompt.py index 5cacba6..aa50aa8 100644 --- a/web-greeter/utils/config.py +++ b/web-greeter/utils/errorPrompt.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# config.py +# errorPrompt.py # # Copyright © 2021 JezerM # @@ -25,11 +25,9 @@ # You should have received a copy of the GNU General Public License # along with Web Greeter; If not, see . -from whither.toolkits.bootstrap import WebPage, MainWindow +from whither.toolkits.bootstrap import WebPage -from PyQt5.QtCore import QUrl, pyqtSignal, Qt -from PyQt5.QtWidgets import QDialogButtonBox, QDialog, QVBoxLayout, QLabel, QPushButton, QAbstractButton -from PyQt5.QtGui import QKeyEvent +from PyQt5.QtWidgets import QDialogButtonBox, QDialog, QVBoxLayout, QLabel, QPushButton from logging import ( getLogger, @@ -138,15 +136,4 @@ def errorPrompt(err): return -def keyPressEvent(self, keyEvent: QKeyEvent): - super(MainWindow, self).keyPressEvent(keyEvent) - if (keyEvent.key() == Qt.Key.Key_MonBrightnessUp): - globals.greeter.greeter.brightnessIncrease(globals.greeter.config.features.backlight["value"]) - if (keyEvent.key() == Qt.Key.Key_MonBrightnessDown): - globals.greeter.greeter.brightnessDecrease(globals.greeter.config.features.backlight["value"]) - - -MainWindow.keyPressEvent = keyPressEvent - -WebPage.javaScriptConsoleMessage = javaScriptConsoleMessage # Yep, you can override functions like this!!! - +WebPage.javaScriptConsoleMessage = javaScriptConsoleMessage diff --git a/web-greeter/utils/keyboard.py b/web-greeter/utils/keyboard.py new file mode 100644 index 0000000..f3d799b --- /dev/null +++ b/web-greeter/utils/keyboard.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +# +# keyboard.py +# +# Copyright © 2021 JezerM +# +# This file is part of Web Greeter. +# +# Web Greeter is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Web Greeter is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# The following additional terms are in effect as per Section 7 of the license: +# +# The preservation of all legal notices and author attributions in +# the material or in the Appropriate Legal Notices displayed +# by works containing it is required. +# +# You should have received a copy of the GNU General Public License +# along with Web Greeter; If not, see . + +from whither.toolkits.bootstrap import WebPage, MainWindow + +from PyQt5.QtCore import QUrl, pyqtSignal, Qt +from PyQt5.QtGui import QKeyEvent + +def keyPressEvent(self, keyEvent: QKeyEvent): + super(MainWindow, self).keyPressEvent(keyEvent) + if (keyEvent.key() == Qt.Key.Key_MonBrightnessUp): + globals.greeter.greeter.brightnessIncrease(globals.greeter.config.features.backlight["value"]) + if (keyEvent.key() == Qt.Key.Key_MonBrightnessDown): + globals.greeter.greeter.brightnessDecrease(globals.greeter.config.features.backlight["value"]) + + +MainWindow.keyPressEvent = keyPressEvent + +