From a3055407f948b36b0abfe98b703c75903f1513ad Mon Sep 17 00:00:00 2001 From: Rob <5183487+Rexeh@users.noreply.github.com> Date: Fri, 22 Mar 2024 17:54:33 +0000 Subject: [PATCH] Integrate to work as plugin --- .../plugins/fs2020_plugin/main.py | 42 +++++++++++++------ .../fs2020_plugin/ms_flight_simulator.py | 19 ++++----- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/joystick_diagrams/plugins/fs2020_plugin/main.py b/joystick_diagrams/plugins/fs2020_plugin/main.py index 04eea0f..86aab6f 100644 --- a/joystick_diagrams/plugins/fs2020_plugin/main.py +++ b/joystick_diagrams/plugins/fs2020_plugin/main.py @@ -1,31 +1,36 @@ +import json import logging from pathlib import Path from joystick_diagrams.input.profile_collection import ProfileCollection +from joystick_diagrams.plugins.fs2020_plugin.ms_flight_simulator import FS2020Parser from joystick_diagrams.plugins.plugin_interface import PluginInterface from .config import settings _logger = logging.getLogger("__name__") +CONFIG_FILE = "data.json" + class ParserPlugin(PluginInterface): def __init__(self): - super().__init__() self.settings = settings self.settings.validators.register() self.path = None + self.instance = None | FS2020Parser def process(self) -> ProfileCollection: - # Process your plugin, to build and return a profile collection instance - return ProfileCollection() + return self.instance.run() def set_path(self, path: Path) -> bool: try: - # Handle the Path provided - # Validate the path / file/ folder provided is suitable for your plugin - # Pre-initialise any instances if required - pass + inst = FS2020Parser(path) + + if inst: + self.instance = inst + self.path = path + self.save_plugin_state() except Exception: return False @@ -33,19 +38,30 @@ def set_path(self, path: Path) -> bool: return True def save_plugin_state(self): - # Persist any data changes when they are modified - # self.get_plugin_data_path() - Gets your plugins app data path automatically - pass + with open( + Path.joinpath(self.get_plugin_data_path(), CONFIG_FILE), + "w", + encoding="UTF8", + ) as f: + f.write(json.dumps({"path": str(self.path)})) def load_settings(self) -> None: - # Load any data / initialise your plugin - Automatically called on start up if plugin is enabled - pass + try: + with open( + Path.joinpath(self.get_plugin_data_path(), CONFIG_FILE), + "r", + encoding="UTF8", + ) as f: + data = json.loads(f.read()) + self.path = Path(data["path"]) if data["path"] else None + except FileNotFoundError: + pass @property def path_type(self): return self.FolderPath( "This title shows up on the UI select dialog", - Path.joinpath(Path.home(), "Saved Games"), + Path.joinpath(Path.home(), "AppData"), ) @property diff --git a/joystick_diagrams/plugins/fs2020_plugin/ms_flight_simulator.py b/joystick_diagrams/plugins/fs2020_plugin/ms_flight_simulator.py index 995d8d9..e1fe4d6 100644 --- a/joystick_diagrams/plugins/fs2020_plugin/ms_flight_simulator.py +++ b/joystick_diagrams/plugins/fs2020_plugin/ms_flight_simulator.py @@ -1,5 +1,5 @@ import xml.etree.ElementTree as eT -from dataclasses import dataclass, field +from dataclasses import dataclass from pathlib import Path from joystick_diagrams.input.axis import Axis, AxisDirection @@ -30,13 +30,13 @@ default_profile_name = "Default" -@dataclass -class Parser: - path: str - data: list = field(init=False, default_factory=list) +class FS2020Parser: + def __init__(self, folder_path): + self.folder_path = Path(folder_path) + self.data = list() def run(self): - folders = Path(self.path).iterdir() + folders = Path(self.folder_path).iterdir() pc = ProfileCollection() pc.create_profile(default_profile_name) @@ -211,9 +211,4 @@ def __repr__(self): if __name__ == "__main__": - ps = Parser( - r"C:\Users\RCox\AppData\Local\Packages\Microsoft.FlightSimulator_8wekyb3d8bbwe\SystemAppData\wgs\00090000038358E7_00000000000000000000000069F80140" - ) - data = ps.run() - - print(data) + pass