a
    g	gP                     @  s   d dl mZ d dlmZ d dlmZ d dlmZ d dlZd dlZd dl	Z	d dl
mZ d dl
mZ d dl
mZ d d	l
mZ d d
l
mZ ddlmZ ddlmZ ddlmZ ddlmZ G dd dZG dd dZdddZedkre  dS )    )annotations)ArgumentParser)	Namespace)ConfigParserN)Dict)Optional)overload)TextIO)Union   )__version__)command)util)compatc                
   @  sd  e Zd ZU dZdddejde dfdddddd	d
ddddZdZ	de
d< dZde
d< dZde
d< ejdd ZdddddZejdd ZddddZeddddddZed6dd!d!dd"dZd7dd#d$dZdddd%d&d'Zddd(d)d*Zddddd+d,d-Zd8ddd.d.d/d0d1Zeddddd2d3Zed9dd.d.dd4d3Zd:d5d3ZdS );Configa_  Represent an Alembic configuration.

    Within an ``env.py`` script, this is available
    via the :attr:`.EnvironmentContext.config` attribute,
    which in turn is available at ``alembic.context``::

        from alembic import context

        some_param = context.config.get_main_option("my option")

    When invoking Alembic programatically, a new
    :class:`.Config` can be created by passing
    the name of an .ini file to the constructor::

        from alembic.config import Config
        alembic_cfg = Config("/path/to/yourapp/alembic.ini")

    With a :class:`.Config` object, you can then
    run Alembic commands programmatically using the directives
    in :mod:`alembic.command`.

    The :class:`.Config` object can also be constructed without
    a filename.   Values can be set programmatically, and
    new sections will be created as needed::

        from alembic.config import Config
        alembic_cfg = Config()
        alembic_cfg.set_main_option("script_location", "myapp:migrations")
        alembic_cfg.set_main_option("sqlalchemy.url", "postgresql://foo/bar")
        alembic_cfg.set_section_option("mysection", "foo", "bar")

    .. warning::

       When using programmatic configuration, make sure the
       ``env.py`` file in use is compatible with the target configuration;
       including that the call to Python ``logging.fileConfig()`` is
       omitted if the programmatic configuration doesn't actually include
       logging directives.

    For passing non-string values to environments, such as connections and
    engines, use the :attr:`.Config.attributes` dictionary::

        with engine.begin() as connection:
            alembic_cfg.attributes['connection'] = connection
            command.upgrade(alembic_cfg, "head")

    :param file\_: name of the .ini file to open.
    :param ini_section: name of the main Alembic section within the
     .ini file
    :param output_buffer: optional file-like input buffer which
     will be passed to the :class:`.MigrationContext` - used to redirect
     the output of "offline generation" when using Alembic programmatically.
    :param stdout: buffer where the "print" output of commands will be sent.
     Defaults to ``sys.stdout``.

    :param config_args: A dictionary of keys and values that will be used
     for substitution in the alembic config file.  The dictionary as given
     is **copied** to a new one, stored locally as the attribute
     ``.config_args``. When the :attr:`.Config.file_config` attribute is
     first invoked, the replacement variable ``here`` will be added to this
     dictionary before the dictionary is passed to ``ConfigParser()``
     to parse the .ini file.

    :param attributes: optional dictionary of arbitrary Python keys/values,
     which will be populated into the :attr:`.Config.attributes` dictionary.

     .. seealso::

        :ref:`connection_sharing`

    Nalembicz"Union[str, os.PathLike[str], None]strzOptional[TextIO]r	   zOptional[Namespace]zutil.immutabledictzOptional[dict]None)file_ini_sectionoutput_bufferstdoutcmd_optsconfig_args
attributesreturnc                 C  s<   || _ || _|| _|| _|| _t|| _|r8| j| dS )z Construct a new :class:`.Config`N)	config_file_nameconfig_ini_sectionr   r   r   dictr   r   update)selfr   r   r   r   r   r   r    r!   Q/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/alembic/config.py__init___   s    
zConfig.__init__r   r   r   c                 C  s   i S )a  A Python dictionary for storage of additional state.


        This is a utility dictionary which can include not just strings but
        engines, connections, schema objects, or anything else.
        Use this to pass objects into an env.py script, such as passing
        a :class:`sqlalchemy.engine.base.Connection` when calling
        commands from :mod:`alembic.command` programmatically.

        .. seealso::

            :ref:`connection_sharing`

            :paramref:`.Config.attributes`

        r!   )r    r!   r!   r"   r      s    zConfig.attributes)textr   c                 G  s.   |rt || }nt |}t| j|d dS )a\  Render a message to standard out.

        When :meth:`.Config.print_stdout` is called with additional args
        those arguments will formatted against the provided text,
        otherwise we simply output the provided text verbatim.

        e.g.::

            >>> config.print_stdout('Some text %s', 'arg')
            Some Text arg

        
