î
Õ­éUO  ã               @   s   Gd  d „  d e  ƒ Z d S)c               @   sd   e  Z d  Z d Z d d „  Z d d „  Z d d „  Z d d	 „  Z d
 d „  Z e	 d d „  ƒ Z
 d S)ÚEntityuW  Generic model container.

    >>> Entity(id = 7, name = u"Abacaxi")
    <Entity {'id': 7, 'name': u'Abacaxi'}>
    >>> Entity(id = 7, class_ = u"Abacaxi")
    <Entity {'class': u'Abacaxi', 'id': 7}>
    >>> Entity(**{'id': 7, 'class': u"Abacaxi"})
    <Entity {'class': u'Abacaxi', 'id': 7}>
    >>> Entity(**{'id': 7, 'class.id': 12, 'class.name': u"Abacaxi"})
    <Entity {'class': <Entity {'id': 12, 'name': u'Abacaxi'}>, 'id': 7}>
    >>> Entity(id = 7, class__id = 12, class__name = u"Abacaxi")
    <Entity {'class': <Entity {'id': 12, 'name': u'Abacaxi'}>, 'id': 7}>
    >>> Entity(**{'id': 7, 'class.id': 12, 'class.name': u"Abacaxi", 'class.campus.id': 19, 'class.campus.name': u"Tomate"})
    <Entity {'class': <Entity {'campus': <Entity {'id': 19, 'name': u'Tomate'}>, 'id': 12, 'name': u'Abacaxi'}>, 'id': 7}>
    >>> m = Entity(id = 7, name = u"Abacaxi")
    >>> m.id = 14
    >>> m
    <Entity {'id': 14, 'name': u'Abacaxi'}>
    >>> m.class_ = Entity(id = 28, name = u"MaÃ§Ã£")
    >>> m
    <Entity {'class': <Entity {'id': 28, 'name': u'Ma\xc3\xa7\xc3\xa3'}>, 'id': 14, 'name': u'Abacaxi'}>
    c                s  t  j |  d i  ƒ d d „  t ˆ j ƒ  ƒ Dƒ ‰ xÛ ˆ rt ˆ j ƒ  ƒ d } d | k r{ ˆ | |  j | <ˆ | =q5 n  | j d d ƒ \ ‰  } ‡  f d d †  t ˆ j ƒ  ƒ Dƒ } t ‡  ‡ f d	 d †  | Dƒ   |  j ˆ  <x( ‡  f d
 d †  | Dƒ D] } ˆ | =qû Wq5 Wd  S)NÚ_datac             S   s(   i  |  ] \ } } | t  j | ƒ “ q S© )r   ÚNormalizeKey)Ú.0ÚkeyÚvaluer   r   ú!/root/lawrence/lawrence/Entity.pyú
<dictcomp>   s   	 z#Entity.__init__.<locals>.<dictcomp>é    Ú.é   c                sN   g  |  ]D } d  | k r | j  d  d ƒ d ˆ  k r | j  d  d ƒ d ‘ q S)r   r   r
   )Úsplit)r   r   )Úheadr   r   ú
<listcomp>#   s   	 z#Entity.__init__.<locals>.<listcomp>c                s%   i  |  ] } ˆ ˆ  d  | | “ q S)r   r   )r   Útail)r   Úkwargsr   r   r	   %   s   	 c             3   s   |  ] } ˆ  d  | Vq d S)r   Nr   )r   r   )r   r   r   ú	<genexpr>&   s    z"Entity.__init__.<locals>.<genexpr>)ÚobjectÚ__setattr__ÚlistÚitemsÚkeysr   r   r   )Úselfr   r   Ú_Ztailsr   )r   r   r   Ú__init__   s    	%) zEntity.__init__c             C   sa   t  | t ƒ s t ‚ t j | ƒ } y t j |  d ƒ | SWn t k
 r\ t | ƒ ‚ Yn Xd  S)Nr   )	Ú
isinstanceÚstrÚAssertionErrorr   r   r   Ú__getattribute__ÚKeyErrorÚAttributeError)r   r   r   r   r   Ú__getattr__)   s    zEntity.__getattr__c             C   s[   t  | t ƒ s t ‚ | j d ƒ r; t j |  | | ƒ d  St j | ƒ } | |  j | <d  S)Nr   )	r   r   r   Ú
startswithr   r   r   r   r   )r   r   r   r   r   r   r   1   s    zEntity.__setattr__c             C   s9   d |  j  j d j d d „  t |  j j ƒ  ƒ Dƒ ƒ f S)Nz	<%s {%s}>z, c             S   s2   g  |  ]( \ } } d  t  | ƒ t  | ƒ f ‘ q S)z%s: %s)Úrepr)r   ÚkÚvr   r   r   r   ;   s   	z#Entity.__repr__.<locals>.<listcomp>)Ú	__class__Ú__name__ÚjoinÚsortedr   r   )r   r   r   r   Ú__repr__9   s    zEntity.__repr__c             C   s"   t  | t ƒ s t ‚ | |  j k S)N)r   r   r   r   )r   r   r   r   r   Ú__contains__?   s    zEntity.__contains__c             C   sn   t  | t ƒ s t ‚ | j d ƒ r7 | d d … } n  | j d d ƒ } t  | t ƒ rj | j d ƒ } n  | S)zjTransform a key to a canonical form.

        This allows using e.g. class_ instead of class as a keyword.r   Nr   Ú__r   zutf-8éÿÿÿÿ)r   r   r   ÚendswithÚreplaceÚbytesÚdecode)Úclsr   r   r   r   r   C   s    zEntity.NormalizeKeyN)r'   Ú
__module__Ú__qualname__Ú__doc__r   r!   r   r*   r+   Úclassmethodr   r   r   r   r   r      s   r   N)r   r   r   r   r   r   Ú<module>   s    