Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update settings editor to use sqlite3 #453

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading