11import orjson
22from typing import Union , List , Optional , Any
3+ from urllib .parse import urlparse
34from .tools import Permission
45from .utils import send_data
56
@@ -37,7 +38,7 @@ def __init__(self, host: str, port: int, username: str, password: str, store: Un
3738 def from_uri (cls , uri : str ) -> 'Engine' :
3839 """
3940 Creates an Engine instance from a URI string in the format:
40- montycat://host/port/ username/ password[/store]
41+ montycat://username: password@host:port [/store]
4142
4243 The store is optional. If not provided, it will be set to None.
4344
@@ -52,21 +53,24 @@ def from_uri(cls, uri: str) -> 'Engine':
5253 """
5354 if not uri .startswith ("montycat://" ):
5455 raise ValueError ("URI must use 'montycat://' protocol" )
55- parts = uri [len ("montycat://" ):].split ("/" )
56- if len (parts ) == 4 :
57- host , port_str , username , password = parts
58- store = None
59- elif len (parts ) == 5 :
60- host , port_str , username , password , store = parts
61- else :
62- raise ValueError ("Missing or extra parts in URI" )
63- if any (part == "" for part in [host , port_str , username , password ]):
64- raise ValueError ("Host, port, username, and password must be non-empty" )
65- try :
66- port = int (port_str )
67- except ValueError :
68- raise ValueError ("Port must be an integer" )
69- return cls (host , port , username , password , store )
56+
57+ parsed = urlparse (uri )
58+
59+ if not parsed .username or not parsed .password :
60+ raise ValueError ("Username and password must be provided" )
61+ if not parsed .hostname or not parsed .port :
62+ raise ValueError ("Host and port must be provided" )
63+
64+ # Optional store
65+ store = parsed .path [1 :] if parsed .path and len (parsed .path ) > 1 else None
66+
67+ return cls (
68+ host = parsed .hostname ,
69+ port = parsed .port ,
70+ username = parsed .username ,
71+ password = parsed .password ,
72+ store = store
73+ )
7074
7175 async def _execute_query_with_credentials (self , command : List [Any ]) -> Any :
7276 """
0 commit comments