U
    W+da                     @   s  d dl 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mZ G dd deZd	d
 ZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd  d eZG d!d" d"eZG d#d$ d$eZG d%d& d&eZG d'd( d(eZ G d)d* d*eZ!G d+d, d,eZ"dS )-    N)Key)Password)Query)Blob)six	long_typec                   @   sv   e Zd ZeZdZdZdZdddZdd Z	dd	 Z
d
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )Property NFc                 C   sH   || _ || _|| _|| _|| _|| _| jr8d| j | _nd| _|| _d S N_)verbose_namenamedefaultrequired	validatorchoices	slot_nameuniqueselfr   r   r   r   r   r   r    r   8/tmp/pip-unpacked-wheel-dlxw5sjy/boto/sdb/db/property.py__init__(   s    zProperty.__init__c                 C   s    |r|   t|| jS d S d S N)loadgetattrr   r   objobjtyper   r   r   __get__6   s    zProperty.__get__c                 C   sv   |  | z2|jr:t|d| j r:t|d| j }||}W n& tk
rb   tjd| j  Y nX t	|| j
| d S )Nz	on_set_%szException running on_set_%s)validate_loadedhasattrr   r   	Exceptionbotolog	exceptionsetattrr   )r   r   valueZfncr   r   r   __set__=   s    
zProperty.__set__c                 C   s   || _ || _d| j | _d S r
   )model_classr   r   r   r*   Zproperty_namer   r   r   __property_config__J   s    zProperty.__property_config__c                 C   sL   t |tjs||  krd S t || jsHtd| jj| j| jt	|f d S )Nz,Validation Error, %s.%s expecting %s, got %s)

isinstancer   string_typesdefault_value	data_type	TypeErrorr*   __name__r   typer   r(   r   r   r   default_validatorO   s    zProperty.default_validatorc                 C   s   | j S r   )r   r   r   r   r   r/   U   s    zProperty.default_valuec                 C   sh   | j r|d krtd| j | jrH|rH|| jkrHtd|| jj| jf | jrZ| | n
| | |S )N%s is a required propertyz%s not a valid choice for %s.%s)r   
ValueErrorr   r   r*   r2   r   r5   r4   r   r   r   r    X   s    
zProperty.validatec                 C   s   | S r   r   r4   r   r   r   emptyc   s    zProperty.emptyc                 C   s   t || jS r   )r   r   r   model_instancer   r   r   get_value_for_datastoref   s    z Property.get_value_for_datastorec                 C   s   |S r   r   r4   r   r   r   make_value_from_datastorei   s    z"Property.make_value_from_datastorec                 C   s   t | jr|  S | jS r   )callabler   r6   r   r   r   get_choicesl   s    
zProperty.get_choices)NNNFNNF)r2   
__module____qualname__strr0   	type_namer   r   r   r   r)   r,   r5   r/   r    r9   r<   r=   r?   r   r   r   r   r   !   s(           
r   c                 C   sB   | d krd S t | tjr.t| dkr>tdntdt|  d S )N   &Length of value greater than maxlengthzExpecting String, got %s)r-   r   r.   lenr8   r1   r3   )r(   r   r   r   validate_stringr   s    
rG   c                       s0   e Zd ZdZddddeddf fdd	Z  ZS )StringPropertyStringNr	   Fc              	      s    t t| ||||||| d S r   )superrH   r   r   	__class__r   r   r      s
      zStringProperty.__init__)r2   r@   rA   rC   rG   r   __classcell__r   r   rK   r   rH   |   s     rH   c                       s.   e Zd ZdZd	 fdd	Z fddZ  ZS )
TextPropertyTextNr	   Fc	           	   	      s&   t t| ||||||| || _d S r   )rJ   rN   r   
max_length)	r   r   r   r   r   r   r   r   rP   rK   r   r   r      s      zTextProperty.__init__c                    sR   t t| |}t|tjs,tdt| | jrNt	|| jkrNt
d| j d S )NzExpecting Text, got %sz)Length of value greater than maxlength %s)rJ   rN   r    r-   r   r.   r1   r3   rP   rF   r8   r4   rK   r   r   r       s
    zTextProperty.validate)NNr	   FNNFN)r2   r@   rA   rC   r   r    rM   r   r   rK   r   rN      s            rN   c                       sb   e Zd ZdZeZdZd fdd	Zdd	 Z fd