N)r   r   Zwrite_outstreamr   )r    r$   argoutputr!   r!   r"   print_stdout   s    zConfig.print_stdoutc                 C  s\   | j rtjtj| j }nd}|| jd< t| j}| j rL|| j g n|| j	 |S )a  Return the underlying ``ConfigParser`` object.

        Direct access to the .ini file is available here,
        though the :meth:`.Config.get_section` and
        :meth:`.Config.get_main_option`
        methods provide a possibly simpler interface.

         here)
r   ospathabspathdirnamer   r   readadd_sectionr   )r    r*   file_configr!   r!   r"   r1      s    

zConfig.file_config)r   c                 C  s,   ddl }tjtj|j}tj|dS )zReturn the directory where Alembic setup templates are found.

        This method is used by the alembic ``init`` and ``list_templates``
        commands.

        r   NZ	templates)r   r+   r,   r-   r.   __file__join)r    r   package_dirr!   r!   r"   get_template_directory   s    zConfig.get_template_directoryzDict[str, str])namedefaultr   c                 C  s   d S Nr!   r    r6   r7   r!   r!   r"   get_section   s    zConfig.get_section.zOptional[Dict[str, str]]c                 C  s   d S r8   r!   r9   r!   r!   r"   r:      s    )r6   c                 C  s    | j |s|S t| j |S )zfReturn all the configuration options from a given .ini file section
        as a dictionary.

        )r1   has_sectionr   itemsr9   r!   r!   r"   r:      s    )r6   valuer   c                 C  s   |  | j|| dS )a:  Set an option programmatically within the 'main' section.

        This overrides whatever was in the .ini file.

        :param name: name of the value

        :param value: the value.  Note that this value is passed to
         ``ConfigParser.set``, which supports variable interpolation using
         pyformat (e.g. ``%(some_value)s``).   A raw percent sign not part of
         an interpolation symbol must therefore be escaped, e.g. ``%%``.
         The given value may refer to another value already in the file
         using the interpolation format.

        N)set_section_optionr   )r    r6   r=   r!   r!   r"   set_main_option   s    zConfig.set_main_option)r6   r   c                 C  s   | j | j| d S r8   )r1   remove_optionr   )r    r6   r!   r!   r"   remove_main_option   s    zConfig.remove_main_option)sectionr6   r=   r   c                 C  s,   | j |s| j | | j ||| dS )a  Set an option programmatically within the given section.

        The section is created if it doesn't exist already.
        The value here will override whatever was in the .ini
        file.

        :param section: name of the section

        :param name: name of the value

        :param value: the value.  Note that this value is passed to
         ``ConfigParser.set``, which supports variable interpolation using
         pyformat (e.g. ``%(some_value)s``).   A raw percent sign not part of
         an interpolation symbol must therefore be escaped, e.g. ``%%``.
         The given value may refer to another value already in the file
         using the interpolation format.

        N)r1   r;   r0   set)r    rB   r6   r=   r!   r!   r"   r>      s    zConfig.set_section_optionOptional[str])rB   r6   r7   r   c                 C  sD   | j |s td| j|f | j ||r<| j ||S |S dS )z9Return an option from the given section of the .ini file.z6No config file %r found, or file has no '[%s]' sectionN)r1   r;   r   CommandErrorr   
has_optionget)r    rB   r6   r7   r!   r!   r"   get_section_option  s    zConfig.get_section_optionc                 C  s   d S r8   r!   r9   r!   r!   r"   get_main_option&  s    zConfig.get_main_optionc                 C  s   d S r8   r!   r9   r!   r!   r"   rI   *  s    c                 C  s   |  | j||S )zReturn an option from the 'main' section of the .ini file.

        This defaults to being a key from the ``[alembic]``
        section, unless the ``-n/--name`` flag were used to
        indicate a different section.

        )rH   r   r9   r!   r!   r"   rI   0  s    ).)N)N)N)N)__name__
__module____qualname____doc__sysr   r   Zimmutabledictr#   r   __annotations__r   r   Zmemoized_propertyr   r(   r1   r5   r   r:   r?   rA   r>   rH   rI   r!   r!   r!   r"   r      sF   
J

 
  r   c                   @  sJ   e Zd ZddddddZdddddZd	d
ddddZdddZdS )CommandLineNrD   r   )progr   c                 C  s   |  | d S r8   )_generate_args)r    rQ   r!   r!   r"   r#   <  s    zCommandLine.__init__c                   s  fdd}t |d}|jdddt d |jdd	ttjd
ddd |jddtddd |jdddd |jdddd | }tj	ddiidd t
tD D ]$ t r jd dkr jdkrt }|d  d ur|d d!t|d    }|d t|d   d  }n|d d!d  }g } v rN fd"d|D } j}|rg }	|d#D ]&}
|
 s~ qn|	|
  qhng }	|j jd$|	d%| || j ||fd& q|| _d S )'Nc           	        s  ddt dtddfddt tdd	fd
