diff --git a/ipcqueue/posixmq.py b/ipcqueue/posixmq.py index 34a1bfb..cc52bf7 100644 --- a/ipcqueue/posixmq.py +++ b/ipcqueue/posixmq.py @@ -2,7 +2,9 @@ Interprocess POSIX message queue implementation. """ -from .serializers import PickleSerializer +from typing import Type + +from .serializers import PickleSerializer, SerializerBase try: import Queue as queue @@ -73,7 +75,7 @@ class Queue(object): POSIX message queue. """ - def __init__(self, name, maxsize=10, maxmsgsize=1024, serializer=PickleSerializer): + def __init__(self, name, maxsize=10, maxmsgsize=1024, serializer: Type[SerializerBase]=PickleSerializer): """ Constructor for message queue. *name* is an unique identifier of the queue, must starts with ``/``. *maxsize* is an integer that sets diff --git a/ipcqueue/serializers.py b/ipcqueue/serializers.py index e3471b5..df3e0b4 100644 --- a/ipcqueue/serializers.py +++ b/ipcqueue/serializers.py @@ -1,9 +1,24 @@ +import abc +from typing import Any + try: import cPickle as pickle except ImportError: import pickle +class SerializerBase(abc.ABC): + @staticmethod + @abc.abstractmethod + def dumps(obj) -> bytes: + raise NotImplementedError() + + @staticmethod + @abc.abstractmethod + def loads(obj) -> Any: + raise NotImplementedError() + + class PickleSerializer: @staticmethod def dumps(obj): diff --git a/ipcqueue/sysvmq.py b/ipcqueue/sysvmq.py index d39f264..4976254 100644 --- a/ipcqueue/sysvmq.py +++ b/ipcqueue/sysvmq.py @@ -2,7 +2,9 @@ Interprocess SYS V message queue implementation. """ -from .serializers import PickleSerializer +from typing import Type + +from .serializers import PickleSerializer, SerializerBase try: import Queue as queue @@ -58,7 +60,7 @@ class Queue(object): SYS V message queue. """ - def __init__(self, key=None, max_bytes=None, serializer=PickleSerializer): + def __init__(self, key=None, max_bytes=None, serializer: Type[SerializerBase]=PickleSerializer): """ Constructor for message queue. *key* is an unique identifier of the queue, must be positive number or ``0`` for private queue.