dZ	 fddZ
 fddZ fddZ  ZS )PasswordPropertya  

    Hashed property whose original value can not be
    retrieved, but still can be compared.

    Works by storing a hash of the original value instead
    of the original value.  Once that's done all that
    can be retrieved is the hash.

    The comparison

       obj.password == 'foo'

    generates a hash of 'foo' and compares it to the
    stored hash.

    Underlying data type for hashing, storing, and comparing
    is boto.utils.Password.  The default hash function is
    defined there ( currently sha512 in most cases, md5
    where sha512 is not available )

    It's unlikely you'll ever need to use a different hash
    function, but if you do, you can control the behavior
    in one of two ways:

      1) Specifying hashfunc in PasswordProperty constructor

         import hashlib

         class MyModel(model):
             password = PasswordProperty(hashfunc=hashlib.sha224)

      2) Subclassing Password and PasswordProperty

         class SHA224Password(Password):
             hashfunc=hashlib.sha224

         class SHA224PasswordProperty(PasswordProperty):
             data_type=MyPassword
             type_name="MyPassword"

         class MyModel(Model):
             password = SHA224PasswordProperty()

    r   Nr	   Fc	           	   	      s&   t t| ||||||| || _dS )z
           The hashfunc parameter overrides the default hashfunc in boto.utils.Password.

           The remaining parameters are passed through to StringProperty.__init__N)rJ   rQ   r   hashfunc)	r   r   r   r   r   r   r   r   rR   rK   r   r   r      s      zPasswordProperty.__init__c                 C   s   | j || jd}|S N)rR   )r0   rR   )r   r(   pr   r   r   r=      s    z*PasswordProperty.make_value_from_datastorec                    s,   t t| |}|r$t|r$t|S d S d S r   )rJ   rQ   r<   rF   rB   r   r;   r(   rK   r   r   r<      s    z(PasswordProperty.get_value_for_datastorec                    s>   t || js(| j| jd}|| |}tt| || d S rS   )r-   r0   rR   setrJ   rQ   r)   )r   r   r(   rT   rK   r   r   r)      s
    
zPasswordProperty.__set__c                    s   | j tt| ||| jdS rS   )r0   rJ   rQ   r   rR   r   rK   r   r   r      s    zPasswordProperty.__get__c                    sP   t t| |}t|| jr2t|dkrLtdntdt| jt|f d S )NrD   rE   zExpecting %s, got %s)	rJ   rQ   r    r-   r0   rF   r8   r1   r3   r4   rK   r   r   r       s
    
zPasswordProperty.validate)NNr	   FNNFN)r2   r@   rA   __doc__r   r0   rC   r   r=   r<   r)   r   r    rM   r   r   rK   r   rQ      s   -        rQ   c                       s$   e Zd ZeZdZ fddZ  ZS )BlobPropertyZblobc                    sZ   ||   krDt|tsD| |t|}d }|r4|j}t||d}|}tt| || d S )N)r(   id)	r/   r-   r   r   r3   rY   rJ   rX   r)   )r   r   r(   ZoldbrY   brK   r   r   r)      s    
zBlobProperty.__set__)r2   r@   rA   r   r0   rC   r)   rM   r   r   rK   r   rX      s   rX   c                       sT   e Zd ZejjjZdZdZ	d fdd	Z
 fddZ fd	d
Z fddZ  ZS )S3KeyPropertyZS3Keyz^s3:\/\/([^\/]*)\/(.*)$NFc              	      s    t t| ||||||| d S r   )rJ   r[   r   r   rK   r   r   r     s
      zS3KeyProperty.__init__c                    st   t t| |}||  ks,|t|  kr4|  S t|| jrDd S t| j	|}|rZd S t
d| jt|f d S Nz&Validation Error, expecting %s, got %s)rJ   r[   r    r/   rB   r-   r0   rematchvalidate_regexr1   r3   )r   r(   r^   rK   r   r   r      s    zS3KeyProperty.validatec                    s   t t| ||}|rt|| jr&|S t| j|}|r|j	 }|j
|ddd}||d}|s||d}|d |S n|S d S )N   F)r       r	   )rJ   r[   r   r-   r0   r]   r^   r_   _managerZget_s3_connectionZ
get_bucketgroupget_keyZnew_keyZset_contents_from_string)r   r   r   r(   r^   s3bucketkrK   r   r   r     s    

zS3KeyProperty.__get__c                    s.   t t| |}|r&d|jj|jf S d S d S )Nz
s3://%s/%s)rJ   r[   r<   rf   r   rU   rK   r   r   r<   ,  s    z%S3KeyProperty.get_value_for_datastore)NNNFNNF)r2   r@   rA   r$   re   keyr   r0   rC   r_   r   r    r   r<   rM   r   r   rK   r   r[     s   
        r[   c                	       sF   e Zd ZeZdZd fdd	Z fd	d
Zdd Z fddZ	  Z
S )IntegerPropertyZIntegerNr   F   c
           
   	      s,   t t| ||||||| || _|	| _d S r   )rJ   ri   r   maxmin)
