B
    ?F[a                 @   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   3lib/python3.7/site-packages/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   |  | y2|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)validateZ_loadedhasattrr   r   	ExceptionbotologZ	exceptionsetattrr   )r   r   valueZfncr   r   r   __set__=   s    
zProperty.__set__c             C   s   || _ || _d| j | _d S )Nr
   )model_classr   r   )r   r$   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 )N)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 )Nz%s is a required propertyz%s not a valid choice for %s.%s)r   
ValueErrorr   r   r$   r,   r   r.   )r   r"   r   r   r   r   X   s    
zProperty.validatec             C   s   | S )Nr   )r   r"   r   r   r   emptyc   s    zProperty.emptyc             C   s   t || jS )N)r   r   )r   model_instancer   r   r   get_value_for_datastoref   s    z Property.get_value_for_datastorec             C   s   |S )Nr   )r   r"   r   r   r   make_value_from_datastorei   s    z"Property.make_value_from_datastorec             C   s   t | jr|  S | jS )N)callabler   )r   r   r   r   get_choicesl   s    
zProperty.get_choices)NNNFNNF)r,   
__module____qualname__strr*   	type_namer   r   r   r   r#   r&   r.   r)   r   r0   r2   r3   r5   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 )Ni   z&Length of value greater than maxlengthzExpecting String, got %s)r'   r   r(   lenr/   r+   r-   )r"   r   r   r   validate_stringr   s    
r;   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 )N)superr<   r   )r   r   r   r   r   r   r   r   )	__class__r   r   r      s    zStringProperty.__init__)r,   r6   r7   r9   r;   r   __classcell__r   r   )r?   r   r<   |   s   r<   c                   s.   e Zd ZdZd	 fdd	Z fddZ  ZS )
TextPropertyZTextNr	   Fc	       	   	      s&   t t| ||||||| || _d S )N)r>   rA   r   
max_length)	r   r   r   r   r   r   r   r   rB   )r?   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)r>   rA   r   r'   r   r(   r+   r-   rB   r:   r/   )r   r"   )r?   r   r   r      s
    zTextProperty.validate)NNr	   FNNFN)r,   r6   r7   r9   r   r   r@   r   r   )r?   r   rA      s
     rA   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)r>   rC   r   hashfunc)	r   r   r   r   r   r   r   r   rD   )r?   r   r   r      s    
zPasswordProperty.__init__c             C   s   | j || jd}|S )N)rD   )r*   rD   )r   r"   pr   r   r   r3      s    z*PasswordProperty.make_value_from_datastorec                s,   t t| |}|r$t|r$t|S d S d S )N)r>   rC   r2   r:   r8   )r   r1   r"   )r?   r   r   r2      s    z(PasswordProperty.get_value_for_datastorec                s>   t || js(| j| jd}|| |}tt| || d S )N)rD   )r'   r*   rD   setr>   rC   r#   )r   r   r"   rE   )r?   r   r   r#      s
    
zPasswordProperty.__set__c                s   | j tt| ||| jdS )N)rD   )r*   r>   rC   r   rD   )r   r   r   )r?   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 )Ni   z&Length of value greater than maxlengthzExpecting %s, got %s)	r>   rC   r   r'   r*   r:   r/   r+   r-   )r   r"   )r?   r   r   r      s
    
zPasswordProperty.validate)NNr	   FNNFN)r,   r6   r7   __doc__r   r*   r9   r   r3   r2   r#   r   r   r@   r   r   )r?   r   rC      s   - rC   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   r-   rI   r>   rH   r#   )r   r   r"   ZoldbrI   b)r?   r   r   r#      s    
zBlobProperty.__set__)r,   r6   r7   r   r*   r9   r#   r@   r   r   )r?   r   rH      s   rH   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 )N)r>   rK   r   )r   r   r   r   r   r   r   r   )r?   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)r>   rK   r   r)   r8   r'   r*   rematchvalidate_regexr+   r-   )r   r"   rM   )r?   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	   )r>   rK   r   r'   r*   rL   rM   rN   Z_managerZget_s3_connectionZ
get_bucketgroupZget_keyZnew_keyZset_contents_from_string)r   r   r   r"   rM   s3bucketk)r?   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)r>   rK   r2   rS   r   )r   r1   r"   )r?   r   r   r2   ,  s    z%S3KeyProperty.get_value_for_datastore)NNNFNNF)r,   r6   r7   r   rR   keyr   r*   r9   rN   r   r   r   r2   r@   r   r   )r?   r   rK     s   
 rK   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 )N)r>   rV   r   maxmin)
r   r   r   r   r   r   r   r   rY   rZ   )r?   r   r   r   9  s    zIntegerProperty.__init__c                sL   t |}tt| |}|| jkr0td| j || jk rHtd| j |S )NzMaximum value is %dzMinimum value is %d)intr>   rV   r   rY   r/   rZ   )r   r"   )r?   r   r   r   ?  s    

