Added Singleton class to inherit as needed

This commit is contained in:
2023-03-27 20:07:17 -05:00
parent 4b69622cc6
commit ca61d5348f
46 changed files with 6455 additions and 97 deletions

Binary file not shown.

View File

@@ -56,11 +56,12 @@ class Controller(UIMixin, SignalsMixins, Controller_Data):
def _setup_styling(self):
...
def _setup_signals(self):
self.window.connect("focus-out-event", self.unset_keys_and_data)
self.window.connect("key-press-event", self.on_global_key_press_controller)
self.window.connect("key-release-event", self.on_global_key_release_controller)
def _setup_signals(self):
FileSystemActions()
def _subscribe_to_events(self):

View File

@@ -10,6 +10,7 @@ from gi.repository import Gtk
# Application imports
from .widgets.dialogs.message_widget import MessageWidget
from .widgets.dialogs.user_pass_widget import UserPassWidget
from shellfm.windows.controller import WindowController
from plugins.plugins_controller import PluginsController
@@ -30,7 +31,8 @@ class State:
selected_files: [] = None
to_copy_files: [] = None
to_cut_files: [] = None
message_dialog: type = None
message_dialog: type = None
user_pass_dialog: type = None
class Controller_Data:
@@ -81,17 +83,18 @@ class Controller_Data:
Returns:
state (obj): State
'''
state = State()
state.fm_controller = self.fm_controller
state.notebooks = self.notebooks
state.wid, state.tid = self.fm_controller.get_active_wid_and_tid()
state.tab = self.get_fm_window(state.wid).get_tab_by_id(state.tid)
state.icon_grid = self.builder.get_object(f"{state.wid}|{state.tid}|icon_grid")
# state.icon_grid = event_system.emit_and_await("get_files_view_icon_grid", (state.wid, state.tid))
state.store = state.icon_grid.get_model()
state.message_dialog = MessageWidget()
state = State()
state.fm_controller = self.fm_controller
state.notebooks = self.notebooks
state.wid, state.tid = self.fm_controller.get_active_wid_and_tid()
state.tab = self.get_fm_window(state.wid).get_tab_by_id(state.tid)
state.icon_grid = self.builder.get_object(f"{state.wid}|{state.tid}|icon_grid")
# state.icon_grid = event_system.emit_and_await("get_files_view_icon_grid", (state.wid, state.tid))
state.store = state.icon_grid.get_model()
state.message_dialog = MessageWidget()
state.user_pass_dialog = UserPassWidget()
selected_files = state.icon_grid.get_selected_items()
selected_files = state.icon_grid.get_selected_items()
if selected_files:
state.uris = self.format_to_uris(state.store, state.wid, state.tid, selected_files, True)
state.uris_raw = self.format_to_uris(state.store, state.wid, state.tid, selected_files)

View File

@@ -36,18 +36,18 @@ class FileActionSignalsMixin:
# NOTE: Too lazy to impliment a proper update handler and so just regen store and update tab.
# Use a lock system to prevent too many update calls for certain instances but user can manually refresh if they have urgency
def dir_watch_updates(self, file_monitor, file, other_file=None, eve_type=None, data=None):
if eve_type in [Gio.FileMonitorEvent.CREATED, Gio.FileMonitorEvent.DELETED,
Gio.FileMonitorEvent.RENAMED, Gio.FileMonitorEvent.MOVED_IN,
Gio.FileMonitorEvent.MOVED_OUT]:
if settings.is_debug():
if eve_type == Gio.FileMonitorEvent.CHANGES_DONE_HINT:
if eve_type in [Gio.FileMonitorEvent.CREATED, Gio.FileMonitorEvent.DELETED,
Gio.FileMonitorEvent.RENAMED, Gio.FileMonitorEvent.MOVED_IN,
Gio.FileMonitorEvent.MOVED_OUT]:
logger.debug(eve_type)
if eve_type in [Gio.FileMonitorEvent.MOVED_IN, Gio.FileMonitorEvent.MOVED_OUT]:
self.update_on_soft_lock_end(data[0])
elif data[0] in self.soft_update_lock.keys():
self.soft_update_lock[data[0]]["last_update_time"] = time.time()
else:
self.soft_lock_countdown(data[0])
if eve_type in [Gio.FileMonitorEvent.MOVED_IN, Gio.FileMonitorEvent.MOVED_OUT]:
self.update_on_soft_lock_end(data[0])
elif data[0] in self.soft_update_lock.keys():
self.soft_update_lock[data[0]]["last_update_time"] = time.time()
else:
self.soft_lock_countdown(data[0])
@threaded
def soft_lock_countdown(self, tab_widget):

View File

@@ -54,6 +54,9 @@ class TabMixin(GridMixin):
def close_tab(self, button, eve=None):
notebook = button.get_parent().get_parent()
if notebook.get_n_pages() == 1:
return
wid = int(notebook.get_name()[-1])
tid = self.get_id_from_tab_box(button.get_parent())
scroll = self.builder.get_object(f"{wid}|{tid}")

View File

@@ -0,0 +1,63 @@
# Python imports
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
# Application imports
class UserPassWidget(Gtk.Dialog):
"""docstring for UserPassWidget."""
def __init__(self):
super(UserPassWidget, self).__init__()
self.user_input = Gtk.Entry()
self.pass_input = Gtk.Entry()
self._setup_styling()
self._setup_signals()
self._subscribe_to_events()
self._load_widgets()
def _setup_styling(self):
self.set_modal(True)
self.set_type_hint(Gdk.WindowTypeHint.DIALOG)
self.user_input.set_placeholder_text("User:")
self.pass_input.set_placeholder_text("Password:")
self.pass_input.set_visibility(False)
def _setup_signals(self):
# self.connect("response", self.on_response)
...
def _subscribe_to_events(self):
...
def _load_widgets(self):
vbox = self.get_content_area()
label = Gtk.Label(label="User & Password")
vbox.add(label)
vbox.add(self.user_input)
vbox.add(self.pass_input)
vbox.show_all()
self.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
"Skip Input", Gtk.ResponseType.CLOSE,
"Submit Input", Gtk.ResponseType.OK
)
def do_response(self, response_id):
self.hide()
return response_id
# def on_response(self, widget, response_id):
# self.hide()
# return response_id

View File

@@ -52,10 +52,11 @@ class TabMixin(GridMixin):
self.set_file_watcher(tab)
def close_tab(self, button, eve=None):
notebook = button.get_parent().get_parent()
if notebook.get_n_pages() == 1:
return
wid = int(notebook.get_name()[-1])
tid = self.get_id_from_tab_box(button.get_parent())
scroll = self.builder.get_object(f"{wid}|{tid}")

View File

@@ -10,7 +10,7 @@ from gi.repository import Gtk
class TabHeaderWidget(Gtk.ButtonBox):
class TabHeaderWidget(Gtk.Box):
"""docstring for TabHeaderWidget"""
def __init__(self, tab, close_tab):
@@ -26,6 +26,7 @@ class TabHeaderWidget(Gtk.ButtonBox):
def _setup_styling(self):
self.set_orientation(0)
self.set_hexpand(False)
def _setup_signals(self):
...
@@ -39,6 +40,9 @@ class TabHeaderWidget(Gtk.ButtonBox):
label.set_label(f"{self._tab.get_end_of_path()}")
label.set_width_chars(len(self._tab.get_end_of_path()))
label.set_xalign(0.0)
label.set_margin_left(25)
label.set_margin_right(25)
label.set_hexpand(True)
tid.set_label(f"{self._tab.get_id()}")
close.connect("released", self._close_tab)

View File

@@ -10,6 +10,7 @@ from os.path import isdir
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GLib
from gi.repository import Gio
# Application imports
@@ -52,6 +53,7 @@ class PluginsController:
Gio.FileMonitorEvent.MOVED_OUT]:
self.reload_plugins(file)
@daemon_threaded
def load_plugins(self, file: str = None) -> None:
logger.info(f"Loading plugins...")
parent_path = os.getcwd()
@@ -66,7 +68,9 @@ class PluginsController:
plugin, loading_data = manifest.get_loading_data()
module = self.load_plugin_module(path, folder, target)
self.execute_plugin(module, plugin, loading_data)
GLib.idle_add(self.execute_plugin, *(module, plugin, loading_data))
# self.execute_plugin(module, plugin, loading_data)
except InvalidPluginException as e:
logger.info(f"Malformed Plugin: Not loading -->: '{folder}' !")
logger.debug("Trace: ", traceback.print_exc())

View File

@@ -49,6 +49,8 @@ class Icon(DesktopIconMixin, VideoIconMixin, MeshsIconMixin):
thumbnl = self.find_thumbnail_from_desktop_file(full_path)
if not thumbnl:
# TODO: Detect if not in a thread and use directly for speed get_system_thumbnail
# thumbnl = self.get_system_thumbnail(full_path, full_path, self.sys_icon_wh[0])
thumbnl = self._get_system_thumbnail_gtk_thread(full_path, self.sys_icon_wh[0])
if not thumbnl:
raise IconException("No known icons found.")
@@ -110,7 +112,13 @@ class Icon(DesktopIconMixin, VideoIconMixin, MeshsIconMixin):
elif full_path.lower().endswith(".webp") and PImage:
return self.image2pixbuf(full_path, wxh)
return GdkPixbuf.Pixbuf.new_from_file_at_scale(full_path, wxh[0], wxh[1], True)
pixbuf = None
try:
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(full_path, wxh[0], wxh[1], True)
except Exception as e:
...
return pixbuf
except IconException as e:
print("Image Scaling Issue:")
print( repr(e) )

View File

@@ -3,11 +3,11 @@
# Lib imports
# Application imports
from .singleton import Singleton
class EndpointRegistry():
class EndpointRegistry(Singleton):
def __init__(self):
self._endpoints = {}

View File

@@ -4,11 +4,11 @@ from collections import defaultdict
# Lib imports
# Application imports
from .singleton import Singleton
class EventSystem:
class EventSystem(Singleton):
""" Create event system. """
def __init__(self):

View File

@@ -8,11 +8,11 @@ from multiprocessing.connection import Listener
from gi.repository import GLib
# Application imports
from .singleton import Singleton
class IPCServer:
class IPCServer(Singleton):
""" Create a listener so that other SolarFM instances send requests back to existing instance. """
def __init__(self, ipc_address: str = '127.0.0.1', conn_type: str = "socket"):
self.is_ipc_alive = False

View File

@@ -7,20 +7,20 @@ gi.require_version('Gdk', '3.0')
from gi.repository import Gdk
# Application imports
from .singleton import Singleton
def err(log = ""):
"""Print an error message"""
def logger(log = ""):
print(log)
class KeymapError(Exception):
"""Custom exception for errors in keybinding configurations"""
""" Custom exception for errors in keybinding configurations """
MODIFIER = re.compile('<([^<]+)>')
class Keybindings:
"""Class to handle loading and lookup of Terminator keybindings"""
class Keybindings(Singleton):
""" Class to handle loading and lookup of Terminator keybindings """
modifiers = {
'ctrl': Gdk.ModifierType.CONTROL_MASK,
@@ -46,7 +46,7 @@ class Keybindings:
print(self.keys)
def append_bindings(self, combos):
"""Accept new binding(s) and reload"""
""" Accept new binding(s) and reload """
for item in combos:
method, keys = item.split(":")
self.keys[method] = keys
@@ -54,12 +54,12 @@ class Keybindings:
self.reload()
def configure(self, bindings):
"""Accept new bindings and reconfigure with them"""
""" Accept new bindings and reconfigure with them """
self.keys = bindings
self.reload()
def reload(self):
"""Parse bindings and mangle into an appropriate form"""
""" Parse bindings and mangle into an appropriate form """
self._lookup = {}
self._masks = 0
@@ -77,9 +77,9 @@ class Keybindings:
try:
keyval, mask = self._parsebinding(binding)
# Does much the same, but with poorer error handling.
#keyval, mask = Gtk.accelerator_parse(binding)
# keyval, mask = Gtk.accelerator_parse(binding)
except KeymapError as e:
err ("keybinding reload failed to parse binding '%s': %s" % (binding, e))
logger(f"Keybinding reload failed to parse binding '{binding}': {e}")
else:
if mask & Gdk.ModifierType.SHIFT_MASK:
if keyval == Gdk.KEY_Tab:
@@ -98,7 +98,7 @@ class Keybindings:
self._masks |= mask
def _parsebinding(self, binding):
"""Parse an individual binding using Gtk's binding function"""
""" Parse an individual binding using Gtk's binding function """
mask = 0
modifiers = re.findall(MODIFIER, binding)
@@ -113,25 +113,25 @@ class Keybindings:
keyval = Gdk.keyval_from_name(key)
if keyval == 0:
raise KeymapError("Key '%s' is unrecognised..." % key)
raise KeymapError(f"Key '{key}' is unrecognised...")
return (keyval, mask)
def _lookup_modifier(self, modifier):
"""Map modifier names to gtk values"""
""" Map modifier names to gtk values """
try:
return self.modifiers[modifier.lower()]
except KeyError:
raise KeymapError("Unhandled modifier '<%s>'" % modifier)
raise KeymapError(f"Unhandled modifier '<{modifier}>'")
def lookup(self, event):
"""Translate a keyboard event into a mapped key"""
""" Translate a keyboard event into a mapped key """
try:
_found, keyval, _egp, _lvl, consumed = self.keymap.translate_keyboard_state(
event.hardware_keycode,
Gdk.ModifierType(event.get_state() & ~Gdk.ModifierType.LOCK_MASK),
event.group)
except TypeError:
err ("Keybinding lookup failed to translate keyboard event: %s" % dir(event))
logger("Keybinding lookup failed to translate keyboard event: {dir(event)}")
return None
mask = (event.get_state() & ~consumed) & self._masks

View File

@@ -3,9 +3,11 @@ import os
import logging
# Application imports
from .singleton import Singleton
class Logger:
class Logger(Singleton):
"""
Create a new logging object and return it.
:note:

View File

@@ -11,11 +11,12 @@ from gi.repository import Gtk
from gi.repository import GLib
# Application imports
from ..singleton import Singleton
from .start_check_mixin import StartCheckMixin
class Settings(StartCheckMixin):
class Settings(StartCheckMixin, Singleton):
def __init__(self):
self._SCRIPT_PTH = os.path.dirname(os.path.realpath(__file__))
self._USER_HOME = path.expanduser('~')
@@ -31,6 +32,12 @@ class Settings(StartCheckMixin):
self._KEY_BINDINGS_FILE = f"{self._HOME_CONFIG_PATH}/key-bindings.json"
self._PID_FILE = f"{self._HOME_CONFIG_PATH}/{app_name.lower()}.pid"
self._WINDOW_ICON = f"{self._DEFAULT_ICONS}/icons/{app_name.lower()}.png"
self._UI_WIDEGTS_PATH = f"{self._HOME_CONFIG_PATH}/ui_widgets"
self._CONTEXT_MENU = f"{self._HOME_CONFIG_PATH}/contexct_menu.json"
self._TRASH_FILES_PATH = f"{GLib.get_user_data_dir()}/Trash/files"
self._TRASH_INFO_PATH = f"{GLib.get_user_data_dir()}/Trash/info"
self._ICON_THEME = Gtk.IconTheme.get_default()
if not os.path.exists(self._HOME_CONFIG_PATH):
os.mkdir(self._HOME_CONFIG_PATH)
@@ -64,23 +71,25 @@ class Settings(StartCheckMixin):
self._WINDOW_ICON = f"{self._USR_PATH}/icons/{app_name.lower()}.png"
if not os.path.exists(self._WINDOW_ICON):
raise MissingConfigError("Unable to find the application icon.")
self._UI_WIDEGTS_PATH = f"{self._USR_PATH}/ui_widgets"
self._CONTEXT_MENU = f"{self._USR_PATH}/contexct_menu.json"
self._TRASH_FILES_PATH = f"{GLib.get_user_data_dir()}/Trash/files"
self._TRASH_INFO_PATH = f"{GLib.get_user_data_dir()}/Trash/info"
self._ICON_THEME = Gtk.IconTheme.get_default()
if not os.path.exists(self._UI_WIDEGTS_PATH):
self._UI_WIDEGTS_PATH = f"{self._USR_PATH}/ui_widgets"
if not os.path.exists(self._CONTEXT_MENU):
self._CONTEXT_MENU = f"{self._USR_PATH}/contexct_menu.json"
with open(self._KEY_BINDINGS_FILE) as file:
bindings = json.load(file)["keybindings"]
keybindings.configure(bindings)
with open(self._CONTEXT_MENU) as file:
self._context_menu_data = json.load(file)
try:
with open(self._KEY_BINDINGS_FILE) as file:
bindings = json.load(file)["keybindings"]
keybindings.configure(bindings)
except Exception as e:
print( f"Settings: {self._KEY_BINDINGS_FILE}\n\t\t{repr(e)}" )
try:
with open(self._CONTEXT_MENU) as file:
self._context_menu_data = json.load(file)
except Exception as e:
print( f"Settings: {self._CONTEXT_MENU}\n\t\t{repr(e)}" )
self._main_window = None
self._main_window_w = 1670
@@ -119,8 +128,8 @@ class Settings(StartCheckMixin):
def set_builder(self, builder) -> any: self._builder = builder
def set_main_window(self, window): self._main_window = window
def get_main_window(self) -> Gtk.ApplicationWindow: return self._main_window
def get_main_window_width(self) -> Gtk.ApplicationWindow: return self._main_window_w
def get_main_window(self) -> Gtk.ApplicationWindow: return self._main_window
def get_main_window_width(self) -> Gtk.ApplicationWindow: return self._main_window_w
def get_main_window_height(self) -> Gtk.ApplicationWindow: return self._main_window_h
def get_builder(self) -> Gtk.Builder: return self._builder
def get_glade_file(self) -> str: return self._GLADE_FILE

View File

@@ -0,0 +1,23 @@
# Python imports
# Lib imports
# Application imports
class SingletonError(Exception):
pass
class Singleton:
ccount = 0
def __new__(cls, *args, **kwargs):
obj = super(Singleton, cls).__new__(cls)
cls.ccount += 1
if cls.ccount == 2:
raise SingletonError(f"Exceeded {cls.__name__} instantiation limit...")
return obj