Skip to content

Commit

Permalink
Merge pull request #6 from grobbles/feature/add_classifiers
Browse files Browse the repository at this point in the history
Feature/add classifiers
  • Loading branch information
grobbles committed May 28, 2022
2 parents 3c31e52 + e1a82da commit b035eee
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 51 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Smart Signals

![Build](https://github.com/grobbles/SmartSignals/actions/workflows/test.yml/badge.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
![Build](https://github.com/grobbles/SmartSignals/actions/workflows/release.yml/badge.svg)
[![codecov](https://codecov.io/gh/grobbles/SmartSignals/branch/main/graph/badge.svg?token=GAHKYKS1SD)](https://codecov.io/gh/grobbles/SmartSignals)
[![PyPi version](https://badgen.net/pypi/v/SmartSignals/)](https://pypi.com/project/SmartSignals)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

# Install
# Install

You can install this package with pip from PyPi.

Expand All @@ -15,14 +16,16 @@ pip install SmartSignals
# Usage

````python
from smart_signals import *
from smart_signals import SmartSignal, SmartSignalSlot


class Test:
@SmartSignalSlot(str)
def slot(self, message: str):
print(message)
pass


test = Test()
signal = SmartSignal(str)
signal.connect(test.slot)
Expand Down
4 changes: 3 additions & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ coverage:
project:
default:
target: 100%
threshold: 15%
threshold: 15%
ignore:
- "smart_signals_tests"
11 changes: 9 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@
# "Topic :: Communications",
# "Topic :: Utilities"
# "Programming Language :: Python",

classifiers = [
'License :: OSI Approved :: MIT License',
'Development Status :: 3 - Alpha',
"Intended Audience :: Developers",
"Topic :: Communications",
"Topic :: Utilities",
"Programming Language :: Python",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
Expand All @@ -28,7 +35,7 @@

setuptools.setup(
name="SmartSignals",
version="0.0.0",
version="0.0.1",
author="Uwe Roder",
author_email="uweroder@gmail.com",
description="The SmartSignal lib is an event driven system similar to QT.",
Expand All @@ -37,7 +44,7 @@
url="https://github.com/grobbles/SmartSignals",
packages=["smart_signals"],
license='MIT',
# classifiers=classifiers,
classifiers=classifiers,
python_requires='>=3.7',
keywords=["smart_signals", "SmartSignals", "signals", "events", "slot", "qt"],
install_requires=requirements
Expand Down
86 changes: 61 additions & 25 deletions smart_signals/smart_signals.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import inspect
import sys
import threading
from typing import List, _GenericAlias

Expand All @@ -19,6 +18,11 @@ class SmartSlotWrongDataTypeException(Exception):
class SmartSignalSlot:

def __init__(self, *args):
"""
Constructor for the SmartSignalSlot
:param args: The list of data types for slot
"""
self._slot_types = args
self._owner = self._get_owner()
pass
Expand Down Expand Up @@ -48,6 +52,13 @@ class SmartSignal:
signal_lock = threading.RLock()

def __init__(self, *args, name: str = None, multithreading: bool = False):
"""
Constructor for the Smart Signal
:param args: The list of data types to be transferred.
:param name: The name of the signal for logging
:param multithreading: The multithreading flag to enable multi threading
"""
self._name = name
self._multithreading = multithreading
self._slot_types = args
Expand All @@ -58,37 +69,70 @@ def __init__(self, *args, name: str = None, multithreading: bool = False):
pass

def __str__(self) -> str:
"""
Generate the SmartSignal string
:return: The SmartSignal string
"""
elements = list()
if self._name:
elements.append(f"name: {self._name})")
elements.append(f"multithreading: {self._multithreading}")
elements.append(f"name: '{self._name}'")
elements.append(f"multithreading: '{self._multithreading}'")
return f"PySignal( {', '.join(elements)} )"

@classmethod
def _get_sender(cls):
"""Try to get the bound, class or module method calling the emit."""
prev_frame = sys._getframe(2)
func_name = prev_frame.f_code.co_name

# Faster to try/catch than checking for 'self'
try:
return getattr(prev_frame.f_locals['self'], func_name)

except KeyError:
return getattr(inspect.getmodule(prev_frame), func_name)

@property
def slots(self):
"""
Return the connected slots
:return: A list of connected slots
"""
return self._slots

def reset(self):
"""
Resets all connected slots.
:return: None
"""
self._slots = []
pass

def connect(self, slot):
"""
Connects the slot to the smart signal.
:param slot: The slot must be a callable object.
:return: None
"""
if not callable(slot):
raise SmartSignalWrongSlotTypeException(f"The solt: '{slot.__class__.__name__}' is not a callable object!!!")

def delete_connection(self, slot):
if slot not in self._slots:
self._slots.append(slot)
pass

def disconnect(self, slot) -> bool:
"""
Disconnects the slot from the smart signal.
:param slot: The solt to be disconnected.
:return: If the slot could be disconnected, then return True, otherwise return False
"""
if slot in self._slots:
self._slots.remove(slot)
return True
return False

def emit(self, *args, **kwargs):
"""
Calls all connected slots and passes them the args and the kwargs. This function check the types.
If the multithreading flag has been set, then a thread is created for emitting.
:param args: Args to emit to the slots
:param kwargs: Kwargs to emit to the slots
:return: None
"""
if self._slot_types is not None:
if len(args) != len(self._slot_types):
raise SmartSignalWrongDataTypeException(f"The number of input parameters: {len(args)} does not match those in the slot definition: {len(self._slot_types)}!")
Expand Down Expand Up @@ -129,11 +173,3 @@ def _emit_thread_runner(self, *args, **kwargs):
for slot in self._slots:
slot(*args, **kwargs)
pass

def connect(self, slot):
if not callable(slot):
raise SmartSignalWrongSlotTypeException(f"The solt: '{slot.__class__.__name__}' is not a callable object!!!")

if slot not in self._slots:
self._slots.append(slot)
pass
Loading

0 comments on commit b035eee

Please sign in to comment.