From 4b4db5b3c726f1290da8e4a8f372a9efd40c38f1 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 24 Mar 2023 15:40:16 +0100 Subject: [PATCH] Added `StoppableThread` common interface. --- platypush/common/__init__.py | 19 ++++++++++++++++--- platypush/common/_types.py | 12 ++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 platypush/common/_types.py diff --git a/platypush/common/__init__.py b/platypush/common/__init__.py index 04a43b1aa2..8a0f84215d 100644 --- a/platypush/common/__init__.py +++ b/platypush/common/__init__.py @@ -4,6 +4,8 @@ import os from platypush.utils.manifest import Manifest +from ._types import StoppableThread + logger = logging.getLogger('platypush') @@ -26,9 +28,20 @@ class ExtensionWithManifest: self._manifest = self.get_manifest() def get_manifest(self) -> Manifest: - manifest_file = os.path.join(os.path.dirname(inspect.getfile(self.__class__)), 'manifest.yaml') - assert os.path.isfile(manifest_file), ( - 'The extension {} has no associated manifest.yaml'.format(self.__class__.__name__) + manifest_file = os.path.join( + os.path.dirname(inspect.getfile(self.__class__)), 'manifest.yaml' + ) + assert os.path.isfile( + manifest_file + ), 'The extension {} has no associated manifest.yaml'.format( + self.__class__.__name__ ) return Manifest.from_file(manifest_file) + + +__all__ = [ + 'ExtensionWithManifest', + 'StoppableThread', + 'exec_wrapper', +] diff --git a/platypush/common/_types.py b/platypush/common/_types.py new file mode 100644 index 0000000000..b4455e4c20 --- /dev/null +++ b/platypush/common/_types.py @@ -0,0 +1,12 @@ +from abc import ABC, abstractmethod +from threading import Thread + + +class StoppableThread(Thread, ABC): + """ + Base interface for stoppable threads. + """ + + @abstractmethod + def stop(self): + ...