diff --git a/src/constants.py b/src/constants.py index 12a64d6..eacd607 100644 --- a/src/constants.py +++ b/src/constants.py @@ -6,4 +6,14 @@ grade = [ "#4CAF50", "#2196F3", "#9C27B0", -] \ No newline at end of file +] + +defconf = { + 'isLogged': False, + 'language': 'pl', + } + +usrconf = { + 'fullName': '', + 'grade': '', + } \ No newline at end of file diff --git a/src/i18n.py b/src/i18n.py index 0050219..45e986a 100644 --- a/src/i18n.py +++ b/src/i18n.py @@ -1,3 +1,5 @@ +from utils import getconfigpath +import configparser import gettext import locale import json @@ -5,10 +7,9 @@ import os # Determine the user's locale try: - with open("config.json", "r") as file: - data = json.load(file) - langf = data.get("lang") - lang = langf + config = configparser.ConfigParser() + config.read(f"{getconfigpath()}/config.ini") + lang = config["DEFAULT"]["language"] except FileNotFoundError: lang = "pl" print("Defaulting to Polish due to missing config file") @@ -19,18 +20,17 @@ except Exception as e: lang = "pl" print(f"Defaulting to Polish due to error: {e}") finally: - print("Language set") - - + print(f"Language set to {lang}") + # Set up translation localedir = os.path.join(os.path.dirname(__file__), "locales") -translation = gettext.translation("base", localedir, languages=[lang], fallback=True) +translation = gettext.translation("base", localedir, languages=[lang], fallback=False) translation.install() _ = translation.gettext # Shortcut for gettext # Function to switch language dynamically def set_language(new_lang): global translation, _ - translation = gettext.translation("base", localedir, languages=[new_lang], fallback=True) + translation = gettext.translation("base", localedir, languages=[new_lang], fallback=False) translation.install() _ = translation.gettext # Update global _ diff --git a/src/locales/en/LC_MESSAGES/base.mo b/src/locales/en/LC_MESSAGES/base.mo new file mode 100644 index 0000000..c7cd16f Binary files /dev/null and b/src/locales/en/LC_MESSAGES/base.mo differ diff --git a/src/locales/en/LC_MESSAGES/base.po b/src/locales/en/LC_MESSAGES/base.po index 2bb0372..1c16a3a 100644 --- a/src/locales/en/LC_MESSAGES/base.po +++ b/src/locales/en/LC_MESSAGES/base.po @@ -14,7 +14,7 @@ msgstr "" "Language-Team: LANGUAGE \n" "Language: \n" "MIME-Version: 1.0\n" -"Content-Type: text/plain; charset=CHARSET\n" +"Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" #: src/main.py:115 src/pages/home.py:9 diff --git a/src/main.py b/src/main.py index e46ef1d..3d861a5 100644 --- a/src/main.py +++ b/src/main.py @@ -4,11 +4,11 @@ import base64 import gettext import keyring import threading +import configparser import flet as ft from i18n import * from utils import * from pages.home import * -from pathlib import Path from pages.exams import * from pages.grades import * from pages.homework import * @@ -16,9 +16,12 @@ from pages.settings import * from pages.timetable import * from pages.behaviour import * from pages.attendance import * +from constants import defconf, usrconf from sdk.src.interfaces.prometheus.context import * from sdk.src.interfaces.prometheus.interface import * +config = configparser.ConfigParser() + def sync(): auth_context_raw = loadauth("Fuji", "Auth Context") auth_context = PrometheusAuthContext.model_validate_json(auth_context_raw) @@ -182,9 +185,13 @@ def login(page: ft.Page): saveauth("Fuji", "Auth Context", jsoncontext) - config = {"isLoggedIn": True, "lang": "pl"} - with open("config.json", "w") as file: - json.dump(config, file) + config.read(f"{getconfigpath()}/config.ini") + config['DEFAULT']['isLogged'] = 'True' + config['User']['fullName'] = students[selected_index].full_name + config['User']['grade'] = students[selected_index].class_name + + with open(f"{getconfigpath()}/config.ini", "w") as file: + config.write(file) page.go("/start") @@ -347,14 +354,29 @@ def login(page: ft.Page): # App configuration if __name__ == "__main__": try: - with open("config.json", "r") as file: - data = json.load(file) - if data.get("isLoggedIn", False): - ft.app(target=main) - else: - ft.app(target=login) - except FileNotFoundError: - config = {"isLoggedIn": False, "lang": "pl"} - with open("config.json", "w") as file: - json.dump(config, file) + config.read(f"{getconfigpath()}/config.ini") + + isLogged = config['DEFAULT']['isLogged'] + + match isLogged: + case 'True': + ft.app(target=main) + case 'False': + ft.app(target=login) + + except (FileNotFoundError, KeyError): + os.makedirs(getconfigpath()) + config['DEFAULT'] = defconf + config['User'] = usrconf + + with open(f"{getconfigpath()}/config.ini", "w") as file: + config.write(file) + ft.app(target=login) + + except FileExistsError: + config['DEFAULT'] = defconf + config['User'] = usrconf + + with open(f"{getconfigpath()}/config.ini", "w") as file: + config.write(file) ft.app(target=login) \ No newline at end of file diff --git a/src/pages/home.py b/src/pages/home.py index 5431d41..3606b36 100644 --- a/src/pages/home.py +++ b/src/pages/home.py @@ -2,8 +2,6 @@ import flet as ft from i18n import * from constants import * -set_language("pl") - def HomePage(): return ft.Column([ ft.Text((_("Home")), size=30, weight="bold"), diff --git a/src/utils.py b/src/utils.py index 1eaf228..31a41c4 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,4 +1,6 @@ import keyring +import os +from pathlib import Path def saveauth(service, username, data, chunk_size=1000): chunks = [data[i:i+chunk_size] for i in range(0, len(data), chunk_size)] @@ -13,3 +15,15 @@ def loadauth(service, username): return "".join(keyring.get_password(service, f"{username}_{i}") or "" for i in range(count)) except (TypeError, ValueError): return None + +def getconfigpath(): + platform = os.name + home = Path.home() + + match platform: + case 'posix': + return f"{home}/.config/Fuji" + case 'Darwin': + return f"{home}/.FujiConfig" + case 'nt': + return f"{home}/AppData/Local/Fuji"