2022-01-31 02:29:57 +00:00
|
|
|
# Python imports
|
2022-06-15 04:03:04 +00:00
|
|
|
import os, threading, subprocess, time
|
2022-01-31 02:29:57 +00:00
|
|
|
|
|
|
|
# Gtk imports
|
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
from gi.repository import Gtk
|
|
|
|
|
|
|
|
# Application imports
|
|
|
|
|
|
|
|
|
2022-06-19 03:27:17 +00:00
|
|
|
# NOTE: Threads WILL NOT die with parent's destruction.
|
2022-01-31 02:29:57 +00:00
|
|
|
def threaded(fn):
|
|
|
|
def wrapper(*args, **kwargs):
|
2022-02-01 07:43:09 +00:00
|
|
|
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=False).start()
|
2022-01-31 02:29:57 +00:00
|
|
|
return wrapper
|
|
|
|
|
2022-06-19 03:27:17 +00:00
|
|
|
# NOTE: Threads WILL die with parent's destruction.
|
2022-06-15 04:03:04 +00:00
|
|
|
def daemon_threaded(fn):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=True).start()
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-31 02:29:57 +00:00
|
|
|
|
2022-07-07 04:19:41 +00:00
|
|
|
class Manifest:
|
2022-07-16 07:33:25 +00:00
|
|
|
path: str = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
name: str = "Youtube Download"
|
|
|
|
author: str = "ITDominator"
|
|
|
|
version: str = "0.0.1"
|
|
|
|
support: str = ""
|
|
|
|
requests: {} = {
|
2022-07-07 04:19:41 +00:00
|
|
|
'ui_target': "plugin_control_list",
|
|
|
|
'pass_fm_events': "true"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Plugin(Manifest):
|
|
|
|
def __init__(self):
|
2022-07-16 07:33:25 +00:00
|
|
|
self._event_system = None
|
|
|
|
self._event_sleep_time = .5
|
|
|
|
self._event_message = None
|
2022-02-25 07:29:37 +00:00
|
|
|
|
2022-06-15 04:03:04 +00:00
|
|
|
|
2022-07-07 04:19:41 +00:00
|
|
|
def get_ui_element(self):
|
|
|
|
button = Gtk.Button(label=self.name)
|
2022-02-11 06:58:49 +00:00
|
|
|
button.connect("button-release-event", self._do_download)
|
2022-07-07 04:19:41 +00:00
|
|
|
return button
|
2022-02-25 07:29:37 +00:00
|
|
|
|
2022-07-07 04:19:41 +00:00
|
|
|
def set_fm_event_system(self, fm_event_system):
|
2022-07-16 07:33:25 +00:00
|
|
|
self._event_system = fm_event_system
|
2022-06-19 03:27:17 +00:00
|
|
|
|
2022-07-07 04:19:41 +00:00
|
|
|
def run(self):
|
|
|
|
self._module_event_observer()
|
2022-06-19 03:27:17 +00:00
|
|
|
|
|
|
|
|
2022-07-16 07:33:25 +00:00
|
|
|
@threaded
|
|
|
|
def _do_download(self, widget=None, eve=None):
|
|
|
|
self._event_system.push_gui_event([self.name, "get_current_state", ()])
|
|
|
|
self.wait_for_fm_message()
|
|
|
|
|
|
|
|
state = self._event_message
|
|
|
|
subprocess.Popen([f'{self.path}/download.sh' , state.tab.get_current_directory()])
|
|
|
|
self._event_message = None
|
|
|
|
|
|
|
|
|
|
|
|
def wait_for_fm_message(self):
|
|
|
|
while not self._event_message:
|
|
|
|
pass
|
|
|
|
|
2022-06-15 04:03:04 +00:00
|
|
|
@daemon_threaded
|
|
|
|
def _module_event_observer(self):
|
|
|
|
while True:
|
|
|
|
time.sleep(self._event_sleep_time)
|
2022-07-16 07:33:25 +00:00
|
|
|
event = self._event_system.read_module_event()
|
2022-06-15 04:03:04 +00:00
|
|
|
if event:
|
|
|
|
try:
|
2022-07-07 17:51:51 +00:00
|
|
|
if event[0] == self.name:
|
2022-07-16 07:33:25 +00:00
|
|
|
target_id, method_target, data = self._event_system.consume_module_event()
|
2022-06-15 04:03:04 +00:00
|
|
|
|
|
|
|
if not method_target:
|
2022-07-16 07:33:25 +00:00
|
|
|
self._event_message = data
|
2022-06-15 04:03:04 +00:00
|
|
|
else:
|
|
|
|
method = getattr(self.__class__, f"{method_target}")
|
2022-07-07 17:51:51 +00:00
|
|
|
if data:
|
|
|
|
data = method(*(self, *data))
|
|
|
|
else:
|
|
|
|
method(*(self,))
|
2022-06-15 04:03:04 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(repr(e))
|