From 5beb4bae0450b987ba1fdc59c96e720fe3a10031 Mon Sep 17 00:00:00 2001 From: Rob <5183487+Rexeh@users.noreply.github.com> Date: Tue, 6 Feb 2024 15:27:18 +0000 Subject: [PATCH] Flatten packages out to modules --- joystick_diagrams/classes/__init__.py | 0 joystick_diagrams/classes/export.py | 106 ------------------ joystick_diagrams/classes/version/__init__.py | 0 joystick_diagrams/export.py | 11 +- joystick_diagrams/functions/__init__.py | 0 .../{functions/helper.py => utils.py} | 0 .../{classes/version => }/version.py | 0 7 files changed, 3 insertions(+), 114 deletions(-) delete mode 100644 joystick_diagrams/classes/__init__.py delete mode 100644 joystick_diagrams/classes/export.py delete mode 100644 joystick_diagrams/classes/version/__init__.py delete mode 100644 joystick_diagrams/functions/__init__.py rename joystick_diagrams/{functions/helper.py => utils.py} (100%) rename joystick_diagrams/{classes/version => }/version.py (100%) diff --git a/joystick_diagrams/classes/__init__.py b/joystick_diagrams/classes/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/joystick_diagrams/classes/export.py b/joystick_diagrams/classes/export.py deleted file mode 100644 index 1ec16de..0000000 --- a/joystick_diagrams/classes/export.py +++ /dev/null @@ -1,106 +0,0 @@ -import html -import logging -import os -import re -from datetime import datetime -from os import path -from pathlib import Path - -from joystick_diagrams.config import settings -from joystick_diagrams.functions import helper - -_logger = logging.getLogger(__name__) - - -class Export: - def __init__(self, joystick_listing, parser_id="UNKNOWN"): # pylint disable=too-many-instance-attributes - self.export_directory = "./diagrams/" - self.templates_directory = "./templates/" - self.file_name_divider = "_" - self.joystick_listing = joystick_listing - self.export_progress = None - self.no_bind_text = settings.noBindText - self.executor = parser_id - self.error_bucket = [] - - def export_config(self, progress_bar=None) -> list: - """Manipulates stored templates, and replaces strings with actual values. - - Returns a list of errors. - """ - joystick_count = len(self.joystick_listing) - - _logger.debug(f"Export Started with {joystick_count} joysticks") - _logger.debug(f"Export Data: {self.joystick_listing}") - - for joystick in self.joystick_listing: - base_template = self.get_template(joystick) - if base_template: - for mode in self.joystick_listing[joystick]: - write_template = base_template - _logger.info("Replacing Strings") - completed_template = self.replace_template_strings(joystick, mode, write_template) - _logger.info("Replacing Unused String") - completed_template = self.replace_unused_strings(completed_template) - _logger.info("Branding") - completed_template = self.brand_template(mode, completed_template) - _logger.info("Adding Date") - completed_template = self.date_template(completed_template) - _logger.info(f"Saving: {joystick}") - self.save_template(joystick, mode, completed_template) - - else: - self.error_bucket.append(f"No Template file found for: {joystick}") - - return self.error_bucket - - def get_template(self, joystick): - joystick = joystick.strip() - if path.exists(self.templates_directory + joystick + ".svg"): - data = Path(os.path.join(self.templates_directory, joystick + ".svg")).read_text(encoding="utf-8") - return data - return False - - def save_template(self, joystick, mode, template): - output_path = self.export_directory + self.executor + "_" + joystick.strip() + "_" + mode + ".svg" - - helper.create_directory(self.export_directory) - - try: - outputfile = open(output_path, "w", encoding="UTF-8") - outputfile.write(template) - outputfile.close() - except PermissionError as e: - _logger.error(e) - raise - - def replace_unused_strings(self, template): - regex_search = "\\bButton_\\d+\\b|\\bPOV_\\d+_\\w+\\b" - matches = re.findall(regex_search, template, flags=re.IGNORECASE) - matches = list(dict.fromkeys(matches)) - if matches: - for i in matches: - search = "\\b" + i + "\\b" - template = re.sub( - search, - html.escape(self.no_bind_text), - template, - flags=re.IGNORECASE, - ) - return template - - def replace_template_strings(self, device, mode, template): - for button, value in self.joystick_listing[device][mode]["Buttons"].items(): - if value == "NO BIND": - value = self.no_bind_text - regex_search = "\\b" + button + "\\b" - template = re.sub(regex_search, html.escape(value), template, flags=re.IGNORECASE) - return template - - def brand_template(self, title, template): - template = re.sub("\\bTEMPLATE_NAME\\b", title, template) - return template - - def date_template(self, template): - template = re.sub("\\bCURRENT_DATE\\b", datetime.now().strftime("%d/%m/%Y"), template) - return template diff --git a/joystick_diagrams/classes/version/__init__.py b/joystick_diagrams/classes/version/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/joystick_diagrams/export.py b/joystick_diagrams/export.py index 2f85e9d..febcf51 100644 --- a/joystick_diagrams/export.py +++ b/joystick_diagrams/export.py @@ -14,6 +14,7 @@ from pathlib import Path from typing import Any +from joystick_diagrams import utils from joystick_diagrams.db.db_device_management import get_device_template_path from joystick_diagrams.input.device import Device_ from joystick_diagrams.input.profile import Profile_ @@ -72,15 +73,9 @@ def export_devices_to_templates(devices: dict[str, dict[str, Any]], profile_name save_template(result, file_name) -def create_directory_if_not_exists(directory: Path): - if directory.exists(): - return - - Path.mkdir(directory) - - def save_template(template_data, file_name): - create_directory_if_not_exists(EXPORT_DIRECTORY) + utils.create_directory(EXPORT_DIRECTORY) + with open(EXPORT_DIRECTORY.joinpath(file_name), "w", encoding="UTF-8") as f: f.write(template_data) diff --git a/joystick_diagrams/functions/__init__.py b/joystick_diagrams/functions/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/joystick_diagrams/functions/helper.py b/joystick_diagrams/utils.py similarity index 100% rename from joystick_diagrams/functions/helper.py rename to joystick_diagrams/utils.py diff --git a/joystick_diagrams/classes/version/version.py b/joystick_diagrams/version.py similarity index 100% rename from joystick_diagrams/classes/version/version.py rename to joystick_diagrams/version.py