r   r   r   r   r   r   r   r   rl   rm   rK   r   r   r   9  s    zIntegerProperty.__init__c                    sL   t |}tt| |}|| jkr0td| j || jk rHtd| j |S )NMaximum value is %dMinimum value is %d)intrJ   ri   r    rl   r8   rm   r4   rK   r   r   r    ?  s    

zIntegerProperty.validatec                 C   s   |d kS r   r   r4   r   r   r   r9   H  s    zIntegerProperty.emptyc                    s&   |dks|d krd}t t| ||S )Nr	   r   )rJ   ri   r)   r   r   r(   rK   r   r   r)   K  s    zIntegerProperty.__set__)	NNr   FNNFrj   rk   )r2   r@   rA   rp   r0   rC   r   r    r9   r)   rM   r   r   rK   r   ri   4  s             	ri   c                       s:   e Zd ZeZdZd fdd	Z fddZd	d
 Z  Z	S )LongPropertyZLongNr   Fc              	      s    t t| ||||||| d S r   )rJ   rr   r   r   rK   r   r   r   V  s    zLongProperty.__init__c                    sL   t |}tt| |}d}d}||kr4td| ||k rHtd| |S )Nl         l    rn   ro   )r   rJ   rr   r    r8   )r   r(   rm   rl   rK   r   r   r    Z  s    zLongProperty.validatec                 C   s   |d kS r   r   r4   r   r   r   r9   e  s    zLongProperty.empty)NNr   FNNF)
r2   r@   rA   r   r0   rC   r   r    r9   rM   r   r   rK   r   rr   Q  s         rr   c                       s.   e Zd ZeZdZd fdd	Zdd Z  ZS )	BooleanPropertyBooleanNFc              	      s    t t| ||||||| d S r   )rJ   rs   r   r   rK   r   r   r   n  s    zBooleanProperty.__init__c                 C   s   |d kS r   r   r4   r   r   r   r9   r  s    zBooleanProperty.empty)NNFFNNF)	r2   r@   rA   boolr0   rC   r   r9   rM   r   r   rK   r   rs   i  s         rs   c                       s:   e Zd ZeZdZd fdd	Z fddZd	d
 Z  Z	S )FloatPropertyZFloatN        Fc              	      s    t t| ||||||| d S r   )rJ   rv   r   r   rK   r   r   r   {  s    zFloatProperty.__init__c                    s   t |}tt| |}|S r   )floatrJ   rv   r    r4   rK   r   r   r      s    zFloatProperty.validatec                 C   s   |d kS r   r   r4   r   r   r   r9     s    zFloatProperty.empty)NNrw   FNNF)
r2   r@   rA   rx   r0   rC   r   r    r9   rM   r   r   rK   r   rv   v  s         rv   c                	       sX   e Zd ZdZejZdZd fdd	Z fddZ fd	d
Z	 fddZ
dd Z  ZS )DateTimePropertyzThis class handles both the datetime.datetime object
    And the datetime.date objects. It can return either one,
    depending on the value stored in the databaseDateTimeNFc
           
   	      s,   t t| |||||||	 || _|| _d S r   )rJ   ry   r   auto_nowauto_now_add
r   r   r{   r|   r   r   r   r   r   r   rK   r   r   r     s    zDateTimeProperty.__init__c                    s"   | j s| jr|  S tt|  S r   )r{   r|   nowrJ   ry   r/   r6   rK   r   r   r/     s    zDateTimeProperty.default_valuec                    s,   |d krd S t |tjr|S tt| |S r   )r-   datetimedaterJ   ry   r    r4   rK   r   r   r      s
    zDateTimeProperty.validatec                    s(   | j rt|| j|   tt| |S r   )r{   r'   r   r~   rJ   ry   r<   r:   rK   r   r   r<     s    z(DateTimeProperty.get_value_for_datastorec                 C   s
   t j  S r   )r   utcnowr6   r   r   r   r~     s    zDateTimeProperty.now)	NFFNNFNNF)r2   r@   rA   rW   r   r0   rC   r   r/   r    r<   r~   rM   r   r   rK   r   ry     s             ry   c                	       sT   e Zd ZejZdZd fdd	Z fddZ fdd	Z	 fd
