Skip to content

Commit

Permalink
Updated config tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiannis128 committed Feb 12, 2024
1 parent 8d060b6 commit cdf99c4
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 9 deletions.
7 changes: 7 additions & 0 deletions esbmc_ai_config/contexts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Author: Yiannis Charalambous

from .base_menu import BaseMenu

__all__ = [
"BaseMenu",
]
7 changes: 3 additions & 4 deletions esbmc_ai_config/contexts/base_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from urwid import Button, Text, connect_signal, AttrMap

from esbmc_ai_config.context import Context
from esbmc_ai_config.context_manager import ContextManager
from esbmc_ai_config.widgets.back_button import BackButton


class BaseMenu(Context):
Expand Down Expand Up @@ -35,14 +35,13 @@ def build_ui(self) -> urwid.Widget:
choices: list[str | urwid.Widget] = self.choices.copy()
if self.back_choice:
choices.append(urwid.Divider())
choices.append("Back")
choices.append(BackButton())

menu: urwid.ListBox = self.create_menu(title=self.title, choices=choices)
return self.create_padding(menu)

def item_chosen(self, button, choice) -> None:
if choice == "Back":
ContextManager.pop_context()
pass

def create_menu(self, title, choices: list[str | urwid.Widget]) -> urwid.ListBox:
body: list[urwid.Widget] = [Text(title), urwid.Divider()]
Expand Down
2 changes: 1 addition & 1 deletion esbmc_ai_config/contexts/env_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import urwid
from esbmc_ai_config.models.config_manager import ConfigManager

from esbmc_ai_config.models.env_config_loader import EnvConfigField
from esbmc_ai_config.models import EnvConfigField
from esbmc_ai_config.contexts.base_menu import BaseMenu
from esbmc_ai_config.widgets.text_input_button import TextInputButton

Expand Down
5 changes: 5 additions & 0 deletions esbmc_ai_config/contexts/esbmc_menu/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Author: Yiannis Charalambous

from .esbmc_menu import ESBMCMenu

__all__ = ["ESBMCMenu"]
86 changes: 86 additions & 0 deletions esbmc_ai_config/contexts/esbmc_menu/esbmc_manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Author: Yiannis Charalambous

from typing_extensions import override
import requests
from requests.status_codes import codes as status_codes

import urwid

from esbmc_ai_config.models import ConfigManager
from esbmc_ai_config.context import Context
from esbmc_ai_config.widgets.back_button import BackButton


class ESBMCManage(Context):
license_url: str = "https://raw.githubusercontent.com/esbmc/esbmc/master/COPYING"

def __init__(self) -> None:
response: requests.Response = requests.get(ESBMCManage.license_url)
if response.status_code == status_codes.ok:
self.license: str = response.text
else:
self.license: str = (
f"Couldn't get the license: status code: {response.status_code}"
)

super().__init__(self.build_ui())

@property
def license_accepted(self) -> bool:
return bool(ConfigManager.env_config.values["ESBMC_LICENSE_AGREEMENT"])

@license_accepted.setter
def license_accepted(self, value: bool) -> None:
ConfigManager.env_config.values["ESBMC_LICENSE_AGREEMENT"] = value

def _on_accept_license(self, button) -> None:
self.license_accepted = True
self.frame.set_body(self._build_ui())

def _build_ui_license_agreement(self) -> urwid.Widget:
body: list[urwid.Widget] = [
urwid.Divider(),
urwid.Text("Read and accept the license agreement of ESBMC"),
urwid.Divider(),
urwid.Text(self.license),
urwid.Columns(
[
BackButton(),
urwid.AttrMap(
urwid.Button(
"Accept License",
on_press=self._on_accept_license,
),
None,
focus_map="reversed",
),
]
),
]

return urwid.ScrollBar(urwid.ListBox(urwid.SimpleFocusListWalker(body)))

def _build_ui(self) -> urwid.Widget:
body: list[urwid.Widget] = [
urwid.Divider(),
BackButton(),
]

return urwid.ScrollBar(urwid.ListBox(urwid.SimpleFocusListWalker(body)))

@override
def build_ui(self) -> urwid.Widget:
content: urwid.Widget
if self.license_accepted:
content = self._build_ui()
else:
content = self._build_ui_license_agreement()

self.frame: urwid.Frame = urwid.Frame(
header=urwid.Text("Manage ESBMC Installations"),
footer=urwid.Text("www.esbmc.org"),
body=content,
focus_part="body",
)

return self.frame
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from typing_extensions import override

