improved pid usage logic; stopped inhertance of IPC in App class

This commit is contained in:
itdominator 2024-01-08 19:32:53 -06:00
parent d49bcd8beb
commit d4a38c3e14
5 changed files with 105 additions and 18 deletions

View File

@ -17,7 +17,6 @@ from app import Application
if __name__ == "__main__":
''' Set process title, get arguments, and create GTK main thread. '''

View File

@ -16,34 +16,38 @@ class AppLaunchException(Exception):
class Application(IPCServer):
class Application:
""" docstring for Application. """
def __init__(self, args, unknownargs):
super(Application, self).__init__()
if not settings_manager.is_trace_debug():
self.socket_realization_check()
if not self.is_ipc_alive:
for arg in unknownargs + [args.new_tab,]:
if os.path.isfile(arg):
message = f"FILE|{arg}"
self.send_ipc_message(message)
raise AppLaunchException(f"{app_name} IPC Server Exists: Have sent path(s) to it and closing...")
self.load_ipc(args, unknownargs)
self.setup_debug_hook()
Window(args, unknownargs).main()
def socket_realization_check(self):
def load_ipc(self, args, unknownargs):
ipc_server = IPCServer()
self.ipc_realization_check(ipc_server)
if not ipc_server.is_ipc_alive:
for arg in unknownargs + [args.new_tab,]:
if os.path.isfile(arg):
message = f"FILE|{arg}"
ipc_server.send_ipc_message(message)
raise AppLaunchException(f"{app_name} IPC Server Exists: Have sent path(s) to it and closing...")
def ipc_realization_check(self, ipc_server):
try:
self.create_ipc_listener()
ipc_server.create_ipc_listener()
except Exception:
self.send_test_ipc_message()
ipc_server.send_test_ipc_message()
try:
self.create_ipc_listener()
ipc_server.create_ipc_listener()
except Exception as e:
...

View File

@ -0,0 +1,3 @@
"""
Utils/Mixins module
"""

View File

@ -0,0 +1,70 @@
# Python imports
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Gio
# Application imports
class DnDMixin:
def _setup_dnd(self):
# flags = Gtk.DestDefaults.ALL
PLAIN_TEXT_TARGET_TYPE = 70
URI_TARGET_TYPE = 80
text_target = Gtk.TargetEntry.new('text/plain', Gtk.TargetFlags(0), PLAIN_TEXT_TARGET_TYPE)
uri_target = Gtk.TargetEntry.new('text/uri-list', Gtk.TargetFlags(0), URI_TARGET_TYPE)
targets = [ text_target, uri_target ]
# action = Gdk.DragAction.COPY
self.drag_dest_set_target_list(targets)
# self.drag_dest_set(flags, targets, action)
self._setup_dnd_signals()
def _setup_dnd_signals(self):
self.connect("drag-motion", self._on_drag_motion)
self.connect('drag-drop', self._on_drag_set)
self.connect("drag-data-received", self._on_drag_data_received)
def _on_drag_motion(self, widget, drag_context, x, y, time):
Gdk.drag_status(drag_context, drag_context.get_actions(), time)
return True
def _on_drag_set(self, widget, drag_context, data, info, time):
self.drag_get_data(drag_context, drag_context.list_targets()[-1], time)
return True
def _on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
if info == 70: return
if info == 80:
uris = data.get_uris()
files = []
if len(uris) == 0:
uris = data.get_text().split("\n")
for uri in uris:
gfile = None
try:
gfile = Gio.File.new_for_uri(uri)
except Exception as e:
gfile = Gio.File.new_for_path(uri)
files.append(gfile)
event_system.emit('set_pre_drop_dnd', (files,))
drag_context.finish(True, False, time)

View File

@ -12,9 +12,17 @@ import inspect
class StartCheckMixin:
def is_dirty_start(self) -> bool: return self._dirty_start
def clear_pid(self): self._clean_pid()
def clear_pid(self):
if not self.is_trace_debug():
self._clean_pid()
def do_dirty_start_check(self):
if self.is_trace_debug():
pid = os.getpid()
self._print_pid(pid)
return
if not os.path.exists(self._PID_FILE):
self._write_new_pid()
else:
@ -31,7 +39,7 @@ class StartCheckMixin:
try:
os.kill(pid, 0)
except OSError:
print(f"{app_name} is starting dirty...")
print(f"{app_name} PID exists but is not up; starting dirty...")
self._dirty_start = True
self._write_new_pid()
return
@ -41,6 +49,9 @@ class StartCheckMixin:
def _write_new_pid(self):
pid = os.getpid()
self._write_pid(pid)
self._print_pid(pid)
def _print_pid(self, pid):
print(f"{app_name} PID: {pid}")
def _clean_pid(self):
@ -48,4 +59,4 @@ class StartCheckMixin:
def _write_pid(self, pid):
with open(self._PID_FILE, "w") as _pid:
_pid.write(f"{pid}")
_pid.write(f"{pid}")