dZ
dd Z  ZS )DatePropertyDateNFc
           
   	      s,   t t| |||||||	 || _|| _d S r   )rJ   r   r   r{   r|   r}   rK   r   r   r     s    zDateProperty.__init__c                    s"   | j s| jr|  S tt|  S r   )r{   r|   r~   rJ   r   r/   r6   rK   r   r   r/     s    zDateProperty.default_valuec                    sB   t t| |}|d krd S t|| js>td| jt|f d S r\   )rJ   r   r    r-   r0   r1   r3   r4   rK   r   r   r      s
    zDateProperty.validatec                    s@   | j rt|| j|   tt| |}t|tjr<|	 }|S r   )
r{   r'   r   r~   rJ   r   r<   r-   r   r   )r   r;   valrK   r   r   r<     s    z$DateProperty.get_value_for_datastorec                 C   s
   t j S r   )r   r   todayr6   r   r   r   r~     s    zDateProperty.now)	NFFNNFNNF)r2   r@   rA   r   r   r0   rC   r   r/   r    r<   r~   rM   r   r   rK   r   r     s             r   c                       s4   e Zd ZejZdZd fdd	Z fddZ  Z	S )	TimePropertyZTimeNFc              	      s    t t| ||||||| d S r   )rJ   r   r   r   rK   r   r   r     s    zTimeProperty.__init__c                    sB   t t| |}|d krd S t|| js>td| jt|f d S r\   )rJ   r   r    r-   r0   r1   r3   r4   rK   r   r   r      s
    zTimeProperty.validate)NNNFNNF)
r2   r@   rA   r   timer0   rC   r   r    rM   r   r   rK   r   r     s             r   c                	       s^   e Zd ZeZdZd fdd	Zdd Z fdd	Z fd
dZ	dd Z
dd Zdd Z  ZS )ReferencePropertyZ	ReferenceNFc
           
   	      s,   t t| |||||||	 || _|| _d S r   )rJ   r   r   reference_classcollection_name)
r   r   r   r   r   r   r   r   r   r   rK   r   r   r     s    zReferenceProperty.__init__c                 C   sL   |rHt || j}||  kr |S t|tjrD| |}t|| j| |S d S r   )	r   r   r/   r-   r   r.   r   r'   r   r   r   r   r(   r   r   r   r     s    
zReferenceProperty.__get__c                    sB   |dk	r0|j |ks(t|dr0|j |j kr0tdtt| ||S )z[Don't allow this object to be associated to itself
        This causes bad things to happenNrY   z(Can not associate an object with itself!)rY   r"   r8   rJ   r   r)   rq   rK   r   r   r)     s    (zReferenceProperty.__set__c                    sl   t t| || | jd kr2d|j | jf | _t| j| jrNt	d| j t
| j| jt||| j d S )Nz	%s_%s_setzduplicate property: %s)rJ   r   r,   r   r2   lowerr   r"   r   r8   r'   _ReverseReferencePropertyr+   rK   r   r   r,     s    

z%ReferenceProperty.__property_config__c                 C   s   | d}t|dkrtd S )N-   )splitrF   r8   )r   r(   tr   r   r   
check_uuid  s    
zReferenceProperty.check_uuidc                 C   sT   z6|  }| j  }||r$W d S td||f W n   td| Y nX d S )Nz%s not instance of %sz%s is not a Model)Zget_lineager   
startswithr1   r8   )r   r(   Zobj_lineageZcls_lineager   r   r   check_instance  s    

z ReferenceProperty.check_instancec                 C   sV   | j r|  | | jr,|d kr,td| j ||  kr<d S t|tjsR| | d S )Nr7   )	r   r   r8   r   r/   r-   r   r.   r   r4   r   r   r   r      s    
zReferenceProperty.validate)	NNNNNFNNF)r2   r@   rA   r   r0   rC   r   r   r)   r,   r   r   r    rM   r   r   rK   r   r     s"                 	
r   c                   @   s,   e Zd ZeZdZdd Zdd Zdd ZdS )	r   queryc                 C   s"   || _ || _|| _|| _|| _d S r   ) _ReverseReferenceProperty__model#_ReverseReferenceProperty__propertyr   r   	item_type)r   modelpropr   r   r   r   r   %  s
    z"_ReverseReferenceProperty.__init__c                 C   sd   |dk	r\t | j}t| jtrHg }| jD ]}|d|  q(|||S || jd |S n| S dS )zBFetches collection of model instances of this collection property.Nz%s =z =)r   r   r-   r   listappendfilter)r   r;   r*   r   propsr   r   r   r   r   ,  s    

