U
    xž^€  ã                   @   s¦   d dl Z d dlZd dlmZ d dlmZmZ d dlmZ ddl	m
Z
 dd„ Ze  d	¡Zd
d„ ZG dd„ deƒZG dd„ deƒZG dd„ deeeƒZG dd„ deƒZdS )é    N)Úinspect)ÚDeclarativeMetaÚdeclared_attr)Ú_get_table_keyé   ©Úto_strc                 C   s†   | j  dd¡s*tdd„ | jdd… D ƒƒs.dS | jD ]L}d|j krDq4t|j d tƒrZ dS || kpz|j  dd¡pzt|tƒ    S dS )	a@  Determine whether ``__tablename__`` should be automatically generated
    for a model.

    * If no class in the MRO sets a name, one should be generated.
    * If a declared attr is found, it should be used instead.
    * If a name is found, it should be used if the class is a mixin, otherwise
      one should be generated.
    * Abstract models should not have one generated.

    Later, :meth:`._BoundDeclarativeMeta.__table_cls__` will determine if the
    model looks like single or joined-table inheritance. If no primary key is
    found, the name will be unset.
    Z__abstract__Fc                 s   s   | ]}t |tƒV  qd S ©N)Ú
isinstancer   )Ú.0Úb© r   úD/tmp/pip-install-dq5v43_d/flask-sqlalchemy/flask_sqlalchemy/model.pyÚ	<genexpr>   s     z'should_set_tablename.<locals>.<genexpr>r   NÚ__tablename__T)Ú__dict__ÚgetÚanyÚ__mro__r
   r   r   )ÚclsÚbaser   r   r   Úshould_set_tablename   s     ÿþ

ÿ
ýr   z([A-Z]+)(?=[a-z0-9])c                 C   s   dd„ }t  || ¡ d¡S )Nc                 S   s<   |   ¡ }t|ƒdkr0d|d d… |d f  ¡ S d| ¡  S )Nr   z_%s_%séÿÿÿÿÚ_)ÚgroupÚlenÚlower)ÚmatchÚwordr   r   r   Ú_join3   s    z"camel_to_snake_case.<locals>._joinr   )Úcamelcase_reÚsubÚlstrip)Únamer   r   r   r   Úcamel_to_snake_case2   s    r$   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚNameMetaMixinc                    sR   t | ƒrt| jƒ| _tt| ƒ |||¡ d| jkrNd| jkrN| jd d krN| `d S )Nr   Ú	__table__)	r   r$   Ú__name__r   Úsuperr%   Ú__init__r   r&   )r   r#   ÚbasesÚd©Ú	__class__r   r   r)   ?   s    ÿþýzNameMetaMixin.__init__c                 O   s¤   t |d | d¡ƒ}|| jjkr,tj||ŽS |D ]2}t|tjƒrF|jsRt|tj	ƒr0tj||Ž  S q0| j
dd… D ]}d|jkrr q’qrtj||ŽS d| jkr | `dS )a  This is called by SQLAlchemy during mapper setup. It determines the
        final table object that the model will use.

        If no primary key is found, that indicates single-table inheritance,
        so no table will be created and ``__tablename__`` will be unset.
        r   Zschemar   r   r&   r   N)r   r   ÚmetadataÚtablesÚsaZTabler
   ZColumnZprimary_keyZPrimaryKeyConstraintr   r   r   )r   ÚargsÚkwargsÚkeyÚargr   r   r   r   Ú__table_cls__N   s"    	
ÿÿ
þ

zNameMetaMixin.__table_cls__)r'   Ú
__module__Ú__qualname__r)   r5   Ú__classcell__r   r   r,   r   r%   >   s   r%   c                       s   e Zd Z‡ fdd„Z‡  ZS )ÚBindMetaMixinc                    sT   |  dd ¡pt| dd ƒ}tt| ƒ |||¡ |d k	rPt| dd ƒd k	rP|| jjd< d S )NZ__bind_key__r&   Úbind_key)ÚpopÚgetattrr(   r9   r)   r&   Úinfo)r   r#   r*   r+   r:   r,   r   r   r)   s   s    
þzBindMetaMixin.__init__)r'   r6   r7   r)   r8   r   r   r,   r   r9   r   s   r9   c                   @   s   e Zd ZdS )ÚDefaultMetaN)r'   r6   r7   r   r   r   r   r>      s   r>   c                   @   s    e Zd ZdZdZdZdd„ ZdS )ÚModelzóBase class for SQLAlchemy declarative base model.

    To define models, subclass :attr:`db.Model <SQLAlchemy.Model>`, not this
    class. To customize ``db.Model``, subclass this and pass it as
    ``model_class`` to :class:`SQLAlchemy`.
    Nc                 C   sH   t | ƒj}|d kr"d t| ƒ¡}nd dd„ |D ƒ¡}d t| ƒj|¡S )Nz(transient {0})z, c                 s   s   | ]}t |ƒV  qd S r	   r   )r   Úvaluer   r   r   r   ™   s     z!Model.__repr__.<locals>.<genexpr>z	<{0} {1}>)r   ÚidentityÚformatÚidÚjoinÚtyper'   )ÚselfrA   Úpkr   r   r   Ú__repr__”   s
    
zModel.__repr__)r'   r6   r7   Ú__doc__Zquery_classÚqueryrH   r   r   r   r   r?   ƒ   s   	r?   )ÚreZ
sqlalchemyr0   r   Zsqlalchemy.ext.declarativer   r   Zsqlalchemy.schemar   Ú_compatr   r   Úcompiler    r$   Úobjectr%   r9   r>   r?   r   r   r   r   Ú<module>   s   $
4