1
0
Fork 0
forked from Fuji/Fuji

Switched from json config to ini configuration file; fixed a issue with language being set to polish only

This commit is contained in:
Maarceeli 2025-02-22 16:00:15 +01:00
parent b17be83791
commit 6681ab4cf3
7 changed files with 71 additions and 27 deletions

View file

@ -7,3 +7,13 @@ grade = [
"#2196F3", "#2196F3",
"#9C27B0", "#9C27B0",
] ]
defconf = {
'isLogged': False,
'language': 'pl',
}
usrconf = {
'fullName': '',
'grade': '',
}

View file

@ -1,3 +1,5 @@
from utils import getconfigpath
import configparser
import gettext import gettext
import locale import locale
import json import json
@ -5,10 +7,9 @@ import os
# Determine the user's locale # Determine the user's locale
try: try:
with open("config.json", "r") as file: config = configparser.ConfigParser()
data = json.load(file) config.read(f"{getconfigpath()}/config.ini")
langf = data.get("lang") lang = config["DEFAULT"]["language"]
lang = langf
except FileNotFoundError: except FileNotFoundError:
lang = "pl" lang = "pl"
print("Defaulting to Polish due to missing config file") print("Defaulting to Polish due to missing config file")
@ -19,18 +20,17 @@ except Exception as e:
lang = "pl" lang = "pl"
print(f"Defaulting to Polish due to error: {e}") print(f"Defaulting to Polish due to error: {e}")
finally: finally:
print("Language set") print(f"Language set to {lang}")
# Set up translation # Set up translation
localedir = os.path.join(os.path.dirname(__file__), "locales") 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.install()
_ = translation.gettext # Shortcut for gettext _ = translation.gettext # Shortcut for gettext
# Function to switch language dynamically # Function to switch language dynamically
def set_language(new_lang): def set_language(new_lang):
global translation, _ 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.install()
_ = translation.gettext # Update global _ _ = translation.gettext # Update global _

Binary file not shown.

View file

@ -14,7 +14,7 @@ msgstr ""
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n" "Language: \n"
"MIME-Version: 1.0\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" "Content-Transfer-Encoding: 8bit\n"
#: src/main.py:115 src/pages/home.py:9 #: src/main.py:115 src/pages/home.py:9

View file

@ -4,11 +4,11 @@ import base64
import gettext import gettext
import keyring import keyring
import threading import threading
import configparser
import flet as ft import flet as ft
from i18n import * from i18n import *
from utils import * from utils import *
from pages.home import * from pages.home import *
from pathlib import Path
from pages.exams import * from pages.exams import *
from pages.grades import * from pages.grades import *
from pages.homework import * from pages.homework import *
@ -16,9 +16,12 @@ from pages.settings import *
from pages.timetable import * from pages.timetable import *
from pages.behaviour import * from pages.behaviour import *
from pages.attendance import * from pages.attendance import *
from constants import defconf, usrconf
from sdk.src.interfaces.prometheus.context import * from sdk.src.interfaces.prometheus.context import *
from sdk.src.interfaces.prometheus.interface import * from sdk.src.interfaces.prometheus.interface import *
config = configparser.ConfigParser()
def sync(): def sync():
auth_context_raw = loadauth("Fuji", "Auth Context") auth_context_raw = loadauth("Fuji", "Auth Context")
auth_context = PrometheusAuthContext.model_validate_json(auth_context_raw) auth_context = PrometheusAuthContext.model_validate_json(auth_context_raw)
@ -182,9 +185,13 @@ def login(page: ft.Page):
saveauth("Fuji", "Auth Context", jsoncontext) saveauth("Fuji", "Auth Context", jsoncontext)
config = {"isLoggedIn": True, "lang": "pl"} config.read(f"{getconfigpath()}/config.ini")
with open("config.json", "w") as file: config['DEFAULT']['isLogged'] = 'True'
json.dump(config, file) 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") page.go("/start")
@ -347,14 +354,29 @@ def login(page: ft.Page):
# App configuration # App configuration
if __name__ == "__main__": if __name__ == "__main__":
try: try:
with open("config.json", "r") as file: config.read(f"{getconfigpath()}/config.ini")
data = json.load(file)
if data.get("isLoggedIn", False): isLogged = config['DEFAULT']['isLogged']
match isLogged:
case 'True':
ft.app(target=main) ft.app(target=main)
else: case 'False':
ft.app(target=login) ft.app(target=login)
except FileNotFoundError:
config = {"isLoggedIn": False, "lang": "pl"} except (FileNotFoundError, KeyError):
with open("config.json", "w") as file: os.makedirs(getconfigpath())
json.dump(config, file) 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) ft.app(target=login)

View file

@ -2,8 +2,6 @@ import flet as ft
from i18n import * from i18n import *
from constants import * from constants import *
set_language("pl")
def HomePage(): def HomePage():
return ft.Column([ return ft.Column([
ft.Text((_("Home")), size=30, weight="bold"), ft.Text((_("Home")), size=30, weight="bold"),

View file

@ -1,4 +1,6 @@
import keyring import keyring
import os
from pathlib import Path
def saveauth(service, username, data, chunk_size=1000): def saveauth(service, username, data, chunk_size=1000):
chunks = [data[i:i+chunk_size] for i in range(0, len(data), chunk_size)] 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)) return "".join(keyring.get_password(service, f"{username}_{i}") or "" for i in range(count))
except (TypeError, ValueError): except (TypeError, ValueError):
return None 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"