initial commit
This commit is contained in:
3
src/libs/__init__.py
Normal file
3
src/libs/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
Libs Package
|
||||
"""
|
||||
6
src/libs/db/__init__.py
Normal file
6
src/libs/db/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
"""
|
||||
DB Package
|
||||
"""
|
||||
|
||||
from .models import User
|
||||
from .db import DB
|
||||
42
src/libs/db/db.py
Normal file
42
src/libs/db/db.py
Normal file
@@ -0,0 +1,42 @@
|
||||
# Python imports
|
||||
from typing import Optional
|
||||
from os import path
|
||||
|
||||
# Lib imports
|
||||
from sqlmodel import Session, create_engine
|
||||
|
||||
# Application imports
|
||||
from .models import SQLModel, User
|
||||
|
||||
|
||||
|
||||
class DB:
|
||||
def __init__(self):
|
||||
super(DB, self).__init__()
|
||||
|
||||
self.engine = None
|
||||
|
||||
self.create_engine()
|
||||
|
||||
|
||||
def create_engine(self):
|
||||
db_path = f"sqlite:///{settings_manager.get_home_config_path()}/database.db"
|
||||
self.engine = create_engine(db_path)
|
||||
|
||||
SQLModel.metadata.create_all(self.engine)
|
||||
|
||||
def _add_entry(self, entry):
|
||||
with Session(self.engine) as session:
|
||||
session.add(entry)
|
||||
session.commit()
|
||||
|
||||
|
||||
def add_user_entry(self, name = None, password = None, email = None):
|
||||
if not name or not password or not email: return
|
||||
|
||||
user = User()
|
||||
user.name = name
|
||||
user.password = password
|
||||
user.email = email
|
||||
|
||||
self._add_entry(user)
|
||||
15
src/libs/db/models.py
Normal file
15
src/libs/db/models.py
Normal file
@@ -0,0 +1,15 @@
|
||||
# Python imports
|
||||
from typing import Optional
|
||||
|
||||
# Lib imports
|
||||
from sqlmodel import SQLModel, Field
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
class User(SQLModel, table = True):
|
||||
id: Optional[int] = Field(default = None, primary_key = True)
|
||||
name: str
|
||||
password: str
|
||||
email: Optional[str] = None
|
||||
60
src/libs/debugging.py
Normal file
60
src/libs/debugging.py
Normal file
@@ -0,0 +1,60 @@
|
||||
# Python imports
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
# Break into a Python console upon SIGUSR1 (Linux) or SIGBREAK (Windows:
|
||||
# CTRL+Pause/Break). To be included in all production code, just in case.
|
||||
def debug_signal_handler(signal, frame):
|
||||
del signal
|
||||
del frame
|
||||
|
||||
try:
|
||||
import rpdb2
|
||||
logger.debug("\n\nStarting embedded RPDB2 debugger. Password is 'foobar'\n\n")
|
||||
rpdb2.start_embedded_debugger("foobar", True, True)
|
||||
rpdb2.setbreak(depth=1)
|
||||
return
|
||||
except Exception:
|
||||
...
|
||||
|
||||
try:
|
||||
from rfoo.utils import rconsole
|
||||
logger.debug("\n\nStarting embedded rconsole debugger...\n\n")
|
||||
rconsole.spawn_server()
|
||||
return
|
||||
except Exception as ex:
|
||||
...
|
||||
|
||||
try:
|
||||
from pudb import set_trace
|
||||
logger.debug("\n\nStarting PuDB debugger...\n\n")
|
||||
set_trace(paused = True)
|
||||
return
|
||||
except Exception as ex:
|
||||
...
|
||||
|
||||
try:
|
||||
import ipdb
|
||||
logger.debug("\n\nStarting IPDB debugger...\n\n")
|
||||
ipdb.set_trace()
|
||||
return
|
||||
except Exception as ex:
|
||||
...
|
||||
|
||||
try:
|
||||
import pdb
|
||||
logger.debug("\n\nStarting embedded PDB debugger...\n\n")
|
||||
pdb.Pdb(skip=['gi.*']).set_trace()
|
||||
return
|
||||
except Exception as ex:
|
||||
...
|
||||
|
||||
try:
|
||||
import code
|
||||
code.interact()
|
||||
except Exception as ex:
|
||||
logger.debug(f"{ex}, returning to normal program flow...")
|
||||
5
src/libs/dto/__init__.py
Normal file
5
src/libs/dto/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""
|
||||
Dasta Class Package
|
||||
"""
|
||||
|
||||
from .event import Event
|
||||
18
src/libs/dto/code_event.py
Normal file
18
src/libs/dto/code_event.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Python imports
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
from .observable_event import ObservableEvent
|
||||
|
||||
|
||||
|
||||
@dataclass
|
||||
class CodeEvent(ObservableEvent):
|
||||
etype: str = ""
|
||||
ignore_focus: bool = False
|
||||
view: any = None
|
||||
file: any = None
|
||||
next_file: any = None
|
||||
buffer: any = None
|
||||
14
src/libs/dto/event.py
Normal file
14
src/libs/dto/event.py
Normal file
@@ -0,0 +1,14 @@
|
||||
# Python imports
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
@dataclass
|
||||
class Event:
|
||||
topic: str
|
||||
content: str
|
||||
raw_content: str
|
||||
10
src/libs/dto/observable_event.py
Normal file
10
src/libs/dto/observable_event.py
Normal file
@@ -0,0 +1,10 @@
|
||||
# Python imports
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
class ObservableEvent:
|
||||
...
|
||||
22
src/libs/endpoint_registry.py
Normal file
22
src/libs/endpoint_registry.py
Normal file
@@ -0,0 +1,22 @@
|
||||
# Python imports
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
from .singleton import Singleton
|
||||
|
||||
|
||||
|
||||
class EndpointRegistry(Singleton):
|
||||
def __init__(self):
|
||||
self._endpoints = {}
|
||||
|
||||
def register(self, rule, **options):
|
||||
def decorator(f):
|
||||
self._endpoints[rule] = f
|
||||
return f
|
||||
|
||||
return decorator
|
||||
|
||||
def get_endpoints(self):
|
||||
return self._endpoints
|
||||
73
src/libs/event_system.py
Normal file
73
src/libs/event_system.py
Normal file
@@ -0,0 +1,73 @@
|
||||
# Python imports
|
||||
from collections import defaultdict
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
from .singleton import Singleton
|
||||
|
||||
|
||||
|
||||
class EventSystem(Singleton):
|
||||
""" Create event system. """
|
||||
|
||||
def __init__(self):
|
||||
self.subscribers = defaultdict(list)
|
||||
self._is_paused = False
|
||||
|
||||
self._subscribe_to_events()
|
||||
|
||||
|
||||
def _subscribe_to_events(self):
|
||||
self.subscribe("pause_event_processing", self._pause_processing_events)
|
||||
self.subscribe("resume_event_processing", self._resume_processing_events)
|
||||
|
||||
def _pause_processing_events(self):
|
||||
self._is_paused = True
|
||||
|
||||
def _resume_processing_events(self):
|
||||
self._is_paused = False
|
||||
|
||||
def subscribe(self, event_type, fn):
|
||||
self.subscribers[event_type].append(fn)
|
||||
|
||||
def unsubscribe(self, event_type, fn):
|
||||
self.subscribers[event_type].remove(fn)
|
||||
|
||||
def unsubscribe_all(self, event_type):
|
||||
self.subscribers.pop(event_type, None)
|
||||
|
||||
def emit(self, event_type, data = None):
|
||||
if self._is_paused and event_type != "resume_event_processing":
|
||||
return
|
||||
|
||||
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()
|
||||
|
||||
def emit_and_await(self, event_type, data = None):
|
||||
if self._is_paused and event_type != "resume_event_processing":
|
||||
return
|
||||
|
||||
""" NOTE: Should be used when signal has only one listener and vis-a-vis """
|
||||
if event_type in self.subscribers:
|
||||
response = None
|
||||
for fn in self.subscribers[event_type]:
|
||||
if data:
|
||||
if hasattr(data, '__iter__') and not type(data) is str:
|
||||
response = fn(*data)
|
||||
else:
|
||||
response = fn(data)
|
||||
else:
|
||||
response = fn()
|
||||
|
||||
if not response in (None, ''):
|
||||
break
|
||||
|
||||
return response
|
||||
148
src/libs/ipc_server.py
Normal file
148
src/libs/ipc_server.py
Normal file
@@ -0,0 +1,148 @@
|
||||
# Python imports
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
from contextlib import suppress
|
||||
from multiprocessing.connection import Client
|
||||
from multiprocessing.connection import Listener
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
from .singleton import Singleton
|
||||
|
||||
|
||||
|
||||
class IPCServer(Singleton):
|
||||
""" Create a listener so that other {APP_NAME} instances send requests back to existing instance. """
|
||||
def __init__(self, ipc_address: str = '127.0.0.1', conn_type: str = "socket"):
|
||||
self.is_ipc_alive = False
|
||||
self._ipc_port = 0 # Use 0 to let Listener chose port
|
||||
self._ipc_address = ipc_address
|
||||
self._conn_type = conn_type
|
||||
self._ipc_authkey = b'' + bytes(f'{APP_NAME}-ipc', 'utf-8')
|
||||
self._ipc_timeout = 15.0
|
||||
|
||||
if conn_type == "socket":
|
||||
self._ipc_address = f'/tmp/{APP_NAME}-ipc.sock'
|
||||
elif conn_type == "full_network":
|
||||
self._ipc_address = '0.0.0.0'
|
||||
elif conn_type == "full_network_unsecured":
|
||||
self._ipc_authkey = None
|
||||
self._ipc_address = '0.0.0.0'
|
||||
elif conn_type == "local_network_unsecured":
|
||||
self._ipc_authkey = None
|
||||
|
||||
self._subscribe_to_events()
|
||||
|
||||
def _subscribe_to_events(self):
|
||||
event_system.subscribe("post-file-to-ipc", self.send_ipc_message)
|
||||
|
||||
|
||||
def create_ipc_listener(self) -> None:
|
||||
if self._conn_type == "socket":
|
||||
if settings_manager.is_dirty_start():
|
||||
with suppress(FileNotFoundError, PermissionError):
|
||||
os.unlink(self._ipc_address)
|
||||
|
||||
listener = Listener(address=self._ipc_address, family="AF_UNIX", authkey=self._ipc_authkey)
|
||||
elif "unsecured" not in self._conn_type:
|
||||
listener = Listener((self._ipc_address, self._ipc_port), authkey=self._ipc_authkey)
|
||||
else:
|
||||
listener = Listener((self._ipc_address, self._ipc_port))
|
||||
|
||||
|
||||
self.is_ipc_alive = True
|
||||
self._run_ipc_loop(listener)
|
||||
|
||||
@daemon_threaded
|
||||
def _run_ipc_loop(self, listener) -> None:
|
||||
# NOTE: Not thread safe if using with Gtk. Need to import GLib and use idle_add
|
||||
while self.is_ipc_alive:
|
||||
try:
|
||||
conn = listener.accept()
|
||||
start_time = time.perf_counter()
|
||||
self._handle_ipc_message(conn, start_time)
|
||||
except EOFError as e:
|
||||
logger.debug( repr(e) )
|
||||
except (OSError, ConnectionError, BrokenPipeError) as e:
|
||||
logger.debug( f"IPC connection error: {e}" )
|
||||
except Exception as e:
|
||||
logger.debug( f"Unexpected IPC error: {e}" )
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
listener.close()
|
||||
|
||||
def _handle_ipc_message(self, conn, start_time) -> None:
|
||||
while self.is_ipc_alive:
|
||||
msg = conn.recv()
|
||||
logger.debug(msg)
|
||||
|
||||
if "FILE|" in msg:
|
||||
file = msg.split("FILE|")[1].strip()
|
||||
if file:
|
||||
event_system.emit("handle-file-from-ipc", file)
|
||||
|
||||
conn.close()
|
||||
break
|
||||
|
||||
if "DIR|" in msg:
|
||||
file = msg.split("DIR|")[1].strip()
|
||||
if file:
|
||||
event_system.emit("handle-dir-from-ipc", file)
|
||||
|
||||
conn.close()
|
||||
break
|
||||
|
||||
|
||||
if msg in ['close connection', 'close server', 'Empty Data...']:
|
||||
conn.close()
|
||||
break
|
||||
|
||||
# NOTE: Not perfect but insures we don't lock up the connection for too long.
|
||||
end_time = time.perf_counter()
|
||||
if (end_time - start_time) > self._ipc_timeout:
|
||||
conn.close()
|
||||
break
|
||||
|
||||
|
||||
def send_ipc_message(self, message: str = "Empty Data...") -> None:
|
||||
try:
|
||||
if self._conn_type == "socket":
|
||||
conn = Client(address=self._ipc_address, family="AF_UNIX", authkey=self._ipc_authkey)
|
||||
elif "unsecured" not in self._conn_type:
|
||||
conn = Client((self._ipc_address, self._ipc_port), authkey=self._ipc_authkey)
|
||||
else:
|
||||
conn = Client((self._ipc_address, self._ipc_port))
|
||||
|
||||
conn.send(message)
|
||||
conn.close()
|
||||
except ConnectionRefusedError as e:
|
||||
logger.error("Connection refused...")
|
||||
except (OSError, ConnectionError, BrokenPipeError) as e:
|
||||
logger.error( f"IPC connection error: {e}" )
|
||||
except Exception as e:
|
||||
logger.error( f"Unexpected IPC error: {e}" )
|
||||
|
||||
|
||||
def send_test_ipc_message(self, message: str = "Empty Data...") -> None:
|
||||
try:
|
||||
if self._conn_type == "socket":
|
||||
conn = Client(address=self._ipc_address, family="AF_UNIX", authkey=self._ipc_authkey)
|
||||
elif "unsecured" not in self._conn_type:
|
||||
conn = Client((self._ipc_address, self._ipc_port), authkey=self._ipc_authkey)
|
||||
else:
|
||||
conn = Client((self._ipc_address, self._ipc_port))
|
||||
|
||||
conn.send(message)
|
||||
conn.close()
|
||||
except ConnectionRefusedError as e:
|
||||
if self._conn_type == "socket":
|
||||
logger.error("IPC Socket no longer valid.... Removing.")
|
||||
with suppress(FileNotFoundError, PermissionError):
|
||||
os.unlink(self._ipc_address)
|
||||
except (OSError, ConnectionError, BrokenPipeError) as e:
|
||||
logger.error( f"IPC connection error: {e}" )
|
||||
except Exception as e:
|
||||
logger.error( f"Unexpected IPC error: {e}" )
|
||||
138
src/libs/keybindings.py
Normal file
138
src/libs/keybindings.py
Normal file
@@ -0,0 +1,138 @@
|
||||
# Python imports
|
||||
import re
|
||||
|
||||
# Lib imports
|
||||
import gi
|
||||
gi.require_version('Gdk', '3.0')
|
||||
from gi.repository import Gdk
|
||||
|
||||
# Application imports
|
||||
from .singleton import Singleton
|
||||
|
||||
|
||||
|
||||
def logger(log = ""):
|
||||
print(log)
|
||||
|
||||
|
||||
class KeymapError(Exception):
|
||||
""" Custom exception for errors in keybinding configurations """
|
||||
|
||||
MODIFIER = re.compile('<([^<]+)>')
|
||||
class Keybindings(Singleton):
|
||||
""" Class to handle loading and lookup of Terminator keybindings """
|
||||
|
||||
modifiers = {
|
||||
'ctrl': Gdk.ModifierType.CONTROL_MASK,
|
||||
'control': Gdk.ModifierType.CONTROL_MASK,
|
||||
'primary': Gdk.ModifierType.CONTROL_MASK,
|
||||
'shift': Gdk.ModifierType.SHIFT_MASK,
|
||||
'alt': Gdk.ModifierType.MOD1_MASK,
|
||||
'super': Gdk.ModifierType.SUPER_MASK,
|
||||
'hyper': Gdk.ModifierType.HYPER_MASK,
|
||||
'mod2': Gdk.ModifierType.MOD2_MASK
|
||||
}
|
||||
|
||||
empty = {}
|
||||
keys = None
|
||||
_masks = None
|
||||
_lookup = None
|
||||
|
||||
def __init__(self):
|
||||
self.keymap = Gdk.Keymap.get_default()
|
||||
self.configure({})
|
||||
|
||||
def print_keys(self):
|
||||
print(self.keys)
|
||||
|
||||
def append_bindings(self, combos):
|
||||
""" Accept new binding(s) and reload """
|
||||
for item in combos:
|
||||
method, keys = item.split(":")
|
||||
self.keys[method] = keys
|
||||
|
||||
self.reload()
|
||||
|
||||
def configure(self, bindings):
|
||||
""" Accept new bindings and reconfigure with them """
|
||||
self.keys = bindings
|
||||
self.reload()
|
||||
|
||||
def reload(self):
|
||||
""" Parse bindings and mangle into an appropriate form """
|
||||
self._lookup = {}
|
||||
self._masks = 0
|
||||
|
||||
for action, bindings in list(self.keys.items()):
|
||||
if isinstance(bindings, list):
|
||||
bindings = (*bindings,)
|
||||
elif not isinstance(bindings, tuple):
|
||||
bindings = (bindings,)
|
||||
|
||||
|
||||
for binding in bindings:
|
||||
if not binding or binding == "None":
|
||||
continue
|
||||
|
||||
try:
|
||||
keyval, mask = self._parsebinding(binding)
|
||||
# Does much the same, but with worse error handling.
|
||||
# keyval, mask = Gtk.accelerator_parse(binding)
|
||||
except KeymapError as e:
|
||||
logger(f"Keybinding reload failed to parse binding '{binding}': {e}")
|
||||
else:
|
||||
if mask & Gdk.ModifierType.SHIFT_MASK:
|
||||
if keyval == Gdk.KEY_Tab:
|
||||
keyval = Gdk.KEY_ISO_Left_Tab
|
||||
mask &= ~Gdk.ModifierType.SHIFT_MASK
|
||||
else:
|
||||
keyvals = Gdk.keyval_convert_case(keyval)
|
||||
if keyvals[0] != keyvals[1]:
|
||||
keyval = keyvals[1]
|
||||
mask &= ~Gdk.ModifierType.SHIFT_MASK
|
||||
else:
|
||||
keyval = Gdk.keyval_to_lower(keyval)
|
||||
|
||||
self._lookup.setdefault(mask, {})
|
||||
self._lookup[mask][keyval] = action
|
||||
self._masks |= mask
|
||||
|
||||
def _parsebinding(self, binding):
|
||||
""" Parse an individual binding using Gtk's binding function """
|
||||
mask = 0
|
||||
modifiers = re.findall(MODIFIER, binding)
|
||||
|
||||
if modifiers:
|
||||
for modifier in modifiers:
|
||||
mask |= self._lookup_modifier(modifier)
|
||||
|
||||
key = re.sub(MODIFIER, '', binding)
|
||||
if key == '':
|
||||
raise KeymapError('No key found!')
|
||||
|
||||
keyval = Gdk.keyval_from_name(key)
|
||||
|
||||
if keyval == 0:
|
||||
raise KeymapError(f"Key '{key}' is unrecognised...")
|
||||
return (keyval, mask)
|
||||
|
||||
def _lookup_modifier(self, modifier):
|
||||
""" Map modifier names to gtk values """
|
||||
try:
|
||||
return self.modifiers[modifier.lower()]
|
||||
except KeyError:
|
||||
raise KeymapError(f"Unhandled modifier '<{modifier}>'")
|
||||
|
||||
def lookup(self, event):
|
||||
""" Translate a keyboard event into a mapped key """
|
||||
try:
|
||||
_found, keyval, _egp, _lvl, consumed = self.keymap.translate_keyboard_state(
|
||||
event.hardware_keycode,
|
||||
Gdk.ModifierType(event.get_state() & ~Gdk.ModifierType.LOCK_MASK),
|
||||
event.group)
|
||||
except TypeError:
|
||||
logger(f"Keybinding lookup failed to translate keyboard event: {dir(event)}")
|
||||
return None
|
||||
|
||||
mask = (event.get_state() & ~consumed) & self._masks
|
||||
return self._lookup.get(mask, self.empty).get(keyval, None)
|
||||
61
src/libs/logger.py
Normal file
61
src/libs/logger.py
Normal file
@@ -0,0 +1,61 @@
|
||||
# Python imports
|
||||
import os
|
||||
import logging
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
from .singleton import Singleton
|
||||
|
||||
|
||||
|
||||
class Logger(Singleton):
|
||||
"""
|
||||
Create a new logging object and return it.
|
||||
:note:
|
||||
NOSET # Don't know the actual log level of this... (defaulting or literally none?)
|
||||
Log Levels (From least to most)
|
||||
Type Value
|
||||
CRITICAL 50
|
||||
ERROR 40
|
||||
WARNING 30
|
||||
INFO 20
|
||||
DEBUG 10
|
||||
:param loggerName: Sets the name of the logger object. (Used in log lines)
|
||||
:param createFile: Whether we create a log file or just pump to terminal
|
||||
|
||||
:return: the logging object we created
|
||||
"""
|
||||
|
||||
def __init__(self, config_path: str, _ch_log_lvl = logging.CRITICAL, _fh_log_lvl = logging.INFO):
|
||||
self._CONFIG_PATH = config_path
|
||||
self.global_lvl = logging.DEBUG # Keep this at highest so that handlers can filter to their desired levels
|
||||
self.ch_log_lvl = _ch_log_lvl # Prety much the only one we ever change
|
||||
self.fh_log_lvl = _fh_log_lvl
|
||||
|
||||
def get_logger(self, loggerName: str = "NO_LOGGER_NAME_PASSED", createFile: bool = True) -> logging.Logger:
|
||||
log = logging.getLogger(loggerName)
|
||||
log.setLevel(self.global_lvl)
|
||||
|
||||
# Set our log output styles
|
||||
fFormatter = logging.Formatter('[%(asctime)s] %(pathname)s:%(lineno)d %(levelname)s - %(message)s', '%m-%d %H:%M:%S')
|
||||
cFormatter = logging.Formatter('%(pathname)s:%(lineno)d] %(levelname)s - %(message)s')
|
||||
|
||||
ch = logging.StreamHandler()
|
||||
ch.setLevel(level=self.ch_log_lvl)
|
||||
ch.setFormatter(cFormatter)
|
||||
log.addHandler(ch)
|
||||
|
||||
if createFile:
|
||||
folder = self._CONFIG_PATH
|
||||
file = f"{folder}/application.log"
|
||||
|
||||
if not os.path.exists(folder):
|
||||
os.mkdir(folder)
|
||||
|
||||
fh = logging.FileHandler(file)
|
||||
fh.setLevel(level=self.fh_log_lvl)
|
||||
fh.setFormatter(fFormatter)
|
||||
log.addHandler(fh)
|
||||
|
||||
return log
|
||||
3
src/libs/mixins/__init__.py
Normal file
3
src/libs/mixins/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
Libs.Mixins Package
|
||||
"""
|
||||
70
src/libs/mixins/dnd_mixin.py
Normal file
70
src/libs/mixins/dnd_mixin.py
Normal file
@@ -0,0 +1,70 @@
|
||||
# Python imports
|
||||
|
||||
# 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
|
||||
from gi.repository import Gio
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
class DnDMixin:
|
||||
|
||||
def _setup_dnd(self):
|
||||
flags = Gtk.DestDefaults.ALL
|
||||
|
||||
PLAIN_TEXT_TARGET_TYPE = 70
|
||||
URI_TARGET_TYPE = 80
|
||||
|
||||
text_target = Gtk.TargetEntry.new('text/plain', Gtk.TargetFlags(0), PLAIN_TEXT_TARGET_TYPE)
|
||||
uri_target = Gtk.TargetEntry.new('text/uri-list', Gtk.TargetFlags(0), URI_TARGET_TYPE)
|
||||
|
||||
# targets = [ text_target, uri_target ]
|
||||
targets = [ uri_target ]
|
||||
|
||||
action = Gdk.DragAction.COPY
|
||||
|
||||
# self.drag_dest_set_target_list(targets)
|
||||
self.drag_dest_set(flags, targets, action)
|
||||
|
||||
self._setup_dnd_signals()
|
||||
|
||||
def _setup_dnd_signals(self):
|
||||
# self.connect("drag-motion", self._on_drag_motion)
|
||||
# self.connect('drag-drop', self._on_drag_set)
|
||||
self.connect("drag-data-received", self._on_drag_data_received)
|
||||
|
||||
def _on_drag_motion(self, widget, drag_context, x, y, time):
|
||||
Gdk.drag_status(drag_context, drag_context.get_actions(), time)
|
||||
|
||||
return False
|
||||
|
||||
def _on_drag_set(self, widget, drag_context, data, info, time):
|
||||
self.drag_get_data(drag_context, drag_context.list_targets()[-1], time)
|
||||
|
||||
return False
|
||||
|
||||
def _on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
|
||||
if info == 70: return
|
||||
|
||||
if info == 80:
|
||||
uris = data.get_uris()
|
||||
files = []
|
||||
|
||||
if len(uris) == 0:
|
||||
uris = data.get_text().split("\n")
|
||||
|
||||
for uri in uris:
|
||||
gfile = None
|
||||
try:
|
||||
gfile = Gio.File.new_for_uri(uri)
|
||||
except Exception as e:
|
||||
gfile = Gio.File.new_for_path(uri)
|
||||
|
||||
files.append(gfile)
|
||||
|
||||
event_system.emit('set-pre-drop-dnd', (files,))
|
||||
31
src/libs/mixins/ipc_signals_mixin.py
Normal file
31
src/libs/mixins/ipc_signals_mixin.py
Normal file
@@ -0,0 +1,31 @@
|
||||
# Python imports
|
||||
|
||||
# Lib imports
|
||||
import gi
|
||||
from gi.repository import GLib
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
|
||||
class IPCSignalsMixin:
|
||||
""" IPCSignalsMixin handle messages from another starting {APP_NAME} process. """
|
||||
|
||||
def print_to_console(self, message = None):
|
||||
logger.debug(message)
|
||||
|
||||
def handle_file_from_ipc(self, fpath: str) -> None:
|
||||
logger.debug(f"File From IPC: {fpath}")
|
||||
GLib.idle_add(
|
||||
self.broadcast_message, "handle-file", (fpath,)
|
||||
)
|
||||
|
||||
def handle_dir_from_ipc(self, fpath: str) -> None:
|
||||
logger.debug(f"Dir From IPC: {fpath}")
|
||||
GLib.idle_add(
|
||||
self.broadcast_message, "handle-folder", (fpath,)
|
||||
)
|
||||
|
||||
def broadcast_message(self, message_type: str = "none", data: () = ()) -> None:
|
||||
event_system.emit(message_type, data)
|
||||
96
src/libs/mixins/keyboard_signals_mixin.py
Normal file
96
src/libs/mixins/keyboard_signals_mixin.py
Normal file
@@ -0,0 +1,96 @@
|
||||
# Python imports
|
||||
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
|
||||
|
||||
|
||||
|
||||
valid_keyvalue_pat = re.compile(r"[a-z0-9A-Z-_\[\]\(\)\| ]")
|
||||
|
||||
|
||||
|
||||
class KeyboardSignalsMixin:
|
||||
""" KeyboardSignalsMixin keyboard hooks controller. """
|
||||
|
||||
# TODO: Need to set methods that use this to somehow check the keybindings state instead.
|
||||
def unset_keys_and_data(self, widget = None, eve = None):
|
||||
self.ctrl_down = False
|
||||
self.shift_down = False
|
||||
self.alt_down = False
|
||||
|
||||
def unmap_special_keys(self, keyname):
|
||||
if "control" in keyname:
|
||||
self.ctrl_down = False
|
||||
if "shift" in keyname:
|
||||
self.shift_down = False
|
||||
if "alt" in keyname:
|
||||
self.alt_down = False
|
||||
|
||||
def on_global_key_press_controller(self, eve, user_data):
|
||||
keyname = Gdk.keyval_name(user_data.keyval).lower()
|
||||
modifiers = Gdk.ModifierType(user_data.get_state() & ~Gdk.ModifierType.LOCK_MASK)
|
||||
|
||||
self.was_midified_key = True if modifiers != 0 else False
|
||||
|
||||
if keyname.replace("_l", "").replace("_r", "") in ["control", "alt", "shift"]:
|
||||
if "control" in keyname:
|
||||
self.ctrl_down = True
|
||||
if "shift" in keyname:
|
||||
self.shift_down = True
|
||||
if "alt" in keyname:
|
||||
self.alt_down = True
|
||||
|
||||
def on_global_key_release_controller(self, widget, event):
|
||||
""" Handler for keyboard events """
|
||||
keyname = Gdk.keyval_name(event.keyval).lower()
|
||||
modifiers = Gdk.ModifierType(event.get_state() & ~Gdk.ModifierType.LOCK_MASK)
|
||||
|
||||
if keyname.replace("_l", "").replace("_r", "") in ["control", "alt", "shift"]:
|
||||
should_return = self.was_midified_key and (self.ctrl_down or self.shift_down or self.alt_down)
|
||||
self.unmap_special_keys(keyname)
|
||||
|
||||
if should_return:
|
||||
self.was_midified_key = False
|
||||
return
|
||||
|
||||
mapping = keybindings.lookup(event)
|
||||
logger.debug(f"on_global_key_release_controller > key > {keyname}")
|
||||
logger.debug(f"on_global_key_release_controller > keyval > {event.keyval}")
|
||||
logger.debug(f"on_global_key_release_controller > mapping > {mapping}")
|
||||
|
||||
if mapping:
|
||||
self.handle_mapped_key_event(mapping)
|
||||
else:
|
||||
self.handle_as_key_event_scope(keyname)
|
||||
|
||||
def handle_mapped_key_event(self, mapping):
|
||||
try:
|
||||
self.handle_as_controller_scope(mapping)
|
||||
except Exception:
|
||||
self.handle_as_plugin_scope(mapping)
|
||||
|
||||
def handle_as_controller_scope(self, mapping):
|
||||
getattr(self, mapping)()
|
||||
|
||||
def handle_as_plugin_scope(self, mapping):
|
||||
if "||" in mapping:
|
||||
sender, eve_type = mapping.split("||")
|
||||
else:
|
||||
sender = ""
|
||||
eve_type = mapping
|
||||
|
||||
self.handle_key_event_system(sender, eve_type)
|
||||
|
||||
def handle_as_key_event_scope(self, keyname):
|
||||
if self.ctrl_down and not keyname in ["1", "kp_1", "2", "kp_2", "3", "kp_3", "4", "kp_4"]:
|
||||
self.handle_key_event_system(None, keyname)
|
||||
|
||||
def handle_key_event_system(self, sender, eve_type):
|
||||
event_system.emit(eve_type)
|
||||
26
src/libs/mixins/observable_mixin.py
Normal file
26
src/libs/mixins/observable_mixin.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# Python imports
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
from ..dto.observable_event import ObservableEvent
|
||||
|
||||
|
||||
|
||||
class ObservableMixin:
|
||||
observers = []
|
||||
|
||||
def add_observer(self, observer: any):
|
||||
if not hasattr(observer, 'notification') or not callable(getattr(observer, 'notification')):
|
||||
raise ValueError(f"Observer '{observer}' must implement a `notification` method.")
|
||||
|
||||
self.observers.append(observer)
|
||||
|
||||
def remove_observer(self, observer: any):
|
||||
if not observer in self.observers: return
|
||||
|
||||
self.observers.remove(observer)
|
||||
|
||||
def notify_observers(self, event: ObservableEvent):
|
||||
for observer in self.observers:
|
||||
observer.notification(event)
|
||||
4
src/libs/settings/__init__.py
Normal file
4
src/libs/settings/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
"""
|
||||
Settings Package
|
||||
"""
|
||||
from .manager import SettingsManager
|
||||
126
src/libs/settings/manager.py
Normal file
126
src/libs/settings/manager.py
Normal file
@@ -0,0 +1,126 @@
|
||||
# Python imports
|
||||
import inspect
|
||||
import time
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
from ..singleton import Singleton
|
||||
from .start_check_mixin import StartCheckMixin
|
||||
|
||||
from .path_manager import PathManager
|
||||
from .options.settings import Settings
|
||||
|
||||
|
||||
|
||||
class SettingsManager(StartCheckMixin, Singleton):
|
||||
def __init__(self):
|
||||
self.path_manager: PathManager = PathManager()
|
||||
self.settings: Settings = None
|
||||
|
||||
self._main_window = None
|
||||
self._builder = None
|
||||
|
||||
self._trace_debug: bool = False
|
||||
self._debug: bool = False
|
||||
self._dirty_start: bool = False
|
||||
self._passed_in_file: bool = False
|
||||
self._starting_files: list = []
|
||||
|
||||
self.PAINT_BG_COLOR: tuple = (0, 0, 0, 0.0)
|
||||
|
||||
self.load_keybindings()
|
||||
self.load_context_menu_data()
|
||||
|
||||
|
||||
def get_monitor_data(self) -> list:
|
||||
screen = self._main_window.get_screen()
|
||||
monitors = []
|
||||
for m in range(screen.get_n_monitors()):
|
||||
monitors.append(screen.get_monitor_geometry(m))
|
||||
print("{}x{}+{}+{}".format(monitor.width, monitor.height, monitor.x, monitor.y))
|
||||
|
||||
return monitors
|
||||
|
||||
def get_main_window(self) -> any: return self._main_window
|
||||
def get_builder(self) -> any: return self._builder
|
||||
def get_paint_bg_color(self) -> any: return self.PAINT_BG_COLOR
|
||||
def get_context_menu_data(self) -> str: return self._context_menu_data
|
||||
|
||||
def get_icon_theme(self) -> str: return self._ICON_THEME
|
||||
def get_starting_files(self) -> list: return self._starting_files
|
||||
def get_guake_key(self) -> tuple: return self._guake_key
|
||||
|
||||
def get_starting_args(self):
|
||||
return self.args, self.unknownargs
|
||||
|
||||
def set_main_window(self, window): self._main_window = window
|
||||
def set_builder(self, builder) -> any: self._builder = builder
|
||||
|
||||
def set_main_window_x(self, x: int = 0): self.settings.config.main_window_x = x
|
||||
def set_main_window_y(self, y: int = 0): self.settings.config.main_window_y = y
|
||||
def set_main_window_width(self, width: int = 800): self.settings.config.main_window_width = width
|
||||
def set_main_window_height(self, height: int = 600): self.settings.config.main_window_height = height
|
||||
def set_main_window_min_width(self, width: int = 720): self.settings.config.main_window_min_width = width
|
||||
def set_main_window_min_height(self, height: int = 480): self.settings.config.main_window_min_height = height
|
||||
def set_starting_files(self, files: list): self._starting_files = files
|
||||
def set_start_load_time(self): self._start_load_time = time.perf_counter()
|
||||
def set_end_load_time(self): self._end_load_time = time.perf_counter()
|
||||
|
||||
def set_starting_args(self, args, unknownargs):
|
||||
self.args = args
|
||||
self.unknownargs = unknownargs
|
||||
|
||||
def set_trace_debug(self, trace_debug: bool):
|
||||
self._trace_debug = trace_debug
|
||||
|
||||
def set_debug(self, debug: bool):
|
||||
self._debug = debug
|
||||
|
||||
def set_is_starting_with_file(self, is_passed_in_file: bool = False):
|
||||
self._passed_in_file = is_passed_in_file
|
||||
|
||||
def is_trace_debug(self) -> str: return self._trace_debug
|
||||
def is_debug(self) -> str: return self._debug
|
||||
def is_starting_with_file(self) -> bool: return self._passed_in_file
|
||||
|
||||
def log_load_time(self): logger.info( f"Load Time: {self._end_load_time - self._start_load_time}" )
|
||||
|
||||
|
||||
def register_signals_to_builder(self, classes = None):
|
||||
handlers = {}
|
||||
|
||||
for c in classes:
|
||||
methods = None
|
||||
try:
|
||||
methods = inspect.getmembers(c, predicate = inspect.ismethod)
|
||||
handlers.update(methods)
|
||||
except Exception as e:
|
||||
...
|
||||
|
||||
self._builder.connect_signals(handlers)
|
||||
|
||||
def call_method(self, target_class: any = None, _method_name: str = "", data: any = None):
|
||||
method_name = str(_method_name)
|
||||
method = getattr(target_class, method_name, lambda data: f"No valid key passed...\nkey={method_name}\nargs={data}")
|
||||
return method(data) if data else method()
|
||||
|
||||
def load_keybindings(self):
|
||||
bindings = self.path_manager.load_keybindings()
|
||||
self._guake_key = bindings["guake_key"]
|
||||
|
||||
keybindings.configure(bindings)
|
||||
|
||||
def load_context_menu_data(self):
|
||||
self._context_menu_data = self.path_manager.load_context_menu_data()
|
||||
|
||||
def load_settings(self):
|
||||
data = self.path_manager.load_settings()
|
||||
if not data:
|
||||
self.settings = Settings()
|
||||
return
|
||||
|
||||
self.settings = Settings(**data)
|
||||
|
||||
def save_settings(self):
|
||||
self.path_manager.save_settings(self.settings)
|
||||
8
src/libs/settings/options/__init__.py
Normal file
8
src/libs/settings/options/__init__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
"""
|
||||
Settings.Options Package
|
||||
"""
|
||||
from .settings import Settings
|
||||
from .config import Config
|
||||
from .filters import Filters
|
||||
from .theming import Theming
|
||||
from .debugging import Debugging
|
||||
39
src/libs/settings/options/config.py
Normal file
39
src/libs/settings/options/config.py
Normal file
@@ -0,0 +1,39 @@
|
||||
# Python imports
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
@dataclass
|
||||
class Config:
|
||||
base_of_home: str = ""
|
||||
hide_hidden_files: str = "true"
|
||||
thumbnailer_path: str = "ffmpegthumbnailer"
|
||||
blender_thumbnailer_path: str = ""
|
||||
go_past_home: str = "true"
|
||||
lock_folder: str = "false"
|
||||
locked_folders: list = field(default_factory=lambda: [ "venv", "flasks" ])
|
||||
mplayer_options: str = "-quiet -really-quiet -xy 1600 -geometry 50%:50%",
|
||||
music_app: str = "/opt/deadbeef/bin/deadbeef"
|
||||
media_app: str = "mpv"
|
||||
image_app: str = "mirage"
|
||||
office_app: str = "libreoffice"
|
||||
pdf_app: str = "evince"
|
||||
code_app: str = "atom"
|
||||
text_app: str = "leafpad"
|
||||
file_manager_app: str = "solarfm"
|
||||
terminal_app: str = "terminator"
|
||||
remux_folder_max_disk_usage: str = "8589934592"
|
||||
make_transparent: int = 0
|
||||
main_window_x: int = 721
|
||||
main_window_y: int = 465
|
||||
main_window_min_width: int = 720
|
||||
main_window_min_height: int = 480
|
||||
main_window_width: int = 800
|
||||
main_window_height: int = 600
|
||||
application_dirs: list = field(default_factory=lambda: [
|
||||
"/usr/share/applications",
|
||||
f"{settings_manager.get_home_path()}/.local/share/applications"
|
||||
])
|
||||
12
src/libs/settings/options/debugging.py
Normal file
12
src/libs/settings/options/debugging.py
Normal file
@@ -0,0 +1,12 @@
|
||||
# Python imports
|
||||
from dataclasses import dataclass
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
@dataclass
|
||||
class Debugging:
|
||||
ch_log_lvl: int = 10
|
||||
fh_log_lvl: int = 20
|
||||
90
src/libs/settings/options/filters.py
Normal file
90
src/libs/settings/options/filters.py
Normal file
@@ -0,0 +1,90 @@
|
||||
# Python imports
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
@dataclass
|
||||
class Filters:
|
||||
meshs: list = field(default_factory=lambda: [
|
||||
".blend",
|
||||
".dae",
|
||||
".fbx",
|
||||
".gltf",
|
||||
".obj",
|
||||
".stl"
|
||||
])
|
||||
code: list = field(default_factory=lambda: [
|
||||
".cpp",
|
||||
".css",
|
||||
".c",
|
||||
".go",
|
||||
".html",
|
||||
".htm",
|
||||
".java",
|
||||
".js",
|
||||
".json",
|
||||
".lua",
|
||||
".md",
|
||||
".py",
|
||||
".rs",
|
||||
".toml",
|
||||
".xml",
|
||||
".pom"
|
||||
])
|
||||
videos: list = field(default_factory=lambda:[
|
||||
".mkv",
|
||||
".mp4",
|
||||
".webm",
|
||||
".avi",
|
||||
".mov",
|
||||
".m4v",
|
||||
".mpg",
|
||||
".mpeg",
|
||||
".wmv",
|
||||
".flv"
|
||||
])
|
||||
office: list = field(default_factory=lambda: [
|
||||
".doc",
|
||||
".docx",
|
||||
".xls",
|
||||
".xlsx",
|
||||
".xlt",
|
||||
".xltx",
|
||||
".xlm",
|
||||
".ppt",
|
||||
".pptx",
|
||||
".pps",
|
||||
".ppsx",
|
||||
".odt",
|
||||
".rtf"
|
||||
])
|
||||
images: list = field(default_factory=lambda: [
|
||||
".png",
|
||||
".jpg",
|
||||
".jpeg",
|
||||
".gif",
|
||||
".ico",
|
||||
".tga",
|
||||
".webp"
|
||||
])
|
||||
text: list = field(default_factory=lambda: [
|
||||
".txt",
|
||||
".text",
|
||||
".sh",
|
||||
".cfg",
|
||||
".conf",
|
||||
".log"
|
||||
])
|
||||
music: list = field(default_factory=lambda: [
|
||||
".psf",
|
||||
".mp3",
|
||||
".ogg",
|
||||
".flac",
|
||||
".m4a"
|
||||
])
|
||||
pdf: list = field(default_factory=lambda: [
|
||||
".pdf"
|
||||
])
|
||||
31
src/libs/settings/options/settings.py
Normal file
31
src/libs/settings/options/settings.py
Normal file
@@ -0,0 +1,31 @@
|
||||
# Python imports
|
||||
from dataclasses import dataclass, field
|
||||
from dataclasses import asdict
|
||||
|
||||
# Gtk imports
|
||||
|
||||
# Application imports
|
||||
from .config import Config
|
||||
from .filters import Filters
|
||||
from .theming import Theming
|
||||
from .debugging import Debugging
|
||||
|
||||
|
||||
@dataclass
|
||||
class Settings:
|
||||
load_defaults: bool = True
|
||||
config: Config = field(default_factory=lambda: Config())
|
||||
filters: Filters = field(default_factory=lambda: Filters())
|
||||
theming: Theming = field(default_factory=lambda: Theming())
|
||||
debugging: Debugging = field(default_factory=lambda: Debugging())
|
||||
|
||||
def __post_init__(self):
|
||||
if not self.load_defaults:
|
||||
self.load_defaults = False
|
||||
self.config = Config(**self.config)
|
||||
self.filters = Filters(**self.filters)
|
||||
self.theming = Theming(**self.theming)
|
||||
self.debugging = Debugging(**self.debugging)
|
||||
|
||||
def as_dict(self):
|
||||
return asdict(self)
|
||||
16
src/libs/settings/options/theming.py
Normal file
16
src/libs/settings/options/theming.py
Normal file
@@ -0,0 +1,16 @@
|
||||
# Python imports
|
||||
from dataclasses import dataclass
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
@dataclass
|
||||
class Theming:
|
||||
transparency: int = 64
|
||||
default_zoom: int = 12
|
||||
syntax_theme: str = "penguins-in-space"
|
||||
success_color: str = "#88cc27"
|
||||
warning_color: str = "#ffa800"
|
||||
error_color: str = "#ff0000"
|
||||
3
src/libs/settings/other/__init__.py
Normal file
3
src/libs/settings/other/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
Settings.Other Package
|
||||
"""
|
||||
42
src/libs/settings/other/webkit_ui_settings.py
Normal file
42
src/libs/settings/other/webkit_ui_settings.py
Normal file
@@ -0,0 +1,42 @@
|
||||
# Python imports
|
||||
|
||||
# Lib imports
|
||||
import gi
|
||||
gi.require_version('WebKit2', '4.0')
|
||||
from gi.repository import WebKit2
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
class WebkitUISettings(WebKit2.Settings):
|
||||
def __init__(self):
|
||||
super(WebkitUISettings, self).__init__()
|
||||
|
||||
self._set_default_settings()
|
||||
|
||||
|
||||
# Note: Highly insecure setup but most "app" like setup I could think of.
|
||||
# Audit heavily any scripts/links ran/clicked under this setup!
|
||||
def _set_default_settings(self):
|
||||
self.set_enable_xss_auditor(True)
|
||||
self.set_enable_hyperlink_auditing(True)
|
||||
# self.set_enable_xss_auditor(False)
|
||||
# self.set_enable_hyperlink_auditing(False)
|
||||
self.set_allow_file_access_from_file_urls(True)
|
||||
self.set_allow_universal_access_from_file_urls(True)
|
||||
|
||||
self.set_enable_page_cache(False)
|
||||
self.set_enable_offline_web_application_cache(False)
|
||||
self.set_enable_html5_local_storage(False)
|
||||
self.set_enable_html5_database(False)
|
||||
|
||||
self.set_enable_fullscreen(False)
|
||||
self.set_print_backgrounds(False)
|
||||
self.set_enable_tabs_to_links(False)
|
||||
self.set_enable_developer_extras(True)
|
||||
self.set_enable_webrtc(True)
|
||||
self.set_enable_webaudio(True)
|
||||
self.set_enable_accelerated_2d_canvas(True)
|
||||
|
||||
self.set_user_agent(f"{APP_NAME}")
|
||||
123
src/libs/settings/path_manager.py
Normal file
123
src/libs/settings/path_manager.py
Normal file
@@ -0,0 +1,123 @@
|
||||
# Python imports
|
||||
import json
|
||||
import zipfile
|
||||
|
||||
from os import path
|
||||
from os import mkdir
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
class MissingConfigError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class PathManager:
|
||||
def __init__(self):
|
||||
self._SCRIPT_PTH: str = path.dirname(path.realpath(__file__))
|
||||
self._USER_HOME: str = path.expanduser('~')
|
||||
self._HOME_CONFIG_PATH: str = f"{self._USER_HOME}/.config/{APP_NAME.lower()}"
|
||||
self._USR_PATH: str = f"/usr/share/{APP_NAME.lower()}"
|
||||
self._USR_CONFIG_FILE: str = f"{self._USR_PATH}/settings.json"
|
||||
|
||||
self._CONTEXT_PATH: str = f"{self._HOME_CONFIG_PATH}/context_path"
|
||||
self._PLUGINS_PATH: str = f"{self._HOME_CONFIG_PATH}/plugins"
|
||||
self._DEFAULT_ICONS: str = f"{self._HOME_CONFIG_PATH}/icons"
|
||||
self._CONFIG_FILE: str = f"{self._HOME_CONFIG_PATH}/settings.json"
|
||||
self._GLADE_FILE: str = f"{self._HOME_CONFIG_PATH}/Main_Window.glade"
|
||||
self._CSS_FILE: str = f"{self._HOME_CONFIG_PATH}/stylesheet.css"
|
||||
self._KEY_BINDINGS_FILE: str = f"{self._HOME_CONFIG_PATH}/key-bindings.json"
|
||||
self._PID_FILE: str = f"{self._HOME_CONFIG_PATH}/{APP_NAME.lower()}.pid"
|
||||
self._UI_WIDEGTS_PATH: str = f"{self._HOME_CONFIG_PATH}/ui_widgets"
|
||||
self._CONTEXT_MENU: str = f"{self._HOME_CONFIG_PATH}/contexct_menu.json"
|
||||
self._WINDOW_ICON: str = f"{self._DEFAULT_ICONS}/{APP_NAME.lower()}.png"
|
||||
|
||||
# self._USR_CONFIG_FILE: str = f"{self._USR_PATH}/settings.json"
|
||||
# self._PLUGINS_PATH: str = f"plugins"
|
||||
# self._CONFIG_FILE: str = f"settings.json"
|
||||
# self._GLADE_FILE: str = f"Main_Window.glade"
|
||||
# self._CSS_FILE: str = f"stylesheet.css"
|
||||
# self._KEY_BINDINGS_FILE: str = f"key-bindings.json"
|
||||
# self._PID_FILE: str = f"{APP_NAME.lower()}.pid"
|
||||
# self._WINDOW_ICON: str = f"{APP_NAME.lower()}.png"
|
||||
# self._UI_WIDEGTS_PATH: str = f"ui_widgets"
|
||||
# self._CONTEXT_MENU: str = f"contexct_menu.json"
|
||||
# self._DEFAULT_ICONS: str = f"icons"
|
||||
|
||||
|
||||
# with zipfile.ZipFile("files.zip", mode="r", allowZip64=True) as zf:
|
||||
# with io.TextIOWrapper(zf.open("text1.txt"), encoding="utf-8") as f:
|
||||
|
||||
|
||||
if not path.exists(self._HOME_CONFIG_PATH):
|
||||
mkdir(self._HOME_CONFIG_PATH)
|
||||
if not path.exists(self._PLUGINS_PATH):
|
||||
mkdir(self._PLUGINS_PATH)
|
||||
|
||||
if not path.exists(self._DEFAULT_ICONS):
|
||||
self._DEFAULT_ICONS = f"{self._USR_PATH}/icons"
|
||||
if not path.exists(self._DEFAULT_ICONS):
|
||||
raise MissingConfigError("Unable to find the application icons directory.")
|
||||
if not path.exists(self._GLADE_FILE):
|
||||
self._GLADE_FILE = f"{self._USR_PATH}/Main_Window.glade"
|
||||
if not path.exists(self._GLADE_FILE):
|
||||
raise MissingConfigError("Unable to find the application Glade file.")
|
||||
if not path.exists(self._KEY_BINDINGS_FILE):
|
||||
self._KEY_BINDINGS_FILE = f"{self._USR_PATH}/key-bindings.json"
|
||||
if not path.exists(self._KEY_BINDINGS_FILE):
|
||||
raise MissingConfigError("Unable to find the application Keybindings file.")
|
||||
if not path.exists(self._CSS_FILE):
|
||||
self._CSS_FILE = f"{self._USR_PATH}/stylesheet.css"
|
||||
if not path.exists(self._CSS_FILE):
|
||||
raise MissingConfigError("Unable to find the application Stylesheet file.")
|
||||
if not path.exists(self._WINDOW_ICON):
|
||||
self._WINDOW_ICON = f"{self._USR_PATH}/icons/{APP_NAME.lower()}.png"
|
||||
if not path.exists(self._WINDOW_ICON):
|
||||
raise MissingConfigError("Unable to find the application icon.")
|
||||
if not path.exists(self._UI_WIDEGTS_PATH):
|
||||
self._UI_WIDEGTS_PATH = f"{self._USR_PATH}/ui_widgets"
|
||||
if not path.exists(self._CONTEXT_MENU):
|
||||
self._CONTEXT_MENU = f"{self._USR_PATH}/contexct_menu.json"
|
||||
|
||||
|
||||
def get_glade_file(self) -> str: return self._GLADE_FILE
|
||||
def get_ui_widgets_path(self) -> str: return self._UI_WIDEGTS_PATH
|
||||
def get_context_path(self) -> str: return self._CONTEXT_PATH
|
||||
def get_plugins_path(self) -> str: return self._PLUGINS_PATH
|
||||
def get_css_file(self) -> str: return self._CSS_FILE
|
||||
def get_home_config_path(self) -> str: return self._HOME_CONFIG_PATH
|
||||
def get_window_icon(self) -> str: return self._WINDOW_ICON
|
||||
def get_home_path(self) -> str: return self._USER_HOME
|
||||
|
||||
def load_keybindings(self):
|
||||
try:
|
||||
with open(self._KEY_BINDINGS_FILE) as file:
|
||||
return json.load(file)["keybindings"]
|
||||
except Exception as e:
|
||||
print( f"Settings Path Manager: {self._KEY_BINDINGS_FILE}\n\t\t{repr(e)}" )
|
||||
return {}
|
||||
|
||||
def load_context_menu_data(self):
|
||||
try:
|
||||
with open(self._CONTEXT_MENU) as file:
|
||||
return json.load(file)
|
||||
except Exception as e:
|
||||
print( f"Settings Path Manager: {self._CONTEXT_MENU}\n\t\t{repr(e)}" )
|
||||
return {}
|
||||
|
||||
def load_settings(self):
|
||||
if not path.exists(self._CONFIG_FILE):
|
||||
return None
|
||||
|
||||
with open(self._CONFIG_FILE) as file:
|
||||
data = json.load(file)
|
||||
data["load_defaults"] = False
|
||||
return data
|
||||
|
||||
def save_settings(self, settings: any):
|
||||
with open(self._CONFIG_FILE, 'w') as outfile:
|
||||
json.dump(settings.as_dict(), outfile, separators=(',', ':'), indent=4)
|
||||
65
src/libs/settings/start_check_mixin.py
Normal file
65
src/libs/settings/start_check_mixin.py
Normal file
@@ -0,0 +1,65 @@
|
||||
# Python imports
|
||||
import os
|
||||
import json
|
||||
import inspect
|
||||
from contextlib import suppress
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
|
||||
class StartCheckMixin:
|
||||
def is_dirty_start(self) -> bool:
|
||||
return self._dirty_start
|
||||
|
||||
def clear_pid(self):
|
||||
if not self.is_trace_debug():
|
||||
self._clean_pid()
|
||||
|
||||
def do_dirty_start_check(self):
|
||||
if self.is_trace_debug():
|
||||
pid = os.getpid()
|
||||
self._print_pid(pid)
|
||||
return
|
||||
|
||||
if os.path.exists(self.path_manager._PID_FILE):
|
||||
with open(self.path_manager._PID_FILE, "r") as f:
|
||||
pid = f.readline().strip()
|
||||
if pid not in ("", None):
|
||||
if self.is_pid_alive( int(pid) ):
|
||||
print("PID file exists and PID is alive... Letting downstream errors (sans debug args) handle app closure propigation.")
|
||||
return
|
||||
|
||||
self._write_new_pid()
|
||||
|
||||
""" Check For the existence of a unix pid. """
|
||||
def is_pid_alive(self, pid):
|
||||
print(f"PID Found: {pid}")
|
||||
|
||||
try:
|
||||
os.kill(pid, 0)
|
||||
except OSError:
|
||||
print(f"{APP_NAME} PID file exists but PID is irrelevant; starting dirty...")
|
||||
self._dirty_start = True
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def _write_new_pid(self):
|
||||
pid = os.getpid()
|
||||
self._write_pid(pid)
|
||||
self._print_pid(pid)
|
||||
|
||||
def _print_pid(self, pid):
|
||||
print(f"{APP_NAME} PID: {pid}")
|
||||
|
||||
def _clean_pid(self):
|
||||
with suppress(FileNotFoundError, PermissionError):
|
||||
os.unlink(self.path_manager._PID_FILE)
|
||||
|
||||
def _write_pid(self, pid):
|
||||
with open(self.path_manager._PID_FILE, "w") as _pid:
|
||||
_pid.write(f"{pid}")
|
||||
29
src/libs/singleton.py
Normal file
29
src/libs/singleton.py
Normal file
@@ -0,0 +1,29 @@
|
||||
# Python imports
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
class SingletonError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class Singleton:
|
||||
_instance = None
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
if cls._instance is not None:
|
||||
logger.debug(f"'{cls.__name__}' is a Singleton. Returning instance...")
|
||||
return cls._instance
|
||||
|
||||
cls._instance = super(Singleton, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
if self._instance is not None:
|
||||
return
|
||||
|
||||
super(Singleton, self).__init__()
|
||||
26
src/libs/singleton_raised.py
Normal file
26
src/libs/singleton_raised.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# Python imports
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
class SingletonError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class SingletonRaised:
|
||||
_instance = None
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
if cls._instance is not None:
|
||||
raise SingletonError(f"'{cls.__name__}' is a Singleton. Cannot create a new instance...")
|
||||
|
||||
cls._instance = super(Singleton, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
if cls._instance is not None:
|
||||
return
|
||||
67
src/libs/status_icon.py
Normal file
67
src/libs/status_icon.py
Normal file
@@ -0,0 +1,67 @@
|
||||
# Python imports
|
||||
|
||||
# Lib imports
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
gi.require_version('AppIndicator3', '0.1')
|
||||
from gi.repository import Gtk
|
||||
from gi.repository import GLib
|
||||
from gi.repository import AppIndicator3
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
class StatusIcon():
|
||||
""" StatusIcon for Application to go to Status Tray. """
|
||||
|
||||
def __init__(self):
|
||||
self._setup_styling()
|
||||
self._setup_signals()
|
||||
self._subscribe_to_events()
|
||||
self._load_widgets()
|
||||
|
||||
|
||||
def _setup_styling(self):
|
||||
...
|
||||
|
||||
def _setup_signals(self):
|
||||
...
|
||||
|
||||
def _subscribe_to_events(self):
|
||||
...
|
||||
|
||||
def _load_widgets(self):
|
||||
status_menu = Gtk.Menu()
|
||||
icon_theme = Gtk.IconTheme.get_default()
|
||||
check_menu_item = Gtk.CheckMenuItem.new_with_label("Update icon")
|
||||
quit_menu_item = Gtk.MenuItem.new_with_label("Quit")
|
||||
|
||||
# Create StatusNotifierItem
|
||||
self.indicator = AppIndicator3.Indicator.new(
|
||||
f"{APP_NAME}-statusicon",
|
||||
"gtk-info",
|
||||
AppIndicator3.IndicatorCategory.APPLICATION_STATUS)
|
||||
self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
|
||||
|
||||
check_menu_item.connect("activate", self.check_menu_item_cb)
|
||||
quit_menu_item.connect("activate", self.quit_menu_item_cb)
|
||||
icon_theme.connect('changed', self.icon_theme_changed_cb)
|
||||
|
||||
self.indicator.set_menu(status_menu)
|
||||
status_menu.append(check_menu_item)
|
||||
status_menu.append(quit_menu_item)
|
||||
status_menu.show_all()
|
||||
|
||||
def update_icon(self, icon_name):
|
||||
self.indicator.set_icon(icon_name)
|
||||
|
||||
def check_menu_item_cb(self, widget, data = None):
|
||||
icon_name = "parole" if widget.get_active() else "gtk-info"
|
||||
self.update_icon(icon_name)
|
||||
|
||||
def icon_theme_changed_cb(self, theme):
|
||||
self.update_icon("gtk-info")
|
||||
|
||||
def quit_menu_item_cb(self, widget, data = None):
|
||||
event_system.emit("tear-down")
|
||||
Reference in New Issue
Block a user