refactored import, changed ipc structure

This commit is contained in:
itdominator 2022-03-03 16:32:06 -06:00
parent 891b489107
commit d300033d20
34 changed files with 148 additions and 105 deletions

View File

@ -6,14 +6,15 @@ import builtins
# Lib imports # Lib imports
# Application imports # Application imports
from controller import IPCServerMixin from ipc_server import IPCServer
class Builtins(IPCServerMixin): class EventSystem(IPCServer):
"""Docstring for __builtins__ extender""" """Docstring for __builtins__ extender"""
def __init__(self): def __init__(self):
super(EventSystem, self).__init__()
# NOTE: The format used is list of [type, target, data] Where: # NOTE: The format used is list of [type, target, data] Where:
# type is useful context for control flow, # type is useful context for control flow,
# target is the method to call, # target is the method to call,
@ -21,11 +22,7 @@ class Builtins(IPCServerMixin):
# Where data may be any kind of data # Where data may be any kind of data
self._gui_events = [] self._gui_events = []
self._module_events = [] self._module_events = []
self.is_ipc_alive = False
self.ipc_authkey = b'mirage-ipc'
self.ipc_address = '127.0.0.1'
self.ipc_port = 8877
self.ipc_timeout = 15.0
# Makeshift fake "events" type system FIFO # Makeshift fake "events" type system FIFO
def _pop_gui_event(self): def _pop_gui_event(self):
@ -70,7 +67,7 @@ class Builtins(IPCServerMixin):
# NOTE: Just reminding myself we can add to builtins two different ways... # NOTE: Just reminding myself we can add to builtins two different ways...
# __builtins__.update({"event_system": Builtins()}) # __builtins__.update({"event_system": Builtins()})
builtins.app_name = "Mirage2" builtins.app_name = "Mirage2"
builtins.event_system = Builtins() builtins.event_system = EventSystem()
builtins.event_sleep_time = 0.2 builtins.event_sleep_time = 0.2
builtins.debug = False builtins.debug = False
builtins.trace_debug = False builtins.trace_debug = False

View File

