backend rewrite
This commit is contained in:
@@ -1,2 +1,71 @@
|
|||||||
# This class manages the backend of the application, specifically profiles
|
# This class manages the backend of the application, specifically profiles
|
||||||
#
|
# TODO
|
||||||
|
# - Fetch new data from Wiki/API
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import math
|
||||||
|
|
||||||
|
from pathlib import Path, cwd
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from gi.repository import Gio, GLib, GObject
|
||||||
|
|
||||||
|
# pseudo struct that contains the profile data that gets serialized into the .json profile.
|
||||||
|
@dataclass
|
||||||
|
class Profile:
|
||||||
|
# Dictionary that stores the ownership status of frames
|
||||||
|
owned_frames: [str, bool]
|
||||||
|
unique_frames_owned: int
|
||||||
|
missing_basics: [str]
|
||||||
|
missing_basics_count: int
|
||||||
|
missing_primes: [str]
|
||||||
|
missing_primes_count: int
|
||||||
|
|
||||||
|
class Backend():
|
||||||
|
|
||||||
|
# warframe counts
|
||||||
|
# TODO Update via API/wiki
|
||||||
|
BASIC_WARFRAMES = 63
|
||||||
|
PRIME_WARFRAMES = 49
|
||||||
|
CURRENT_UPDATE = 41
|
||||||
|
|
||||||
|
# Profile
|
||||||
|
profile: Profile
|
||||||
|
|
||||||
|
# tracks whether the onboarding dialogue should be shown
|
||||||
|
fresh_install = True
|
||||||
|
|
||||||
|
def get_config_dir(app_id: str) -> Path:
|
||||||
|
base = Path(GLib.get_user_config_dir())
|
||||||
|
cfg_dir = base / app_id
|
||||||
|
cfg_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
return cfg_dir
|
||||||
|
|
||||||
|
# helpers for profile loading
|
||||||
|
APP_ID = "gay.valhrafnaz.Gnomeframe"
|
||||||
|
CONFIG_DIR = get_config_dir(APP_ID)
|
||||||
|
CONFIG_FILE = CONFIG_DIR / "profile.json"
|
||||||
|
|
||||||
|
def __init__():
|
||||||
|
loaded_profile: dict[str, bool] = Backend.load_profile()
|
||||||
|
|
||||||
|
|
||||||
|
def load_profile() -> dict[str, bool]:
|
||||||
|
try:
|
||||||
|
with Backend.CONFIG_FILE.open("r", encoding="utf-8") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
return {k: bool(v) for k, v in data.items()}
|
||||||
|
except FileNotFoundError:
|
||||||
|
return {}
|
||||||
|
except (json.JSONDecodeError, OSError) as exc:
|
||||||
|
print(f"Could not load profile: {exc}")
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def save_profile(state: dict[str, bool]) -> None:
|
||||||
|
try:
|
||||||
|
Backend.CONFIG_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
|
with Backend.CONFIG_FILE.open("w", encoding="utf-8") as f:
|
||||||
|
json.dump(state, f, indent=2, sort_keys=True)
|
||||||
|
except OSError as exc:
|
||||||
|
print(f"Failed to write profile: {exc}")
|
||||||
|
|||||||
Reference in New Issue
Block a user