Skip to content

Commit

Permalink
Clean prints and file test code. Move __main__ into app_init and pass…
Browse files Browse the repository at this point in the history
… parser into AppState
  • Loading branch information
Rexeh committed Feb 12, 2024
1 parent 728c01c commit 7f037ad
Show file tree
Hide file tree
Showing 23 changed files with 63 additions and 207 deletions.
28 changes: 1 addition & 27 deletions joystick_diagrams/__main__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import logging
import sys

from PySide6 import QtWidgets
from qt_material import apply_stylesheet

from joystick_diagrams import app_init

# from joystick_diagrams.ui.main_window.main_window import MainWindow
from joystick_diagrams.ui.mock_main import MainWindow

logging.basicConfig(
level=logging.DEBUG,
format="%(module)s %(filename)s - %(asctime)s - %(levelname)s - %(message)s",
Expand All @@ -23,26 +16,7 @@

if __name__ == "__main__":
try:
# Initiate pre-loading
init = app_init.init()

app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()

extra = {
# Button colors
"danger": "#dc3545",
"warning": "#ffc107",
"success": "#27ae60",
# Font
"font_family": "Roboto",
}

apply_stylesheet(
app, theme="dark_blue.xml", invert_secondary=False, extra=extra
)
app_init.init()

app.exec()
except Exception as error: # pylint: disable=broad-except
_logger.exception(error)
27 changes: 20 additions & 7 deletions joystick_diagrams/app_init.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
import logging
import sys

from PySide6 import QtWidgets
from qt_material import apply_stylesheet # type: ignore

from joystick_diagrams.app_state import AppState
from joystick_diagrams.db import db_init
from joystick_diagrams.plugin_manager import ParserPluginManager
from joystick_diagrams.ui.main_window import MainWindow

_logger = logging.getLogger(__name__)


def init():
# TODO log and orchestrate this better

# Setup datastore
db_init.init()
# -------------------------------

# Get global state object
_state = AppState()
# -------------------------------

# -- Initialise Plugins System --
plugins = ParserPluginManager()
plugins.load_discovered_plugins()
plugins.create_plugin_wrappers()
_state.init_plugins(plugins)

# Setup global state with plugins
_state = AppState(plugin_manager=plugins)
# -------------------------------

# Setup UI and begin thread
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()

extra = {
"font_family": "Roboto",
}

apply_stylesheet(app, theme="dark_blue.xml", invert_secondary=False, extra=extra)

app.exec()


if __name__ == "__main__":
pass
13 changes: 5 additions & 8 deletions joystick_diagrams/app_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ class AppState:

def __new__(cls, *args, **kwargs):
if not cls._inst:
cls._inst = super(AppState, cls).__new__(cls, *args, **kwargs)
cls._inst._init()
cls._inst = super(AppState, cls).__new__(cls)

cls._inst._init(plugin_manager=kwargs["plugin_manager"])
return cls._inst

def _init(self) -> None:
self.plugin_manager: ParserPluginManager | None = None
def _init(self, plugin_manager: ParserPluginManager) -> None:
self.plugin_manager: ParserPluginManager = plugin_manager
self.profileObjectMapping: dict[str, Profile_] = {}
self.profileParentMapping: dict[
str, list[str]
Expand All @@ -29,10 +30,6 @@ def _init(self) -> None:
str, Profile_
] = {} # TODO Think here about name colissions
self.update_processed_profiles()

def init_plugins(self, plugin_manager: ParserPluginManager):
"""Injects a plugin manager instance to be managed by AppState"""
self.plugin_manager = plugin_manager
self.process_profile_collection_updates()

## Temp code for handling Plugin Wrapper changes > TODO REFACTOR
Expand Down
4 changes: 1 addition & 3 deletions joystick_diagrams/db/db_device_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,4 @@ def get_device_template_path(guid: str):


if __name__ == "__main__":
add_update_device_template_path("123", "2322323")
data = get_device_template_path("1234")
print(data)
pass
18 changes: 1 addition & 17 deletions joystick_diagrams/input/profile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
from copy import deepcopy

from joystick_diagrams.input.button import Button
from joystick_diagrams.input.device import Device_

_logger = logging.getLogger("__name__")
Expand Down Expand Up @@ -79,19 +78,4 @@ def merge_profiles(self, profile: "Profile_"):


if __name__ == "__main__":
profile1 = Profile_("Profile1")

dev1 = profile1.add_device("dev_1", "dev_1")
dev1.create_input(Button(1), "shoot")

dev1.add_modifier_to_input(Button(1), {"ctrl"}, "bang")
dev1.add_modifier_to_input(Button(1), {"alt"}, "bang again")

profile2 = Profile_("Profile2")

dev3 = profile2.add_device("dev_1", "dev_1")
dev3.create_input(Button(3), "potato")
dev3.add_modifier_to_input(Button(1), {"ctrl"}, "hello")

data = profile1.merge_profiles(profile2)
print(data)
pass
11 changes: 1 addition & 10 deletions joystick_diagrams/input/profile_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,4 @@ def get_profile(self, profile_name) -> Profile_ | None:


if __name__ == "__main__":
collection = ProfileCollection()

inst1 = collection.create_profile("test")
inst2 = collection.create_profile("abc")

dev1 = inst1.add_device("guid1", "joystick_1")
dev2 = inst1.add_device("guid2", "joystick_2")

if dev1 is not None:
print(dev1.inputs)
pass
2 changes: 1 addition & 1 deletion joystick_diagrams/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pathlib import Path
from types import ModuleType

from dynaconf import ValidationError
from dynaconf import ValidationError # type: ignore

from joystick_diagrams.exceptions import JoystickDiagramsError, PluginNotValidError
from joystick_diagrams.plugin_wrapper import PluginWrapper
Expand Down
1 change: 0 additions & 1 deletion joystick_diagrams/plugin_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ def enabled(self) -> bool:
@enabled.setter
def enabled(self, value):
self._enabled = False if isinstance(value, property) else bool(value)
print("Setter triggered")
# TODO process the plugins syncronously on enable
if self._enabled is True:
self.process()
Expand Down
3 changes: 1 addition & 2 deletions joystick_diagrams/plugins/Example/example_plugin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@ def get_path(self) -> bool:


if __name__ == "__main__":
plugin = ParserPlugin()
print(plugin)
pass
8 changes: 1 addition & 7 deletions joystick_diagrams/plugins/dcs_world_plugin/dcs_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,4 @@ def p_error(t): # noqa


if __name__ == "__main__":
dci = DCSWorldParser(
"D:\\Git Repos\\joystick-diagrams\\local_test_data\\toasty_test"
)

dta = dci.process_profiles()

print(dta)
pass
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,4 @@ def extract_hats(self, hat_node) -> list[Union[str, str] | None]:


if __name__ == "__main__":
ps = JoystickGremlinParser(
"D:\\Git Repos\\joystick-diagrams\\tests\\data\\joystick_gremlin\\gremlin_pov_multi.xml"
)

dta = ps.create_dictionary()

print(dta)
pass
3 changes: 1 addition & 2 deletions joystick_diagrams/plugins/joystick_gremlin_plugin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,4 @@ def icon(self):


if __name__ == "__main__":
plugin = ParserPlugin()
print(plugin)
pass
11 changes: 1 addition & 10 deletions joystick_diagrams/ui/configure_page.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import logging
import sys

from PySide6.QtCore import Qt, Slot
from PySide6.QtWidgets import (
QAbstractItemView,
QApplication,
QHeaderView,
QMainWindow,
QTreeWidgetItem,
)
from qt_material import apply_stylesheet

from joystick_diagrams.app_state import AppState
from joystick_diagrams.ui import parent_profiles
Expand Down Expand Up @@ -97,10 +94,4 @@ def handle_clicked_profile(self, item):


if __name__ == "__main__":
logger = logging.basicConfig
app = QApplication(sys.argv)

window = configurePage()
window.show()
apply_stylesheet(app, theme="dark_blue.xml", invert_secondary=False)
app.exec()
pass
22 changes: 6 additions & 16 deletions joystick_diagrams/ui/device_setup.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import logging
import sys

import qtawesome as qta
import qtawesome as qta # type: ignore
from PySide6.QtCore import QSize, Qt, Signal
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import (
QAbstractItemView,
QApplication,
QHeaderView,
QMainWindow,
QMessageBox,
QPushButton,
QTreeWidgetItem,
)
from qt_material import apply_stylesheet

from joystick_diagrams.export_device import ExportDevice
from joystick_diagrams.ui.device_setup_controller import (
Expand Down Expand Up @@ -76,7 +73,6 @@ def __init__(self, *args, **kwargs):
self.initialise_ui()

def initialise_ui(self):
print("Initialise UI called")
devices = get_export_devices()

self.add_devices_to_widget(devices)
Expand All @@ -87,7 +83,7 @@ def device_item_clicked(self, data):

def view_device_errors(self):
current_treewidget_row = self.treeWidget.currentItem()
data = current_treewidget_row.data(0, Qt.UserRole)
data = current_treewidget_row.data(0, Qt.ItemDataRole.UserRole)

# Format the text, this is horrible

Expand Down Expand Up @@ -134,7 +130,7 @@ def return_top_level_icon_state(
root_item = QTreeWidgetItem()
# root_item.setStatusTip(0, identifier)
root_item.setToolTip(0, f"Device GUID: {device_identifier}")
root_item.setData(0, Qt.UserRole, device_identifier)
root_item.setData(0, Qt.ItemDataRole.UserRole, device_identifier)
root_item.setText(0, device_name)

# Get the child items for the root identifier
Expand Down Expand Up @@ -164,7 +160,7 @@ def return_top_level_icon_state(
if not children_have_template_issues:
for child in child_items:
child_item = QTreeWidgetItem()
child_item.setData(0, Qt.UserRole, child)
child_item.setData(0, Qt.ItemDataRole.UserRole, child)
child_item.setText(1, child.description)

# Get the child icon state where template not exists / or errors bucket contains entries
Expand Down Expand Up @@ -196,7 +192,7 @@ def add_view_error_controls_to_tree(self):
item = self.treeWidget.topLevelItem(i)
for c in range(item.childCount()):
child_item = item.child(c)
child_data = child_item.data(0, Qt.UserRole)
child_data = child_item.data(0, Qt.ItemDataRole.UserRole)

if child_data.errors:
button = QPushButton("View Errors")
Expand All @@ -212,10 +208,4 @@ def add_view_error_controls_to_tree(self):


if __name__ == "__main__":
logger = logging.basicConfig
app = QApplication(sys.argv)

window = DeviceSetup()
window.show()
apply_stylesheet(app, theme="dark_blue.xml", invert_secondary=False)
app.exec()
pass
4 changes: 0 additions & 4 deletions joystick_diagrams/ui/device_setup_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ def get_export_devices() -> list[ExportDevice]:


def get_template_for_device(device_guid: str) -> Union[Template, None]:
print(f"Getting template for {device_guid}")
template = get_device_template_path(device_guid)

print(f"Template was {template}")

return Template(template) if template else None


Expand Down
Loading

0 comments on commit 7f037ad

Please sign in to comment.