zIntegerProperty.validatec             C   s   |d kS )Nr   )r   r"   r   r   r   r0   H  s    zIntegerProperty.emptyc                s&   |dks|d krd}t t| ||S )Nr	   r   )r>   rV   r#   )r   r   r"   )r?   r   r   r#   K  s    zIntegerProperty.__set__)	NNr   FNNFrW   rX   )r,   r6   r7   r[   r*   r9   r   r   r0   r#   r@   r   r   )r?   r   rV   4  s    	rV   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 )N)r>   r\   r   )r   r   r   r   r   r   r   r   )r?   r   r   r   V  s    zLongProperty.__init__c                sL   t |}tt| |}d}d}||kr4td| ||k rHtd| |S )Nl         l    zMaximum value is %dzMinimum value is %d)r   r>   r\   r   r/   )r   r"   rZ   rY   )r?   r   r   r   Z  s    zLongProperty.validatec             C   s   |d kS )Nr   )r   r"   r   r   r   r0   e  s    zLongProperty.empty)NNr   FNNF)
r,   r6   r7   r   r*   r9   r   r   r0   r@   r   r   )r?   r   r\   Q  s    r\   c                   s.   e Zd ZeZdZd fdd	Zdd Z  ZS )	BooleanPropertyZBooleanNFc          	      s    t t| ||||||| d S )N)r>   r]   r   )r   r   r   r   r   r   r   r   )r?   r   r   r   n  s    zBooleanProperty.__init__c             C   s   |d kS )Nr   )r   r"   r   r   r   r0   r  s    zBooleanProperty.empty)NNFFNNF)	r,   r6   r7   boolr*   r9   r   r0   r@   r   r   )r?   r   r]   i  s
    r]   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 )N)r>   r_   r   )r   r   r   r   r   r   r   r   )r?   r   r   r   {  s    zFloatProperty.__init__c                s   t |}tt| |}|S )N)floatr>   r_   r   )r   r"   )r?   r   r   r     s    zFloatProperty.validatec             C   s   |d kS )Nr   )r   r"   r   r   r   r0     s    zFloatProperty.empty)NNr`   FNNF)
r,   r6   r7   ra   r*   r9   r   r   r0   r@   r   r   )r?   r   r_   v  s    r_   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 databaseZDateTimeNFc
       
   	      s,   t t| |||||||	 || _|| _d S )N)r>   rb   r   auto_nowauto_now_add)
r   r   rc   rd   r   r   r   r   r   r   )r?   r   r   r     s    zDateTimeProperty.__init__c                s"   | j s| jr|  S tt|  S )N)rc   rd   nowr>   rb   r)   )r   )r?   r   r   r)     s    zDateTimeProperty.default_valuec                s,   |d krd S t |tjr|S tt| |S )N)r'   datetimedater>   rb   r   )r   r"   )r?   r   r   r     s
    zDateTimeProperty.validatec                s(   | j rt|| j|   tt| |S )N)rc   r!   r   re   r>   rb   r2   )r   r1   )r?   r   r   r2     s    z(DateTimeProperty.get_value_for_datastorec             C   s
   t j  S )N)rf   Zutcnow)r   r   r   r   re     s    zDateTimeProperty.now)	NFFNNFNNF)r,   r6   r7   rG   rf   r*   r9   r   r)   r   r2   re   r@   r   r   )r?   r   rb     s    rb   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 )DatePropertyZDateNFc
       
   	      s,   t t| |||||||	 || _|| _d S )N)r>   rh   r   rc   rd   )
r   r   rc   rd   r   r   r   r   r   r   )r?   r   r   r     s    zDateProperty.__init__c                s"   | j s| jr|  S tt|  S )N)rc   rd   re   r>   rh   r)   )r   )r?   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 )Nz&Validation Error, expecting %s, got %s)r>   rh   r   r'   r*   r+   r-   )r   r"   )r?   r   r   r     s
    zDateProperty.validatec                s@   | j rt|| j|   tt| |}t|tjr<|	 }|S )N)
rc   r!   r   re   r>   rh   r2   r'   rf   rg   )r   r1   val)r?   r   r   r2     s    z$DateProperty.get_value_for_datastorec             C   s
   t j S )N)rf   rg   Ztoday)r   r   r   r   re     s    zDateProperty.now)	NFFNNFNNF)r,   r6   r7   rf   rg   r*   r9   r   r)   r   r2   re   r@   r   r   )r?   r   rh     s    rh   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 )N)r>   rj   r   )r   r   r   r   r   r   r   r   )r?   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 )Nz&Validation Error, expecting %s, got %s)r>   rj   r   r'   r*   r+   r-   )r   r"   )r?   r   r   r     s
    zTimeProperty.validate)NNNFNNF)
r,   r6   r7   rf   Ztimer*   r9   r   r   r@   r   r   )r?   r   rj     s
    rj   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 )N)r>   rk   r   reference_classcollection_name)
r   rl   rm   r   r   r   r   r   r   r   )r?   r   r   r     s    zReferenceProperty.__init__c             C   sL   |rHt || j}||  kr |S t|tjrD| |}t|| j| |S d S )N)	r   r   r)   r'   r   r(   rl   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 happenNrI   z(Can not associate an object with itself!)rI   r   r/   r>   rk   r#   )r   r   r"   )r?   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)r>   rk   r&   rm   r,   lowerr   r   rl   r/   r!   _ReverseReferenceProperty)r   r$   r%   )r?   r   r   r&     s    

z%ReferenceProperty.__property_config__c             C   s   | d}t|dkrtd S )N-   )splitr:   r/   )r   r"   tr   r   r   
check_uuid  s    
zReferenceProperty.check_uuidc             C   sR   y4|  }| j  }||r"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_lineagerl   
startswithr+   r/   )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 )Nz%s is a required property)	r   r   r/   r   r)   r'   r   r(   rv   )r   r"   r   r   r   r     s    
zReferenceProperty.validate)	NNNNNFNNF)r,   r6   r7   r   r*   r9   r   r   r#   r&   rt   rv   r   r@   r   r   )r?   r   rk     s    	
rk   c               @   s,   e Zd ZeZdZdd Zdd Zdd ZdS )	ro   queryc             C   s"   || _ || _|| _|| _|| _d S )N) _ReverseReferenceProperty__model#_ReverseReferenceProperty__propertyrm   r   	item_type)r   Zmodelpropr   r   r   r   r   %  s
    z"_ReverseReferenceProperty.__init__c             C   sh   |dk	r`t | j}t| jtrLg }x| jD ]}|d|  q*W |||S || jd |S n| S dS )zBFetches collection of model instances of this collection property.Nz%s =z =)r   rx   r'   ry   listappendfilter)r   r1   r$   rw   Z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)r/   )r   r1   r"   r   r   r   r#   :  s    z!_ReverseReferenceProperty.__set__N)	r,   r6   r7   r   r*   r9   r   r   r#   r   r   r   r   ro   !  s
   ro   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 )N)r>   r   r   calculated_type
