o ckF[a@sddlZddlmZddlmZddlmZddlZddlZddl Zddl m Z ddl m Z mZGdddeZd d ZGd d d eZGd ddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdd d eZGd!d"d"eZGd#d$d$eZGd%d&d&eZGd'd(d(eZ Gd)d*d*eZ!Gd+d,d,eZ"dS)-N)Key)Password)Query)Blob)six long_typec@szeZdZeZdZdZdZ  dddZddZ dd Z d d Z d d Z ddZ ddZddZddZddZddZdS)PropertyNFcCsH||_||_||_||_||_||_|jrd|j|_nd|_||_dSN_) verbose_namenamedefaultrequired validatorchoices slot_nameuniqueselfr r rrrrrr6/usr/lib/python3/dist-packages/boto/sdb/db/property.py__init__(s zProperty.__init__cCs|r |t||jSdSN)loadgetattrrrobjobjtyperrr__get__6s zProperty.__get__cCst||z|jrt|d|jrt|d|j}||}Wnty0tjd|jYnwt ||j |dS)Nz on_set_%szException running on_set_%s) validate_loadedhasattrr r Exceptionbotolog exceptionsetattrr)rrvaluefncrrr__set__=s  zProperty.__set__cCs||_||_d|j|_dSr ) model_classr rrr+ property_namerrr__property_config__JszProperty.__property_config__cCsLt|tjs ||krdSt||js$td|jj|j|jt |fdS)Nz,Validation Error, %s.%s expecting %s, got %s) isinstancer string_types default_value data_type TypeErrorr+__name__r typerr(rrrdefault_validatorOs   zProperty.default_validatorcCs|jSr)rrrrrr1UzProperty.default_valuecCsj|jr|durtd|j|jr$|r$||jvr$td||jj|jf|jr.|||S|||S)N%s is a required propertyz%s not a valid choice for %s.%s)r ValueErrorr rr+r4rr7r6rrrr Xs  zProperty.validatecCs| Srrr6rrremptycr9zProperty.emptycCs t||jSr)rr rmodel_instancerrrget_value_for_datastorefs z Property.get_value_for_datastorecCs|Srrr6rrrmake_value_from_datastoreiz"Property.make_value_from_datastorecCst|jr |S|jSr)callablerr8rrr get_choicesls zProperty.get_choicesNNNFNNF)r4 __module__ __qualname__strr2 type_namer r rrr*r.r7r1r r<r?r@rCrrrrr!s$    rcCs@|durdSt|tjrt|dkrtddStdt|)N&Length of value greater than maxlengthzExpecting String, got %s)r/rr0lenr;r3r5)r(rrrvalidate_stringrs  rLcs0eZdZdZddddeddffdd ZZS)StringPropertyStringNr Fc  tt||||||||dSr)superrMrr __class__rrrszStringProperty.__init__)r4rErFrHrLr __classcell__rrrQrrM|s rMcs4eZdZdZ   d fdd ZfddZZS) TextPropertyTextNr Fc s&tt||||||||||_dSr)rPrTr max_length) rr r rrrrrrVrQrrrs zTextProperty.__init__csVtt||}t|tjstdt||jr't ||jkr)t d|jdSdS)NzExpecting Text, got %sz)Length of value greater than maxlength %s) rPrTr r/rr0r3r5rVrKr;r6rQrrr s  zTextProperty.validateNNr FNNFN)r4rErFrHrr rSrrrQrrTsrTcsfeZdZdZeZdZ  dfdd Zdd Zfd d Z fd d Z fddZ fddZ 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() rNr Fc s&tt||||||||||_dS)z The hashfunc parameter overrides the default hashfunc in boto.utils.Password. The remaining parameters are passed through to StringProperty.__init__N)rPrXrhashfunc) rr r rrrrrrYrQrrrs zPasswordProperty.__init__cCs|j||jd}|SN)rY)r2rY)rr(prrrr@sz*PasswordProperty.make_value_from_datastorecs(tt||}|rt|rt|SdSr)rPrXr?rKrGrr>r(rQrrr?s z(PasswordProperty.get_value_for_datastorecs>t||js|j|jd}|||}tt|||dSrZ)r/r2rYsetrPrXr*)rrr(r[rQrrr*s  zPasswordProperty.__set__cs|jtt||||jdSrZ)r2rPrXrrYrrQrrrszPasswordProperty.__get__csNtt||}t||jrt|dkrtddStdt|jt|f)NrIrJzExpecting %s, got %s) rPrXr r/r2rKr;r3r5r6rQrrr s   zPasswordProperty.validaterW)r4rErF__doc__rr2rHrr@r?r*rr rSrrrQrrXs-   rXcs$eZdZeZdZfddZZS) BlobPropertyblobcsZ||kr"t|ts"||t|}d}|r|j}t||d}|}tt|||dS)N)r(id) r1r/rrr5rarPr_r*)rrr(oldbrabrQrrr*s   zBlobProperty.__set__)r4rErFrr2rHr*rSrrrQrr_sr_csXeZdZejjjZdZdZ  d fdd Z fddZ fd d Z fd d Z ZS) S3KeyPropertyS3Keyz^s3:\/\/([^\/]*)\/(.*)$NFc rOr)rPrdrrrQrrr szS3KeyProperty.__init__csptt||}||ks|t|kr|St||jr"dSt|j |}|r-dSt d|jt |fNz&Validation Error, expecting %s, got %s) rPrdr r1rGr/r2rematchvalidate_regexr3r5)rr(rhrQrrr s zS3KeyProperty.validatecstt|||}|rFt||jr|St|j|}|rD|j }|j | ddd}| | d}|sB| | d}|d|SdS|S)NF)r r )rPrdrr/r2rgrhri_managerget_s3_connection get_bucketgroupget_keynew_keyset_contents_from_string)rrrr(rhs3bucketkrQrrrs    zS3KeyProperty.__get__cs*tt||}|rd|jj|jfSdS)Nz s3://%s/%s)rPrdr?rtr r\rQrrr?,sz%S3KeyProperty.get_value_for_datastorerD)r4rErFr$rskeyrr2rHrirr rr?rSrrrQrrds   rdcsJeZdZeZdZ  dfdd Zfd d Zd d Zfd dZ Z S)IntegerPropertyIntegerNrFc s,tt||||||||||_| |_dSr)rPrwrmaxmin) rr r rrrrrr{r|rQrrr9 zIntegerProperty.__init__csLt|}tt||}||jkrtd|j||jkr$td|j|S)NMaximum value is %dMinimum value is %d)intrPrwr r{r;r|r6rQrrr ?s  zIntegerProperty.validatecC|duSrrr6rrrr<HzIntegerProperty.emptycs&|dks|dur d}tt|||S)Nr r)rPrwr*rrr(rQrrr*KszIntegerProperty.__set__) NNrFNNFryrz) r4rErFrr2rHrr r<r*rSrrrQrrw4s  rwc>eZdZeZdZ  d fdd ZfddZd d ZZ S) LongPropertyLongNrFc rOr)rPrrrrQrrrV zLongProperty.__init__csLt|}tt||}d}d}||krtd|||kr$td||S)Nllr~r)rrPrr r;)rr(r|r{rQrrr Zs  zLongProperty.validatecCrrrr6rrrr<erzLongProperty.empty)NNrFNNF) r4rErFrr2rHrr r<rSrrrQrrQs  rcs2eZdZeZdZ  dfdd ZddZZS) BooleanPropertyBooleanNFc rOr)rPrrrrQrrrnrzBooleanProperty.__init__cCrrrr6rrrr<rrzBooleanProperty.empty)NNFFNNF) r4rErFboolr2rHrr<rSrrrQrrisrcr) FloatPropertyFloatNFc rOr)rPrrrrQrrr{rzFloatProperty.__init__cst|}tt||}|Sr)floatrPrr r6rQrrr szFloatProperty.validatecCrrrr6rrrr<rzFloatProperty.empty)NNrFNNF) r4rErFrr2rHrr r<rSrrrQrrvs rcs\eZdZdZejZdZ  dfdd ZfddZfd d Z fd d Z d dZ 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 databaseDateTimeNFc ,tt|||||||| ||_||_dSr)rPrrauto_now auto_now_add rr rrr rrrrrrQrrrr}zDateTimeProperty.__init__c"|js|jr |Stt|Sr)rrnowrPrr1r8rQrrr1 zDateTimeProperty.default_valuecs,|durdSt|tjr|Stt||Sr)r/datetimedaterPrr r6rQrrr s  zDateTimeProperty.validatecs(|jr t||j|tt||Sr)rr'r rrPrr?r=rQrrr?sz(DateTimeProperty.get_value_for_datastorecCs tjSr)rutcnowr8rrrr zDateTimeProperty.now NFFNNFNNF) r4rErFr^rr2rHrr1r r?rrSrrrQrrs   rcsXeZdZejZdZ  dfdd ZfddZfdd Z fd d Z d d Z Z S) DatePropertyDateNFc rr)rPrrrrrrQrrrr}zDateProperty.__init__crr)rrrrPrr1r8rQrrr1rzDateProperty.default_valuecBtt||}|durdSt||jstd|jt|fdSrf)rPrr r/r2r3r5r6rQrrr   zDateProperty.validatecs@|jr t||j|tt||}t|tjr| }|Sr) rr'r rrPrr?r/rr)rr>valrQrrr?s  z$DateProperty.get_value_for_datastorecCs tjSr)rrtodayr8rrrrrzDateProperty.nowr) r4rErFrrr2rHrr1r r?rrSrrrQrrs   rcs8eZdZejZdZ  dfdd ZfddZZ S) TimePropertyTimeNFc rOr)rPrrrrQrrrrzTimeProperty.__init__crrf)rPrr r/r2r3r5r6rQrrr rzTimeProperty.validaterD) r4rErFrtimer2rHrr rSrrrQrrsrcsbeZdZeZdZ  dfdd ZddZfdd Zfd d Z d d Z ddZ ddZ Z S)ReferenceProperty ReferenceNFc s,tt|||||||| ||_||_dSr)rPrrreference_classcollection_name) rrrr r rrrrrrQrrrr}zReferenceProperty.__init__cCsL|r$t||j}||kr|St|tjr"||}t||j||SdSr) rrr1r/rr0rr'r rrrr(rrrrs    zReferenceProperty.__get__csB|dur|j|kst|dr|j|jkrtdtt|||S)z[Don't allow this object to be associated to itself This causes bad things to happenNraz(Can not associate an object with itself!)rar"r;rPrr*rrQrrr*s(zReferenceProperty.__set__csltt||||jdurd|j|jf|_t|j|jr't d|jt |j|jt |||jdS)Nz %s_%s_setzduplicate property: %s) rPrr.rr4lowerr r"rr;r'_ReverseReferencePropertyr,rQrrr.s   z%ReferenceProperty.__property_config__cCs|d}t|dkr tdS)N-)splitrKr;)rr(trrr check_uuids  zReferenceProperty.check_uuidcCsFz|}|j}||rWdStd||ftd|)Nz%s not instance of %sz%s is not a Model) get_lineager startswithr3r;)rr( obj_lineage cls_lineagerrrcheck_instance s   z ReferenceProperty.check_instancecCsZ|jr|||jr|durtd|j||krdSt|tjs+||dSdS)Nr:) rrr;r r1r/rr0rr6rrrr s   zReferenceProperty.validate) NNNNNFNNF)r4rErFrr2rHrrr*r.rrr rSrrrQrrs   rc@s,eZdZeZdZddZddZddZdS) rquerycCs"||_||_||_||_||_dSr) _ReverseReferenceProperty__model#_ReverseReferenceProperty__propertyrr item_type)rmodelpropr rrrr%s  z"_ReverseReferenceProperty.__init__cCs^|dur-t|j}t|jtr$g}|jD] }|d|q|||S||jd|S|S)zBFetches collection of model instances of this collection property.Nz%s =z =)rrr/rlistappendfilter)rr>r+rpropsrrrrr,s    z!_ReverseReferenceProperty.__get__cCstd)z%Not possible to set a new collection.zVirtual property is read-only)r;r\rrrr*:sz!_ReverseReferenceProperty.__set__N) r4rErFrr2rHrrr*rrrrr!s  rc sPeZdZddddddeddf fdd ZddZddZd d Zd d ZZ S) CalculatedPropertyNFc s,tt||||||||||_| |_dSr)rPrrcalculated_type use_method) rr r rrrrrrrrQrrrAs  zCalculatedProperty.__init__cCsJ|}|r#zt||j}|jr|}W|SW|Sty"Y|Sw|Sr)r1rrrAttributeErrorrrrrrIs  zCalculatedProperty.__get__cCsdS)z!Not possible to set a new AutoID.Nrrrrrr*TszCalculatedProperty.__set__cCs|js t||j|dSdSr)rr'rrrrr _set_directXszCalculatedProperty._set_directcCs&|jtttfvr|||j}|SdSr)rrGrrrrRr\rrrr?\sz*CalculatedProperty.get_value_for_datastore) r4rErFrrrr*rr?rSrrrQrr?s rcsNeZdZeZdZd fdd ZddZddZfd d Z fd d Z Z S) ListPropertyListNc s6|durg}||_tt|j||f|dd|dSNT)rr)rrPrrrrr r rkwdsrQrrri$zListProperty.__init__cCs|jr|||durt|ts|g}|jtjvrtj}n |jtjvr(tj}n|j}|D]}t||sK|tjkr@td|jtd|j|jj fq-|S)Nz*Items in the %s list must all be integers.z-Items in the %s list must all be %s instances) rr/rrr integer_typesr0r;r r4)rr(ritemrrrr os&       zListProperty.validatecCrrrr6rrrr<rzListProperty.emptycsttt|Sr)rrPrr1r8rQrrr1szListProperty.default_valuecs^|jtjvr tj}n |jtjvrtj}n|j}t||r |g}n|dur&g}tt|||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)rrrr0r/rPrr*)rrr(rrQrrr*s   zListProperty.__set__)NNN) r4rErFrr2rHrr r<r1r*rSrrrQrrds rcsJeZdZeZdZedddffdd ZfddZddZ d d Z Z S) MapPropertyMapNc s6|duri}||_tt|j||f|dd|dSr)rrPrrrrQrrrrzMapProperty.__init__cstt||}|durt|tstd|jtjvrtj}n |jtj vr)tj }n|j}|D] }t|||sN|tjkrCtd|j td|j |jj fq.|S)NzValue must of type dictz*Values in the %s Map must all be integers.z-Values in the %s Map must all be %s instances) rPrr r/dictr;rrrr0r r4)rr(rrvrQrrr s$     zMapProperty.validatecCrrrr6rrrr<rzMapProperty.emptycCsiSrrr8rrrr1rAzMapProperty.default_value) r4rErFrr2rHrGrr r<r1rSrrrQrrs r)#rboto.sdb.db.keyr boto.utilsrboto.sdb.db.queryrrgr$ boto.s3.keyboto.sdb.db.blobr boto.compatrrobjectrrLrMrTrXr_rdrwrrrrrrrrrrrrrrrs8    Q  [/ ##C%7