o
    ȷea                     @   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                   @   sz   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rd| j | _nd| _|| _d S N_)verbose_namenamedefaultrequired	validatorchoices	slot_nameuniqueselfr   r   r   r   r   r   r    r   K/var/www/ideatree/venv/lib/python3.10/site-packages/boto/sdb/db/property.py__init__(   s   
zProperty.__init__c                 C   s   |r|   t|| jS d S N)loadgetattrr   r   objobjtyper   r   r   __get__6   s   zProperty.__get__c                 C   st   |  | z|jrt|d| j rt|d| j }||}W n ty0   tjd| j  Y nw t	|| j
| d S )Nz	on_set_%szException running on_set_%s)validate_loadedhasattrr   r   	Exceptionbotolog	exceptionsetattrr   )r   r   valuefncr   r   r   __set__=   s   
zProperty.__set__c                 C   s   || _ || _d| j | _d S r
   )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s$t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   r1   U      zProperty.default_valuec                 C   sj   | j r|d u rtd| j | jr$|r$|| jvr$td|| jj| jf | jr.| | |S | | |S )N%s is a required propertyz%s not a valid choice for %s.%s)r   
ValueErrorr   r   r+   r4   r   r7   r6   r   r   r   r    X   s   

zProperty.validatec                 C   s   | S r   r   r6   r   r   r   emptyc   r9   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   r6   r   r   r   make_value_from_datastorei      z"Property.make_value_from_datastorec                 C   s   t | jr	|  S | jS r   )callabler   r8   r   r   r   get_choicesl   s   
zProperty.get_choicesNNNFNNF)r4   
__module____qualname__strr2   	type_namer   r   r   r   r*   r.   r7   r1   r    r<   r?   r@   rC   r   r   r   r   r   !   s$    
r   c                 C   s@   | d u rd S t | tjrt| dkrtdd S tdt|  )N   &Length of value greater than maxlengthzExpecting String, got %s)r/   r   r0   lenr;   r3   r5   )r(   r   r   r   validate_stringr   s   rL   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              	          t t| ||||||| d S r   )superrM   r   r   	__class__r   r   r      s   zStringProperty.__init__)r4   rE   rF   rH   rL   r   __classcell__r   r   rQ   r   rM   |   s    rM   c                       s4   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   )rP   rT   r   
max_length)	r   r   r   r   r   r   r   r   rV   rQ   r   r   r      s   
zTextProperty.__init__c                    sV   t t| |}t|tjstdt| | jr't	|| jkr)t
d| j d S d S )NzExpecting Text, got %sz)Length of value greater than maxlength %s)rP   rT   r    r/   r   r0   r3   r5   rV   rK   r;   r6   rQ   r   r   r       s   zTextProperty.validateNNr	   FNNFN)r4   rE   rF   rH   r   r    rS   r   r   rQ   r   rT      s    rT   c                       sf   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)rP   rX   r   hashfunc)	r   r   r   r   r   r   r   r   rY   rQ   r   r   r      s   
zPasswordProperty.__init__c                 C   s   | j || jd}|S N)rY   )r2   rY   )r   r(   pr   r   r   r@      s   z*PasswordProperty.make_value_from_datastorec                    s(   t t| |}|rt|rt|S d S r   )rP   rX   r?   rK   rG   r   r>   r(   rQ   r   r   r?      s   z(PasswordProperty.get_value_for_datastorec                    s>   t || js| j| jd}|| |}tt| || d S rZ   )r/   r2   rY   setrP   rX   r*   )r   r   r(   r[   rQ   r   r   r*      s
   
zPasswordProperty.__set__c                    s   | j tt| ||| jdS rZ   )r2   rP   rX   r   rY   r   rQ   r   r   r      s   zPasswordProperty.__get__c                    sN   t t| |}t|| jrt|dkrtdd S tdt| jt|f )NrI   rJ   zExpecting %s, got %s)	rP   rX   r    r/   r2   rK   r;   r3   r5   r6   rQ   r   r   r       s   zPasswordProperty.validaterW   )r4   rE   rF   __doc__r   r2   rH   r   r@   r?   r*   r   r    rS   r   r   rQ   r   rX      s    -rX   c                       s$   e Zd ZeZdZ fddZ  ZS )BlobPropertyblobc                    sZ   ||   kr"t|ts"| |t|}d }|r|j}t||d}|}tt| || d S )N)r(   id)	r1   r/   r   r   r5   ra   rP   r_   r*   )r   r   r(   oldbra   brQ   r   r   r*      s   
zBlobProperty.__set__)r4   rE   rF   r   r2   rH   r*   rS   r   r   rQ   r   r_      s    r_   c                       sX   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 )S3KeyPropertyS3Keyz^s3:\/\/([^\/]*)\/(.*)$NFc              	      rO   r   )rP   rd   r   r   rQ   r   r   r     s   zS3KeyProperty.__init__c                    sp   t t| |}||  ks|t|  kr|  S t|| jr"d S t| j	|}|r-d S t
d| jt|f Nz&Validation Error, expecting %s, got %s)rP   rd   r    r1   rG   r/   r2   rematchvalidate_regexr3   r5   )r   r(   rh   rQ   r   r   r      s   zS3KeyProperty.validatec                    s   t t| ||}|rFt|| jr|S t| j|}|rD|j	 }|j
|ddd}||d}|sB||d}|d |S d S |S )N   F)r       r	   )rP   rd   r   r/   r2   rg   rh   ri   _managerget_s3_connection
get_bucketgroupget_keynew_keyset_contents_from_string)r   r   r   r(   rh   s3bucketkrQ   r   r   r     s   

	zS3KeyProperty.__get__c                    s*   t t| |}|rd|jj|jf S d S )Nz
