11from urllib .parse import urlunparse , urlencode
2- from contextlib import contextmanager , AbstractContextManager
3- from idom .core .element import ElementConstructor
2+ from contextlib import contextmanager
43from typing import (
54 Callable ,
6- NamedTuple ,
75 Tuple ,
86 Iterator ,
97 Type ,
108 Optional ,
11- Union ,
129 Any ,
1310 Dict ,
11+ Generic ,
12+ TypeVar ,
1413)
1514
1615from selenium .webdriver .remote .webdriver import WebDriver
2423
2524
2625__all__ = [
27- "server_base_url" ,
2826 "find_available_port" ,
2927 "create_simple_selenium_web_driver" ,
30- "create_mount_and_server" ,
31- "MountAndServer" ,
28+ "ServerMountPoint" ,
3229]
3330
3431
35- MountType = Union [Callable [[ElementConstructor ], None ], Any ]
36- MountContext = Callable [[], "AbstractContextManager[MountType]" ]
37- AnyAbstractRenderServer = AbstractRenderServer [Any , Any ]
38-
39-
40- def server_base_url (host : str , port : int , path : str = "" , query : Optional [Any ] = None ):
41- return urlunparse (["http" , f"{ host } :{ port } " , path , "" , urlencode (query or ()), "" ])
42-
43-
44- class MountAndServer (NamedTuple ):
45- mount : MountContext
46- server : AnyAbstractRenderServer
32+ AnyRenderServer = AbstractRenderServer [Any , Any ]
4733
4834
4935def create_simple_selenium_web_driver (
@@ -62,49 +48,68 @@ def create_simple_selenium_web_driver(
6248 return driver
6349
6450
65- def create_mount_and_server (
66- server_type : Type [AnyAbstractRenderServer ] = PerClientStateServer ,
67- host : str = "127.0.0.1" ,
68- port : Optional [int ] = None ,
69- server_config : Optional [Any ] = None ,
70- run_kwargs : Optional [Dict [str , Any ]] = None ,
71- mount_and_server_constructor : Callable [..., Any ] = hotswap_server ,
72- app : Optional [Any ] = None ,
73- ** other_options : Any ,
74- ) -> MountAndServer :
75- mount , server = mount_and_server_constructor (
76- (
77- server_type
78- if issubclass (server_type , _RenderServerWithLastError )
79- else type (
80- server_type .__name__ ,
81- (_RenderServerWithLastError , server_type ),
82- {"last_server_error_for_idom_testing" : Ref (None )},
83- )
84- ),
85- host ,
86- port or find_available_port (host ),
87- server_config ,
88- run_kwargs ,
89- app ,
90- ** other_options ,
91- )
92-
93- assert isinstance (server , _RenderServerWithLastError )
51+ _Mount = TypeVar ("_Mount" )
52+ _Server = TypeVar ("_Server" , bound = AnyRenderServer )
53+
54+
55+ class ServerMountPoint (Generic [_Mount , _Server ]):
56+
57+ __slots__ = "server" , "host" , "port" , "_mount"
58+
59+ def __init__ (
60+ self ,
61+ server_type : Type [_Server ] = PerClientStateServer ,
62+ host : str = "127.0.0.1" ,
63+ port : Optional [int ] = None ,
64+ server_config : Optional [Any ] = None ,
65+ run_kwargs : Optional [Dict [str , Any ]] = None ,
66+ mount_and_server_constructor : "Callable[..., Tuple[_Mount, _Server]]" = hotswap_server ,
67+ app : Optional [Any ] = None ,
68+ ** other_options : Any ,
69+ ):
70+ self .host = host
71+ self .port = port or find_available_port (host )
72+ self ._mount , self .server = mount_and_server_constructor (
73+ (
74+ server_type
75+ if issubclass (server_type , _RenderServerWithLastError )
76+ else type (
77+ server_type .__name__ ,
78+ (_RenderServerWithLastError , server_type ),
79+ {"last_server_error_for_idom_testing" : Ref (None )},
80+ )
81+ ),
82+ self .host ,
83+ self .port ,
84+ server_config ,
85+ run_kwargs ,
86+ app ,
87+ ** other_options ,
88+ )
89+
90+ def url (self , path : str = "" , query : Optional [Any ] = None ) -> str :
91+ return urlunparse (
92+ [
93+ "http" ,
94+ f"{ self .host } :{ self .port } " ,
95+ path ,
96+ "" ,
97+ urlencode (query or ()),
98+ "" ,
99+ ]
100+ )
94101
95102 @contextmanager
96- def mount_context ( ) -> Iterator [MountType ]:
97- server .last_server_error_for_idom_testing .current = None
103+ def open_mount_function ( self ) -> Iterator [_Mount ]:
104+ self . server .last_server_error_for_idom_testing .current = None
98105 try :
99- yield mount
106+ yield self . _mount
100107 finally :
101- if server .last_server_error_for_idom_testing .current is not None :
102- raise server .last_server_error_for_idom_testing .current
103-
104- return MountAndServer (mount_context , server )
108+ if self .server .last_server_error_for_idom_testing .current is not None :
109+ raise self .server .last_server_error_for_idom_testing .current
105110
106111
107- class _RenderServerWithLastError (AnyAbstractRenderServer ):
112+ class _RenderServerWithLastError (AnyRenderServer ):
108113 """A server that updates the ``last_server_error`` fixture"""
109114
110115 last_server_error_for_idom_testing : Ref [Optional [Exception ]]
0 commit comments