Skip to content

Commit

Permalink
Update settings editor to use sqlite3
Browse files Browse the repository at this point in the history
Fixes #451

Add functionality to update config manager state and store settings using sqlite3 in the settings editor.

* **Config Manager (`src/biscuit/settings/config.py`):**
  - Add methods to save and load settings from sqlite3.
  - Update `__init__` method to initialize sqlite3 database in `datadir`.
  - Update `load_data` method to load settings from sqlite3.

* **Settings Editor (`src/biscuit/settings/editor/editor.py`):**
  - Add methods to save and load settings from sqlite3.
  - Update `add_sections` method to load settings from config manager.

* **Settings (`src/biscuit/settings/settings.py`):**
  - Update `Settings` class to use sqlite3 for configuration.
  - Update `late_setup` method to load settings from sqlite3.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/tomlin7/biscuit/issues/451?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
tomlin7 committed Nov 3, 2024
1 parent 8bf062e commit bc566b1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
65 changes: 61 additions & 4 deletions src/biscuit/settings/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os

import sqlite3
import toml

from .theme.catppuccin_mocha import CatppuccinMocha
Expand All @@ -21,6 +21,19 @@ def __init__(self, master) -> None:
self.auto_save_enabled = False
self.auto_save_timer_ms = 10000

self.db_path = os.path.join(self.base.datadir, "settings.db")
self.db = sqlite3.connect(self.db_path)
self.cursor = self.db.cursor()

self.cursor.executescript(
"""
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY NOT NULL,
value TEXT
);
"""
)

def get_config_path(self, relative_path: str) -> str:
"""Get the absolute path to the resource
Expand All @@ -36,6 +49,50 @@ def load_config(self) -> dict:
return config

def load_data(self) -> None:
# TODO testing
self.theme = GruvboxDark()
self.font = (self.config["font"], self.config["font_size"])
self.cursor.execute("SELECT value FROM settings WHERE key='theme'")
theme = self.cursor.fetchone()
if theme:
self.theme = theme[0]

self.cursor.execute("SELECT value FROM settings WHERE key='font'")
font = self.cursor.fetchone()
if font:
self.font = font[0]

self.cursor.execute("SELECT value FROM settings WHERE key='uifont'")
uifont = self.cursor.fetchone()
if uifont:
self.uifont = uifont[0]

self.cursor.execute("SELECT value FROM settings WHERE key='auto_save_enabled'")
auto_save_enabled = self.cursor.fetchone()
if auto_save_enabled:
self.auto_save_enabled = auto_save_enabled[0]

self.cursor.execute("SELECT value FROM settings WHERE key='auto_save_timer_ms'")
auto_save_timer_ms = self.cursor.fetchone()
if auto_save_timer_ms:
self.auto_save_timer_ms = auto_save_timer_ms[0]

def save_data(self) -> None:
self.cursor.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('theme', ?)",
(self.theme,),
)
self.cursor.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('font', ?)",
(self.font,),
)
self.cursor.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('uifont', ?)",
(self.uifont,),
)
self.cursor.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('auto_save_enabled', ?)",
(self.auto_save_enabled,),
)
self.cursor.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('auto_save_timer_ms', ?)",
(self.auto_save_timer_ms,),
)
self.db.commit()
37 changes: 37 additions & 0 deletions src/biscuit/settings/editor/editor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import tkinter as tk
import sqlite3

from biscuit.common.ui import Button, Frame, ScrollableFrame
from biscuit.editor import BaseEditor
Expand Down Expand Up @@ -45,6 +46,7 @@ def __init__(self, master, exists=False, editable=False, *args, **kwargs) -> Non
def add_sections(self):
self.add_commonly_used()
self.add_text_editor()
self.load_settings()

def add_commonly_used(self):
"""Add commonly used settings to the settings editor"""
Expand Down Expand Up @@ -98,3 +100,38 @@ def show_result(self, items):
def show_no_results(self):
"""Show no results found message in the settings editor"""
...

def load_settings(self):
"""Load settings from the config manager and update the editor state"""
config = self.base.settings.config
config.load_data()

for section in self.sections:
for item in section.items:
if isinstance(item, DropdownItem):
item.var.set(config.get(item.name, item.var.get()))
elif isinstance(item, IntegerItem):
item.entry.delete(0, tk.END)
item.entry.insert(0, config.get(item.name, item.entry.get()))
elif isinstance(item, StringItem):
item.entry.delete(0, tk.END)
item.entry.insert(0, config.get(item.name, item.entry.get()))
elif isinstance(item, CheckboxItem):
item.var.set(config.get(item.name, item.var.get()))

def save_settings(self):
"""Save settings from the editor state to the config manager"""
config = self.base.settings.config

for section in self.sections:
for item in section.items:
if isinstance(item, DropdownItem):
config.set(item.name, item.var.get())
elif isinstance(item, IntegerItem):
config.set(item.name, item.entry.get())
elif isinstance(item, StringItem):
config.set(item.name, item.entry.get())
elif isinstance(item, CheckboxItem):
config.set(item.name, item.var.get())

config.save_data()
5 changes: 3 additions & 2 deletions src/biscuit/settings/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import re
import sqlite3
import tkinter as tk
import tkinter.font as tkfont
import typing
Expand Down Expand Up @@ -33,8 +34,6 @@ def format(self, term) -> str:
return super().format(f"{default}{term}")


# TODO: functional settings editor
# TODO: load/store config in sqlite3 db
class Settings:
"""Settings for the application
Expand Down Expand Up @@ -129,6 +128,8 @@ def late_setup(self) -> None:
self.symbols_actionset = ActionSet("Go to symbol in editor", "@", [])
self.base.palette.register_actionset(lambda: self.symbols_actionset)

self.config.load_data()

@property
def actionset(self):
return self._actionset

0 comments on commit bc566b1

Please sign in to comment.