10
10
import socket
11
11
import sys
12
12
from abc import ABCMeta , abstractmethod
13
- from typing import Any , Callable , ClassVar , Dict , List , Literal , Tuple , Type
13
+ from typing import Any , Callable , ClassVar , Tuple , Type
14
14
from urllib .parse import urljoin , urlsplit , urlunsplit
15
15
16
16
from . import pytestrunner
17
17
from .actions import actions
18
- from .asyncactions import async_actions
19
18
from .protocol import Protocol , WdspecProtocol
20
19
21
20
@@ -725,14 +724,14 @@ def __init__(self, logger, protocol, test_window):
725
724
self .protocol = protocol
726
725
self .test_window = test_window
727
726
self .logger = logger
728
- self .callbacks : Dict [ str , Callable [[ str , Any ], Tuple [ bool , Any ]]] = {
727
+ self .callbacks = {
729
728
"action" : self .process_action ,
730
729
"complete" : self .process_complete
731
730
}
732
731
733
732
self .actions = {cls .name : cls (self .logger , self .protocol ) for cls in actions }
734
733
735
- def __call__ (self , result : Tuple [ str , str , Dict ] ):
734
+ def __call__ (self , result ):
736
735
url , command , payload = result
737
736
self .logger .debug ("Got async callback: %s" % result [1 ])
738
737
try :
@@ -741,11 +740,11 @@ def __call__(self, result: Tuple[str, str, Dict]):
741
740
raise ValueError ("Unknown callback type %r" % result [1 ]) from e
742
741
return callback (url , payload )
743
742
744
- def process_complete (self , url : str , payload : List ) -> Tuple [ Literal [ True ], Any ] :
743
+ def process_complete (self , url , payload ) :
745
744
rv = [strip_server (url )] + payload
746
745
return True , rv
747
746
748
- def process_action (self , url : str , payload : Dict ) -> Tuple [ Literal [ False ], None ] :
747
+ def process_action (self , url , payload ) :
749
748
action = payload ["action" ]
750
749
cmd_id = payload ["id" ]
751
750
self .logger .debug (f"Got action: { action } " )
@@ -783,67 +782,10 @@ def process_action(self, url: str, payload: Dict) -> Tuple[Literal[False], None]
783
782
784
783
return False , None
785
784
786
- def _send_message (self , cmd_id : int , message_type : str , status : str , message : str = None ):
785
+ def _send_message (self , cmd_id , message_type , status , message = None ):
787
786
self .protocol .testdriver .send_message (cmd_id , message_type , status , message = message )
788
787
789
788
790
- class AsyncCallbackHandler (CallbackHandler ):
791
- """
792
- Handle synchronous and asynchronous actions. Extends `CallbackHandler` with support of async actions.
793
- """
794
-
795
- def __init__ (self , logger , protocol , test_window , loop ):
796
- super ().__init__ (logger , protocol , test_window )
797
- self .loop = loop
798
- self .async_actions = {cls .name : cls (self .logger , self .protocol ) for cls in async_actions }
799
-
800
- def process_action (self , url : str , payload : Dict [str , Any ]) -> Tuple [Literal [False ], None ]:
801
- action = payload ["action" ]
802
- if action in self .async_actions :
803
- # Schedule async action to be processed in the event loop and return immediately.
804
- self .logger .debug (f"Scheduling async action processing: { action } , { payload } " )
805
- self .loop .create_task (self ._process_async_action (action , payload ))
806
- return False , None
807
- else :
808
- # Fallback to the default action processing, which will fail if the action is not implemented.
809
- self .logger .debug (f"Processing synchronous action: { action } , { payload } " )
810
- return super ().process_action (url , payload )
811
-
812
- async def _process_async_action (self , action : str , payload : Dict [str , Any ]):
813
- """
814
- Process async action and send the result back to the test driver.
815
-
816
- This method is analogous to `process_action` but is intended to be used with async actions in a task, so it does
817
- not raise unexpected exceptions. However, the unexpected exceptions are logged and the error message is sent
818
- back to the test driver.
819
- """
820
- async_action_handler = self .async_actions [action ]
821
- cmd_id = payload ["id" ]
822
- try :
823
- result = await async_action_handler (payload )
824
- except AttributeError as e :
825
- # If we fail to get an attribute from the protocol presumably that's a
826
- # ProtocolPart we don't implement
827
- # AttributeError got an obj property in Python 3.10, for older versions we
828
- # fall back to looking at the error message.
829
- if ((hasattr (e , "obj" ) and getattr (e , "obj" ) == self .protocol ) or
830
- f"'{ self .protocol .__class__ .__name__ } ' object has no attribute" in str (e )):
831
- raise NotImplementedError from e
832
- except self .unimplemented_exc :
833
- self .logger .warning ("Action %s not implemented" % action )
834
- self ._send_message (cmd_id , "complete" , "error" , f"Action { action } not implemented" )
835
- except self .expected_exc as e :
836
- self .logger .debug (f"Action { action } failed with an expected exception: { e } " )
837
- self ._send_message (cmd_id , "complete" , "error" , f"Action { action } failed: { e } " )
838
- except Exception as e :
839
- self .logger .warning (f"Action { action } failed with an unexpected exception: { e } " )
840
- self ._send_message (cmd_id , "complete" , "error" , f"Unexpected exception: { e } " )
841
- else :
842
- self .logger .debug (f"Action { action } completed with result { result } " )
843
- return_message = {"result" : result }
844
- self ._send_message (cmd_id , "complete" , "success" , json .dumps (return_message ))
845
-
846
-
847
789
class ActionContext :
848
790
def __init__ (self , logger , protocol , context ):
849
791
self .logger = logger
0 commit comments