@@ -56,7 +56,7 @@ def parse_spanner_url(spanner_url):
5656 for k , v in amap .items ():
5757 combined [k ] = v
5858
59- return combined
59+ return filter_out_unset_keys ( combined )
6060
6161
6262def parse_properties (kv_pairs , sep = ';' ):
@@ -75,6 +75,22 @@ def parse_properties(kv_pairs, sep=';'):
7575 else :
7676 kvp [kvs [0 ]] = kvs [1 :]
7777
78+ as_bools = ['autocommit' , 'readonly' ]
79+ for as_bool_key in as_bools :
80+ value = kvp .get (as_bool_key , None )
81+ if value is None :
82+ continue
83+
84+ as_bool_value = True
85+ if value == '1' or value == '0' :
86+ as_bool_value = value == '1'
87+ elif value == 'True' or value == 'true' :
88+ as_bool_value = True
89+ elif value == 'False' or value == 'False' :
90+ as_bool_value = False
91+
92+ kvp [as_bool_key ] = as_bool_value
93+
7894 return kvp
7995
8096
@@ -102,31 +118,69 @@ def parse_projectid_instance_dbname(url_path):
102118
103119
104120def extract_connection_params (settings_dict ):
105- # We'll expect settings in the form of either:
106- # {
107- # "NAME": "spanner",
108- # "INSTANCE": "instance",
109- # "AUTOCOMMIT": True or False,
110- # "READ_ONLY": True or False,
111- # "PROJECT_ID": "<project_id>",
112- # }
113- #
114- # OR
115- # {
116- # "SPANNER_URL": "cloudspanner:[//host[:port]]/project/<project_id>/instances/
117- # <instance-id>/databases/<database-name>?property-name=property-value
118- # }
119- if settings_dict ['SPANNER_URL' ]:
120- return parse_spanner_url (settings_dict ['SPANNER_URL' ])
121+ """
122+ Examines settings_dict and depending on the provided
123+ keys will try to retrieve Cloud Spanner connection parameters.
124+
125+ Args:
126+ settings_dict: a dict containing either:
127+ a) 'SPANNER_URL' as the key and expecting a URL of the
128+ form:
129+ "cloudspanner:[//host[:port]]/project/<project_id>/
130+ instances/<instance-id>/databases/<database-name>?
131+ property-name=property-value"
132+ for example:
133+ {
134+ "SPANNER_URL": "cloudspanner:/projects/appdev/instances/dev1/databases/db1?"
135+ "instance_config=projects/appdev/instanceConfigs/regional-us-west2"
136+ }
137+
138+ b) Otherwise expects parameters whose keys are capitalized and
139+ are of the form:
140+ {
141+ "NAME": "<database_name>",
142+ "INSTANCE": "<instance_name>",
143+ "AUTOCOMMIT": True or False,
144+ "READONLY": True or False,
145+ "PROJECT_ID": "<project_id>",
146+ "INSTANCE_CONFIG": "[instance configuration if using a brand new database]",
147+ }
148+
149+ Returns:
150+ A dict of the form:
151+ {
152+ "autocommit": <True otherwise omitted if zero-value>,
153+ "database": <database name otherwise omitted if zero-value>,
154+ "instance": <instance name otherwise omitted if zero-value>,
155+ "instance_config": <instance configuration otherwise omitted if zero-value>,
156+ "project_id": <project_id otherwise omitted if zero-value>,
157+ "readonly": <True otherwise omitted if zero-value>
158+ }
159+ """
160+
161+ spanner_url = settings_dict .get ('SPANNER_URL' , None )
162+ if spanner_url :
163+ return parse_spanner_url (spanner_url )
121164 else :
122- return dict (
123- auto_commit = settings_dict ['AUTO_COMMIT' ],
124- database = settings_dict ['NAME' ] or 'spanner' ,
125- instance = settings_dict ['INSTANCE' ],
126- project_id = resolve_project_id (settings_dict ['PROJECT_ID' ]),
127- read_only = settings_dict ['READ_ONLY' ],
165+ all_unfiltered = dict (
166+ autocommit = settings_dict .get ('AUTOCOMMIT' ),
167+ database = settings_dict .get ('NAME' ),
168+ instance = settings_dict .get ('INSTANCE' ),
169+ instance_config = settings_dict .get ('INSTANCE_CONFIG' ),
170+ project_id = resolve_project_id (settings_dict .get ('PROJECT_ID' )),
171+ readonly = settings_dict .get ('READONLY' ),
128172 )
129173
174+ # Filter them to remove any unnecessary
175+ # None's whose keys have no associated value.
176+ return filter_out_unset_keys (all_unfiltered )
177+
178+
179+ def filter_out_unset_keys (unfiltered ):
180+ # Filter them to remove any unnecessary
181+ # None's whose keys have no associated value.
182+ return {key : value for key , value in unfiltered .items () if value }
183+
130184
131185reINSTANCE_CONFIG = re .compile ('^projects/([^/]+)/instanceConfigs/([^/]+)$' , re .UNICODE )
132186
0 commit comments