3
ni|  ã               @   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 j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 jddƒs,tdd„ | jdd… D ƒƒ r0dS xP| jD ]F}d|j krHq8t|j d tƒr\dS || kp||j jddƒp|t|tƒ  S W 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   úW/srv/DBW/html/py_pdb_browser/venv/lib/python3.6/site-packages/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 j|| ƒjdƒS )Nc             S   s<   | j ƒ }t|ƒdkr0d|d d… |d f jƒ S d|jƒ  S )Nr   z_%s_%sÚ_éÿÿÿÿr   )Ú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| ƒj|||ƒ 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 |jdƒƒ}|| jjkr,tj||ŽS x6|D ].}t|tjƒrH|jsTt|tj	ƒr2tj||ŽS q2W x.| j
dd… D ]}d|jkrtP qtW t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   Nr   )r   r   ÚmetadataZ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'   r1   Ú__classcell__r   r   )r*   r   r#   >   s   r#   c                   s   e Zd Z‡ fdd„Z‡  ZS )ÚBindMetaMixinc                sT   |j dd ƒpt| dd ƒ}tt| ƒj|||ƒ |d k	rPt| dd ƒd k	rP|| jjd< d S )NZ__bind_key__r$   Úbind_key)ÚpopÚgetattrr&   r5   r'   r$   Úinfo)r   r!   r(   r)   r6   )r*   r   r   r'   s   s
    zBindMetaMixin.__init__)r%   r2   r3   r'   r4   r   r   )r*   r   r5   r   s   r5   c               @   s   e Zd ZdS )ÚDefaultMetaN)r%   r2   r3   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jt| ƒƒ}ndjdd„ |D ƒƒ}djt| ƒj|ƒS )Nz(transient {0})z, c             s   s   | ]}t |ƒV  qd S )N)r   )r	   Úvaluer   r   r   r   ™   s    z!Model.__repr__.<locals>.<genexpr>z	<{0} {1}>)r   ÚidentityÚformatÚidÚjoinÚtyper%   )Úselfr=   Úpkr   r   r   Ú__repr__”   s
    
zModel.__repr__)r%   r2   r3   Ú__doc__Zquery_classÚqueryrD   r   r   r   r   r;   ƒ   s   r;   )ÚreZ
sqlalchemyr,   r   Zsqlalchemy.ext.declarativer   r   Zsqlalchemy.schemar   Z_compatr   r   Úcompiler   r"   rA   r#   r5   r:   Úobjectr;   r   r   r   r   Ú<module>   s   $
4