use_method)
r   r   r   r   r   r   r   r   r   r   )r?   r   r   r   A  s    
zCalculatedProperty.__init__c             C   sB   |   }|r>yt|| j}| jr&| }W n tk
r<   Y nX |S )N)r)   r   r   r   AttributeError)r   r   r   r"   r   r   r   r   I  s    
zCalculatedProperty.__get__c             C   s   dS )z!Not possible to set a new AutoID.Nr   )r   r   r"   r   r   r   r#   T  s    zCalculatedProperty.__set__c             C   s   | j st|| j| d S )N)r   r!   r   )r   r   r"   r   r   r   _set_directX  s    zCalculatedProperty._set_directc             C   s*   | j tttgkr"| ||j}|S d S d S )N)r   r8   r[   r^   r   r?   )r   r1   r"   r   r   r   r2   \  s    z*CalculatedProperty.get_value_for_datastore)
r,   r6   r7   r[   r   r   r#   r   r2   r@   r   r   )r?   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 )ListPropertyZListNc                s6   |d krg }|| _ tt| j||f|dd| d S )NT)r   r   )rz   r>   r   r   )r   rz   r   r   r   kwds)r?   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}xF|D ]>}t||s\|tjkrtd| j q\td| j| jj	f q\W |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|   rz   r   integer_typesr(   r/   r   r,   )r   r"   rz   itemr   r   r   r   o  s"    




zListProperty.validatec             C   s   |d kS )Nr   )r   r"   r   r   r   r0     s    zListProperty.emptyc                s   t tt|  S )N)r|   r>   r   r)   )r   )r?   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)rz   r   r   r(   r'   r>   r   r#   )r   r   r"   rz   )r?   r   r   r#     s    
zListProperty.__set__)NNN)r,   r6   r7   r|   r*   r9   r   r   r0   r)   r#   r@   r   r   )r?   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 )NT)r   r   )rz   r>   r   r   )r   rz   r   r   r   r   )r?   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}xJ|D ]B}t|| |s^|tjkrtd| j
 q^td| j
| jjf q^W |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)r>   r   r   r'   dictr/   rz   r   r   r(   r   r,   )r   r"   rz   rU   )r?   r   r   r     s     


zMapProperty.validatec             C   s   |d kS )Nr   )r   r"   r   r   r   r0     s    zMapProperty.emptyc             C   s   i S )Nr   )r   r   r   r   r)     s    zMapProperty.default_value)r,   r6   r7   r   r*   r9   r8   r   r   r0   r)   r@   r   r   )r?   r   r     s   r   )#rf   Zboto.sdb.db.keyr   Z
boto.utilsr   Zboto.sdb.db.queryr   rL   r   Zboto.s3.keyZboto.sdb.db.blobr   Zboto.compatr   r   objectr   r;   r<   rA   rC   rH   rK   rV   r\   r]   r_   rb   rh   rj   rk   ro   r   r   r   r   r   r   r   <module>   s6   Q
[/##C%7