s3://%s/%s)rP   rd   r?   rt   r   r\   rQ   r   r   r?   ,  s   z%S3KeyProperty.get_value_for_datastorerD   )r4   rE   rF   r$   rs   keyr   r2   rH   ri   r   r    r   r?   rS   r   r   rQ   r   rd     s    
rd   c                       sJ   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 )IntegerPropertyIntegerNr   F   c
           
   	      s,   t t| ||||||| || _|	| _d S r   )rP   rw   r   maxmin)
r   r   r   r   r   r   r   r   r{   r|   rQ   r   r   r   9     
zIntegerProperty.__init__c                    sL   t |}tt| |}|| jkrtd| j || jk r$td| j |S )NMaximum value is %dMinimum value is %d)intrP   rw   r    r{   r;   r|   r6   rQ   r   r   r    ?  s   

zIntegerProperty.validatec                 C      |d u S r   r   r6   r   r   r   r<   H     zIntegerProperty.emptyc                    s&   |dks|d u r
d}t t| ||S )Nr	   r   )rP   rw   r*   r   r   r(   rQ   r   r   r*   K  s   zIntegerProperty.__set__)	NNr   FNNFry   rz   )r4   rE   rF   r   r2   rH   r   r    r<   r*   rS   r   r   rQ   r   rw   4  s    	rw   c                       >   e Zd ZeZdZ		d fdd	Z fddZd	d
 Z  Z	S )LongPropertyLongNr   Fc              	      rO   r   )rP   r   r   r   rQ   r   r   r   V      zLongProperty.__init__c                    sL   t |}tt| |}d}d}||krtd| ||k r$td| |S )Nl         l    r~   r   )r   rP   r   r    r;   )r   r(   r|   r{   rQ   r   r   r    Z  s   zLongProperty.validatec                 C   r   r   r   r6   r   r   r   r<   e  r   zLongProperty.empty)NNr   FNNF)
