Skip to content

Commit

Permalink
extend Command with creation_time & from_command (#172)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitrii Proskurin <[email protected]>
  • Loading branch information
DimaProskurin and Dmitrii Proskurin authored Nov 23, 2023
1 parent 015dbc6 commit 0fe8eb6
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions core/basic_models/actions/command.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
# coding: utf-8
import time
from copy import deepcopy
from typing import Dict, Any


class Command:
def __init__(self, name=None, params=None, action_id=None, request_type=None, request_data=None, loader=None,
need_payload_wrap=True, need_message_name=True):
def __init__(
self,
name=None,
params=None,
action_id=None,
request_type=None,
request_data=None,
loader=None,
need_payload_wrap=True,
need_message_name=True,
**kwargs
):
"""
Initialize Command instance with params
Expand All @@ -24,10 +36,35 @@ def __init__(self, name=None, params=None, action_id=None, request_type=None, re
self.loader: str = loader or "json.dumps"
self.need_payload_wrap: bool = need_payload_wrap
self.need_message_name: bool = need_message_name
self.creation_time = time.time()

@property
def raw(self):
message: Dict[str, Any] = {"payload": self.payload} if self.need_payload_wrap else self.payload
if self.need_message_name:
message["messageName"] = self.name
return message

@classmethod
def from_command(
cls,
command: "Command",
name=None,
params=None,
action_id=None,
request_type=None,
request_data=None,
loader=None,
need_payload_wrap=None,
need_message_name=None,
) -> "Command":
return cls(
name=(name or command.name),
params=(params or deepcopy(command.payload)),
action_id=(action_id or command.action_id),
request_type=(request_type or command.request_type),
request_data=(request_data or deepcopy(command.request_data)),
loader=(loader or command.loader),
need_payload_wrap=(need_payload_wrap or command.need_payload_wrap),
need_message_name=(need_message_name or command.need_message_name),
)

0 comments on commit 0fe8eb6

Please sign in to comment.