From aaecf1d99f87b8345347f293a434df36b5983ce3 Mon Sep 17 00:00:00 2001 From: Jonathan Weth Date: Wed, 30 Jun 2021 10:40:04 +0200 Subject: Clean up doc strings and debug statements --- rwa/support/sessionservice/service.py | 71 +++++++++++++---------------------- 1 file changed, 27 insertions(+), 44 deletions(-) (limited to 'rwa/support') diff --git a/rwa/support/sessionservice/service.py b/rwa/support/sessionservice/service.py index bdd8fbe..23fd660 100755 --- a/rwa/support/sessionservice/service.py +++ b/rwa/support/sessionservice/service.py @@ -35,14 +35,14 @@ import click import dbus import dbus.mainloop.glib import dbus.service -from gi.repository import GLib import validators +from gi.repository import GLib from .config import user_settings from .lock import is_locked, lock, unlock +from .log import logging from .session import Session from .trigger import TriggerServerThread -from .log import logging ALLOW_ONLY_ONE_SESSION = True @@ -79,13 +79,13 @@ class RWASupportSessionService(dbus.service.Object): logging.info("D-Bus service has been started.") def _is_url(self, url: str) -> bool: - """Test if the given string contains an url. + """Test if the given string is an url. :param url: The string which should be an URL. - :return: Bool (True=is url) or (False=no url) + :return: Whether the string is an URL. """ valid = validators.url(url) - logging.debug('is url?: "%s": %s', url, valid) + logging.debug(f"Is {url} an URL: {valid}") return valid def _get_web_app_hosts(self) -> str: @@ -108,7 +108,7 @@ class RWASupportSessionService(dbus.service.Object): ["https://example.org", "http://127.0.0.1:8000"] """ - logging.info('D-Bus method call: %s()', 'get_web_app_hosts') + logging.info("D-Bus method call: %s()", "get_web_app_hosts") logging.debug('Return to D-Bus caller: "%s"', self._get_web_app_hosts()) return self._get_web_app_hosts() @@ -128,18 +128,17 @@ class RWASupportSessionService(dbus.service.Object): ["https://example.org", "http://127.0.0.1:8000"] """ - host = str(host) - logging.info('D-Bus method call: %s("%s")', 'add_web_app_host', host) + logging.info('D-Bus method call: %s("%s")', "add_web_app_host", host) if self._is_url(host): user_settings.save_settings() user_settings.web_app_hosts.append(host) logging.debug('Added "%s" to "web_app_hosts" in user_settings', host) else: - logging.warning('Given URL is not valid!') - logging.debug('Didn\'t add "%s" to "web_app_hosts" in user_settings', host) + logging.warning("Given URL is not valid!") + logging.debug('Did not add "%s" to "web_app_hosts" in user_settings', host) return self._get_web_app_hosts() @dbus.service.method( @@ -157,19 +156,18 @@ class RWASupportSessionService(dbus.service.Object): ["https://example.org", "http://127.0.0.1:8000"] """ + logging.info("D-Bus method call: %s(%d)", "remove_web_app_host", host_idx) - logging.info('D-Bus method call: %s(%d)',"remove_web_app_host", host_idx) - - hosts = json.loads(self._get_web_app_hosts()) - if host_idx >= 0 and host_idx < len(hosts): + if host_idx >= 0 and host_idx < len(user_settings.web_app_hosts): + host = user_settings.web_app_hosts[host_idx] del user_settings.web_app_hosts[host_idx] user_settings.save_settings() - logging.debug('Removed web_app_hosts[%d]="%s" in user_settings', - host_idx, hosts[host_idx]) + logging.debug('Removed web_app_hosts[%d]="%s" in user_settings', host_idx, host) else: - logging.warning('Given host index is not valid!') - logging.debug("Didn't remove web_app_hosts[%d] (not existant!) in " - "user_settings", host_idx) + logging.warning("Given host index is not valid!") + logging.debug( + "Did not remove web_app_hosts[%d] (not existent!) in " "user_settings", host_idx + ) return self._get_web_app_hosts() @@ -198,8 +196,7 @@ class RWASupportSessionService(dbus.service.Object): ``connection``, ``host_not_found`` """ - - logging.info('D-Bus method call: %s(%d)', "start", host_idx) + logging.info("D-Bus method call: %s(%d)", "start", host_idx) if ALLOW_ONLY_ONE_SESSION and len(self.sessions.values()) > 0: logging.warning( @@ -213,12 +210,11 @@ class RWASupportSessionService(dbus.service.Object): try: host = user_settings.web_app_hosts[host_idx] - logging.debug('web_app_hosts[%d] is the following host: "%s"', - host_idx, host) + logging.debug('web_app_hosts[%d] is the following host: "%s"', host_idx, host) except IndexError: logging.error("web_app_hosts[%d] does not exist!", host_idx) - logging.debug("The response to the D-Bus caller: '%s'", response) response = json.dumps({"status": "error", "type": "host_not_found"}) + logging.debug("The response to the D-Bus caller: '%s'", response) return response # Start session @@ -240,8 +236,10 @@ class RWASupportSessionService(dbus.service.Object): logging.debug("The response to the D-Bus caller: '%s'", response) return response except ConnectionError: - logging.error("There was a connection error while trying to reach " - "the RWA.Support.WebApp server.") + logging.error( + "There was a connection error while trying to reach " + "the RWA.Support.WebApp server." + ) response = json.dumps({"status": "error", "type": "connection"}) logging.debug("The response to the D-Bus caller: '%s'", response) @@ -275,7 +273,7 @@ class RWASupportSessionService(dbus.service.Object): ``dead`` There was a problem, so that the session is dead. ============ ====================== """ - logging.info('D-Bus method call: %s(%d)', "status", pid) + logging.info("D-Bus method call: %s(%d)", "status", pid) response = self._get_status(pid) logging.debug("The response to the D-Bus caller: '%s'", response) return response @@ -285,7 +283,7 @@ class RWASupportSessionService(dbus.service.Object): ) def refresh_status(self, pid: int) -> str: """Update status from WebApp before returning it here like :meth:`status`.""" - logging.info('D-Bus method call: %s(%d)', "refresh_status", pid) + logging.info("D-Bus method call: %s(%d)", "refresh_status", pid) self._update_session(pid) response = self._get_status(pid) @@ -307,7 +305,7 @@ class RWASupportSessionService(dbus.service.Object): {"id": , "status": "stopped"} """ - logging.info('D-Bus method call: %s(%d)', "stop", pid) + logging.info("D-Bus method call: %s(%d)", "stop", pid) try: session = self.sessions[pid] @@ -409,21 +407,6 @@ class RWASupportSessionService(dbus.service.Object): time.sleep(1) -def str2bool(v: Union[str, bool, int]) -> bool: - """Return true or false if the given string can be interpreted as a boolean. - - If it fails, raise an exception. - """ - if isinstance(v, bool): - return v - if v.lower() in ("yes", "true", "t", "y", "1", 1): - return True - elif v.lower() in ("no", "false", "f", "n", "0", 0): - return False - else: - raise argparse.ArgumentTypeError("Boolean value expected.") - - @click.command() @click.option( "-m", -- cgit v1.2.3