Refactored to use better event system

This commit is contained in:
itdominator 2023-01-16 22:47:28 -06:00
parent e714edcbb7
commit 3911421356
22 changed files with 386 additions and 309 deletions

View File

@ -1,101 +1,34 @@
# Python imports # Python imports
import builtins, os, re import builtins
import threading
from os import path from os import path
# Lib imports # Lib imports
# Application imports # Application imports
from utils.event_system import EventSystem
class Builtins:
""" Create an pub/sub systems. """
def __init__(self): # NOTE: Threads WILL NOT die with parent's destruction.
self.USER_HOME = path.expanduser('~') def threaded_wrapper(fn):
self.block_from_update = False def wrapper(*args, **kwargs):
self.block_to_update = False threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=False).start()
self.active_path = None return wrapper
self.from_changes = []
self.to_changes = []
# NOTE: The format used is list of [type, target, (data,)] Where: # NOTE: Threads WILL die with parent's destruction.
# type is useful context for control flow, def daemon_threaded_wrapper(fn):
# target is the method to call, def wrapper(*args, **kwargs):
# data is the method parameters to give threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=True).start()
# Where data may be any kind of data return wrapper
self._gui_events = []
self._module_events = []
# Makeshift fake "events" type system FIFO
def _pop_gui_event(self):
if len(self._gui_events) > 0:
return self._gui_events.pop(0)
return None
def _pop_module_event(self):
if len(self._module_events) > 0:
return self._module_events.pop(0)
return None
def set_active_path(self, _file):
if os.path.isdir(_file) :
self.from_changes.clear()
self.active_path = _file
for f in os.listdir(_file):
self.from_changes.append(f)
self.from_changes.sort(key=self._natural_keys)
self.to_changes = self.from_changes
event_system.push_gui_event(["update-from", None, ()])
event_system.push_gui_event(["update-to", None, ()])
def reset_to_view(self):
self.to_changes = self.from_changes
event_system.push_gui_event(["update-to", None, ()])
def reset_from_view(self):
self.set_active_path(self.active_path)
def push_gui_event(self, event):
if len(event) == 3:
self._gui_events.append(event)
return None
raise Exception("Invald event format! Please do: [type, target, (data,)]")
def push_module_event(self, event):
if len(event) == 3:
self._module_events.append(event)
return None
raise Exception("Invald event format! Please do: [type, target, (data,)]")
def read_gui_event(self):
return self._gui_events[0]
def read_module_event(self):
return self._module_events[0]
def consume_gui_event(self):
return self._pop_gui_event()
def consume_module_event(self):
return self._pop_module_event()
def _atoi(self, text):
return int(text) if text.isdigit() else text
def _natural_keys(self, text):
return [ self._atoi(c) for c in re.split('(\d+)',text) ]
# 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 = "BulkR" builtins.app_name = "BulkR"
builtins.event_system = Builtins() builtins.USER_HOME = path.expanduser('~')
builtins.event_sleep_time = 0.1 builtins.event_system = EventSystem()
builtins.debug = False builtins.debug = False
builtins.trace_debug = False builtins.trace_debug = False

View File

