Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] pyDKB/message: introduce for_update() method. #321

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Utils/Dataflow/pyDKB/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.20200210
0.3.20200217
34 changes: 34 additions & 0 deletions Utils/Dataflow/pyDKB/dataflow/communication/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class AbstractMessage(object):
encoded = None

incompl = None
update = None

def __init__(self, message=None):
""" Save initial message. """
Expand Down Expand Up @@ -109,6 +110,20 @@ def incomplete(self, status=None):
self.incompl = bool(status)
return old

def for_update(self, status=None):
""" Set message for-update marker and/or get previous/current value.

:param status: new status (if not passed, current status is returned)
:type status: bool, NoneType

:return: for-update marker status (previous value, if reset)
:rtype: bool
"""
old = self.update
if status is not None:
self.update = bool(status)
return old


class JSONMessage(AbstractMessage):
""" Message in JSON format. """
Expand All @@ -118,6 +133,7 @@ class JSONMessage(AbstractMessage):
_ext = ".json"

incompl_key = "_incomplete"
update_key = "_update"

def decode(self, code=codeType.STRING):
""" Decode original data as JSON. """
Expand Down Expand Up @@ -159,6 +175,24 @@ def incomplete(self, status=None):
self.encoded = None
return super(JSONMessage, self).incomplete(status)

def for_update(self, status=None):
""" Set message for-update marker and/or get previous/current value.

For JSON messages the marker is implemented as additional field:
"_update".

:param status: new status (if not passed, current status is returned)
:type status: bool, NoneType

:return: for-update marker status (previous value, if reset)
:rtype: bool
"""
if status is not None:
decoded = self.decode()
decoded[self.update_key] = status
self.encoded = None
return super(JSONMessage, self).for_update(status)


__message_class[messageType.JSON] = JSONMessage

Expand Down