From 0fe8eb63add91b2f4cd08e849af2fda59f0584a3 Mon Sep 17 00:00:00 2001 From: Proskurin Dmitrii Date: Thu, 23 Nov 2023 11:15:17 +0300 Subject: [PATCH] extend Command with creation_time & from_command (#172) Co-authored-by: Dmitrii Proskurin --- core/basic_models/actions/command.py | 41 ++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/core/basic_models/actions/command.py b/core/basic_models/actions/command.py index 7ee5245d..997b81a3 100644 --- a/core/basic_models/actions/command.py +++ b/core/basic_models/actions/command.py @@ -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 @@ -24,6 +36,7 @@ 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): @@ -31,3 +44,27 @@ def raw(self): 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), + )