import refactoring

This commit is contained in:
2022-02-25 17:53:58 -06:00
parent b4564c2540
commit f3b222ec1b
18 changed files with 214 additions and 108 deletions

3
src/context/__init__.py Normal file
View File

@@ -0,0 +1,3 @@
"""
Gtk Bound Signal Module
"""

64
src/context/controller.py Normal file
View File

@@ -0,0 +1,64 @@
# 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.dummy_mixin import DummyMixin
from .controller_data 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()

View File

@@ -0,0 +1,59 @@
# Python imports
import os, signal
# Lib imports
from gi.repository import GLib
# Application imports
from plugins.plugins import Plugins
class Controller_Data:
''' Controller_Data contains most of the state of the app at ay given time. It also has some support methods. '''
def setup_controller_data(self, _settings):
self.plugins = Plugins(_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)
def clear_console(self):
''' Clears the terminal screen. '''
os.system('cls' if os.name == 'nt' else 'clear')
def call_method(self, _method_name, data = None):
'''
Calls a method from scope of class.
Parameters:
a (obj): self
b (str): method name to be called
c (*): Data (if any) to be passed to the method.
Note: It must be structured according to the given methods requirements.
Returns:
Return data is that which the calling method gives.
'''
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):
''' Checks if a given method exists. '''
return callable(getattr(obj, name, None))
def clear_children(self, widget):
''' Clear children of a gtk widget. '''
for child in widget.get_children():
widget.remove(child)

View File

@@ -0,0 +1,3 @@
"""
Generic Mixins Module
"""

View File

@@ -0,0 +1,4 @@
class DummyMixin:
""" DummyMixin is an example of how mixins are used and structured in a project. """
def print_hello_world(self):
print("Hello, World!")