r4   rE   rF   r   r2   rH   r   r    r<   rS   r   r   rQ   r   r   Q  s    r   c                       s2   e Zd ZeZdZ		d fdd	Zdd Z  ZS )	BooleanPropertyBooleanNFc              	      rO   r   )rP   r   r   r   rQ   r   r   r   n  r   zBooleanProperty.__init__c                 C   r   r   r   r6   r   r   r   r<   r  r   zBooleanProperty.empty)NNFFNNF)	r4   rE   rF   boolr2   rH   r   r<   rS   r   r   rQ   r   r   i  s    r   c                       r   )FloatPropertyFloatN        Fc              	      rO   r   )rP   r   r   r   rQ   r   r   r   {  r   zFloatProperty.__init__c                    s   t |}tt| |}|S r   )floatrP   r   r    r6   rQ   r   r   r      s   zFloatProperty.validatec                 C   r   r   r   r6   r   r   r   r<     r   zFloatProperty.empty)NNr   FNNF)
r4   rE   rF   r   r2   rH   r   r    r<   rS   r   r   rQ   r   r   v  s    r   c                       s\   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
           
   	      ,   t t| |||||||	 || _|| _d S r   )rP   r   r   auto_nowauto_now_add
r   r   r   r   r   r   r   r   r   r   rQ   r   r   r     r}   zDateTimeProperty.__init__c                    "   | j s| jr
|  S tt|  S r   )r   r   nowrP   r   r1   r8   rQ   r   r   r1        zDateTimeProperty.default_valuec                    s,   |d u rd S t |tjr|S tt| |S r   )r/   datetimedaterP   r   r    r6   rQ   r   r   r      s
   zDateTimeProperty.validatec                    s(   | j rt|| j|   tt| |S r   )r   r'   r   r   rP   r   r?   r=   rQ   r   r   r?     s   z(DateTimeProperty.get_value_for_datastorec                 C   s
   t j  S r   )r   utcnowr8   r   r   r   r        
zDateTimeProperty.now	NFFNNFNNF)r4   rE   rF   r^   r   r2   rH   r   r1   r    r?   r   rS   r   r   rQ   r   r     s    r   c                       sX   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
           
   	      r   r   )rP   r   r   r   r   r   rQ   r   r   r     r}   zDateProperty.__init__c                    r   r   )r   r   r   rP   r   r1   r8   rQ   r   r   r1     r   zDateProperty.default_valuec                    B   t t| |}|d u rd S t|| jstd| jt|f d S rf   )rP   r   r    r/   r2   r3   r5   r6   rQ   r   r   r         zDateProperty.validatec                    s@   | j rt|| j|   tt| |}t|tjr|	 }|S r   )
r   r'   r   r   rP   r   r?   r/   r   r   )r   r>   valrQ   r   r   r?     s   z$DateProperty.get_value_for_datastorec                 C   s
   t j S r   )r   r   todayr8   r   r   r   r     r   zDateProperty.nowr   )r4   rE   rF   r   r   r2   rH   r   r1   r    r?   r   rS   r   r   rQ   r   r     s    r   c                       s8   e Zd ZejZdZ		d fdd	Z fddZ  Z	S )	TimePropertyTimeNFc              	      rO   r   )rP   r   r   r   rQ   r   r   r     r   zTimeProperty.__init__c                    r   rf   )rP   r   r    r/   r2   r3   r5   r6   rQ   r   r   r      r   zTimeProperty.validaterD   )
r4   rE   rF   r   timer2   rH   r   r    rS   r   r   rQ   r   r     s    r   c                       sb   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 )ReferenceProperty	ReferenceNFc
           
   	      s,   t t| |||||||	 || _|| _d S r   )rP   r   r   reference_classcollection_name)
r   r   r   r   r   r   r   r   r   r   rQ   r   r   r     r}   zReferenceProperty.__init__c                 C   sL   |r$t || j}||  kr|S t|tjr"| |}t|| j| |S d S r   )	r   r   r1   r/   r   r0   r   r'   r   r   r   r   r(   r   r   r   r     s   
zReferenceProperty.__get__c                    sB   |dur|j |kst|dr|j |j krtdtt| ||S )z[Don't allow this object to be associated to itself
        This causes bad things to happenNra   z(Can not associate an object with itself!)ra   r"   r;   rP   r   r*   r   rQ   r   r   r*     s   (zReferenceProperty.__set__c                    sl   t t| || | jd u rd|j | jf | _t| j| jr't	d| j t
| j| jt||| j d S )Nz	%s_%s_setzduplicate property: %s)rP   r   r.   r   r4   lowerr   r"   r   r;   r'   _ReverseReferencePropertyr,   rQ   r   r   r.     s   

