L i[yVdZddlmZddlmZddlZddlmZddlm Z ddlm Z ddlm Z ddlm Z dd lm Z dd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!GddeZ"d dZ#d!dZ$y)"aProvides the :class:`~sqlalchemy.engine.url.URL` class which encapsulates information about a database connection specification. The URL object is created automatically when :func:`~sqlalchemy.engine.create_engine` is called with a string argument; alternatively, the URL is a public-facing construct which can be used directly and is also accepted directly by ``create_engine()``. ) annotationsN)Any)cast)Dict)Iterable)List)Mapping) NamedTuple)Optional)overload)Sequence)Tuple)Type)Union) parse_qsl)quote) quote_plus)unquote)Dialect)exc)util)plugins)registrycfeZdZUdZded< ded< ded< ded< ded < ded < d ed < ed d d d d ejf d*dZed+dZ ed,dZ e d-dZ e d.dZ d/ d0dZ d1dZ d2 d3dZ d2 d4dZ d2 d5dZd6dZed7dZej*ddd8d9dZd8d9dZd:dZd;dZdd"Zd>d#Zd:d$Zd:d%Z d?d&Z d@d'Z!d2dAd(Z" dB dCd)Z#y )DURLa Represent the components of a URL used to connect to a database. URLs are typically constructed from a fully formatted URL string, where the :func:`.make_url` function is used internally by the :func:`_sa.create_engine` function in order to parse the URL string into its individual components, which are then used to construct a new :class:`.URL` object. When parsing from a formatted URL string, the parsing format generally follows `RFC-1738 `_, with some exceptions. A :class:`_engine.URL` object may also be produced directly, either by using the :func:`.make_url` function with a fully formed URL string, or by using the :meth:`_engine.URL.create` constructor in order to construct a :class:`_engine.URL` programmatically given individual fields. The resulting :class:`.URL` object may be passed directly to :func:`_sa.create_engine` in place of a string argument, which will bypass the usage of :func:`.make_url` within the engine's creation process. .. versionchanged:: 1.4 The :class:`_engine.URL` object is now an immutable object. To create a URL, use the :func:`_engine.make_url` or :meth:`_engine.URL.create` function / method. To modify a :class:`_engine.URL`, use methods like :meth:`_engine.URL.set` and :meth:`_engine.URL.update_query_dict` to return a new :class:`_engine.URL` object with modifications. See notes for this change at :ref:`change_5526`. .. seealso:: :ref:`database_urls` :class:`_engine.URL` contains the following attributes: * :attr:`_engine.URL.drivername`: database backend and driver name, such as ``postgresql+psycopg2`` * :attr:`_engine.URL.username`: username string * :attr:`_engine.URL.password`: password string * :attr:`_engine.URL.host`: string hostname * :attr:`_engine.URL.port`: integer port number * :attr:`_engine.URL.database`: string database name * :attr:`_engine.URL.query`: an immutable mapping representing the query string. contains strings for keys and either strings or tuples of strings for values. str drivername Optional[str]usernamepasswordhost Optional[int]portdatabase4util.immutabledict[str, Union[Tuple[str, ...], str]]queryNc ||j|d|j|d||j|d|j||j|d|j|S)aCreate a new :class:`_engine.URL` object. .. seealso:: :ref:`database_urls` :param drivername: the name of the database backend. This name will correspond to a module in sqlalchemy/databases or a third party plug-in. :param username: The user name. :param password: database password. Is typically a string, but may also be an object that can be stringified with ``str()``. .. note:: The password string should **not** be URL encoded when passed as an argument to :meth:`_engine.URL.create`; the string should contain the password characters exactly as they would be typed. .. note:: A password-producing object will be stringified only **once** per :class:`_engine.Engine` object. For dynamic password generation per connect, see :ref:`engines_dynamic_tokens`. :param host: The name of the host. :param port: The port number. :param database: The database name. :param query: A dictionary of string keys to string values to be passed to the dialect and/or the DBAPI upon connect. To specify non-string parameters to a Python DBAPI directly, use the :paramref:`_sa.create_engine.connect_args` parameter to :func:`_sa.create_engine`. See also :attr:`_engine.URL.normalized_query` for a dictionary that is consistently string->list of string. :return: new :class:`_engine.URL` object. .. versionadded:: 1.4 The :class:`_engine.URL` object is now an **immutable named tuple**. In addition, the ``query`` dictionary is also immutable. To create a URL, use the :func:`_engine.url.make_url` or :meth:`_engine.URL.create` function/ method. To modify a :class:`_engine.URL`, use the :meth:`_engine.URL.set` and :meth:`_engine.URL.update_query` methods. rr!r#r&) _assert_str_assert_none_str _assert_port _str_dict)clsrr!r"r#r%r&r(s [/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sqlalchemy/engine/url.pycreatez URL.createsmp OOJ 5  : 6   v .   T "  : 6 MM%   cP|y t|S#t$r tdwxYw)Nz(Port argument must be an integer or None)int TypeError)r.r%s r/r,zURL._assert_ports7 < Ht9  HFG G Hs %cBt|tstd|z|S)Nz%s must be a string isinstancerr4r.v paramnames r/r*zURL._assert_strs"!S!1I=> >r1c.||S|j||SN)r*r8s r/r+zURL._assert_none_strs 9Hq),,r1c X|tjSt ddt dd dfd dd}t|tj r|}n|j }tj|Dcic]\}}|||c}}Scc}}w) Ncyr<vals r/ _assert_valuez$URL._str_dict.._assert_valuesr1cyr<r?r@s r/rBz$URL._str_dict.._assert_values+.r1ct|tr|St|tjrt fd|DSt d)Nc3.K|] }|ywr<r?).0elemrBs r/ z7URL._str_dict.._assert_value..sAT]40Asz?Query dictionary values must be strings or sequences of strings)r7rcollections_abcr tupler4)rArBs r/rBz$URL._str_dict.._assert_value sG#s# C!9!9:ASAAA+r1c<t|ts td|S)Nz%Query dictionary keys must be stringsr6)r9s r/r*z"URL._str_dict.._assert_strsa% GHHHr1)rArreturnr)rAz Sequence[str]rLUnion[str, Tuple[str, ...]])rAzUnion[str, Sequence[str]]rLrM)r9rrLr)r EMPTY_DICTr r7rIr items immutabledict)r.dict_r* dict_itemskeyvaluerBs @r/r-z URL._str_dicts =?? "         . . ( .  . *  (   e_55 6JJ!! #-  CC -#    sB& ci}|||d<|||d<|||d<|||d<|||d<|||d<|||d<|jdi|S) areturn a new :class:`_engine.URL` object with modifications. Values are used if they are non-None. To set a value to ``None`` explicitly, use the :meth:`_engine.URL._replace` method adapted from ``namedtuple``. :param drivername: new drivername :param username: new username :param password: new password :param host: new hostname :param port: new port :param query: new query parameters, passed a dict of string keys referring to string or sequence of string values. Fully replaces the previous list of arguments. :return: new :class:`_engine.URL` object. .. versionadded:: 1.4 .. seealso:: :meth:`_engine.URL.update_query_dict` rr!r"r#r%r&r(r?)_assert_replace) selfrr!r"r#r%r&r(kws r/setzURL.set*sF   !)B|   %BzN  %BzN  BvJ  BvJ  %BzN  BwK#t##)b))r1c d|vr|j|dddD]}||vs|j|||d|vr|j|dd|vr|j|d|d<|jdi|S)z)argument checks before calling _replace()r)r!r#r&r%r(r?)r*r+r,r-_replace)rWrXnames r/rVzURL._assert_replace_s 2    R -| <2 6Drz%%bh5 6 R<   bj ) b=..G5BwKt}}"r""r1c:|jt||S)akReturn a new :class:`_engine.URL` object with the :attr:`_engine.URL.query` parameter dictionary updated by the given query string. E.g.:: >>> from sqlalchemy.engine import make_url >>> url = make_url("postgresql+psycopg2://user:pass@host/dbname") >>> url = url.update_query_string( ... "alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt" ... ) >>> str(url) 'postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt' :param query_string: a URL escaped query string, not including the question mark. :param append: if True, parameters in the existing query string will not be removed; new parameters will be in addition to those present. If left at its default of False, keys present in the given query parameters will replace those of the existing query string. .. versionadded:: 1.4 .. seealso:: :attr:`_engine.URL.query` :meth:`_engine.URL.update_query_dict` append)update_query_pairsr)rW query_stringr_s r/update_query_stringzURL.update_query_stringns B&&y'>v&NNr1c "|j}i}|D]w\}}||vrHtj||||<td||j tt |Rt |ttfr t|n|||<y|ri}|D]L}||vr>ttj||tj||z||<E||||<N|jt|j|Dcic]}||| c}nX|jj|jD cic]#\}} |t | tr t| n| %c} }}|j|Scc}wcc} }w)aReturn a new :class:`_engine.URL` object with the :attr:`_engine.URL.query` parameter dictionary updated by the given sequence of key/value pairs E.g.:: >>> from sqlalchemy.engine import make_url >>> url = make_url("postgresql+psycopg2://user:pass@host/dbname") >>> url = url.update_query_pairs( ... [ ... ("alt_host", "host1"), ... ("alt_host", "host2"), ... ("ssl_cipher", "/path/to/crt"), ... ] ... ) >>> str(url) 'postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt' :param key_value_pairs: A sequence of tuples containing two strings each. :param append: if True, parameters in the existing query string will not be removed; new parameters will be in addition to those present. If left at its default of False, keys present in the given query parameters will replace those of the existing query string. .. versionadded:: 1.4 .. seealso:: :attr:`_engine.URL.query` :meth:`_engine.URL.difference_update_query` :meth:`_engine.URL.set` List[str])r()r(rto_listrr_rr7listrJupdaterY differenceunionrO) rWkey_value_pairsr_existing_querynew_keysrSrT new_querykr9s r/r`zURL.update_query_pairssV57) JCh $ Xc] ; [(3-077S%8HI$.edE]#CDK   I /&#( ^A%67,,x{34$IaL $,A;IaL /   !0;;HE~a((  ((!) 01:a#6uQxA=I xxix((s  F(F cD|j|j|S)aReturn a new :class:`_engine.URL` object with the :attr:`_engine.URL.query` parameter dictionary updated by the given dictionary. The dictionary typically contains string keys and string values. In order to represent a query parameter that is expressed multiple times, pass a sequence of string values. E.g.:: >>> from sqlalchemy.engine import make_url >>> url = make_url("postgresql+psycopg2://user:pass@host/dbname") >>> url = url.update_query_dict( ... {"alt_host": ["host1", "host2"], "ssl_cipher": "/path/to/crt"} ... ) >>> str(url) 'postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt' :param query_parameters: A dictionary with string keys and values that are either strings, or sequences of strings. :param append: if True, parameters in the existing query string will not be removed; new parameters will be in addition to those present. If left at its default of False, keys present in the given query parameters will replace those of the existing query string. .. versionadded:: 1.4 .. seealso:: :attr:`_engine.URL.query` :meth:`_engine.URL.update_query_string` :meth:`_engine.URL.update_query_pairs` :meth:`_engine.URL.difference_update_query` :meth:`_engine.URL.set` r^)r`rO)rWquery_parametersr_s r/update_query_dictzURL.update_query_dicts%b&&'7'='='?&OOr1ct|j|js|St|j|j |j |j|j|jtjt|jj|Dcic]}||j|c}Scc}w)a Remove the given names from the :attr:`_engine.URL.query` dictionary, returning the new :class:`_engine.URL`. E.g.:: url = url.difference_update_query(["foo", "bar"]) Equivalent to using :meth:`_engine.URL.set` as follows:: url = url.set( query={ key: url.query[key] for key in set(url.query).difference(["foo", "bar"]) } ) .. versionadded:: 1.4 .. seealso:: :attr:`_engine.URL.query` :meth:`_engine.URL.update_query_dict` :meth:`_engine.URL.set` ) rY intersectionr(rrr!r"r#r%r&rrPrh)rWnamesrSs r/difference_update_queryzURL.difference_update_querys<5z&&tzz2K OO MM MM II II MM    #4::99%@C(   s#Cc tj|jjDcic]\}}|t |t s|fn|c}}Scc}}w)a&Return the :attr:`_engine.URL.query` dictionary with values normalized into sequences. As the :attr:`_engine.URL.query` dictionary may contain either string values or sequences of string values to differentiate between parameters that are specified multiple times in the query string, code that needs to handle multiple parameters generically will wish to use this attribute so that all parameters present are presented as sequences. Inspiration is from Python's ``urllib.parse.parse_qs`` function. E.g.:: >>> from sqlalchemy.engine import make_url >>> url = make_url( ... "postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt" ... ) >>> url.query immutabledict({'alt_host': ('host1', 'host2'), 'ssl_cipher': '/path/to/crt'}) >>> url.normalized_query immutabledict({'alt_host': ('host1', 'host2'), 'ssl_cipher': ('/path/to/crt',)}) )rrPr(rOr7rJ)rWrnr9s r/normalized_queryzURL.normalized_queryGsW2!!!JJ,,. Aqz!U3A4:    s A 1.4zThe :meth:`_engine.URL.__to_string__ method is deprecated and will be removed in a future release. Please use the :meth:`_engine.URL.render_as_string` method.c&|j|S)zRender this :class:`_engine.URL` object as a string. :param hide_password: Defaults to True. The password is not shown in the string unless this is set to False. ) hide_passwordrender_as_string)rWrzs r/ __to_string__zURL.__to_string__gs$$=$AAr1cjdz}jU|tjdz }j*|d|rdntt jdzz }|dz }j 1dj vr|dj dz }n|j z }j |dt j zz }j|d jzz }jrEtj}|j|d d jfd |Dzz }|S) aRRender this :class:`_engine.URL` object as a string. This method is used when the ``__str__()`` or ``__repr__()`` methods are used. The method directly includes additional options. :param hide_password: Defaults to True. The password is not shown in the string unless this is set to False. z://z +)safe:z***@[]/?&c3K|]D}tjj|D]}t|dt|Fyw)=N)rrer(r)rFrnelementrWs r/rHz'URL.render_as_string..sT #||DJJqM: a=/:g#6"78 8 sA A ) rr!rr"rr#r%r&r(rfsortjoin)rWrzskeyss` r/r|zURL.render_as_stringvs9 OOe # == $ t}}40 0A}}(S$s4==1= HA 99 diiq 1%%TYY 99 s499~% %A == $ t}}$ $A :: #D IIK sxx   A r1c"|jSr<r{rWs r/__repr__z URL.__repr__s$$&&r1c |jj|j|j|j|j |j |j|jSr<) __class__r0rr!r"r#r%r&r(rs r/__copy__z URL.__copy__sL~~$$ OO MM MM II II MM JJ  r1c"|jSr<)r)rWmemos r/ __deepcopy__zURL.__deepcopy__s}}r1c*tt|Sr<)hashrrs r/__hash__z URL.__hash__sCIr1ct|txr|j|jk(xr|j|jk(xr|j|jk(xrj|j |j k(xrO|j |j k(xr4|j|jk(xr|j|jk(Sr<) r7rrr!r"r#r&r(r%rWothers r/__eq__z URL.__eq__s uc " (5#3#33 ( / ( / ( UZZ'  (  /  (  ekk)  ( UZZ' r1c||k( Sr<r?rs r/__ne__z URL.__ne__s5=  r1crd|jvr |jS|jjddS)zReturn the backend name. This is the name that corresponds to the database backend in use, and is the portion of the :attr:`_engine.URL.drivername` that is to the left of the plus sign. +r)rsplitrs r/get_backend_namezURL.get_backend_names4 doo %?? "??((-a0 0r1cd|jvr|jjS|jjddS)aReturn the backend name. This is the name that corresponds to the DBAPI driver in use, and is the portion of the :attr:`_engine.URL.drivername` that is to the right of the plus sign. If the :attr:`_engine.URL.drivername` does not include a plus sign, then the default :class:`_engine.Dialect` for this :class:`_engine.URL` is imported in order to get the driver name. rr)r get_dialectdriverrrs r/get_driver_namezURL.get_driver_names? doo %##%,, ,??((-a0 0r1ctj|jjdd}||jdgz }t |}|Dcgc]}t j ||| }}|jddg}|D]}|j|}||}|jdd|||fScc}w)Npluginr?r) rrer(getdictrloadru update_urlpop)rWkwargs plugin_names plugin_nameloaded_pluginsurnew_us r/_instantiate_pluginszURL._instantiate_pluginss||DJJNN8R$@A  9b11 f ,  &GLL %dF 3   ( ((I)> ?$ F%%a(E    9d#.&(( s#CcJd|jvr |j}n|jjdd}tj|}t |dr@t |j tr&t|j tr |j Std|S)zReturn the "entry point" dialect class. This is normally the dialect itself except in the case when the returned class implements the get_dialect_cls() method. r.dialect Type[Dialect]) rreplacerrhasattrr7rtype issubclassrr)rWr\r.s r/_get_entrypointzURL._get_entrypoints| doo %??D??**34DmmD! C #3;;-3;;0;; - -r1cr|j}|r|j|}|S|j|}|S)zoReturn the SQLAlchemy :class:`_engine.Dialect` class corresponding to this URL's driver name. )rget_async_dialect_clsget_dialect_cls)rW _is_async entrypoint dialect_clss r/rzURL.get_dialectsE ))+ $::4@K%44T:Kr1c  |tjddi}gd}|D]`}|r|jd}n ||vr||}n|}|&t||ds4|dk(rt t||||<Rt||||<b|S)a2Translate url attributes into a dictionary of connection arguments. Returns attributes of this url (`host`, `database`, `username`, `password`, `port`) as a plain dictionary. The attribute names are used as the keys by default. Unset or false attributes are omitted from the final dictionary. :param \**kw: Optional, alternate key names for url attributes. :param names: Deprecated. Same purpose as the keyword-based alternate names, but correlates the name to the original positionally. zpThe `URL.translate_connect_args.name`s parameter is deprecated. Please pass the alternate names as kw arguments.rx)r#r&r!r"r%rFr")rwarn_deprecatedrgetattrr)rWrtrX translatedattribute_namessnamer\s r/translate_connect_argszURL.translate_connect_argss    3   N$ #'"&"""&9=? ?  ?  ?  ?  ?  ? 7?  ? ? BHH --*-- --5  5  >5 5 r%)"&"&"""&CG3*!3* 3* 3*  3*  3* 3*A3* 3*j # 16!O!O)-!O !OLQ)DQ)Q) Q)l1P=1P1P 1Pf. `  >T__  7 B  B%N'    ! 11$)') .)0.0 ,0'('7:' 'r1rct|tr t|St|ts$t |dst j d||S)aGiven a string, produce a new URL instance. The format of the URL generally follows `RFC-1738 `_, with some exceptions, including that underscores, and not dashes or periods, are accepted within the "scheme" portion. If a :class:`.URL` object is passed, it is returned as is. .. seealso:: :ref:`database_urls` )_sqla_is_testing_if_this_is_a_mock_objectz#Expected string or URL object, got )r7r _parse_urlrrr ArgumentError) name_or_urls r/make_urlrGsW +s#+&&  S )'@31+ A  r1ctjdtj}|j|}| |j }|d\i}t |dD]H\}}||vr:t j||||<td||j|D|||<Jnd}||d<|dt|d|d<|dt|d|d<|jd}|jd}|xs||d<|jd }|d rt|d |d <tj|fi|Stj d ) Na (?P[\w\+]+):// (?: (?P[^:/]*) (?::(?P[^@]*))? @)? (?: (?: \[(?P[^/\?]+)\] | (?P[^/:\?]+) )? (?::(?P[^/\?]*))? )? (?:/(?P[^\?]*))? (?:\?(?P.*))? r(rdr!r"ipv4hostipv6hostr#r\r%z4Could not parse SQLAlchemy URL from given URL string)recompileXmatch groupdictrrrerr_rrr3rr0rr) r\patternm componentsr(rSrTrrs r/rrcszjj  #G(  dA}[[] g  *E' 7(;< ' U%>*->>*-%1 6~~f% f !$Z%7!8Jv zz$-*-- B  r1)rzUnion[str, URL]rLr)r\rrLr)%r __future__rcollections.abcabcrIrtypingrrrrrr r r r r rrr urllib.parserrrr interfacesrrrdialectsrrrrrr?r1r/rsp#) "# V *V r89 r1