You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

776 lines
25KB

  1. # engine/url.py
  2. # Copyright (C) 2005-2021 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. """Provides the :class:`~sqlalchemy.engine.url.URL` class which encapsulates
  8. information about a database connection specification.
  9. The URL object is created automatically when
  10. :func:`~sqlalchemy.engine.create_engine` is called with a string
  11. argument; alternatively, the URL is a public-facing construct which can
  12. be used directly and is also accepted directly by ``create_engine()``.
  13. """
  14. import re
  15. from .interfaces import Dialect
  16. from .. import exc
  17. from .. import util
  18. from ..dialects import plugins
  19. from ..dialects import registry
  20. from ..util import collections_abc
  21. from ..util import compat
  22. class URL(
  23. util.namedtuple(
  24. "URL",
  25. [
  26. "drivername",
  27. "username",
  28. "password",
  29. "host",
  30. "port",
  31. "database",
  32. "query",
  33. ],
  34. )
  35. ):
  36. """
  37. Represent the components of a URL used to connect to a database.
  38. This object is suitable to be passed directly to a
  39. :func:`_sa.create_engine` call. The fields of the URL are parsed
  40. from a string by the :func:`.make_url` function. The string
  41. format of the URL is an RFC-1738-style string.
  42. To create a new :class:`_engine.URL` object, use the
  43. :func:`_engine.url.make_url` function. To construct a :class:`_engine.URL`
  44. programmatically, use the :meth:`_engine.URL.create` constructor.
  45. .. versionchanged:: 1.4
  46. The :class:`_engine.URL` object is now an immutable object. To
  47. create a URL, use the :func:`_engine.make_url` or
  48. :meth:`_engine.URL.create` function / method. To modify
  49. a :class:`_engine.URL`, use methods like
  50. :meth:`_engine.URL.set` and
  51. :meth:`_engine.URL.update_query_dict` to return a new
  52. :class:`_engine.URL` object with modifications. See notes for this
  53. change at :ref:`change_5526`.
  54. :class:`_engine.URL` contains the following attributes:
  55. * :attr:`_engine.URL.drivername`: database backend and driver name, such as
  56. ``postgresql+psycopg2``
  57. * :attr:`_engine.URL.username`: username string
  58. * :attr:`_engine.URL.password`: password, which is normally a string but
  59. may also be any object that has a ``__str__()`` method.
  60. * :attr:`_engine.URL.host`: string hostname
  61. * :attr:`_engine.URL.port`: integer port number
  62. * :attr:`_engine.URL.database`: string database name
  63. * :attr:`_engine.URL.query`: an immutable mapping representing the query
  64. string. contains strings for keys and either strings or tuples of
  65. strings for values.
  66. """
  67. def __new__(self, *arg, **kw):
  68. if not kw and len(arg) == 7:
  69. return super(URL, self).__new__(self, *arg, **kw)
  70. else:
  71. util.warn_deprecated(
  72. "Calling URL() directly is deprecated and will be disabled "
  73. "in a future release. The public constructor for URL is "
  74. "now the URL.create() method.",
  75. "1.4",
  76. )
  77. return URL.create(*arg, **kw)
  78. @classmethod
  79. def create(
  80. cls,
  81. drivername,
  82. username=None,
  83. password=None,
  84. host=None,
  85. port=None,
  86. database=None,
  87. query=util.EMPTY_DICT,
  88. ):
  89. """Create a new :class:`_engine.URL` object.
  90. :param drivername: the name of the database backend. This name will
  91. correspond to a module in sqlalchemy/databases or a third party
  92. plug-in.
  93. :param username: The user name.
  94. :param password: database password. May be a string or an object that
  95. can be stringified with ``str()``.
  96. :param host: The name of the host.
  97. :param port: The port number.
  98. :param database: The database name.
  99. :param query: A dictionary of string keys to string values to be passed
  100. to the dialect and/or the DBAPI upon connect. To specify non-string
  101. parameters to a Python DBAPI directly, use the
  102. :paramref:`_sa.create_engine.connect_args` parameter to
  103. :func:`_sa.create_engine`. See also
  104. :attr:`_engine.URL.normalized_query` for a dictionary that is
  105. consistently string->list of string.
  106. :return: new :class:`_engine.URL` object.
  107. .. versionadded:: 1.4
  108. The :class:`_engine.URL` object is now an **immutable named
  109. tuple**. In addition, the ``query`` dictionary is also immutable.
  110. To create a URL, use the :func:`_engine.url.make_url` or
  111. :meth:`_engine.URL.create` function/ method. To modify a
  112. :class:`_engine.URL`, use the :meth:`_engine.URL.set` and
  113. :meth:`_engine.URL.update_query` methods.
  114. """
  115. return cls(
  116. cls._assert_str(drivername, "drivername"),
  117. cls._assert_none_str(username, "username"),
  118. password,
  119. cls._assert_none_str(host, "host"),
  120. cls._assert_port(port),
  121. cls._assert_none_str(database, "database"),
  122. cls._str_dict(query),
  123. )
  124. @classmethod
  125. def _assert_port(cls, port):
  126. if port is None:
  127. return None
  128. try:
  129. return int(port)
  130. except TypeError:
  131. raise TypeError("Port argument must be an integer or None")
  132. @classmethod
  133. def _assert_str(cls, v, paramname):
  134. if v is None:
  135. return v
  136. if not isinstance(v, compat.string_types):
  137. raise TypeError("%s must be a string" % paramname)
  138. return v
  139. @classmethod
  140. def _assert_none_str(cls, v, paramname):
  141. if v is None:
  142. return v
  143. return cls._assert_str(v, paramname)
  144. @classmethod
  145. def _str_dict(cls, dict_):
  146. if dict_ is None:
  147. return util.EMPTY_DICT
  148. def _assert_value(val):
  149. if isinstance(val, str):
  150. return val
  151. elif isinstance(val, collections_abc.Sequence):
  152. return tuple(_assert_value(elem) for elem in val)
  153. else:
  154. raise TypeError(
  155. "Query dictionary values must be strings or "
  156. "sequences of strings"
  157. )
  158. def _assert_str(v):
  159. if not isinstance(v, compat.string_types):
  160. raise TypeError("Query dictionary keys must be strings")
  161. return v
  162. if isinstance(dict_, collections_abc.Sequence):
  163. dict_items = dict_
  164. else:
  165. dict_items = dict_.items()
  166. return util.immutabledict(
  167. {
  168. _assert_str(key): _assert_value(
  169. value,
  170. )
  171. for key, value in dict_items
  172. }
  173. )
  174. def set(
  175. self,
  176. drivername=None,
  177. username=None,
  178. password=None,
  179. host=None,
  180. port=None,
  181. database=None,
  182. query=None,
  183. ):
  184. """return a new :class:`_engine.URL` object with modifications.
  185. Values are used if they are non-None. To set a value to ``None``
  186. explicitly, use the :meth:`_engine.URL._replace` method adapted
  187. from ``namedtuple``.
  188. :param drivername: new drivername
  189. :param username: new username
  190. :param password: new password
  191. :param host: new hostname
  192. :param port: new port
  193. :param query: new query parameters, passed a dict of string keys
  194. referring to string or sequence of string values. Fully
  195. replaces the previous list of arguments.
  196. :return: new :class:`_engine.URL` object.
  197. .. versionadded:: 1.4
  198. .. seealso::
  199. :meth:`_engine.URL.update_query_dict`
  200. """
  201. kw = {}
  202. if drivername is not None:
  203. kw["drivername"] = drivername
  204. if username is not None:
  205. kw["username"] = username
  206. if password is not None:
  207. kw["password"] = password
  208. if host is not None:
  209. kw["host"] = host
  210. if port is not None:
  211. kw["port"] = port
  212. if database is not None:
  213. kw["database"] = database
  214. if query is not None:
  215. kw["query"] = query
  216. return self._replace(**kw)
  217. def _replace(self, **kw):
  218. """Override ``namedtuple._replace()`` to provide argument checking."""
  219. if "drivername" in kw:
  220. self._assert_str(kw["drivername"], "drivername")
  221. for name in "username", "host", "database":
  222. if name in kw:
  223. self._assert_none_str(kw[name], name)
  224. if "port" in kw:
  225. self._assert_port(kw["port"])
  226. if "query" in kw:
  227. kw["query"] = self._str_dict(kw["query"])
  228. return super(URL, self)._replace(**kw)
  229. def update_query_string(self, query_string, append=False):
  230. """Return a new :class:`_engine.URL` object with the :attr:`_engine.URL.query`
  231. parameter dictionary updated by the given query string.
  232. E.g.::
  233. >>> from sqlalchemy.engine import make_url
  234. >>> url = make_url("postgresql://user:pass@host/dbname")
  235. >>> url = url.update_query_string("alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt")
  236. >>> str(url)
  237. 'postgresql://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt'
  238. :param query_string: a URL escaped query string, not including the
  239. question mark.
  240. :param append: if True, parameters in the existing query string will
  241. not be removed; new parameters will be in addition to those present.
  242. If left at its default of False, keys present in the given query
  243. parameters will replace those of the existing query string.
  244. .. versionadded:: 1.4
  245. .. seealso::
  246. :attr:`_engine.URL.query`
  247. :meth:`_engine.URL.update_query_dict`
  248. """ # noqa: E501
  249. return self.update_query_pairs(
  250. util.parse_qsl(query_string), append=append
  251. )
  252. def update_query_pairs(self, key_value_pairs, append=False):
  253. """Return a new :class:`_engine.URL` object with the
  254. :attr:`_engine.URL.query`
  255. parameter dictionary updated by the given sequence of key/value pairs
  256. E.g.::
  257. >>> from sqlalchemy.engine import make_url
  258. >>> url = make_url("postgresql://user:pass@host/dbname")
  259. >>> url = url.update_query_pairs([("alt_host", "host1"), ("alt_host", "host2"), ("ssl_cipher", "/path/to/crt")])
  260. >>> str(url)
  261. 'postgresql://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt'
  262. :param key_value_pairs: A sequence of tuples containing two strings
  263. each.
  264. :param append: if True, parameters in the existing query string will
  265. not be removed; new parameters will be in addition to those present.
  266. If left at its default of False, keys present in the given query
  267. parameters will replace those of the existing query string.
  268. .. versionadded:: 1.4
  269. .. seealso::
  270. :attr:`_engine.URL.query`
  271. :meth:`_engine.URL.difference_update_query`
  272. :meth:`_engine.URL.set`
  273. """ # noqa: E501
  274. existing_query = self.query
  275. new_keys = {}
  276. for key, value in key_value_pairs:
  277. if key in new_keys:
  278. new_keys[key] = util.to_list(new_keys[key])
  279. new_keys[key].append(value)
  280. else:
  281. new_keys[key] = value
  282. if append:
  283. new_query = {}
  284. for k in new_keys:
  285. if k in existing_query:
  286. new_query[k] = util.to_list(
  287. existing_query[k]
  288. ) + util.to_list(new_keys[k])
  289. else:
  290. new_query[k] = new_keys[k]
  291. new_query.update(
  292. {
  293. k: existing_query[k]
  294. for k in set(existing_query).difference(new_keys)
  295. }
  296. )
  297. else:
  298. new_query = self.query.union(new_keys)
  299. return self.set(query=new_query)
  300. def update_query_dict(self, query_parameters, append=False):
  301. """Return a new :class:`_engine.URL` object with the
  302. :attr:`_engine.URL.query` parameter dictionary updated by the given
  303. dictionary.
  304. The dictionary typically contains string keys and string values.
  305. In order to represent a query parameter that is expressed multiple
  306. times, pass a sequence of string values.
  307. E.g.::
  308. >>> from sqlalchemy.engine import make_url
  309. >>> url = make_url("postgresql://user:pass@host/dbname")
  310. >>> url = url.update_query_dict({"alt_host": ["host1", "host2"], "ssl_cipher": "/path/to/crt"})
  311. >>> str(url)
  312. 'postgresql://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt'
  313. :param query_parameters: A dictionary with string keys and values
  314. that are either strings, or sequences of strings.
  315. :param append: if True, parameters in the existing query string will
  316. not be removed; new parameters will be in addition to those present.
  317. If left at its default of False, keys present in the given query
  318. parameters will replace those of the existing query string.
  319. .. versionadded:: 1.4
  320. .. seealso::
  321. :attr:`_engine.URL.query`
  322. :meth:`_engine.URL.update_query_string`
  323. :meth:`_engine.URL.update_query_pairs`
  324. :meth:`_engine.URL.difference_update_query`
  325. :meth:`_engine.URL.set`
  326. """ # noqa: E501
  327. return self.update_query_pairs(query_parameters.items(), append=append)
  328. def difference_update_query(self, names):
  329. """
  330. Remove the given names from the :attr:`_engine.URL.query` dictionary,
  331. returning the new :class:`_engine.URL`.
  332. E.g.::
  333. url = url.difference_update_query(['foo', 'bar'])
  334. Equivalent to using :meth:`_engine.URL.set` as follows::
  335. url = url.set(
  336. query={
  337. key: url.query[key]
  338. for key in set(url.query).difference(['foo', 'bar'])
  339. }
  340. )
  341. .. versionadded:: 1.4
  342. .. seealso::
  343. :attr:`_engine.URL.query`
  344. :meth:`_engine.URL.update_query_dict`
  345. :meth:`_engine.URL.set`
  346. """
  347. if not set(names).intersection(self.query):
  348. return self
  349. return URL(
  350. self.drivername,
  351. self.username,
  352. self.password,
  353. self.host,
  354. self.port,
  355. self.database,
  356. util.immutabledict(
  357. {
  358. key: self.query[key]
  359. for key in set(self.query).difference(names)
  360. }
  361. ),
  362. )
  363. @util.memoized_property
  364. def normalized_query(self):
  365. """Return the :attr:`_engine.URL.query` dictionary with values normalized
  366. into sequences.
  367. As the :attr:`_engine.URL.query` dictionary may contain either
  368. string values or sequences of string values to differentiate between
  369. parameters that are specified multiple times in the query string,
  370. code that needs to handle multiple parameters generically will wish
  371. to use this attribute so that all parameters present are presented
  372. as sequences. Inspiration is from Python's ``urllib.parse.parse_qs``
  373. function. E.g.::
  374. >>> from sqlalchemy.engine import make_url
  375. >>> url = make_url("postgresql://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt")
  376. >>> url.query
  377. immutabledict({'alt_host': ('host1', 'host2'), 'ssl_cipher': '/path/to/crt'})
  378. >>> url.normalized_query
  379. immutabledict({'alt_host': ('host1', 'host2'), 'ssl_cipher': ('/path/to/crt',)})
  380. """ # noqa: E501
  381. return util.immutabledict(
  382. {
  383. k: (v,) if not isinstance(v, tuple) else v
  384. for k, v in self.query.items()
  385. }
  386. )
  387. @util.deprecated(
  388. "1.4",
  389. "The :meth:`_engine.URL.__to_string__ method is deprecated and will "
  390. "be removed in a future release. Please use the "
  391. ":meth:`_engine.URL.render_as_string` method.",
  392. )
  393. def __to_string__(self, hide_password=True):
  394. """Render this :class:`_engine.URL` object as a string.
  395. :param hide_password: Defaults to True. The password is not shown
  396. in the string unless this is set to False.
  397. """
  398. return self.render_as_string(hide_password=hide_password)
  399. def render_as_string(self, hide_password=True):
  400. """Render this :class:`_engine.URL` object as a string.
  401. This method is used when the ``__str__()`` or ``__repr__()``
  402. methods are used. The method directly includes additional options.
  403. :param hide_password: Defaults to True. The password is not shown
  404. in the string unless this is set to False.
  405. """
  406. s = self.drivername + "://"
  407. if self.username is not None:
  408. s += _rfc_1738_quote(self.username)
  409. if self.password is not None:
  410. s += ":" + (
  411. "***"
  412. if hide_password
  413. else _rfc_1738_quote(str(self.password))
  414. )
  415. s += "@"
  416. if self.host is not None:
  417. if ":" in self.host:
  418. s += "[%s]" % self.host
  419. else:
  420. s += self.host
  421. if self.port is not None:
  422. s += ":" + str(self.port)
  423. if self.database is not None:
  424. s += "/" + self.database
  425. if self.query:
  426. keys = list(self.query)
  427. keys.sort()
  428. s += "?" + "&".join(
  429. "%s=%s" % (util.quote_plus(k), util.quote_plus(element))
  430. for k in keys
  431. for element in util.to_list(self.query[k])
  432. )
  433. return s
  434. def __str__(self):
  435. return self.render_as_string(hide_password=False)
  436. def __repr__(self):
  437. return self.render_as_string()
  438. def __hash__(self):
  439. return hash(str(self))
  440. def __eq__(self, other):
  441. return (
  442. isinstance(other, URL)
  443. and self.drivername == other.drivername
  444. and self.username == other.username
  445. and self.password == other.password
  446. and self.host == other.host
  447. and self.database == other.database
  448. and self.query == other.query
  449. and self.port == other.port
  450. )
  451. def __ne__(self, other):
  452. return not self == other
  453. def get_backend_name(self):
  454. """Return the backend name.
  455. This is the name that corresponds to the database backend in
  456. use, and is the portion of the :attr:`_engine.URL.drivername`
  457. that is to the left of the plus sign.
  458. """
  459. if "+" not in self.drivername:
  460. return self.drivername
  461. else:
  462. return self.drivername.split("+")[0]
  463. def get_driver_name(self):
  464. """Return the backend name.
  465. This is the name that corresponds to the DBAPI driver in
  466. use, and is the portion of the :attr:`_engine.URL.drivername`
  467. that is to the right of the plus sign.
  468. If the :attr:`_engine.URL.drivername` does not include a plus sign,
  469. then the default :class:`_engine.Dialect` for this :class:`_engine.URL`
  470. is imported in order to get the driver name.
  471. """
  472. if "+" not in self.drivername:
  473. return self.get_dialect().driver
  474. else:
  475. return self.drivername.split("+")[1]
  476. def _instantiate_plugins(self, kwargs):
  477. plugin_names = util.to_list(self.query.get("plugin", ()))
  478. plugin_names += kwargs.get("plugins", [])
  479. kwargs = dict(kwargs)
  480. loaded_plugins = [
  481. plugins.load(plugin_name)(self, kwargs)
  482. for plugin_name in plugin_names
  483. ]
  484. u = self.difference_update_query(["plugin", "plugins"])
  485. for plugin in loaded_plugins:
  486. new_u = plugin.update_url(u)
  487. if new_u is not None:
  488. u = new_u
  489. kwargs.pop("plugins", None)
  490. return u, loaded_plugins, kwargs
  491. def _get_entrypoint(self):
  492. """Return the "entry point" dialect class.
  493. This is normally the dialect itself except in the case when the
  494. returned class implements the get_dialect_cls() method.
  495. """
  496. if "+" not in self.drivername:
  497. name = self.drivername
  498. else:
  499. name = self.drivername.replace("+", ".")
  500. cls = registry.load(name)
  501. # check for legacy dialects that
  502. # would return a module with 'dialect' as the
  503. # actual class
  504. if (
  505. hasattr(cls, "dialect")
  506. and isinstance(cls.dialect, type)
  507. and issubclass(cls.dialect, Dialect)
  508. ):
  509. return cls.dialect
  510. else:
  511. return cls
  512. def get_dialect(self):
  513. """Return the SQLAlchemy :class:`_engine.Dialect` class corresponding
  514. to this URL's driver name.
  515. """
  516. entrypoint = self._get_entrypoint()
  517. dialect_cls = entrypoint.get_dialect_cls(self)
  518. return dialect_cls
  519. def translate_connect_args(self, names=[], **kw):
  520. r"""Translate url attributes into a dictionary of connection arguments.
  521. Returns attributes of this url (`host`, `database`, `username`,
  522. `password`, `port`) as a plain dictionary. The attribute names are
  523. used as the keys by default. Unset or false attributes are omitted
  524. from the final dictionary.
  525. :param \**kw: Optional, alternate key names for url attributes.
  526. :param names: Deprecated. Same purpose as the keyword-based alternate
  527. names, but correlates the name to the original positionally.
  528. """
  529. translated = {}
  530. attribute_names = ["host", "database", "username", "password", "port"]
  531. for sname in attribute_names:
  532. if names:
  533. name = names.pop(0)
  534. elif sname in kw:
  535. name = kw[sname]
  536. else:
  537. name = sname
  538. if name is not None and getattr(self, sname, False):
  539. translated[name] = getattr(self, sname)
  540. return translated
  541. def make_url(name_or_url):
  542. """Given a string or unicode instance, produce a new URL instance.
  543. The given string is parsed according to the RFC 1738 spec. If an
  544. existing URL object is passed, just returns the object.
  545. """
  546. if isinstance(name_or_url, util.string_types):
  547. return _parse_rfc1738_args(name_or_url)
  548. else:
  549. return name_or_url
  550. def _parse_rfc1738_args(name):
  551. pattern = re.compile(
  552. r"""
  553. (?P<name>[\w\+]+)://
  554. (?:
  555. (?P<username>[^:/]*)
  556. (?::(?P<password>[^@]*))?
  557. @)?
  558. (?:
  559. (?:
  560. \[(?P<ipv6host>[^/\?]+)\] |
  561. (?P<ipv4host>[^/:\?]+)
  562. )?
  563. (?::(?P<port>[^/\?]*))?
  564. )?
  565. (?:/(?P<database>[^\?]*))?
  566. (?:\?(?P<query>.*))?
  567. """,
  568. re.X,
  569. )
  570. m = pattern.match(name)
  571. if m is not None:
  572. components = m.groupdict()
  573. if components["query"] is not None:
  574. query = {}
  575. for key, value in util.parse_qsl(components["query"]):
  576. if util.py2k:
  577. key = key.encode("ascii")
  578. if key in query:
  579. query[key] = util.to_list(query[key])
  580. query[key].append(value)
  581. else:
  582. query[key] = value
  583. else:
  584. query = None
  585. components["query"] = query
  586. if components["username"] is not None:
  587. components["username"] = _rfc_1738_unquote(components["username"])
  588. if components["password"] is not None:
  589. components["password"] = _rfc_1738_unquote(components["password"])
  590. ipv4host = components.pop("ipv4host")
  591. ipv6host = components.pop("ipv6host")
  592. components["host"] = ipv4host or ipv6host
  593. name = components.pop("name")
  594. if components["port"]:
  595. components["port"] = int(components["port"])
  596. return URL.create(name, **components)
  597. else:
  598. raise exc.ArgumentError(
  599. "Could not parse rfc1738 URL from string '%s'" % name
  600. )
  601. def _rfc_1738_quote(text):
  602. return re.sub(r"[:@/]", lambda m: "%%%X" % ord(m.group(0)), text)
  603. def _rfc_1738_unquote(text):
  604. return util.unquote(text)
  605. def _parse_keyvalue_args(name):
  606. m = re.match(r"(\w+)://(.*)", name)
  607. if m is not None:
  608. (name, args) = m.group(1, 2)
  609. opts = dict(util.parse_qsl(args))
  610. return URL(name, *opts)
  611. else:
  612. return None