z%ReferenceProperty.__property_config__c                 C   s   | d}t|dkrtd S )N-   )splitrK   r;   )r   r(   tr   r   r   
check_uuid  s   
zReferenceProperty.check_uuidc                 C   sF   z|  }| j  }||rW d S td||f    td| )Nz%s not instance of %sz%s is not a Model)get_lineager   
startswithr3   r;   )r   r(   obj_lineagecls_lineager   r   r   check_instance  s   

z ReferenceProperty.check_instancec                 C   sZ   | j r|  | | jr|d u rtd| j ||  krd S t|tjs+| | d S d S )Nr:   )	r   r   r;   r   r1   r/   r   r0   r   r6   r   r   r   r      s   
zReferenceProperty.validate)	NNNNNFNNF)r4   rE   rF   r   r2   rH   r   r   r*   r.   r   r   r    rS   r   r   rQ   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   s^   |dur-t | j}t| jtr$g }| jD ]	}|d|  q|||S || jd |S | 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)z%Not possible to set a new collection.zVirtual property is read-only)r;   r\   r   r   r   r*   :  s   z!_ReverseReferenceProperty.__set__N)	r4   rE   rF   r   r2   rH   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   )rP   r   r   calculated_type
use_method)
r   r   r   r   r   r   r   r   r   r   rQ   r   r   r   A  s
   
zCalculatedProperty.__init__c                 C   sJ   |   }|r#zt|| j}| jr| }W |S W |S  ty"   Y |S w |S r   )r1   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   r   r   r   r   r*   T  s   zCalculatedProperty.__set__c                 C   s   | j st|| j| d S d S r   )r   r'   r   r   r   r   r   _set_directX  s   zCalculatedProperty._set_directc                 C   s&   | j tttfv r| ||j}|S d S r   )r   rG   r   r   r   rR   r\   r   r   r   r?   \  s   z*CalculatedProperty.get_value_for_datastore)
r4   rE   rF   r   r   r   r*   r   r?   rS   r   r   rQ   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 u rg }|| _ tt| j||f|dd| d S NT)r   r   )r   rP   r   r   r   r   r   r   r   kwdsrQ   r   r   r   i     $zListProperty.__init__c                 C   s   | j r|  | |d urt|ts|g}| jtjv rtj}n| jtjv r(tj}n| j}|D ]}t||sK|tjkr@td| j td| j| jj	f q-|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_typesr0   r;   r   r4   )r   r(   r   itemr   r   r   r    o  s&   



zListProperty.validatec                 C   r   r   r   r6   r   r   r   r<     r   zListProperty.emptyc                    s   t tt|  S r   )r   rP   r   r1   r8   rQ   r   r   r1     s   zListProperty.default_valuec                    s^   | j tjv r
tj}n| j tjv rtj}n| j }t||r |g}n|du r&g }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   r0   r/   rP   r   r*   )r   r   r(   r   rQ   r   r   r*     s   
zListProperty.__set__)NNN)r4   rE   rF   r   r2   rH   r   r    r<   r1   r*   rS   r   r   rQ   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 )MapPropertyMapNc                    s6   |d u ri }|| _ tt| j||f|dd| d S r   )r   rP   r   r   r   rQ   r   r   r     r   zMapProperty.__init__c                    s   t t| |}|d urt|tstd| jtjv rtj}n| jtj	v r)tj	}n| j}|D ] }t|| |sN|tjkrCtd| j
 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)rP   r   r    r/   dictr;   r   r   r   r0   r   r4   )r   r(   r   rv   rQ   r   r   r      s$   

zMapProperty.validatec                 C   r   r   r   r6   r   r   r   r<     r   zMapProperty.emptyc                 C   s   i S r   r   r8   r   r   r   r1     rA   zMapProperty.default_value)r4   rE   rF   r   r2   rH   rG   r   r    r<   r1   rS   r   r   rQ   r   r     s    r   )#r   boto.sdb.db.keyr   
boto.utilsr   boto.sdb.db.queryr   rg   r$   boto.s3.keyboto.sdb.db.blobr   boto.compatr   r   objectr   rL   rM   rT   rX   r_   rd   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s8   Q
[/##C%7