Code Widget refactor; observable refactor
This commit is contained in:
16
src/libs/dto/code_event.py
Normal file
16
src/libs/dto/code_event.py
Normal file
@@ -0,0 +1,16 @@
|
||||
# Python imports
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
from .observable_event import ObservableEvent
|
||||
|
||||
|
||||
|
||||
@dataclass
|
||||
class CodeEvent(ObservableEvent):
|
||||
etype: str = ""
|
||||
view: any = None
|
||||
file: any = None
|
||||
buffer: any = None
|
||||
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:
|
||||
...
|
||||
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)
|
||||
@@ -15,8 +15,15 @@ class Singleton:
|
||||
_instance = None
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
if cls._instance:
|
||||
raise SingletonError(f"'{cls.__name__}' is a Singleton. Cannot create a new instance...")
|
||||
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
|
||||
Reference in New Issue
Block a user