import urwid
from esbmc_ai_config.context_manager import ContextManager
from esbmc_ai_config.contexts.base_menu import BaseMenu
from esbmc_ai_config.contexts.esbmc_menu.esbmc_manage import ESBMCManage
from esbmc_ai_config.widgets.text_input_button import TextInputButton


Expand All @@ -16,6 +18,14 @@ def _on_esbmc_path(self, title: str, value: str, ok_pressed: bool) -> None:

def _get_menu_choices(self) -> list[str | urwid.Widget]:
return [
urwid.AttrMap(
urwid.Button(
"Manage ESBMC installations",
on_press=lambda button: ContextManager.push_context(ESBMCManage()),
),
None,
"reversed",
),
TextInputButton(
"ESBMC Path",
"",
Expand Down
5 changes: 5 additions & 0 deletions esbmc_ai_config/contexts/esbmc_menu/esbmc_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Author: Yiannis Charalambous


class ESBMCParams(object):
pass
4 changes: 2 additions & 2 deletions esbmc_ai_config/contexts/main_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import urwid
from urwid import Text, Widget

from esbmc_ai_config.contexts.base_menu import BaseMenu
from esbmc_ai_config.context_manager import ContextManager
from esbmc_ai_config.contexts import BaseMenu
from esbmc_ai_config.contexts.save_menu import SaveMenu
from esbmc_ai_config.contexts.env_menu import EnvMenu
from esbmc_ai_config.contexts.esbmc_menu import ESBMCMenu
from esbmc_ai_config.contexts.save_menu import SaveMenu


class MainMenu(BaseMenu):
Expand Down
2 changes: 1 addition & 1 deletion esbmc_ai_config/contexts/save_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from esbmc_ai_config.context import Context
from esbmc_ai_config.context_manager import ContextManager
from esbmc_ai_config.contexts.dialog_context import DialogContext
from esbmc_ai_config.models.config_manager import ConfigManager
from esbmc_ai_config.models import ConfigManager


class SaveMenu(Context):
Expand Down
13 changes: 13 additions & 0 deletions esbmc_ai_config/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Author: Yiannis Charalambous

from .config_manager import ConfigManager
from .env_config_loader import EnvConfigField, EnvConfigLoader
from .json_config_loader import JsonConfigField, JsonConfigLoader

__all__ = [
"ConfigManager",
"EnvConfigField",
"EnvConfigLoader",
"JsonConfigField",
"JsonConfigLoader",
]
6 changes: 6 additions & 0 deletions esbmc_ai_config/models/env_config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from dataclasses import dataclass
from typing_extensions import override, Optional
from platform import system as system_name

from esbmc_ai_config.models.config_loader import ConfigLoader

__ALLOWED_ENV_TYPES = bool | float | int | str
Expand All @@ -31,6 +32,11 @@ def __init__(
EnvConfigField("ESBMC_AI_CFG_PATH", "", is_optional=False),
EnvConfigField("OPENAI_API_KEY", "", is_optional=True),
EnvConfigField("HUGGINGFACE_API_KEY", "", is_optional=True),
EnvConfigField(
"ESBMC_LICENSE_AGREEMENT",
default_value=False,
is_optional=True,
),
],
create_missing_fields: bool = False,
) -> None:
Expand Down
1 change: 1 addition & 0 deletions esbmc_ai_config/models/json_config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from dataclasses import dataclass
from typing_extensions import override

from esbmc_ai_config.models.config_loader import ConfigLoader

__ALLOWED_ENV_TYPES = bool | float | int | str
Expand Down
21 changes: 21 additions & 0 deletions esbmc_ai_config/widgets/back_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Author: Yiannis Charalambous

import urwid
from urwid.widget import Button

from esbmc_ai_config.context_manager import ContextManager


class BackButton(urwid.WidgetWrap):
def __init__(self):
super().__init__(self.build_ui())

def _on_pressed(self, button) -> None:
ContextManager.pop_context()

def build_ui(self) -> urwid.Widget:
self.button: urwid.Button = Button(
"Back",
on_press=self._on_pressed,
)
return urwid.AttrMap(self.button, None, focus_map="reversed")
3 changes: 2 additions & 1 deletion esbmc_ai_config/widgets/text_input_button.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Author: Yiannis Charalambous

from typing import Callable, Optional

import urwid
from esbmc_ai_config.context import Context

from esbmc_ai_config.context import Context
from esbmc_ai_config.context_manager import ContextManager


Expand Down

0 comments on commit cdf99c4

Please sign in to comment.