@ -1,51 +1,3 @@
# Python imports """
import os, inspect, time Base module
"""
# Lib imports
# Application imports
from utils import Settings
from controller import Controller
from __builtins__ import Builtins
class Main(Builtins):
def __init__(self, args, unknownargs):
if not debug:
event_system.create_ipc_server()
# NOTE: Keeping here just incase I change my mind...
# time.sleep(0.2)
# if not trace_debug:
# if not event_system.is_ipc_alive:
# if unknownargs:
# for arg in unknownargs:
# if os.path.isdir(arg):
# message = f"FILE|{arg}"
# event_system.send_ipc_message(message)
#
# raise Exception("IPC Server Exists: Will send data to it and close...")
settings = Settings()
settings.create_window()
controller = Controller(settings, args, unknownargs)
if not controller:
raise Exception("Controller exited and doesn't exist...")
# Gets the methods from the classes and sets to handler.
# Then, builder from settings will connect to any signals it needs.
classes = [controller]
handlers = {}
for c in classes:
methods = None
try:
methods = inspect.getmembers(c, predicate=inspect.ismethod)
handlers.update(methods)
except Exception as e:
print(repr(e))
settings.get_builder().connect_signals(handlers)

View File

@ -15,7 +15,7 @@ gi.require_version('Gtk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk
# Application imports # Application imports
from __init__ import Main from app import Application
if __name__ == "__main__": if __name__ == "__main__":
@ -28,13 +28,13 @@ if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
# Add long and short arguments # Add long and short arguments
parser.add_argument("--file", "-f", default=None, help="JOpen an image.") parser.add_argument("--file", "-f", default=None, help="Open an image.")
parser.add_argument("--dir", "-d", default=None, help="load a dir with images.") parser.add_argument("--dir", "-d", default=None, help="Load a dir with images.")
# Read arguments (If any...) # Read arguments (If any...)
args, unknownargs = parser.parse_known_args() args, unknownargs = parser.parse_known_args()
Main(args, unknownargs) Application(args, unknownargs)
Gtk.main() Gtk.main()
except Exception as e: except Exception as e:
traceback.print_exc() traceback.print_exc()

51
src/app.py Normal file
View File

@ -0,0 +1,51 @@
# Python imports
import os, inspect, time
# Lib imports
# Application imports
from utils.settings import Settings
from context.controller import Controller
from __builtins__ import EventSystem
class Application(EventSystem):
def __init__(self, args, unknownargs):
if not debug:
event_system.create_ipc_server()
# NOTE: Keeping here just in case I change my mind...
# time.sleep(0.2)
# if not trace_debug:
# if not event_system.is_ipc_alive:
# if unknownargs:
# for arg in unknownargs:
# if os.path.isdir(arg):
# message = f"FILE|{arg}"
# event_system.send_ipc_message(message)
#
# raise Exception("IPC Server Exists: Will send data to it and close...")
settings = Settings()
settings.create_window()
controller = Controller(settings, args, unknownargs)
if not controller:
raise Exception("Controller exited and doesn't exist...")
# Gets the methods from the classes and sets to handler.
# Then, builder from settings will connect to any signals it needs.
classes = [controller]
handlers = {}
for c in classes:
methods = None
try:
methods = inspect.getmembers(c, predicate=inspect.ismethod)
handlers.update(methods)
except Exception as e:
print(repr(e))
settings.get_builder().connect_signals(handlers)

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

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

View File

@ -9,9 +9,8 @@ gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib, GdkPixbuf from gi.repository import Gtk, GLib, GdkPixbuf
# Application imports # Application imports
from .mixins import * from .controller_data import Controller_Data
from . import Controller_Data from .mixins.tree_view_update_mixin import TreeViewUpdateMixin
def threaded(fn): def threaded(fn):
@ -51,6 +50,7 @@ class Controller(TreeViewUpdateMixin, Controller_Data):
GLib.idle_add(method, *(self, type, target, data)) GLib.idle_add(method, *(self, type, target, data))
except Exception as e: except Exception as e:
print(repr(e)) print(repr(e))
self.logger.debug(e)
def hadle_gui_event_and_call_back(self, type, target, parameters): def hadle_gui_event_and_call_back(self, type, target, parameters):
method = getattr(self.__class__, target) method = getattr(self.__class__, target)
@ -59,25 +59,24 @@ class Controller(TreeViewUpdateMixin, Controller_Data):
def handle_args(self, args=None, unknownargs=None): def handle_args(self, args=None, unknownargs=None):
print(f"Args: {args}")
print(f"Unknownargs: {unknownargs}")
if args.dir and os.path.isdir(args.dir): if args.dir and os.path.isdir(args.dir):
self.load_store(self.view, self.thumbnail_store, arg) self.load_store(self.view, self.thumbnail_store, arg)
if args.file and os.path.isfile(args.file): if args.file and os.path.isfile(args.file):
path = "/" + '/'.join(rgs.file.split("/")[:-1]) path = '/'.join(rgs.file.split("/")[:-1])
self.load_store(self.view, self.thumbnail_store, path) self.load_store(self.view, self.thumbnail_store, path)
image = Gtk.Image.new_from_pixbuf(self._get_pixbuf(args.file)) self.process_path(args.file)
self._load_image(image)
if unknownargs: if unknownargs:
for arg in unknownargs: for arg in unknownargs:
if os.path.isdir(arg): if os.path.isdir(arg):
self.load_store(self.view, self.thumbnail_store, arg) self.load_store(self.view, self.thumbnail_store, arg)
elif os.path.isfile(arg): elif os.path.isfile(arg):
path = "/" + '/'.join(arg.split("/")[:-1]) path = '/'.join(arg.split("/")[:-1])
self.load_store(self.view, self.thumbnail_store, path) self.load_store(self.view, self.thumbnail_store, path)
image = Gtk.Image.new_from_pixbuf(self._get_pixbuf(arg)) self.process_path(arg)
self._load_image(image)
def _on_drag_data_received(self, widget, drag_context, x, y, data, info, time): def _on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
@ -97,21 +96,35 @@ class Controller(TreeViewUpdateMixin, Controller_Data):
def load_image_from_treeview(self, widget): def load_image_from_treeview(self, widget):
store, iter = widget.get_selection().get_selected() store, iter = widget.get_selection().get_selected()
uri = store.get_value(iter, 1) uri = store.get_value(iter, 1)
image = Gtk.Image.new_from_pixbuf(self._get_pixbuf(uri))
self._load_image(image) if uri == self.current_img_uri:
return
self.process_path(uri)
def process_path(self, uri):
self.current_img_uri = uri
self.current_path_label.set_label(uri)
if not uri.endswith(".gif"):
self.is_img_gif = False
self.current_img = Gtk.Image.new_from_pixbuf(self._get_pixbuf(uri))
self._load_image()
else:
self.is_img_gif = True
self.current_img = Gtk.Image.new_from_file(uri)
self.gif_animation = self.current_img.get_animation()
self._load_image()
def _get_pixbuf(self, uri): def _get_pixbuf(self, uri):
self.current_path_label.set_label(uri)
self.current_img = uri
geom_rec = self.image_area.get_parent().get_parent().get_allocated_size()[0] geom_rec = self.image_area.get_parent().get_parent().get_allocated_size()[0]
width = geom_rec.width - 15 width = geom_rec.width - 15
height = geom_rec.height - 15 height = geom_rec.height - 15
self.image_area.set_size_request(width, height) self.image_area.set_size_request(width, height)
return GdkPixbuf.Pixbuf.new_from_file_at_scale(uri, width, height, True) return GdkPixbuf.Pixbuf.new_from_file_at_scale(uri, width, height, True)
def _load_image(self, img): def _load_image(self):
self.clear_children(self.image_area) self.clear_children(self.image_area)
self.image_area.add(img) self.image_area.add(self.current_img)
self.image_area.show_all() self.image_area.show_all()
@threaded @threaded
@ -121,10 +134,19 @@ class Controller(TreeViewUpdateMixin, Controller_Data):
def _on_scale_image_from_parent_resize(self, eve): def _on_scale_image_from_parent_resize(self, eve):
if self.current_img: if self.current_img_uri:
self.image_update_lock = True self.image_update_lock = True
image = Gtk.Image.new_from_pixbuf(self._get_pixbuf(self.current_img))
self._load_image(image) if not self.is_img_gif:
self.current_img = Gtk.Image.new_from_pixbuf(self._get_pixbuf(self.current_img_uri))
self._load_image()
else:
try:
self.gif_animation.advance()
self.current_img.set_from_animation(self.gif_animation)
except Exception:
pass
self.image_update_lock = False self.image_update_lock = False
def get_clipboard_data(self): def get_clipboard_data(self):

View File

@ -8,7 +8,7 @@ gi.require_version('Gdk', '3.0')
from gi.repository import Gtk, Gdk, GLib from gi.repository import Gtk, Gdk, GLib
# Application imports # Application imports
from . import View from .view import View
@ -40,14 +40,12 @@ class Controller_Data:
self.warning_color = self.settings.get_warning_color() self.warning_color = self.settings.get_warning_color()
self.error_color = self.settings.get_error_color() self.error_color = self.settings.get_error_color()
self.current_path_label = self.builder.get_object("current_path_label") self.current_path_label = self.builder.get_object("current_path_label")
self.thumbnails_view = self.builder.get_object("thumbnails_view") self.thumbnails_view = self.builder.get_object("thumbnails_view")
self.thumbnail_store = self.builder.get_object("thumbnail_store") self.thumbnail_store = self.builder.get_object("thumbnail_store")
self.image_area = self.builder.get_object("image_area") self.image_area = self.builder.get_object("image_area")
self.blank_image = self.settings.get_blank_image() self.blank_image = self.settings.get_blank_image()
self.thumbnails_view.connect("drag-data-received", self._on_drag_data_received) self.thumbnails_view.connect("drag-data-received", self._on_drag_data_received)
URI_TARGET_TYPE = 80 URI_TARGET_TYPE = 80
uri_target = Gtk.TargetEntry.new('text/uri-list', Gtk.TargetFlags(0), URI_TARGET_TYPE) uri_target = Gtk.TargetEntry.new('text/uri-list', Gtk.TargetFlags(0), URI_TARGET_TYPE)
@ -58,7 +56,10 @@ class Controller_Data:
self.images_filter = self.settings.get_images_filter() self.images_filter = self.settings.get_images_filter()
self.view = View(self.images_filter, self.blank_image) self.view = View(self.images_filter, self.blank_image)
self.current_img_uri = None
self.current_img = None self.current_img = None
self.gif_animation = None
self.is_img_gif = False
self.image_update_lock = False self.image_update_lock = False

View File

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

View File

@ -1,8 +0,0 @@
"""
Gtk Bound Signal Module
"""
from .mixins import *
from .View import View
from .IPCServerMixin import IPCServerMixin
from .Controller_Data import Controller_Data
from .Controller import Controller

View File

@ -1 +0,0 @@
from .TreeViewUpdateMixin import TreeViewUpdateMixin

View File

@ -1,5 +1,5 @@
# Python imports # Python imports
import threading, socket, time import os, threading, time
from multiprocessing.connection import Listener, Client from multiprocessing.connection import Listener, Client
# Lib imports # Lib imports
@ -15,11 +15,32 @@ def threaded(fn):
class IPCServerMixin: class IPCServer:
""" Create a listener so that other SolarFM instances send requests back to existing instance. """
def __init__(self, conn_type="socket"):
self.is_ipc_alive = False
self._conn_type = conn_type
self.ipc_authkey = b'mirage2-ipc'
self.ipc_timeout = 15.0
if conn_type == "socket":
self.ipc_address = '/tmp/mirage2-ipc.sock'
else:
self.ipc_address = '127.0.0.1'
self.ipc_port = 4848
@threaded @threaded
def create_ipc_server(self): def create_ipc_server(self):
if self._conn_type == "socket":
if os.path.exists(self.ipc_address):
return
listener = Listener(address=self.ipc_address, family="AF_UNIX", authkey=self.ipc_authkey)
else:
listener = Listener((self.ipc_address, self.ipc_port), authkey=self.ipc_authkey) listener = Listener((self.ipc_address, self.ipc_port), authkey=self.ipc_authkey)
self.is_ipc_alive = True self.is_ipc_alive = True
while True: while True:
conn = listener.accept() conn = listener.accept()
@ -47,7 +68,7 @@ class IPCServerMixin:
conn.close() conn.close()
break break
# NOTE: Not perfect but insures we don't lockup the connection for too long. # NOTE: Not perfect but insures we don't lock up the connection for too long.
end_time = time.time() end_time = time.time()
if (end - start) > self.ipc_timeout: if (end - start) > self.ipc_timeout:
conn.close() conn.close()
@ -57,7 +78,12 @@ class IPCServerMixin:
def send_ipc_message(self, message="Empty Data..."): def send_ipc_message(self, message="Empty Data..."):
try: try:
if self._conn_type == "socket":
conn = Client(address=self.ipc_address, family="AF_UNIX", authkey=self.ipc_authkey)
else:
conn = Client((self.ipc_address, self.ipc_port), authkey=self.ipc_authkey) conn = Client((self.ipc_address, self.ipc_port), authkey=self.ipc_authkey)
conn.send(message) conn.send(message)
conn.send('close connection') conn.send('close connection')
except Exception as e: except Exception as e:

View File

@ -1,6 +1,3 @@
""" """
Utils module Utils module
""" """
from .Logger import Logger
from .Settings import Settings

View File

@ -11,7 +11,7 @@ from gi.repository import Gdk
# Application imports # Application imports
from . import Logger from .logger import Logger