t dddfdt tdd	fdt tdd	fdt dddfdt dddfdt tdd	fdt tdd	fdt tdd	fddt dddfd t dd!dfd"t dd#dfd$d%t d&d'dfd(d)t dd*dfd+t dd,dfd-t dd.dfd/}d0d1d2d3}|D ]>}||v r|| }|d4d5 |d5  }}|j|i | q|D ]X}|d6ks~|  v r |  | d6krjd6d7|d6d8 nj|||d9 qTd S ):Nz-tz
--templateZgenericz"Setup template for use with 'init')r7   typehelpz-mz	--messagez%Message string to use with 'revision')rS   rT   z--sql
store_truez\Don't emit SQL to database - dump to standard output/file instead. See docs on offline mode.actionrT   z--tagz<Arbitrary 'tag' name - can be used by custom env.py scripts.z--headzCSpecify head revision or <branchname>@head to base new revision on.z--splicez6Allow a non-head revision as the 'head' to splice ontoz--depends-onappendzNSpecify one or more revision identifiers which this revision should depend on.z--rev-idz9Specify a hardcoded revision id instead of generating onez--version-pathz2Specify specific path from config for version filez--branch-labelz3Specify a branch label to apply to the new revisionz-vz	--verbosezUse more verbose outputz--resolve-dependenciesz+Treat dependency versions as down revisionsz--autogeneratezgPopulate revision script with candidate migration operations, based on comparison of database to model.z-rz--rev-rangestorez1Specify a revision range; format is [start]:[end]z-iz--indicate-currentzIndicate the current revisionz--purgez7Unconditionally erase the version table before stampingz	--packagezFWrite empty __init__.py files to the environment and version locations)templatemessageZsqltagheadZspliceZ
depends_onZrev_idZversion_pathZbranch_labelverboseZresolve_dependenciesZautogenerateZ	rev_rangeZindicate_currentpurgepackagezlocation of scripts directoryzrevision identifierz/one or more revisions, or 'heads' for all heads)	directoryrevision	revisionsr   rc   +)nargsrT   rT   )r   r   add_argumentrG   )	fnparser
positionalkwargsZkwargs_optsZpositional_helpr&   argskw)positional_translations	subparserr!   r"   add_options@  s    

							


		 
z/CommandLine._generate_args.<locals>.add_optionsrQ   z	--versionversionz%%(prog)s %s)rW   rs   z-cz--configZALEMBIC_CONFIGzalembic.inizaAlternate config file; defaults to value of ALEMBIC_CONFIG environment variable, or "alembic.ini")rS   r7   rT   z-nz--namer   z6Name of section in .ini file to use for Alembic configz-xrX   zlAdditional arguments consumed by custom env.py scripts, e.g. -x setting1=somesetting -x setting2=somesettingrV   z
--raiseerrrU   z!Raise a full stack trace on errorrb   rc   c                 S  s   g | ]}t t|qS r!   )getattrr   ).0nr!   r!   r"   
<listcomp>      z.CommandLine._generate_args.<locals>.<listcomp>r   _zalembic.command   r   c                   s   g | ]}   ||qS r!   )rG   )ru   r6   )ri   ro   r!   r"   rw     s   r%    rg   )cmd)r   rh   r   r   r+   environrG   add_subparsersr   Zstampdirinspect
isfunctionrJ   rK   r   Zinspect_getfullargspeclenrM   splitstriprX   
add_parserr3   set_defaultsrj   )r    rQ   rq   rj   Z
subparsersspecrk   kwarghelp_	help_textliner!   )ri   ro   rp   r"   rR   ?  s~     $




zCommandLine._generate_argsr   r   )configoptionsr   c              
     s    j \}}}z6||g fdd|D R i  fdd|D  W n> tjy } z$ jr^ ntt| W Y d }~n
d }~0 0 d S )Nc                   s   g | ]}t  |d qS r8   rt   ru   kr   r!   r"   rw   4  rx   z'CommandLine.run_cmd.<locals>.<listcomp>c                   s   i | ]}|t  |d qS r8   r   r   r   r!   r"   
<dictcomp>5  rx   z'CommandLine.run_cmd.<locals>.<dictcomp>)r|   r   rE   Zraiseerrerrr   )r    r   r   ri   rk   r   er!   r   r"   run_cmd.  s    
zCommandLine.run_cmdc                 C  sF   | j |}t|ds$| j d nt|j|j|d}| || d S )Nr|   ztoo few arguments)r   r   r   )rj   
parse_argshasattrerrorr   r   r6   r   )r    argvr   cfgr!   r!   r"   main=  s    
zCommandLine.main)N)N)rJ   rK   rL   r#   rR   r   r   r!   r!   r!   r"   rP   ;  s
    prP   c                 K  s   t |dj| d dS )z(The console runner function for Alembic.rr   )r   N)rP   r   )r   rQ   rl   r!   r!   r"   r   L  s    r   __main__)NN)
__future__r   argparser   r   configparserr   r   r+   rN   typingr   r   r   r	   r
   r)   r   r   r   r   r   rP   r   rJ   r!   r!   r!   r"   <module>   s0     (  
