Unpacks a url of the form protocol://[username[:pw]]@hostname[:port]/db/collection :rtype: tuple of strings :returns: protocol, username, password, hostname, port, dbname, collection :note: If the password is not given in the url but the username is, then this function
(url, pwfile=None)
| 185 | |
| 186 | |
| 187 | def parse_url(url, pwfile=None): |
| 188 | """Unpacks a url of the form |
| 189 | protocol://[username[:pw]]@hostname[:port]/db/collection |
| 190 | |
| 191 | :rtype: tuple of strings |
| 192 | :returns: protocol, username, password, hostname, port, dbname, collection |
| 193 | |
| 194 | :note: |
| 195 | If the password is not given in the url but the username is, then |
| 196 | this function will read the password from file by calling |
| 197 | ``open(pwfile).read()[:-1]`` |
| 198 | |
| 199 | """ |
| 200 | |
| 201 | protocol = url[: url.find(":")] |
| 202 | ftp_url = "ftp" + url[url.find(":") :] |
| 203 | |
| 204 | # -- parse the string as if it were an ftp address |
| 205 | tmp = urllib.parse.urlparse(ftp_url) |
| 206 | query_params = urllib.parse.parse_qs(tmp.query) |
| 207 | |
| 208 | logger.info("PROTOCOL %s" % protocol) |
| 209 | logger.info("USERNAME %s" % tmp.username) |
| 210 | logger.info("HOSTNAME %s" % tmp.hostname) |
| 211 | logger.info("PORT %s" % tmp.port) |
| 212 | logger.info("PATH %s" % tmp.path) |
| 213 | |
| 214 | authdbname = None |
| 215 | if "authSource" in query_params and len(query_params["authSource"]): |
| 216 | authdbname = query_params["authSource"][-1] |
| 217 | |
| 218 | logger.info("AUTH DB %s" % authdbname) |
| 219 | |
| 220 | try: |
| 221 | _, dbname, collection = tmp.path.split("/") |
| 222 | except: |
| 223 | print("Failed to parse '%s'" % (str(tmp.path)), file=sys.stderr) |
| 224 | raise |
| 225 | logger.info("DB %s" % dbname) |
| 226 | logger.info("COLLECTION %s" % collection) |
| 227 | |
| 228 | if tmp.password is None: |
| 229 | if (tmp.username is not None) and pwfile: |
| 230 | password = open(pwfile).read()[:-1] |
| 231 | else: |
| 232 | password = None |
| 233 | else: |
| 234 | password = tmp.password |
| 235 | if password is not None: |
| 236 | logger.info("PASS ***") |
| 237 | port = int(float(tmp.port)) # port has to be casted explicitly here. |
| 238 | |
| 239 | return ( |
| 240 | protocol, |
| 241 | tmp.username, |
| 242 | password, |
| 243 | tmp.hostname, |
| 244 | port, |