Browse Source

Brightness is now a signal. Some fixes

sisyphus
JezerM 4 years ago
parent
commit
8308d84794
  1. 28
      web-greeter/bridge/Greeter.py
  2. 36
      web-greeter/bridge/__init__.py
  3. 66
      web-greeter/globals.py
  4. 26
      web-greeter/greeter.py
  5. 6
      web-greeter/resources/js/ThemeUtils.js
  6. 11
      web-greeter/resources/js/docs/Greeter.js
  7. 46
      web-greeter/utils/config.py

28
web-greeter/bridge/Greeter.py

@ -47,6 +47,7 @@ from . import (
session_to_dict, session_to_dict,
user_to_dict, user_to_dict,
battery_to_dict, battery_to_dict,
debugLog
) )
LightDMGreeter = LightDM.Greeter() LightDMGreeter = LightDM.Greeter()
@ -62,9 +63,9 @@ def changeBrightness(self, method: str, quantity: int):
if child.returncode == 1: if child.returncode == 1:
raise ChildProcessError("xbacklight returned 1") raise ChildProcessError("xbacklight returned 1")
except Exception as err: except Exception as err:
print("[ERROR] Brightness:", err) debugLog("Brightness: {}".format(err), 4)
finally: else:
self.property_changed.emit() self.brightness_update.emit()
pass pass
def getBrightness(self): def getBrightness(self):
@ -74,7 +75,7 @@ def getBrightness(self):
level = subprocess.run(["xbacklight", "-get"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True) level = subprocess.run(["xbacklight", "-get"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
return int(level.stdout) return int(level.stdout)
except Exception as err: except Exception as err:
print("[ERROR] Battery:", err) debugLog("Battery: {}".format(err), 4)
return -1 return -1
def updateBattery(self): def updateBattery(self):
@ -90,7 +91,7 @@ def updateBattery(self):
self._battery = int(level) self._battery = int(level)
self._acpi = acpi.stdout self._acpi = acpi.stdout
except Exception as err: except Exception as err:
print("[ERROR] Battery: ", err) debugLog("Battery: {}".format(err), 4)
else: else:
self.property_changed.emit() self.property_changed.emit()
@ -104,6 +105,8 @@ class Greeter(BridgeObject):
show_message = bridge.signal(str, LightDM.MessageType, arguments=('text', 'type')) show_message = bridge.signal(str, LightDM.MessageType, arguments=('text', 'type'))
show_prompt = bridge.signal(str, LightDM.PromptType, arguments=('text', 'type')) show_prompt = bridge.signal(str, LightDM.PromptType, arguments=('text', 'type'))
brightness_update = bridge.signal()
noop_signal = bridge.signal() noop_signal = bridge.signal()
property_changed = bridge.signal() property_changed = bridge.signal()
@ -149,6 +152,7 @@ class Greeter(BridgeObject):
lambda greeter, msg, mtype: self._emit_signal(self.show_prompt, msg, mtype) lambda greeter, msg, mtype: self._emit_signal(self.show_prompt, msg, mtype)
) )
def _emit_signal(self, _signal, *args): def _emit_signal(self, _signal, *args):
self.property_changed.emit() self.property_changed.emit()
QTimer().singleShot(300, lambda: _signal.emit(*args)) QTimer().singleShot(300, lambda: _signal.emit(*args))
@ -173,7 +177,7 @@ class Greeter(BridgeObject):
def batteryData(self): def batteryData(self):
return battery_to_dict(self._acpi) return battery_to_dict(self._acpi)
@bridge.prop(int, notify=property_changed) @bridge.prop(int, notify=brightness_update)
def brightness(self): def brightness(self):
return getBrightness(self) return getBrightness(self)
@ -291,6 +295,10 @@ class Greeter(BridgeObject):
LightDMGreeter.authenticate_as_guest() LightDMGreeter.authenticate_as_guest()
self.property_changed.emit() self.property_changed.emit()
@bridge.method()
def batteryUpdate(self):
return updateBattery(self)
@bridge.method(int) @bridge.method(int)
def brightnessSet(self, quantity): def brightnessSet(self, quantity):
return changeBrightness(self, "-set", quantity) return changeBrightness(self, "-set", quantity)
@ -338,13 +346,11 @@ class Greeter(BridgeObject):
@bridge.method(str, result=bool) @bridge.method(str, result=bool)
def start_session(self, session): def start_session(self, session):
return LightDMGreeter.start_session_sync(session) if not session.strip():
return
return LightDMGreeter.start_session(session)
@bridge.method(result=bool) @bridge.method(result=bool)
def suspend(self): def suspend(self):
return LightDM.suspend() return LightDM.suspend()
@bridge.method()
def batteryUpdate(self):
return updateBattery(self)

36
web-greeter/bridge/__init__.py

@ -27,6 +27,38 @@
# along with Web Greeter; If not, see <http://www.gnu.org/licenses/>. # along with Web Greeter; If not, see <http://www.gnu.org/licenses/>.
import re import re
from logging import (
getLogger,
DEBUG,
ERROR,
Formatter,
StreamHandler,
)
log_format = ''.join([
'%(asctime)s [ %(levelname)s ] %(filename)s %(',
'lineno)d: %(message)s'
])
formatter = Formatter(fmt=log_format, datefmt="%Y-%m-%d %H:%M:%S")
logger = getLogger("greeter")
logger.propagate = False
stream_handler = StreamHandler()
stream_handler.setLevel(DEBUG)
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)
def language_to_dict(lang): def language_to_dict(lang):
@ -60,10 +92,6 @@ def user_to_dict(user):
logged_in=user.get_logged_in(), logged_in=user.get_logged_in(),
session=user.get_session(), session=user.get_session(),
username=user.get_name(), username=user.get_name(),
# ---->>> BEGIN DEPRECATED! <<<----
name=user.get_name(),
real_name=user.get_real_name(),
# ---->>> END DEPRECATED! <<<----
) )
def battery_to_dict(batt): def battery_to_dict(batt):

