diff --git a/ks_includes/KlippyGtk.py b/ks_includes/KlippyGtk.py index 74df3649..6ec78227 100644 --- a/ks_includes/KlippyGtk.py +++ b/ks_includes/KlippyGtk.py @@ -2,11 +2,12 @@ import gi import logging import os +import pathlib gi.require_version("Gtk", "3.0") from gi.repository import Gtk, Gdk, GdkPixbuf, Gio, GLib, Pango -klipperscreendir = os.getcwd() +klipperscreendir = pathlib.Path(__file__).parent.resolve().parent class KlippyGtk: labels = {} @@ -110,7 +111,7 @@ class KlippyGtk: def ImageLabel(self, image_name, text, size=20, style=False, width_scale=.32, height_scale=.32): box1 = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=15) pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale( - "%s/styles/%s/images/%s.svg" % (klipperscreendir, self.theme, str(image_name)), + os.path.join(klipperscreendir, "styles", self.theme, "images", str(image_name) + ".svg"), int(round(self.img_width * width_scale)), int(round(self.img_height * height_scale)), True) image = Gtk.Image.new_from_pixbuf(pixbuf) @@ -129,7 +130,7 @@ class KlippyGtk: def ImageMenuButton(self, image_name, text, size=20, style=False, width_scale=.32, height_scale=.32, popover=False): box1 = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=15) pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale( - "%s/styles/%s/images/%s.svg" % (klipperscreendir, self.theme, str(image_name)), + os.path.join(klipperscreendir, "styles", self.theme, "images", str(image_name) + ".svg"), int(round(self.img_width * width_scale)), int(round(self.img_height * height_scale)), True) image = Gtk.Image.new_from_pixbuf(pixbuf) @@ -147,7 +148,7 @@ class KlippyGtk: def Image(self, image_name, style=False, width_scale=1, height_scale=1): pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale( - "%s/styles/%s/images/%s" % (klipperscreendir, self.theme, str(image_name)), + os.path.join(klipperscreendir, "styles", self.theme, "images", str(image_name)), int(round(self.img_width * width_scale)), int(round(self.img_height * height_scale)), True) return Gtk.Image.new_from_pixbuf(pixbuf) @@ -197,10 +198,10 @@ class KlippyGtk: def ButtonImage(self, image_name, label=None, style=None, width_scale=1.38, height_scale=1.38, position=Gtk.PositionType.TOP, word_wrap=True): - filename = "%s/styles/%s/images/%s.svg" % (klipperscreendir, self.theme, str(image_name)) + filename = os.path.join(klipperscreendir, "styles", self.theme, "images", str(image_name) + ".svg") if not os.path.exists(filename): logging.error("Unable to find button image (theme, image): (%s, %s)" % (self.theme, str(image_name))) - filename = "%s/styles/%s/images/%s.svg" % (klipperscreendir, self.theme, "warning") + filename = os.path.join(klipperscreendir, "styles", self.theme, "images", "warning.svg") b = Gtk.Button(label=label) @@ -270,7 +271,7 @@ class KlippyGtk: def ToggleButtonImage(self, image_name, label, style=False, width_scale=1.38, height_scale=1.38): - filename = "%s/styles/%s/images/%s.svg" % (klipperscreendir, self.theme, str(image_name)) + filename = os.path.join(klipperscreendir, "styles", self.theme, "images", str(image_name) + ".svg") pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale( filename, int(round(self.img_width * width_scale)), diff --git a/ks_includes/config.py b/ks_includes/config.py index 99dc6864..a83c0622 100644 --- a/ks_includes/config.py +++ b/ks_includes/config.py @@ -5,6 +5,7 @@ import logging import json import re import copy +import pathlib from io import StringIO @@ -19,6 +20,8 @@ SCREEN_BLANKING_OPTIONS = [ "14400", # 4 Hours ] +klipperscreendir = pathlib.Path(__file__).parent.resolve().parent + class ConfigError(Exception): pass @@ -29,7 +32,7 @@ class KlipperScreenConfig: do_not_edit_prefix = "#~#" def __init__(self, configfile, screen=None): - self.default_config_path = "%s/ks_includes/%s" % (os.getcwd(), "defaults.conf") + self.default_config_path = os.path.join(klipperscreendir, "ks_includes", "defaults.conf") self.config = configparser.ConfigParser() self.config_path = self.get_config_file_location(configfile) logging.debug("Config path location: %s" % self.config_path) @@ -146,7 +149,7 @@ class KlipperScreenConfig: # {"": {"section": "main", "name": _(""), "type": ""}} ] - lang_path = os.path.join(os.getcwd(), 'ks_includes/locales') + lang_path = os.path.join(klipperscreendir, "ks_includes", "locales") langs = [d for d in os.listdir(lang_path) if not os.path.isfile(os.path.join(lang_path, d))] langs.sort() lang_opt = self.configurable_options[3]['language']['options'] @@ -154,7 +157,7 @@ class KlipperScreenConfig: for lang in langs: lang_opt.append({"name": lang, "value": lang}) - t_path = os.path.join(os.getcwd(), 'styles') + t_path = os.path.join(klipperscreendir, 'styles') themes = [d for d in os.listdir(t_path) if (not os.path.isfile(os.path.join(t_path, d)) and d != "z-bolt")] themes.sort() theme_opt = self.configurable_options[8]['theme']['options'] @@ -237,7 +240,7 @@ class KlipperScreenConfig: def get_config_file_location(self, file): logging.info("Passed config file: %s" % file) if not path.exists(file): - file = "%s/%s" % (os.getcwd(), self.configfile_name) + file = os.path.join(klipperscreendir, self.configfile_name) if not path.exists(file): file = os.path.expanduser("~/") + "klipper_config/%s" % (self.configfile_name) if not path.exists(file): diff --git a/screen.py b/screen.py index 3852be8c..c5647819 100644 --- a/screen.py +++ b/screen.py @@ -18,7 +18,7 @@ import signal import subprocess import sys import traceback - +import pathlib gi.require_version("Gtk", "3.0") from gi.repository import Gtk, Gdk, GLib, Pango @@ -55,7 +55,7 @@ PRINTER_BASE_STATUS_OBJECTS = [ 'webhooks' ] -klipperscreendir = os.getcwd() +klipperscreendir = pathlib.Path(__file__).parent.resolve() class KlipperScreen(Gtk.Window): """ Class for creating a screen for Klipper via HDMI """ @@ -440,21 +440,21 @@ class KlipperScreen(Gtk.Window): def init_style(self): style_provider = Gtk.CssProvider() - - css = open(klipperscreendir + "/styles/base.css") + css = open(os.path.join(klipperscreendir, "styles", "base.css")) css_base_data = css.read() css.close() - css = open(klipperscreendir + "/styles/%s/style.css" % (self.theme)) + css = open(os.path.join(klipperscreendir, "styles", self.theme, "style.css")) css_data = css_base_data + css.read() css.close() - f = open(klipperscreendir + "/styles/base.conf") + f = open(os.path.join(klipperscreendir, "styles", "base.conf")) style_options = json.load(f) f.close() - if os.path.exists(klipperscreendir + "/styles/%s/style.conf" % (self.theme)): + theme_style_conf = os.path.join(klipperscreendir, "styles", self.theme, "style.conf") + if os.path.exists(theme_style_conf): try: - f = open(klipperscreendir + "/styles/%s/style.conf" % (self.theme)) + f = open(theme_style_conf) style_options.update(json.load(f)) f.close() except Exception: