-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switched to use ProfileWrappers for UI, can now save Profile Parents …
…to DB, profile origin maintained through lifecycle
- Loading branch information
Showing
19 changed files
with
576 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import logging | ||
import os | ||
import sqlite3 | ||
from sqlite3 import connect | ||
|
||
DB_DIR = "data" | ||
DB_NAME = "joystick_diagrams.db" | ||
TABLE_NAME = "profile_parents" | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def create_new_db_if_not_exist(): | ||
path = os.path.join(os.getcwd(), DB_DIR, DB_NAME) | ||
connection = connect(path) | ||
connection.execute("PRAGMA foreign_keys = 1") | ||
cur = connection.cursor() | ||
cur.execute( | ||
f"CREATE TABLE IF NOT EXISTS {TABLE_NAME}(\ | ||
parent_profile_key TEXT NOT NULL,\ | ||
ordering INT NOT NULL,\ | ||
profile_key TEXT NOT NULL,\ | ||
PRIMARY KEY(parent_profile_key, profile_key),\ | ||
FOREIGN KEY(profile_key) REFERENCES profiles(profile_key) \ | ||
)" | ||
) | ||
|
||
|
||
def add_parents_to_profile(profile_key: str, parents: list): | ||
path = os.path.join(os.getcwd(), DB_DIR, DB_NAME) | ||
connection = connect(path) | ||
connection.execute("PRAGMA foreign_keys = 1") | ||
cur = connection.cursor() | ||
|
||
query = "SELECT * from profiles WHERE profile_key = ?" | ||
params = (profile_key,) | ||
|
||
cur.execute(query, params) | ||
result = cur.fetchone() | ||
|
||
if not result: | ||
_logger.error( | ||
f"Tried to add a parent to a profile that does not exist in the DB {profile_key=}, and {parents=}" | ||
) | ||
|
||
if result: | ||
# Delete existing relationships | ||
query = "DELETE FROM profile_parents where profile_key = ?" | ||
params = (profile_key,) | ||
cur.execute(query, params) | ||
|
||
try: | ||
for index, parent in enumerate(parents, 1): | ||
query = "INSERT INTO profile_parents (parent_profile_key, ordering, profile_key) VALUES(?,?,?)" | ||
params = (parent, index, result[0]) | ||
cur.execute(query, params) | ||
|
||
except sqlite3.IntegrityError: | ||
_logger.error( | ||
f"Integrity errors when inserting which suggests the {profile_key=} no longer exists in profiles" | ||
) | ||
|
||
connection.commit() | ||
|
||
|
||
if __name__ == "__main__": | ||
create_new_db_if_not_exist() | ||
|
||
add_parents_to_profile("my_profile_key", ["profile_parent_1", "profile_parent_2"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import os | ||
from sqlite3 import connect | ||
|
||
DB_DIR = "data" | ||
DB_NAME = "joystick_diagrams.db" | ||
TABLE_NAME = "profiles" | ||
|
||
|
||
def create_new_db_if_not_exist(): | ||
path = os.path.join(os.getcwd(), DB_DIR, DB_NAME) | ||
connection = connect(path) | ||
cur = connection.cursor() | ||
cur.execute( | ||
f"CREATE TABLE IF NOT EXISTS {TABLE_NAME}(profile_key TEXT PRIMARY KEY)" | ||
) | ||
|
||
|
||
def get_profile(profile_key: str) -> list[str]: | ||
path = os.path.join(os.getcwd(), DB_DIR, DB_NAME) | ||
connection = connect(path) | ||
cur = connection.cursor() | ||
|
||
query = "SELECT * from profiles WHERE profile_key = ?" | ||
params = (profile_key,) | ||
|
||
cur.execute(query, params) | ||
result = cur.fetchone() | ||
|
||
if not result: | ||
return add_profile(profile_key) | ||
|
||
return result[0] | ||
|
||
|
||
def add_profile(profile_key: str) -> list[str]: | ||
path = os.path.join(os.getcwd(), DB_DIR, DB_NAME) | ||
connection = connect(path) | ||
cur = connection.cursor() | ||
|
||
query = "INSERT OR IGNORE INTO profiles (profile_key) VALUES(?)" | ||
params = (profile_key,) | ||
cur.execute(query, params) | ||
|
||
connection.commit() | ||
|
||
query = "SELECT * from profiles WHERE profile_key = ?" | ||
params = (profile_key,) | ||
|
||
cur.execute(query, params) | ||
result = cur.fetchall() | ||
return result[0] | ||
|
||
|
||
def get_profile_parents(profile_key: str): | ||
path = os.path.join(os.getcwd(), DB_DIR, DB_NAME) | ||
connection = connect(path) | ||
cur = connection.cursor() | ||
|
||
query = "SELECT parent_profile_key,ordering from profile_parents WHERE profile_key = ? ORDER BY ordering asc" | ||
params = (profile_key,) | ||
|
||
cur.execute(query, params) | ||
result = cur.fetchall() | ||
|
||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
create_new_db_if_not_exist() | ||
|
||
data = get_profile_parents("my_profile_key") | ||
print(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from joystick_diagrams import app_state | ||
|
||
# Run each plugin | ||
# Get the profiles from all the executed plugins | ||
# Find/Restore and Merge any Profile Configurations | ||
# Find/Restore any XYZ i.e. Layer in NAME edits / Profile Name Changes | ||
|
||
|
||
def run_parser_plugins(): | ||
"""Run the parser plugins available""" | ||
_state = app_state.AppState() | ||
|
||
for wrapper in _state.plugin_manager.plugin_wrappers: | ||
if wrapper.ready: | ||
wrapper.process() |
Oops, something went wrong.