66
web-greeter/globals.py

@ -48,14 +48,75 @@ from bridge import (
Greeter, Greeter,
ThemeUtils, ThemeUtils,
) )
from logging import (
getLogger,
DEBUG,
ERROR,
Formatter,
StreamHandler,
)
from PyQt5.QtWidgets import QMainWindow from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtGui import QColor from PyQt5.QtGui import QColor
import subprocess
# Typing Helpers # Typing Helpers
BridgeObj = Type[BridgeObject] BridgeObj = Type[BridgeObject]
log_format = ''.join([
'%(asctime)s [ %(levelname)s ] %(module)s - %(filename)s:%(',
'lineno)d : %(funcName)s | %(message)s'
])
formatter = Formatter(fmt=log_format, datefmt="%Y-%m-%d %H:%M:%S")
stream_handler = StreamHandler()
logger = getLogger("debug")
stream_handler.setLevel(DEBUG)
stream_handler.setFormatter(formatter)
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):
global initial_timeout
timeout = timeout if timeout >= 0 else 300
try:
child = subprocess.Popen(["xset", "q"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
awk = subprocess.run(["awk", "/^ timeout: / {print $2}"], stdout=subprocess.PIPE, stdin=child.stdout, text=True)
initial_timeout = int(awk.stdout.replace("\n", ""))
subprocess.run(["xset", "s", str(timeout)], check=True)
except Exception as err:
debugLog("Screensaver timeout couldn't be set", 4)
else:
debugLog("Screensaver timeout set")
def resetScreenSaver():
try:
subprocess.run(["xset", "s", str(initial_timeout)])
except Exception as err:
debugLog("Screensaver reset failed", 4)
else:
debugLog("Screensaver reset")
BASE_DIR = os.path.dirname(os.path.realpath(__file__)) BASE_DIR = os.path.dirname(os.path.realpath(__file__))
CONFIG_FILE = os.path.join(BASE_DIR, 'whither.yml') CONFIG_FILE = os.path.join(BASE_DIR, 'whither.yml')
@ -75,6 +136,8 @@ class WebGreeter(App):
page = self._main_window.widget.centralWidget().page() page = self._main_window.widget.centralWidget().page()
page.setBackgroundColor(QColor(0,0,0)) page.setBackgroundColor(QColor(0,0,0))
setScreenSaver(self.config.greeter["screensaver_timeout"])
self._web_container.initialize_bridge_objects() self._web_container.initialize_bridge_objects()
self._web_container.load_script(':/_greeter/js/bundle.js', 'Web Greeter Bundle') self._web_container.load_script(':/_greeter/js/bundle.js', 'Web Greeter Bundle')
self.load_theme() self.load_theme()
@ -86,6 +149,9 @@ class WebGreeter(App):
def _before_web_container_init(self): def _before_web_container_init(self):
self.get_and_apply_user_config() self.get_and_apply_user_config()
def _before_exit(self):
resetScreenSaver()
@classmethod @classmethod
def validate_greeter_config_data(cls, key: str, data: str) -> str: def validate_greeter_config_data(cls, key: str, data: str) -> str:
if "'@" not in data: if "'@" not in data:

26
web-greeter/greeter.py

@ -37,12 +37,6 @@ from typing import (
List, List,
Tuple, Tuple,
) )
from logging import (
getLogger,
DEBUG,
Formatter,
StreamHandler,
)
# 3rd-Party Libs # 3rd-Party Libs
@ -64,23 +58,6 @@ def loadWhitherConf():
whither_yaml = yaml.safe_load(file_test) whither_yaml = yaml.safe_load(file_test)
webGreeter_conf = whither_yaml["WebGreeter"] webGreeter_conf = whither_yaml["WebGreeter"]
def debugLog(txt: str):
log_format = ''.join([
'%(asctime)s [ %(levelname)s ] %(module)s - %(filename)s:%(',
'lineno)d : %(funcName)s | %(message)s'
])
formatter = Formatter(fmt=log_format, datefmt="%Y-%m-%d %H:%M:%S")
stream_handler = StreamHandler()
logger = getLogger()
stream_handler.setLevel(DEBUG)
stream_handler.setFormatter(formatter)
logger.setLevel(DEBUG)
logger.addHandler(stream_handler)
logger.debug(txt)
def show_help(): def show_help():
version = webGreeter_conf["app"]["version"]["full"] version = webGreeter_conf["app"]["version"]["full"]
help_text = """Usage: help_text = """Usage:
@ -124,7 +101,7 @@ def changeTheme(theme: str):
if theme in dirlist: if theme in dirlist:
custom_config["theme"] = theme custom_config["theme"] = theme
else: else:
debugLog("Theme not found. Going with config theme") globals.debugLog("Theme not found. Going with config theme", 4)
return return
def listThemes(quiet = False): def listThemes(quiet = False):
@ -196,7 +173,6 @@ if __name__ == '__main__':
args.pop(0) args.pop(0)
yargs(args) yargs(args)
globals.greeter = WebGreeter() globals.greeter = WebGreeter()
globals.greeter.run() globals.greeter.run()

6
web-greeter/resources/js/ThemeUtils.js

@ -100,11 +100,11 @@ class ThemeUtils {
*/ */
dirlist( path, only_images = true, callback ) { dirlist( path, only_images = true, callback ) {
if ( '' === path || 'string' !== typeof path ) { if ( '' === path || 'string' !== typeof path ) {
console.error('[ERROR] theme_utils.dirlist(): path must be a non-empty string!'); console.error(`theme_utils.dirlist(): path must be a non-empty string!`);
return callback([]); return callback([]);
} else if ( null !== path.match(/^[^/].+/) ) { } else if ( null !== path.match(/^[^/].+/) ) {
console.error('[ERROR] theme_utils.dirlist(): path must be absolute!'); console.error(`theme_utils.dirlist(): path must be absolute!`);
return callback([]); return callback([]);
} }
@ -116,7 +116,7 @@ class ThemeUtils {
try { try {
return _ThemeUtils.dirlist( path, only_images, callback ); return _ThemeUtils.dirlist( path, only_images, callback );
} catch( err ) { } catch( err ) {
console.error( `[ERROR] theme_utils.dirlist(): ${err}` ); console.error(`theme_utils.dirlist(): ${err}`);
return callback([]); return callback([]);
} }
} }

11
web-greeter/resources/js/docs/Greeter.js vendored

@ -314,19 +314,19 @@ class Greeter {
get brightness() {} get brightness() {}
/** /**
* Set the brightness * Set the brightness to quantity
* @arg {Number} quantity The quantity to set * @arg {Number} quantity The quantity to set
*/ */
brightnessSet( quantity ) {} brightnessSet( quantity ) {}
/** /**
* Increase the brightness * Increase the brightness by quantity
* @arg {Number} quantity The quantity to increase * @arg {Number} quantity The quantity to increase
*/ */
brightnessIncrease( quantity ) {} brightnessIncrease( quantity ) {}
/** /**
* Decrease the brightness * Decrease the brightness by quantity
* @arg {Number} quantity The quantity to decrease * @arg {Number} quantity The quantity to decrease
*/ */
brightnessDecrease( quantity ) {} brightnessDecrease( quantity ) {}
@ -345,6 +345,11 @@ class Greeter {
*/ */
get can_access_battery() {} get can_access_battery() {}
/**
* Updates the battery data
*/
batteryUpdate() {}
/** /**
* Whether or not the greeter can control display brightness * Whether or not the greeter can control display brightness
* @type {boolean} * @type {boolean}

46
web-greeter/utils/config.py

@ -31,28 +31,56 @@ from PyQt5.QtCore import QUrl, pyqtSignal, Qt
from PyQt5.QtWidgets import QDialogButtonBox, QDialog, QVBoxLayout, QLabel, QPushButton, QAbstractButton from PyQt5.QtWidgets import QDialogButtonBox, QDialog, QVBoxLayout, QLabel, QPushButton, QAbstractButton
from PyQt5.QtGui import QKeyEvent from PyQt5.QtGui import QKeyEvent
from logging import (
getLogger,
DEBUG,
ERROR,
Formatter,
StreamHandler,
)
import globals import globals
log_format = ''.join([
'%(asctime)s [ %(levelname)s ] %(filename)s %(',
'lineno)d: %(message)s'
])
formatter = Formatter(fmt=log_format, datefmt="%Y-%m-%d %H:%M:%S")
logger = getLogger("javascript")
logger.propagate = False
stream_handler = StreamHandler()
stream_handler.setLevel(DEBUG)
stream_handler.setFormatter(formatter)
logger.setLevel(DEBUG)
logger.addHandler(stream_handler)
def javaScriptConsoleMessage(self, level: WebPage.JavaScriptConsoleMessageLevel, message: str, lineNumber: int, sourceID: str): def javaScriptConsoleMessage(self, level: WebPage.JavaScriptConsoleMessageLevel, message: str, lineNumber: int, sourceID: str):
if sourceID == "": if sourceID == "":
sourceID = "console" sourceID = "console"
error = False logLevel = 0
typeLog = ""
if level == WebPage.JavaScriptConsoleMessageLevel.ErrorMessageLevel: if level == WebPage.JavaScriptConsoleMessageLevel.ErrorMessageLevel:
typeLog = "[ERROR]" logLevel = 40
error = True
elif level == WebPage.JavaScriptConsoleMessageLevel.WarningMessageLevel: elif level == WebPage.JavaScriptConsoleMessageLevel.WarningMessageLevel:
typeLog = "[WARNING]" logLevel = 30
elif level == WebPage.JavaScriptConsoleMessageLevel.InfoMessageLevel: elif level == WebPage.JavaScriptConsoleMessageLevel.InfoMessageLevel:
return return
else: else:
return return
logMessage = "{typ} {source} {line}: {msg}".format(typ = typeLog, msg = message, source = sourceID, line = lineNumber) record = logger.makeRecord(
print(logMessage) name="javascript",
level=logLevel,
if error: fn="",
lno=lineNumber,
msg=message,
args=(),
exc_info=None
)
record.filename = sourceID
logger.handle(record)
if logLevel == 40:
errorMessage = "{source} {line}: {msg}".format(source = sourceID, line = lineNumber, msg = message) errorMessage = "{source} {line}: {msg}".format(source = sourceID, line = lineNumber, msg = message)
errorPrompt(errorMessage) errorPrompt(errorMessage)

Loading…
Cancel
Save