Big refactor
This commit is contained in:
62
src/signal_classes/Controller.py
Normal file
62
src/signal_classes/Controller.py
Normal file
@@ -0,0 +1,62 @@
|
||||
# Python imports
|
||||
import threading, signal, inspect, os, time
|
||||
|
||||
|
||||
# Gtk imports
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
from gi.repository import Gtk, GLib
|
||||
|
||||
# Application imports
|
||||
from .mixins import *
|
||||
from . import Controller_Data
|
||||
|
||||
|
||||
|
||||
def threaded(fn):
|
||||
def wrapper(*args, **kwargs):
|
||||
threading.Thread(target=fn, args=args, kwargs=kwargs).start()
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
class Controller(DummyMixin, Controller_Data):
|
||||
def __init__(self, _settings, args, unknownargs):
|
||||
self.setup_controller_data(_settings)
|
||||
self.window.show()
|
||||
self.print_hello_world() # A mixin method from the DummyMixin file
|
||||
|
||||
|
||||
def tear_down(self, widget=None, eve=None):
|
||||
event_system.send_ipc_message("close server")
|
||||
|
||||
time.sleep(event_sleep_time)
|
||||
Gtk.main_quit()
|
||||
|
||||
|
||||
@threaded
|
||||
def gui_event_observer(self):
|
||||
while True:
|
||||
time.sleep(event_sleep_time)
|
||||
event = event_system.consume_gui_event()
|
||||
if event:
|
||||
try:
|
||||
type, target, data = event
|
||||
method = getattr(self.__class__, type)
|
||||
GLib.idle_add(method, (self, data,))
|
||||
except Exception as e:
|
||||
print(repr(e))
|
||||
|
||||
|
||||
|
||||
def get_clipboard_data(self):
|
||||
proc = subprocess.Popen(['xclip','-selection', 'clipboard', '-o'], stdout=subprocess.PIPE)
|
||||
retcode = proc.wait()
|
||||
data = proc.stdout.read()
|
||||
return data.decode("utf-8").strip()
|
||||
|
||||
def set_clipboard_data(self, data):
|
||||
proc = subprocess.Popen(['xclip','-selection','clipboard'], stdin=subprocess.PIPE)
|
||||
proc.stdin.write(data)
|
||||
proc.stdin.close()
|
||||
retcode = proc.wait()
|
||||
26
src/signal_classes/Controller_Data.py
Normal file
26
src/signal_classes/Controller_Data.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# Python imports
|
||||
|
||||
# Lib imports
|
||||
from gi.repository import GLib
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
class Controller_Data:
|
||||
def has_method(self, obj, name):
|
||||
return callable(getattr(obj, name, None))
|
||||
|
||||
def setup_controller_data(self, _settings):
|
||||
self.settings = _settings
|
||||
self.builder = self.settings.get_builder()
|
||||
self.window = self.settings.get_main_window()
|
||||
self.logger = self.settings.get_logger()
|
||||
|
||||
self.home_path = self.settings.get_home_path()
|
||||
self.success_color = self.settings.get_success_color()
|
||||
self.warning_color = self.settings.get_warning_color()
|
||||
self.error_color = self.settings.get_error_color()
|
||||
|
||||
self.window.connect("delete-event", self.tear_down)
|
||||
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, self.tear_down)
|
||||
64
src/signal_classes/IPCServerMixin.py
Normal file
64
src/signal_classes/IPCServerMixin.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# Python imports
|
||||
import threading, socket, time
|
||||
from multiprocessing.connection import Listener, Client
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
def threaded(fn):
|
||||
def wrapper(*args, **kwargs):
|
||||
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=True).start()
|
||||
return wrapper
|
||||
|
||||
|
||||
|
||||
|
||||
class IPCServerMixin:
|
||||
|
||||
@threaded
|
||||
def create_ipc_server(self):
|
||||
listener = Listener((self.ipc_address, self.ipc_port), authkey=self.ipc_authkey)
|
||||
self.is_ipc_alive = True
|
||||
while True:
|
||||
conn = listener.accept()
|
||||
start_time = time.time()
|
||||
|
||||
print(f"New Connection: {listener.last_accepted}")
|
||||
while True:
|
||||
msg = conn.recv()
|
||||
if debug:
|
||||
print(msg)
|
||||
|
||||
if "FILE|" in msg:
|
||||
file = msg.split("FILE|")[1].strip()
|
||||
if file:
|
||||
event_system.push_gui_event(["create_tab_from_ipc", None, file])
|
||||
|
||||
conn.close()
|
||||
break
|
||||
|
||||
|
||||
if msg == 'close connection':
|
||||
conn.close()
|
||||
break
|
||||
if msg == 'close server':
|
||||
conn.close()
|
||||
break
|
||||
|
||||
# NOTE: Not perfect but insures we don't lockup the connection for too long.
|
||||
end_time = time.time()
|
||||
if (end - start) > self.ipc_timeout:
|
||||
conn.close()
|
||||
|
||||
listener.close()
|
||||
|
||||
|
||||
def send_ipc_message(self, message="Empty Data..."):
|
||||
try:
|
||||
conn = Client((self.ipc_address, self.ipc_port), authkey=self.ipc_authkey)
|
||||
conn.send(message)
|
||||
conn.send('close connection')
|
||||
except Exception as e:
|
||||
print(repr(e))
|
||||
@@ -1,35 +0,0 @@
|
||||
# Python imports
|
||||
import threading, subprocess, os
|
||||
|
||||
# Gtk imports
|
||||
|
||||
# Application imports
|
||||
from .mixins import *
|
||||
|
||||
|
||||
def threaded(fn):
|
||||
def wrapper(*args, **kwargs):
|
||||
threading.Thread(target=fn, args=args, kwargs=kwargs).start()
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
class Signals(DummyMixin):
|
||||
def __init__(self, settings):
|
||||
self.settings = settings
|
||||
self.builder = self.settings.returnBuilder()
|
||||
|
||||
hello_world() # A global method from the __builtins__ file that added it
|
||||
|
||||
|
||||
def getClipboardData(self):
|
||||
proc = subprocess.Popen(['xclip','-selection', 'clipboard', '-o'], stdout=subprocess.PIPE)
|
||||
retcode = proc.wait()
|
||||
data = proc.stdout.read()
|
||||
return data.decode("utf-8").strip()
|
||||
|
||||
def setClipboardData(self, data):
|
||||
proc = subprocess.Popen(['xclip','-selection','clipboard'], stdin=subprocess.PIPE)
|
||||
proc.stdin.write(data)
|
||||
proc.stdin.close()
|
||||
retcode = proc.wait()
|
||||
@@ -2,4 +2,6 @@
|
||||
Gtk Bound Signal Module
|
||||
"""
|
||||
from .mixins import *
|
||||
from .Signals import Signals
|
||||
from .IPCServerMixin import IPCServerMixin
|
||||
from .Controller_Data import Controller_Data
|
||||
from .Controller import Controller
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class DummyMixin:
|
||||
"""docstring for DummyMixin"""
|
||||
def printHelloWorld(self):
|
||||
print("Hello World!")
|
||||
def print_hello_world(self):
|
||||
print("Hello, World!")
|
||||
|
||||
Reference in New Issue
Block a user