Skip to content

Commit

Permalink
Added type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
tisnik committed Dec 17, 2024
1 parent b7ea4ef commit 97afd64
Show file tree
Hide file tree
Showing 20 changed files with 101 additions and 67 deletions.
18 changes: 11 additions & 7 deletions src/config_editor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Configuration editor for Road Core service."""

from typing import Optional

import yaml

from gui.main_window import MainWindow
Expand All @@ -10,32 +12,34 @@
class ConfigEditor:
"""Class representing instances of configuration editor."""

def __init__(self):
def __init__(self) -> None:
"""Initialize configuration editor."""
self.configuration = None
self.filename = None
self.filename: Optional[str] = None

def new_configuration(self):
def new_configuration(self) -> None:
"""Create new configuration to be edited."""
self.configuration = None

def load_configuration(self, filename):
def load_configuration(self, filename: str) -> None:
"""Load configuration from YAML file."""
with open(filename, encoding="utf-8") as fin:
self.configuration = yaml.safe_load(fin)
self.filename = filename

def save_configuration_as(self, filename):
def save_configuration_as(self, filename: str) -> None:
"""Store configuration into YAML file."""
with open(filename, "w", encoding="utf-8") as fout:
yaml.dump(self.configuration, fout)

def save_configuration(self):
def save_configuration(self) -> None:
"""Store configuration into YAML file."""
if self.filename is None:
return
with open(self.filename, "w", encoding="utf-8") as fout:
yaml.dump(self.configuration, fout)

def check_configuration(self):
def check_configuration(self) -> None:
"""Check if configuration is correct one."""


Expand Down
2 changes: 1 addition & 1 deletion src/gui/dialogs/about_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from tkinter import messagebox


def about():
def about() -> None:
"""Show 'about' dialog."""
messagebox.showinfo(
"Config editor", "Configuration editor for the Road Core service"
Expand Down
4 changes: 3 additions & 1 deletion src/gui/dialogs/auth_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import tkinter

from gui.icons import Icons


class AuthDialog(tkinter.Toplevel):
"""Dialog for editing authentication configuration."""

def __init__(self, parent, icons):
def __init__(self, parent: tkinter.Toplevel, icons: Icons) -> None:
"""Initialize authentication configuration dialog."""
tkinter.Toplevel.__init__(self, parent)
self.title("Authentication configuration")
Expand Down
14 changes: 8 additions & 6 deletions src/gui/dialogs/cache_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import tkinter
from tkinter import ttk

from gui.icons import Icons


class ConversationCacheDialog(tkinter.Toplevel):
"""Dialog for editing conversation settings."""

def __init__(self, parent, icons):
def __init__(self, parent: tkinter.Toplevel, icons: Icons) -> None:
"""Initialize dialog for editing conversation settings."""
tkinter.Toplevel.__init__(self, parent)
self.title("Conversation cache")
Expand All @@ -24,19 +26,19 @@ def __init__(self, parent, icons):
self.grab_set()

# UI groups
self.group = tkinter.LabelFrame(self, text="Conversation cache", padx=5, pady=8)
self.uigroup = tkinter.LabelFrame(self, text="Conversation cache", padx=5, pady=8)

label1 = tkinter.Label(self.group, text="Type")
label1 = tkinter.Label(self.uigroup, text="Type")

label1.grid(row=1, column=1, sticky="W", padx=5, pady=5)

cache_types = ("In-memory", "PostgreSQL", "Redis")

cache_type = tkinter.StringVar(self.group, cache_types[0], "cache_type")
cache_type = tkinter.StringVar(self.uigroup, cache_types[0], "cache_type")
print(cache_type)

cb1 = ttk.Combobox(
self.group,
self.uigroup,
values=cache_types,
# textvariable=app_log_levels,
state="readonly",
Expand All @@ -54,7 +56,7 @@ def __init__(self, parent, icons):
)

# UI groups placement
self.group.grid(row=1, column=1, sticky="NSWE", padx=5, pady=5)
self.uigroup.grid(row=1, column=1, sticky="NSWE", padx=5, pady=5)

ok_button.grid(row=2, column=1, sticky="W", padx=10, pady=10)

Expand Down
4 changes: 3 additions & 1 deletion src/gui/dialogs/check_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import tkinter

from gui.icons import Icons


class CheckConfigurationDialog(tkinter.Toplevel):
"""Dialog for checking the configuration."""

def __init__(self, parent, icons):
def __init__(self, parent: tkinter.Toplevel, icons: Icons) -> None:
"""Initialize dialog for checking the configuration."""
tkinter.Toplevel.__init__(self, parent)
self.title("Check configuration")
Expand Down
4 changes: 3 additions & 1 deletion src/gui/dialogs/data_collection_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import tkinter

from gui.icons import Icons


class DataCollectionDialog(tkinter.Toplevel):
"""Data collection dialog."""

def __init__(self, parent, icons):
def __init__(self, parent: tkinter.Toplevel, icons: Icons) -> None:
"""Initialize data collection dialog."""
tkinter.Toplevel.__init__(self, parent)
self.title("Data collection settings")
Expand Down
4 changes: 3 additions & 1 deletion src/gui/dialogs/default_model_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import tkinter

from gui.icons import Icons


class DefaultModelSelection(tkinter.Toplevel):
"""Default model selection dialog."""

def __init__(self, parent, icons):
def __init__(self, parent: tkinter.Toplevel, icons: Icons) -> None:
"""Initialize default model selection dialog."""
tkinter.Toplevel.__init__(self, parent)
self.title("Default model selection")
Expand Down
4 changes: 3 additions & 1 deletion src/gui/dialogs/default_provider_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import tkinter

from gui.icons import Icons


class DefaultProviderSelection(tkinter.Toplevel):
"""Default provider selection dialog."""

def __init__(self, parent, icons):
def __init__(self, parent: tkinter.Toplevel, icons: Icons) -> None:
"""Initialize default provider selection dialog."""
tkinter.Toplevel.__init__(self, parent)
self.title("Default provider selection")
Expand Down
26 changes: 16 additions & 10 deletions src/gui/dialogs/edit_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import tkinter
from tkinter import ttk
from typing import Optional

from gui.dialogs.auth_dialog import AuthDialog
from gui.dialogs.cache_dialog import ConversationCacheDialog
Expand All @@ -12,35 +13,38 @@
from gui.dialogs.logging_dialog import LoggingDialog
from gui.dialogs.security_profile_dialog import TLSSecurityProfileDialog
from gui.dialogs.tls_dialog import TLSConfigurationDialog
from gui.icons import Icons

BUTTON_WIDTH = 100


class EditDialog(tkinter.Toplevel):
"""Implementation of configuration editor."""

def __init__(self, parent, icons):
def __init__(self, parent: tkinter.Toplevel, icons: Icons) -> None:
"""Initialize the dialog."""
tkinter.Toplevel.__init__(self, parent)
self.title("Configuration editor")
self.icons = icons
self.parent = parent
self.group1 = None
self.group2 = None
self.group3 = None
self.group1: Optional[tkinter.LabelFrame] = None
self.group2: Optional[tkinter.LabelFrame] = None
self.group3: Optional[tkinter.LabelFrame] = None

# don't display the dialog in list of opened windows
self.transient(parent)
self.add_widgets()
self.set_dialog_properties()

def add_widgets(self):
def add_widgets(self) -> None:
"""Add all widgets on the dialog."""
self.add_llm_group()
self.add_service_settings_group()
self.add_devel_settings_group()

# UI groups placement
if self.group1 is None or self.group2 is None or self.group3 is None:
return
self.group1.grid(row=1, column=1, sticky="NSWE", padx=5, pady=5)
self.group2.grid(row=1, column=2, sticky="NSWE", padx=5, pady=5)
self.group3.grid(row=1, column=3, sticky="NSWE", padx=5, pady=5)
Expand Down Expand Up @@ -68,7 +72,7 @@ def add_widgets(self):
# set the focus
ok_button.focus_set()

def set_dialog_properties(self):
def set_dialog_properties(self) -> None:
"""Set edit dialog properties."""
# close the dialog on 'x' click
self.protocol("WM_DELETE_WINDOW", self.destroy)
Expand All @@ -80,7 +84,7 @@ def set_dialog_properties(self):
self.bind("<Return>", lambda _: self.ok())
self.bind("<Escape>", lambda _: self.destroy())

def add_llm_group(self):
def add_llm_group(self) -> None:
"""Add LLM group widgets onto the dialog."""
# UI groups
self.group1 = tkinter.LabelFrame(self, text="LLM section", padx=5, pady=8)
Expand All @@ -96,7 +100,7 @@ def add_llm_group(self):
)
button_new_llm.grid(row=1, column=1, sticky="WE", padx=5, pady=5)

def add_service_settings_group(self):
def add_service_settings_group(self) -> None:
"""Add service settings widgets onto the dialog."""
self.group2 = tkinter.LabelFrame(self, text="Service settings", padx=5, pady=8)
# service settings
Expand Down Expand Up @@ -199,7 +203,9 @@ def add_service_settings_group(self):
label9 = tkinter.Label(self.group2, text="Query validation method")
label9.grid(row=9, column=1, sticky="W", padx=5, pady=5)
query_validation_methods = ("LLM", "Keyword")
query_validation_method = query_validation_methods[0]
query_validation_method = tkinter.StringVar(
self.group2, query_validation_methods[0], "query_validation_method"
)

cb = ttk.Combobox(
self.group2,
Expand All @@ -210,7 +216,7 @@ def add_service_settings_group(self):
cb.current(0)
cb.grid(row=9, column=2, sticky="W", padx=5, pady=5)

def add_devel_settings_group(self):
def add_devel_settings_group(self) -> None:
"""Add devel settings widgets onto the dialog."""
self.group3 = tkinter.LabelFrame(self, text="Devel settings", padx=5, pady=8)
# devel settings
Expand Down
7 changes: 4 additions & 3 deletions src/gui/dialogs/help_dialog.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Help dialog implementation."""

import tkinter
from typing import Optional


class HelpDialog(tkinter.Toplevel):
"""Help dialog implementation."""

def __init__(self, parent):
def __init__(self, parent: Optional[tkinter.Toplevel]) -> None:
"""Perform initialization of help dialog."""
tkinter.Toplevel.__init__(self, parent)
self.title("Nápověda")
Expand Down Expand Up @@ -46,11 +47,11 @@ def __init__(self, parent):
# close the dialog on 'x' click
self.protocol("WM_DELETE_WINDOW", self.destroy)

def ok(self):
def ok(self) -> None:
"""Ok button handler."""
self.destroy()


def show_help():
def show_help() -> None:
"""Display help dialog."""
HelpDialog(None)
4 changes: 3 additions & 1 deletion src/gui/dialogs/llm_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import tkinter

from gui.icons import Icons


class LLMDialog(tkinter.Toplevel):
"""New LLM dialog."""

def __init__(self, parent, icons):
def __init__(self, parent: tkinter.Toplevel, icons: Icons) -> None:
"""Initialize new LLM dialog."""
tkinter.Toplevel.__init__(self, parent)
self.title("New LLM")
Expand Down
Loading

0 comments on commit 97afd64

Please sign in to comment.