z!_ReverseReferenceProperty.__get__c                 C   s   t ddS )z%Not possible to set a new collection.zVirtual property is read-onlyN)r8   rU   r   r   r   r)   :  s    z!_ReverseReferenceProperty.__set__N)	r2   r@   rA   r   r0   rC   r   r   r)   r   r   r   r   r   !  s
   r   c                	       sP   e Zd Zddddddeddf	 fdd	Zdd Zdd Zd	d
 Zdd Z  Z	S )CalculatedPropertyNFc
           
   	      s,   t t| ||||||| || _|	| _d S r   )rJ   r   r   calculated_type
use_method)
r   r   r   r   r   r   r   r   r   r   rK   r   r   r   A  s      zCalculatedProperty.__init__c                 C   sB   |   }|r>zt|| j}| jr&| }W n tk
r<   Y nX |S r   )r/   r   r   r   AttributeErrorr   r   r   r   r   I  s    
zCalculatedProperty.__get__c                 C   s   dS )z!Not possible to set a new AutoID.Nr   rq   r   r   r   r)   T  s    zCalculatedProperty.__set__c                 C   s   | j st|| j| d S r   )r   r'   r   rq   r   r   r   _set_directX  s    zCalculatedProperty._set_directc                 C   s*   | j tttfkr"| ||j}|S d S d S r   )r   rB   rp   ru   r   rL   rU   r   r   r   r<   \  s    z*CalculatedProperty.get_value_for_datastore)
r2   r@   rA   rp   r   r   r)   r   r<   rM   r   r   rK   r   r   ?  s       r   c                       sN   e Zd ZeZdZd fdd	Zdd Zdd Z fd	d
Z	 fddZ
  ZS )ListPropertyListNc                    s6   |d krg }|| _ tt| j||f|dd| d S NT)r   r   )r   rJ   r   r   r   r   r   r   r   kwdsrK   r   r   r   i  s    zListProperty.__init__c                 C   s   | j r|  | |d k	r(t|ts(|g}| jtjkr<tj}n| jtjkrPtj}n| j}|D ]>}t||sZ|tjkrtd| j qZtd| j| jj	f qZ|S )Nz*Items in the %s list must all be integers.z-Items in the %s list must all be %s instances)
r   r-   r   r   r   integer_typesr.   r8   r   r2   )r   r(   r   itemr   r   r   r    o  s$    



zListProperty.validatec                 C   s   |d kS r   r   r4   r   r   r   r9     s    zListProperty.emptyc                    s   t tt|  S r   )r   rJ   r   r/   r6   rK   r   r   r/     s    zListProperty.default_valuec                    s^   | j tjkrtj}n| j tjkr(tj}n| j }t||r@|g}n|dkrLg }tt| ||S )zOverride the set method to allow them to set the property to an instance of the item_type instead of requiring a list to be passed inN)r   r   r   r.   r-   rJ   r   r)   )r   r   r(   r   rK   r   r   r)     s    
zListProperty.__set__)NNN)r2   r@   rA   r   r0   rC   r   r    r9   r/   r)   rM   r   r   rK   r   r   d  s   r   c                       sJ   e Zd ZeZdZedddf fdd	Z fddZdd Z	d	d
 Z
  ZS )MapPropertyZMapNc                    s6   |d kri }|| _ tt| j||f|dd| d S r   )r   rJ   r   r   r   rK   r   r   r     s    zMapProperty.__init__c                    s   t t| |}|d k	r*t|ts*td| jtjkr>tj}n| jtj	krRtj	}n| j}|D ]B}t|| |s\|tjkrtd| j
 q\td| j
| jjf q\|S )NzValue must of type dictz*Values in the %s Map must all be integers.z-Values in the %s Map must all be %s instances)rJ   r   r    r-   dictr8   r   r   r   r.   r   r2   )r   r(   r   rh   rK   r   r   r      s"    

zMapProperty.validatec                 C   s   |d kS r   r   r4   r   r   r   r9     s    zMapProperty.emptyc                 C   s   i S r   r   r6   r   r   r   r/     s    zMapProperty.default_value)r2   r@   rA   r   r0   rC   rB   r   r    r9   r/   rM   r   r   rK   r   r     s   r   )#r   Zboto.sdb.db.keyr   Z
boto.utilsr   Zboto.sdb.db.queryr   r]   r$   Zboto.s3.keyZboto.sdb.db.blobr   Zboto.compatr   r   objectr   rG   rH   rN   rQ   rX   r[   ri   rr   rs   rv   ry   r   r   r   r   r   r   r   r   r   r   r   <module>   s6   Q
[/##C%7