@ -1,21 +1,3 @@
#!/usr/bin/python3 """
Base module
"""
# Python imports
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
# Application imports
from __builtins__ import Builtins
from Window import Window
from controller import Controller
class Main(Window):
"""docstring for Main."""
def __init__(self, args):
super(Main, self).__init__(args)

View File

@ -2,33 +2,37 @@
# Python imports # Python imports
import sys, argparse import argparse
import faulthandler
from setproctitle import setproctitle from setproctitle import setproctitle
import signal
# Gtk imports # Gtk imports
import gi, faulthandler, signal import gi
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk
from gi.repository import GLib from gi.repository import GLib
# Application imports # Application imports
from __init__ import Main from __builtins__ import *
from app import Application
if __name__ == "__main__": if __name__ == "__main__":
try: try:
setproctitle('BulkR') setproctitle('{app_name}')
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, Gtk.main_quit) GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, Gtk.main_quit)
faulthandler.enable() # For better debug info faulthandler.enable() # For better debug info
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
# Add long and short arguments # Add long and short arguments
parser.add_argument("--path", "-p", default="default", help="Path to folder.") parser.add_argument("--path", "-p", default=None, help="Path to folder.")
# Read arguments (If any...) # Read arguments (If any...)
args = parser.parse_args() args, unknownargs = parser.parse_known_args()
main = Main(args)
Application(args, unknownargs)
Gtk.main() Gtk.main()
except Exception as e: except Exception as e:
print( repr(e) ) print( repr(e) )

19
src/app.py Normal file
View File

@ -0,0 +1,19 @@
#!/usr/bin/python3
# Python imports
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
# Application imports
from core.window import Window
class Application(Window):
"""docstring for Application."""
def __init__(self, args, unknownargs):
super(Application, self).__init__(args, unknownargs)

View File

@ -1,73 +0,0 @@
# Python imports
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk, Gdk
# Application imports
from mixins import CommonWidgetGeneratorMixin, CommonActionsMixin
class ChangeView(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin):
def __init__(self):
super(ChangeView, self).__init__()
from_container = Gtk.Box()
to_container = Gtk.Box()
from_scroll_vw, \
self.from_store = self._create_treeview_widget(title="From:")
to_scroll_vw, \
self.to_store = self._create_treeview_widget(title="To:")
from_container.add(from_scroll_vw)
to_container.add(to_scroll_vw)
from_container.set_orientation(1)
to_container.set_orientation(1)
fchild = from_scroll_vw.get_children()[0]
fchild.connect("drag-data-received", self._on_drag_data_received)
URI_TARGET_TYPE = 80
uri_target = Gtk.TargetEntry.new('text/uri-list', Gtk.TargetFlags(0), URI_TARGET_TYPE)
targets = [ uri_target ]
action = Gdk.DragAction.COPY
fchild.enable_model_drag_dest(targets, action)
fchild.enable_model_drag_source(0, targets, action)
self.set_spacing(20)
self.set_border_width(2)
self.set_homogeneous(True)
self.add(from_container)
self.add(to_container)
self.show_all()
def _on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
if info == 80:
uri = data.get_uris()[0].split("file://")[1]
event_system.set_active_path(uri)
def update_from_list(self):
if event_system.block_from_update:
return
print("Updating From List...")
if self.from_store:
self.from_store.clear()
for i, change in enumerate(event_system.from_changes):
self.from_store.insert(i, [change])
def update_to_list(self):
if event_system.block_to_update:
return
print("Updating To List...")
if self.to_store:
self.to_store.clear()
for i, change in enumerate(event_system.to_changes):
self.to_store.insert(i, [change])

View File

@ -1,2 +0,0 @@
from .ChangeView import ChangeView
from .Controller import Controller

View File

@ -1,5 +0,0 @@
from .Insert import Insert
from .Replace import Replace
from .Remove import Remove
from .RemoveFromTo import RemoveFromTo
from .Case import Case

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

@ -0,0 +1,3 @@
"""
Core Module
"""

147
src/core/change_view.py Normal file
View File

@ -0,0 +1,147 @@
# Python imports
import os
import re
# 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
# Application imports
from mixins import CommonWidgetGeneratorMixin
from mixins import CommonActionsMixin
class ChangeView(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin):
def __init__(self):
super(ChangeView, self).__init__()
self._active_path = None
self._from_store = None
self._to_store = None
self._from_changes = []
self._to_changes = []
self._setup_styling()
self._setup_signals()
self._subscribe_to_events()
self._load_widgets()
self.show_all()
def _setup_styling(self):
self.set_spacing(20)
self.set_border_width(2)
self.set_homogeneous(True)
def _setup_signals(self):
...
def _subscribe_to_events(self):
event_system.subscribe("reset-from-view", self.reset_from_view)
event_system.subscribe("reset-to-view", self.reset_to_view)
event_system.subscribe("get-from", self.get_from_list)
event_system.subscribe("get-to", self.get_to_list)
event_system.subscribe("set-from", self.set_from_list)
event_system.subscribe("set-to", self.set_to_list)
event_system.subscribe("update-from", self.update_from_list)
event_system.subscribe("update-to", self.update_to_list)
event_system.subscribe("get-active-path", self._get_active_path)
event_system.subscribe("set-active-path", self._set_active_path)
def _load_widgets(self):
from_container = Gtk.Box()
to_container = Gtk.Box()
from_scroll_vw, \
self._from_store = self._create_treeview_widget(title="From:")
to_scroll_vw, \
self._to_store = self._create_treeview_widget(title="To:")
from_container.add(from_scroll_vw)
to_container.add(to_scroll_vw)
from_container.set_orientation(1)
to_container.set_orientation(1)
fchild = from_scroll_vw.get_children()[0]
fchild.connect("drag-data-received", self._on_drag_data_received)
URI_TARGET_TYPE = 80
uri_target = Gtk.TargetEntry.new('text/uri-list', Gtk.TargetFlags(0), URI_TARGET_TYPE)
targets = [ uri_target ]
action = Gdk.DragAction.COPY
fchild.enable_model_drag_dest(targets, action)
fchild.enable_model_drag_source(0, targets, action)
self.add(from_container)
self.add(to_container)
def _on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
if info == 80:
uri = data.get_uris()[0].split("file://")[1]
self._set_active_path(uri)
def _get_active_path(self):
return self._active_path
def _set_active_path(self, _file):
if os.path.isdir(_file) :
self._from_changes.clear()
self._active_path = _file
for f in os.listdir(_file):
self._from_changes.append(f)
self._from_changes.sort(key=self._natural_keys)
self._to_changes = self._from_changes
event_system.emit("update-from")
event_system.emit("update-to")
def get_from_list(self):
return self._from_changes
def get_to_list(self):
return self._to_changes
def set_from_list(self, from_list):
self._from_changes = from_list
def set_to_list(self, to_list):
self._to_changes = to_list
def update_from_list(self):
print("Updating From List...")
if self._from_store:
self._from_store.clear()
for i, change in enumerate(self._from_changes):
self._from_store.insert(i, [change])
def update_to_list(self):
print("Updating To List...")
if self._to_store:
self._to_store.clear()
for i, change in enumerate(self._to_changes):
self._to_store.insert(i, [change])
def reset_to_view(self):
self._to_changes = self._from_changes
event_system.emit("update-to")
def reset_from_view(self):
self._set_active_path(self._active_path)
def _atoi(self, text):
return int(text) if text.isdigit() else text
def _natural_keys(self, text):
return [ self._atoi(c) for c in re.split('(\d+)',text) ]

View File

@ -1,35 +1,69 @@
# Python imports # Python imports
import os, sys, threading, time import os
import sys
# lib imports # lib imports
import gi import gi
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk
from gi.repository import GLib
# Application imports # Application imports
from mixins import CommonWidgetGeneratorMixin from mixins import CommonWidgetGeneratorMixin
from . import ChangeView from .change_view import ChangeView
from .widgets import * from .widgets import *
def threaded(fn):
def wrapper(*args, **kwargs):
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=True).start()
return wrapper
class Controller(Gtk.Box, CommonWidgetGeneratorMixin): class Controller(Gtk.Box, CommonWidgetGeneratorMixin):
def __init__(self, args): def __init__(self, args, unknownargs):
super(Controller, self).__init__() super(Controller, self).__init__()
# Add header # # Add header
self.change_view = None
self.copy_window = None
self.store = None
self.combo_box = None
self.action_collection = []
self._setup_styling()
self._setup_signals()
self._subscribe_to_events()
self._load_widgets()
self.show_all()
if unknownargs:
for arg in unknownargs:
if os.path.isdir(arg):
event_system.emit("set-active-path", (arg,))
if args.path and os.path.isdir(args.path):
event_system.emit("set-active-path", (args.path,))
def _setup_styling(self):
self.set_spacing(20)
self.set_margin_top(5)
self.set_margin_bottom(10)
self.set_margin_left(15)
self.set_margin_right(15)
self.set_orientation(1)
def _setup_signals(self):
...
def _subscribe_to_events(self):
event_system.subscribe("handle-gui-event", self._handle_gui_event)
def _load_widgets(self):
self.change_view = ChangeView() self.change_view = ChangeView()
action_bar = Gtk.Box() action_bar = Gtk.Box()
file_choser = Gtk.FileChooserButton(title="Directory Chooser", action=2) # 2 = SELECT_FOLDER
file_filter = Gtk.FileFilter() file_choser = Gtk.FileChooserButton(title="Directory Chooser", action=2) # 2 = SELECT_FOLDER
file_filter = Gtk.FileFilter()
file_choser.show() file_choser.show()
file_choser.set_filename(event_system.USER_HOME) file_choser.set_filename(USER_HOME)
file_filter.add_mime_type("inode/directory") file_filter.add_mime_type("inode/directory")
file_choser.add_filter(file_filter) file_choser.add_filter(file_filter)
@ -64,56 +98,19 @@ class Controller(Gtk.Box, CommonWidgetGeneratorMixin):
self.copy_window = Gtk.Box() self.copy_window = Gtk.Box()
self.set_spacing(20)
self.set_margin_top(5)
self.set_margin_bottom(10)
self.set_margin_left(15)
self.set_margin_right(15)
self.set_orientation(1)
self.add(file_choser) self.add(file_choser)
self.add(action_bar) self.add(action_bar)
self.add(self.change_view) self.add(self.change_view)
self.add(actions_scroll_label) self.add(actions_scroll_label)
self.add(actions_scroll_view) self.add(actions_scroll_view)
self.add(run_button) self.add(run_button)
self.show_all()
self.gui_event_observer()
self.action_collection = []
@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
if type:
method = getattr(self.__class__, "_handle_gui_event")
GLib.idle_add(method, *(self, type, target, data))
else:
method = getattr(self.__class__, target)
GLib.idle_add(method, *(self, *data,))
except Exception as e:
print(repr(e))
def update_dir_path(self, widget): def update_dir_path(self, widget):
path = widget.get_filename() path = widget.get_filename()
event_system.set_active_path(path) event_system.emit("set-active-path", (path,))
def _handle_gui_event(self, type, target, parameters): def _handle_gui_event(self, type, target, parameters):
if type == "update-from":
self.change_view.update_from_list()
return
if type == "update-to":
self.change_view.update_to_list()
return
for i, action in enumerate(self.action_collection): for i, action in enumerate(self.action_collection):
if action == target: if action == target:
if type == "move-up": if type == "move-up":
@ -150,34 +147,33 @@ class Controller(Gtk.Box, CommonWidgetGeneratorMixin):
self.action_collection.append(widget) self.action_collection.append(widget)
def _test_all(self, widget=None): def _test_all(self, widget=None):
event_system.block_to_update = True event_system.emit("reset-to-view")
event_system.reset_to_view()
for action in self.action_collection: for action in self.action_collection:
action.run() action.run()
event_system.block_to_update = False event_system.emit("update-to")
event_system.push_gui_event(["update-to", self, ()])
def _reset_to_view(self, widget): def _reset_to_view(self, widget):
event_system.reset_to_view() event_system.emit("reset-to-view")
def _run_all(self, widget): def _run_all(self, widget):
if not event_system.active_path: dir = event_system.emit_and_await("get-active-path")
if not dir:
print("No active path set. Returning...") print("No active path set. Returning...")
return return
self._test_all() self._test_all()
dir = event_system.active_path to_changes = event_system.emit_and_await("get-to")
for i, file in enumerate(event_system.from_changes): for i, file in enumerate(event_system.emit_and_await("get-from")):
fPath = f"{dir}/{file}" fPath = f"{dir}/{file}"
tPath = f"{dir}/{event_system.to_changes[i]}" tPath = f"{dir}/{to_changes[i]}"
if fPath != tPath: if fPath != tPath:
try: try:
os.rename(fPath, tPath) os.rename(fPath, tPath)
except Exception as e: except Exception as e:
print(f"Cant Move: {fPath}\nTo File: {tPath}") print(f"Cant Move: {fPath}\nTo File: {tPath}")
event_system.reset_from_view() event_system.emit("reset-from-view")
def _clean_text(self, text): def _clean_text(self, text):
return text.replace(" ", "") \ return text.replace(" ", "") \

View File

@ -0,0 +1,6 @@
from .case import Case
from .insert import Insert
from .time import Time
from .replace import Replace
from .remove import Remove
from .remove_from_to import RemoveFromTo

View File

@ -6,7 +6,10 @@ gi.require_version('Gtk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk
# Application imports # Application imports
from mixins import CommonWidgetGeneratorMixin, CommonActionsMixin from mixins import CommonWidgetGeneratorMixin
from mixins import CommonActionsMixin
class Case(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin): class Case(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin):
@ -27,20 +30,21 @@ class Case(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin):
new_collection = [] new_collection = []
itr = self.combo_box.get_active_iter() itr = self.combo_box.get_active_iter()
type = self.store.get(itr, 0)[0] type = self.store.get(itr, 0)[0]
to_changes = event_system.emit_and_await("get-to")
print(f"Changing Case...") print(f"Changing Case...")
if type == "Title Case": if type == "Title Case":
for name in event_system.to_changes: for name in to_changes:
new_collection.append(name.title()) new_collection.append(name.title())
if type == "UPPER": if type == "UPPER":
for name in event_system.to_changes: for name in to_changes:
new_collection.append(name.upper()) new_collection.append(name.upper())
if type == "lower": if type == "lower":
for name in event_system.to_changes: for name in to_changes:
new_collection.append(name.lower()) new_collection.append(name.lower())
if type == "InVert CaSe --> iNvERT cAsE": if type == "InVert CaSe --> iNvERT cAsE":
for name in event_system.to_changes: for name in to_changes:
new_collection.append(name.swapcase()) new_collection.append(name.swapcase())
event_system.to_changes = new_collection event_system.emit("set-to", (new_collection,))
event_system.push_gui_event(["update-to", self, ()]) event_system.emit("update-to")

View File

@ -7,7 +7,10 @@ gi.require_version('Gtk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk
# Application imports # Application imports
from mixins import CommonWidgetGeneratorMixin, CommonActionsMixin from mixins import CommonWidgetGeneratorMixin
from mixins import CommonActionsMixin
class Insert(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin): class Insert(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin):
@ -33,23 +36,24 @@ class Insert(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin):
insert_str = self.insert_entry.get_text() insert_str = self.insert_entry.get_text()
itr = self.combo_box.get_active_iter() itr = self.combo_box.get_active_iter()
type = self.store.get(itr, 0)[0] type = self.store.get(itr, 0)[0]
to_changes = event_system.emit_and_await("get-to")
print(f"Inserting...") print(f"Inserting...")
if type == "Start": if type == "Start":
for name in event_system.to_changes: for name in to_changes:
new_collection.append(f"{insert_str}{name}") new_collection.append(f"{insert_str}{name}")
if type == "End": if type == "End":
for name in event_system.to_changes: for name in to_changes:
base, file_extension = self.get_file_parts() base, file_extension = self.get_file_parts()
new_collection.append(f"{base}{insert_str}{file_extension}") new_collection.append(f"{base}{insert_str}{file_extension}")
if type == "Position": if type == "Position":
position = self.spin_button.get_value_as_int() position = self.spin_button.get_value_as_int()
for name in event_system.to_changes: for name in to_changes:
name = f"{name[:position]}{insert_str}{name[position:]}" name = f"{name[:position]}{insert_str}{name[position:]}"
new_collection.append(f"{name}") new_collection.append(f"{name}")
event_system.to_changes = new_collection event_system.emit("set-to", (new_collection,))
event_system.push_gui_event(["update-to", self, ()]) event_system.emit("update-to")
def _combo_box_changed(self, widget, eve=None): def _combo_box_changed(self, widget, eve=None):

View File

@ -6,7 +6,10 @@ gi.require_version('Gtk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk
# Application imports # Application imports
from mixins import CommonWidgetGeneratorMixin, CommonActionsMixin from mixins import CommonWidgetGeneratorMixin
from mixins import CommonActionsMixin
class Remove(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin): class Remove(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin):
@ -32,23 +35,24 @@ class Remove(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin):
new_collection = [] new_collection = []
itr = self.combo_box.get_active_iter() itr = self.combo_box.get_active_iter()
type = self.store.get(itr, 0)[0] type = self.store.get(itr, 0)[0]
print(f"To Remove: {from_str}") to_changes = event_system.emit_and_await("get-to")
print(f"To Remove: {from_str}")
if type == "All": if type == "All":
for name in event_system.to_changes: for name in to_changes:
new_collection.append(name.replace(from_str, '')) new_collection.append(name.replace(from_str, ''))
if type == "Word Start": if type == "Word Start":
print("Stub...") print("Stub...")
if type == "Word End": if type == "Word End":
print("Stub...") print("Stub...")
if type == "First Instance": if type == "First Instance":
for name in event_system.to_changes: for name in to_changes:
new_collection.append( name.replace(from_str, "", 1) ) new_collection.append( name.replace(from_str, "", 1) )
if type == "Last Instance": if type == "Last Instance":
for name in event_system.to_changes: for name in to_changes:
new_collection.append( self._replace_last(name, from_str, "") ) new_collection.append( self._replace_last(name, from_str, "") )
if type == "RegEx": if type == "RegEx":
print("Stub...") print("Stub...")
event_system.to_changes = new_collection event_system.emit("set-to", (new_collection,))
event_system.push_gui_event(["update-to", self, ()]) event_system.emit("update-to")

View File

@ -6,7 +6,10 @@ gi.require_version('Gtk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk
# Application imports # Application imports
from mixins import CommonWidgetGeneratorMixin, CommonActionsMixin from mixins import CommonWidgetGeneratorMixin
from mixins import CommonActionsMixin
class RemoveFromTo(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin): class RemoveFromTo(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin):
@ -46,13 +49,14 @@ class RemoveFromTo(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin):
new_collection = [] new_collection = []
itr = self.combo_box.get_active_iter() itr = self.combo_box.get_active_iter()
type = self.store.get(itr, 0)[0] type = self.store.get(itr, 0)[0]
to_changes = event_system.emit_and_await("get-to")
if type == "Using Sub String": if type == "Using Sub String":
fsub = self.entry_from.get_text() fsub = self.entry_from.get_text()
tsub = self.entry_to.get_text() tsub = self.entry_to.get_text()
print(f"From: {fsub}\nTo: {tsub}") print(f"From: {fsub}\nTo: {tsub}")
for name in event_system.to_changes: for name in to_changes:
startIndex = name.index(fsub) + 1 startIndex = name.index(fsub) + 1
endIndex = name.index(tsub) endIndex = name.index(tsub)
toRemove = name[startIndex:endIndex] toRemove = name[startIndex:endIndex]
@ -62,12 +66,12 @@ class RemoveFromTo(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin):
tsub = self.spin_button_to.get_value_as_int() tsub = self.spin_button_to.get_value_as_int()
print(f"From: {fsub}\nTo: {tsub}") print(f"From: {fsub}\nTo: {tsub}")
for name in event_system.to_changes: for name in to_changes:
toRemove = name[fsub:tsub] toRemove = name[fsub:tsub]
new_collection.append(name.replace(toRemove, '')) new_collection.append(name.replace(toRemove, ''))
event_system.to_changes = new_collection event_system.emit("set-to", (new_collection,))
event_system.push_gui_event(["update-to", self, ()]) event_system.emit("update-to")
def _combo_box_changed(self, widget, eve=None): def _combo_box_changed(self, widget, eve=None):
itr = widget.get_active_iter() itr = widget.get_active_iter()

View File

@ -6,7 +6,10 @@ gi.require_version('Gtk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk
# Application imports # Application imports
from mixins import CommonWidgetGeneratorMixin, CommonActionsMixin from mixins import CommonWidgetGeneratorMixin
from mixins import CommonActionsMixin
class Replace(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin): class Replace(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin):
@ -28,13 +31,15 @@ class Replace(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin):
def run(self): def run(self):
fsub = self.entry_from.get_text() fsub = self.entry_from.get_text()
tsub = self.entry_to.get_text() tsub = self.entry_to.get_text()
to_changes = event_system.emit_and_await("get-to")
if fsub and tsub: if fsub and tsub:
new_collection = [] new_collection = []
print(f"From: {fsub}\nTo: {tsub}") print(f"From: {fsub}\nTo: {tsub}")
for name in event_system.to_changes: for name in to_changes:
new_collection.append(name.replace(fsub, tsub)) new_collection.append(name.replace(fsub, tsub))
event_system.to_changes = new_collection event_system.emit("set-to", (new_collection,))
event_system.push_gui_event(["update-to", self, ()]) event_system.emit("update-to")

View File

@ -7,10 +7,13 @@ gi.require_version('Gtk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk
# Application imports # Application imports
from mixins import CommonWidgetGeneratorMixin
from mixins import CommonActionsMixin from mixins import CommonActionsMixin
class Time(Gtk.Box, CommonActionsMixin):
class Time(Gtk.Box, CommonWidgetGeneratorMixin, CommonActionsMixin):
def __init__(self): def __init__(self):
super(Time, self).__init__() super(Time, self).__init__()
@ -34,23 +37,24 @@ class Time(Gtk.Box, CommonActionsMixin):
insert_str = self.insert_entry.get_text() insert_str = self.insert_entry.get_text()
itr = self.combo_box.get_active_iter() itr = self.combo_box.get_active_iter()
type = self.store.get(itr, 0)[0] type = self.store.get(itr, 0)[0]
to_changes = event_system.emit_and_await("get-to")
print(f"Inserting...") print(f"Inserting...")
if type == "Start": if type == "Start":
for name in event_system.to_changes: for name in to_changes:
new_collection.append(f"{insert_str}{name}") new_collection.append(f"{insert_str}{name}")
if type == "End": if type == "End":
for name in event_system.to_changes: for name in to_changes:
base, file_extension = self.get_file_parts() base, file_extension = self.get_file_parts()
new_collection.append(f"{base}{insert_str}{file_extension}") new_collection.append(f"{base}{insert_str}{file_extension}")
if type == "Position": if type == "Position":
position = self.spin_button.get_value_as_int() position = self.spin_button.get_value_as_int()
for name in event_system.to_changes: for name in to_changes:
name = f"{name[:position]}{insert_str}{name[position:]}" name = f"{name[:position]}{insert_str}{name[position:]}"
new_collection.append(f"{name}") new_collection.append(f"{name}")
event_system.to_changes = new_collection event_system.emit("set-to", (new_collection,))
event_system.push_gui_event(["update-to", self, ()]) event_system.emit("update-to")
def _combo_box_changed(self, widget, eve=None): def _combo_box_changed(self, widget, eve=None):

View File

@ -9,21 +9,20 @@ gi.require_version('Gtk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk
# Application imports # Application imports
from __builtins__ import Builtins from .controller import Controller
from controller import Controller
class Window(Gtk.Window, Builtins): class Window(Gtk.Window):
"""docstring for Main.""" """docstring for Window."""
def __init__(self, args): def __init__(self, args, unknownargs):
super(Window, self).__init__() super(Window, self).__init__()
self.add(Controller(args)) self.add(Controller(args, unknownargs))
self.connect("delete-event", Gtk.main_quit) self.connect("delete-event", Gtk.main_quit)
self.set_default_size(850, 600) self.set_default_size(850, 600)
self.set_title(f"{app_name}") self.set_title(f"{app_name}")
self.set_icon_from_file("/usr/share/bulkr/bulkr.png") self.set_icon_from_file("/usr/share/bulkr/bulkr.png")
self.set_gravity(5) # 5 = CENTER self.set_gravity(5) # 5 = CENTER
self.set_position(3) # 4 = CENTER_ALWAYS self.set_position(1) # 1 = CENTER, 4 = CENTER_ALWAYS
self.show_all() self.show_all()

View File

@ -40,11 +40,19 @@ class CommonActionsMixin:
def delete(self): def delete(self):
self.get_parent().destroy() self.get_parent().destroy()
def _move_up(self, widget): def _move_up(self, widget):
event_system.push_gui_event(["move-up", self, ()]) event_system.emit("handle-gui-event", ("move-up", self, ()))
def _move_down(self, widget): def _move_down(self, widget):
event_system.push_gui_event(["move-down", self, ()]) event_system.emit("handle-gui-event", ("move-down", self, ()))
def _remove_self(self, widget):
event_system.emit("handle-gui-event", ("delete", self, ()))
def _do_run(self, widget):
event_system.emit("handle-gui-event", ("run", self, ()))
def _has_method(self, obj, name): def _has_method(self, obj, name):
''' Checks if a given method exists. ''' ''' Checks if a given method exists. '''
@ -59,12 +67,3 @@ class CommonActionsMixin:
reversed = string[::-1] reversed = string[::-1]
replaced = reversed.replace(find[::-1], replace[::-1], 1) replaced = reversed.replace(find[::-1], replace[::-1], 1)
return replaced[::-1] return replaced[::-1]
def _remove_self(self, widget):
event_system.push_gui_event(["delete", self, ()])
def _do_run(self, widget):
event_system.push_gui_event(["run", self, ()])

View File

@ -1,5 +1,4 @@
# Python imports # Python imports
import pathlib
# lib imports # lib imports
import gi import gi
@ -16,7 +15,7 @@ class CommonWidgetGeneratorMixin:
spin_button.set_wrap(True) spin_button.set_wrap(True)
spin_button.set_digits(0) spin_button.set_digits(0)
spin_button.set_increments(1.0, 1.0) spin_button.set_increments(1.0, 1.0)
spin_button.set_range(1, 1000000) spin_button.set_range(0, 1000000)
spin_button.set_sensitive(False) spin_button.set_sensitive(False)
return spin_button return spin_button

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

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

42
src/utils/event_system.py Normal file
View File

@ -0,0 +1,42 @@
# Python imports
from collections import defaultdict
# Lib imports
# Application imports
class EventSystem:
""" Create event system. """
def __init__(self):
self.subscribers = defaultdict(list)
def subscribe(self, event_type, fn):
self.subscribers[event_type].append(fn)
def emit(self, event_type, data = None):
if event_type in self.subscribers:
for fn in self.subscribers[event_type]:
if data:
if hasattr(data, '__iter__') and not type(data) is str:
fn(*data)
else:
fn(data)
else:
fn()
# NOTE: Should be used when signal has only one listener and vis-a-vis
def emit_and_await(self, event_type, data = None):
if event_type in self.subscribers:
for fn in self.subscribers[event_type]:
if data:
if hasattr(data, '__iter__') and not type(data) is str:
return fn(*data)
else:
return fn(data)
else:
return fn()