1- from typing import Type , List , Optional , Union , Dict
1+ from typing import Type , Optional , Union , Dict
22from itertools import zip_longest
3- import dsnparse
43from contextlib import suppress
4+ import dsnparse
55
66from runtype import dataclass
77
2424@dataclass
2525class MatchUriPath :
2626 database_cls : Type [Database ]
27- params : List [str ]
28- kwparams : List [str ] = []
29- help_str : str = "<unspecified>"
30-
31- def __post_init__ (self ):
32- assert self .params == self .database_cls .CONNECT_URI_PARAMS , self .params
33- assert self .help_str == self .database_cls .CONNECT_URI_HELP , "\n %s\n %s" % (
34- self .help_str ,
35- self .database_cls .CONNECT_URI_HELP ,
36- )
37- assert self .kwparams == self .database_cls .CONNECT_URI_KWPARAMS
3827
3928 def match_path (self , dsn ):
29+ help_str = self .database_cls .CONNECT_URI_HELP
30+ params = self .database_cls .CONNECT_URI_PARAMS
31+ kwparams = self .database_cls .CONNECT_URI_KWPARAMS
32+
4033 dsn_dict = dict (dsn .query )
4134 matches = {}
42- for param , arg in zip_longest (self . params , dsn .paths ):
35+ for param , arg in zip_longest (params , dsn .paths ):
4336 if param is None :
44- raise ValueError (f"Too many parts to path. Expected format: { self . help_str } " )
37+ raise ValueError (f"Too many parts to path. Expected format: { help_str } " )
4538
4639 optional = param .endswith ("?" )
4740 param = param .rstrip ("?" )
@@ -51,26 +44,26 @@ def match_path(self, dsn):
5144 arg = dsn_dict .pop (param )
5245 except KeyError :
5346 if not optional :
54- raise ValueError (f"URI must specify '{ param } '. Expected format: { self . help_str } " )
47+ raise ValueError (f"URI must specify '{ param } '. Expected format: { help_str } " )
5548
5649 arg = None
5750
5851 assert param and param not in matches
5952 matches [param ] = arg
6053
61- for param in self . kwparams :
54+ for param in kwparams :
6255 try :
6356 arg = dsn_dict .pop (param )
6457 except KeyError :
65- raise ValueError (f"URI must specify '{ param } '. Expected format: { self . help_str } " )
58+ raise ValueError (f"URI must specify '{ param } '. Expected format: { help_str } " )
6659
6760 assert param and arg and param not in matches , (param , arg , matches .keys ())
6861 matches [param ] = arg
6962
7063 for param , value in dsn_dict .items ():
7164 if param in matches :
7265 raise ValueError (
73- f"Parameter '{ param } ' already provided as positional argument. Expected format: { self . help_str } "
66+ f"Parameter '{ param } ' already provided as positional argument. Expected format: { help_str } "
7467 )
7568
7669 matches [param ] = value
@@ -100,7 +93,7 @@ class Connect:
10093 def __init__ (self , database_by_scheme : Dict [str , Database ]):
10194 self .database_by_scheme = database_by_scheme
10295 self .match_uri_path = {
103- name : MatchUriPath (cls , cls . CONNECT_URI_PARAMS , cls . CONNECT_URI_KWPARAMS , help_str = cls . CONNECT_URI_HELP )
96+ name : MatchUriPath (cls )
10497 for name , cls in database_by_scheme .items ()
10598 }
10699 self .conn_cache = WeakCache ()
0 commit comments