rename folder
This commit is contained in:
66
src/controller/Controller.py
Normal file
66
src/controller/Controller.py
Normal file
@@ -0,0 +1,66 @@
|
||||
# Python imports
|
||||
import threading, subprocess, 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__, target)
|
||||
GLib.idle_add(method, *(self, data,))
|
||||
except Exception as e:
|
||||
print(repr(e))
|
||||
|
||||
|
||||
|
||||
def handle_file_from_ipc(self, path):
|
||||
print(f"Path From IPC: {path}")
|
||||
|
||||
|
||||
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()
|
35
src/controller/Controller_Data.py
Normal file
35
src/controller/Controller_Data.py
Normal file
@@ -0,0 +1,35 @@
|
||||
# Python imports
|
||||
import os, signal
|
||||
|
||||
# Lib imports
|
||||
from gi.repository import GLib
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
class Controller_Data:
|
||||
def clear_console(self):
|
||||
os.system('cls' if os.name == 'nt' else 'clear')
|
||||
|
||||
def call_method(self, _method_name, data = None):
|
||||
method_name = str(_method_name)
|
||||
method = getattr(self, method_name, lambda data: f"No valid key passed...\nkey={method_name}\nargs={data}")
|
||||
return method(data) if data else method()
|
||||
|
||||
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/controller/IPCServerMixin.py
Normal file
64
src/controller/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([None, "handle_file_from_ipc", 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))
|
7
src/controller/__init__.py
Normal file
7
src/controller/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
"""
|
||||
Gtk Bound Signal Module
|
||||
"""
|
||||
from .mixins import *
|
||||
from .IPCServerMixin import IPCServerMixin
|
||||
from .Controller_Data import Controller_Data
|
||||
from .Controller import Controller
|
4
src/controller/mixins/DummyMixin.py
Normal file
4
src/controller/mixins/DummyMixin.py
Normal file
@@ -0,0 +1,4 @@
|
||||
class DummyMixin:
|
||||
"""docstring for DummyMixin"""
|
||||
def print_hello_world(self):
|
||||
print("Hello, World!")
|
1
src/controller/mixins/__init__.py
Normal file
1
src/controller/mixins/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .DummyMixin import DummyMixin
|
Reference in New Issue
Block a user