diff options
author | suchinton2001 <suchinton.2001@gmail.com> | 2023-10-25 12:12:04 +0530 |
---|---|---|
committer | suchinton2001 <suchinton.2001@gmail.com> | 2023-10-25 12:17:23 +0530 |
commit | 6dbb82d17cfaf77de0cd1b49b0997eab241907bc (patch) | |
tree | be92cb9538828806cfa9137f5f31fe1e516a3471 | |
parent | b100328904e366b72042a1493bbc9c04c1c45da2 (diff) |
agl-demo-control-panel: Save user preferences for next session
Read from config.ini to load users last uses settings.
SPEC-4905
Signed-off-by: suchinton2001 <suchinton.2001@gmail.com>
Change-Id: I7a8da78a745f56596a84eb6c791af10881c0f7e5
-rw-r--r-- | Widgets/settings.py | 57 | ||||
-rw-r--r-- | extras/UI_Handeler.py | 9 | ||||
-rw-r--r-- | extras/config.ini | 2 | ||||
-rw-r--r-- | extras/config.py | 69 |
4 files changed, 88 insertions, 49 deletions
diff --git a/Widgets/settings.py b/Widgets/settings.py index f1eed41..d6efb66 100644 --- a/Widgets/settings.py +++ b/Widgets/settings.py @@ -15,6 +15,7 @@ from extras import config import extras.Kuksa_Instance as kuksa_instance + import os import sys import time @@ -98,6 +99,7 @@ class settings(Base, Form): self.reconnectBtn = self.findChild(QPushButton, "reconnectBtn") self.startClientBtn = self.findChild(QPushButton, "startClientBtn") self.startClientBtn.setCheckable(True) + self.startClientBtn.setStyleSheet("border: 1px solid green;") self.startClientBtn.clicked.connect(self.start_stop_client) self.reconnectBtn.clicked.connect(self.reconnectClient) @@ -123,24 +125,12 @@ class settings(Base, Form): def start_stop_client(self): if self.startClientBtn.isChecked(): - # turn button red and change icon to stop from resources self.set_instance() - if self.client is not None: - self.startClientBtn.setStyleSheet("border: 1px solid red;") - self.startClientBtn.setIcon(QtGui.QIcon( - ":/Carbon_Icons/carbon_icons/stop.svg")) - self.startClientBtn.setText("Stop Client") - else: - self.startClientBtn.setChecked(False) - else: - # turn button green and change icon to start from resources - if self.client is not None: - self.client.stop() + if self.client is not None: + self.client.stop() - self.startClientBtn.setStyleSheet("border: 1px solid green;") - self.startClientBtn.setIcon(QtGui.QIcon( - ":/Carbon_Icons/carbon_icons/play.svg")) - self.startClientBtn.setText("Start Client") + self.refreshThread = RefreshThread(self) + self.refreshThread.start() def toggleSSL(self): """ @@ -185,14 +175,6 @@ class settings(Base, Form): self.kuksa.reconnect(new_config, self.kuksa_token) self.client = self.kuksa.get_client() - time.sleep(2) - - if (self.client is None): - self.connectionStatus.setText('Not Connected') - self.connectionLogo.setStyleSheet("background-color: red") - - self.refreshStatus() - def refreshStatus(self): """ Refreshes the connection status. @@ -203,6 +185,12 @@ class settings(Base, Form): self.connectionLogo.setStyleSheet("background-color: red") self.connectionLogo.setPixmap(QtGui.QPixmap( ":/Carbon_Icons/carbon_icons/connection-signal--off.svg")) + + self.startClientBtn.setStyleSheet("border: 1px solid green;") + self.startClientBtn.setIcon(QtGui.QIcon( + ":/Carbon_Icons/carbon_icons/play.svg")) + self.startClientBtn.setText("Start Client") + self.startClientBtn.setChecked(False) return None if (self.client.checkConnection() == True): @@ -210,6 +198,12 @@ class settings(Base, Form): self.connectionLogo.setStyleSheet("background-color: green") self.connectionLogo.setPixmap(QtGui.QPixmap( ":/Carbon_Icons/carbon_icons/connection-signal.svg")) + + self.startClientBtn.setStyleSheet("border: 1px solid red;") + self.startClientBtn.setIcon(QtGui.QIcon( + ":/Carbon_Icons/carbon_icons/stop.svg")) + self.startClientBtn.setText("Stop Client") + self.startClientBtn.setChecked(True) self.client.start() return True @@ -219,6 +213,12 @@ class settings(Base, Form): self.connectionLogo.setStyleSheet("background-color: yellow") self.connectionLogo.setPixmap(QtGui.QPixmap( ":/Carbon_Icons/carbon_icons/connection-signal--off.svg")) + + self.startClientBtn.setStyleSheet("border: 1px solid green;") + self.startClientBtn.setIcon(QtGui.QIcon( + ":/Carbon_Icons/carbon_icons/play.svg")) + self.startClientBtn.setText("Start Client") + self.startClientBtn.setChecked(False) return False except: pass @@ -233,7 +233,6 @@ class settings(Base, Form): self.client.stop() self.client = self.kuksa.reconnect(config, self.kuksa_token) self.client.start() - self.refreshStatus() self.refreshThread = RefreshThread(self) self.refreshThread.start() @@ -242,6 +241,9 @@ class settings(Base, Form): logging.error(e) self.set_instance() + self.refreshThread = RefreshThread(self) + self.refreshThread.start() + def make_new_config(self): """ Makes a new configuration using fields in the settings widget. @@ -254,7 +256,7 @@ class settings(Base, Form): widget.setStyleSheet( "border: 1px solid #4BD7D6 ; /* light blue */") if key: - self.new_config[key] = text + new_config[key] = text else: self.kuksa_token = text else: @@ -271,6 +273,9 @@ class settings(Base, Form): validate_and_set_style(self, self.CA_File, "cacertificate") validate_and_set_style(self, self.Auth_Token) + config.save_session_config( + new_config, self.kuksa_token, self.CA_File.text()) + return new_config def set_settings(self, config_name): diff --git a/extras/UI_Handeler.py b/extras/UI_Handeler.py index 3e653e4..fa2c3a8 100644 --- a/extras/UI_Handeler.py +++ b/extras/UI_Handeler.py @@ -73,21 +73,12 @@ class UI_Handeler(MainWindow): self.stackedWidget.setCurrentIndex(index) def toggleMaximized(self): - """ - This method toggles the maximized state of the window. - """ if self.isMaximized(): self.showNormal() else: self.showMaximized() def moveWindow(self, event): - """ - This method moves the window by dragging the header. - - Args: - - event: The event object containing information about the mouse event. - """ if event.buttons() == QtCore.Qt.LeftButton: self.move(self.pos() + event.globalPos() - self.clickPosition) self.clickPosition = event.globalPos() diff --git a/extras/config.ini b/extras/config.ini index 0f64a4f..1c367f9 100644 --- a/extras/config.ini +++ b/extras/config.ini @@ -1,5 +1,5 @@ [default] -preferred-config=AGL-kuksa-val-server +preferred-config=user-session # [cutom-config-template] # ip=<ip address> diff --git a/extras/config.py b/extras/config.py index 2b92342..48a63e2 100644 --- a/extras/config.py +++ b/extras/config.py @@ -20,43 +20,53 @@ from configparser import ConfigParser python_version = f"python{'.'.join(platform.python_version_tuple()[:2])}" + def check_paths(*paths): return {path: os.path.exists(path) for path in paths} + CONFIG_PATHS = check_paths( "/etc/agl-demo-control-panel.ini", - os.path.join(os.path.expanduser("~"), ".local/share/agl-demo-control-panel/config.ini"), + os.path.join(os.path.expanduser("~"), + ".local/share/agl-demo-control-panel/config.ini"), os.path.abspath(os.path.join(os.path.dirname(__file__), 'config.ini')) ) CA_PATHS = check_paths( "/etc/kuksa-val/CA.pem", f"/usr/lib/{python_version}/site-packages/kuksa_certificates/CA.pem", - os.path.abspath(os.path.join(os.path.dirname(__file__), "../assets/cert/CA.pem")) + os.path.abspath(os.path.join(os.path.dirname( + __file__), "../assets/cert/CA.pem")) ) WS_TOKEN_PATHS = check_paths( f"/usr/lib/{python_version}/site-packages/kuksa_certificates/jwt/super-admin.json.token", - os.path.join(os.path.expanduser("~"), f".local/lib/{python_version}/site-packages/kuksa_certificates/jwt/super-admin.json.token") + os.path.join(os.path.expanduser( + "~"), f".local/lib/{python_version}/site-packages/kuksa_certificates/jwt/super-admin.json.token") ) GRPC_TOKEN_PATHS = check_paths( f"/usr/lib/{python_version}/site-packages/kuksa_certificates/jwt/super-admin.json.token", - os.path.abspath(os.path.join(os.path.dirname(__file__), "../assets/token/grpc/actuate-provide-all.token")) + os.path.abspath(os.path.join(os.path.dirname(__file__), + "../assets/token/grpc/actuate-provide-all.token")) ) config = ConfigParser() -config_path = next((path for path, exists in CONFIG_PATHS.items() if exists), None) +config_path = next( + (path for path, exists in CONFIG_PATHS.items() if exists), None) if config_path: config.read(config_path) CA_PATH = next((path for path, exists in CA_PATHS.items() if exists), None) -WS_TOKEN = next((path for path, exists in WS_TOKEN_PATHS.items() if exists), None) -GRPC_TOKEN = next((path for path, exists in GRPC_TOKEN_PATHS.items() if exists), None) +WS_TOKEN = next( + (path for path, exists in WS_TOKEN_PATHS.items() if exists), None) +GRPC_TOKEN = next( + (path for path, exists in GRPC_TOKEN_PATHS.items() if exists), None) KUKSA_CONFIG = {} KUKSA_TOKEN = None + def select_config(preferred_config): """ Selects a configuration from the config.ini file based on the preferred_config parameter. @@ -77,9 +87,10 @@ def select_config(preferred_config): KUKSA_CONFIG['port'] = config[preferred_config]['port'] KUKSA_CONFIG['protocol'] = config[preferred_config]['protocol'] KUKSA_CONFIG['insecure'] = False if config[preferred_config]['insecure'] == 'false' else True - + if config.has_option(preferred_config, 'cacert'): - KUKSA_CONFIG['cacertificate'] = config[preferred_config]['cacert'] if os.path.exists(config[preferred_config]['cacert']) else CA_PATH + KUKSA_CONFIG['cacertificate'] = config[preferred_config]['cacert'] if os.path.exists( + config[preferred_config]['cacert']) else CA_PATH else: KUKSA_CONFIG['cacertificate'] = None @@ -91,21 +102,53 @@ def select_config(preferred_config): elif os.path.exists(config[preferred_config]['token']): KUKSA_TOKEN = config[preferred_config]['token'] else: - ValueError(f"Token file {config[preferred_config]['token']} not found") + ValueError( + f"Token file {config[preferred_config]['token']} not found") else: - raise ValueError(f"Config section {preferred_config} not found in config.ini") + raise ValueError( + f"Config section {preferred_config} not found in config.ini") + + return preferred_config, KUKSA_CONFIG, KUKSA_TOKEN - return preferred_config, KUKSA_CONFIG , KUKSA_TOKEN def get_list_configs(): return config.sections()[1:] + def get_default_config(): defaultConfigName = config.get('default', 'preferred-config') return defaultConfigName + def get_default_token(protocol): if protocol == 'grpc': return GRPC_TOKEN else: - return WS_TOKEN
\ No newline at end of file + return WS_TOKEN + + +def save_session_config(session_config, auth_token, CA_File=None): + """ + save values to config.ini under [user-session] + """ + if not config.has_section('user-session'): + config.add_section('user-session') + + config.set('user-session', 'ip', str(session_config['ip'])) + config.set('user-session', 'port', str(session_config['port'])) + config.set('user-session', 'protocol', str(session_config['protocol'])) + config.set('user-session', 'insecure', str(session_config['insecure'])) + config.set('user-session', 'tls_server_name', + str(session_config['tls_server_name'])) + if auth_token in WS_TOKEN_PATHS or auth_token in GRPC_TOKEN_PATHS: + config.set('user-session', 'token', 'default') + else: + config.set('user-session', 'token', str(auth_token)) + + if CA_File in CA_PATHS: + config.set('user-session', 'cacert', 'default') + else: + config.set('user-session', 'cacert', str(CA_File)) + + with open(config_path, 'w') as configfile: + config.write(configfile) |