o ckF[´Hã@sŒddlZddlmZmZddlmZddlmZddlm Z m Z ddl m Z ddl m Z mZmZmZdZGd d „d eƒZGd d „d eƒZdS) éN)Ú find_classÚPassword)ÚKey)ÚModel)ÚsixÚ encodebytes)Údatetime)ÚgetDOMImplementationÚparseÚ parseStringÚNodez%Y-%m-%dT%H:%M:%SZc@s eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd„Zd d!„Zd"d#„Zd$d%„Zd&S)'Ú XMLConvertera¤ Responsible for converting base Python types to format compatible with underlying database. For SimpleDB, that means everything needs to be converted to a string when stored in SimpleDB and from a string when retrieved. To convert a value, pass it to the encode or decode method. The encode method will take a Python native value and convert to DB format. The decode method will take a DB format value and convert it to Python native format. To find the appropriate method to call, the generic encode/decode methods will look for the type-specific method by searching for a method called "encode_" or "decode_". c Cst||_t|j|jft|j|jft|j|j ft |j|j ft |j |j ft|j|jfi|_tjr8|j|jf|jt<dSdS©N)ÚmanagerÚboolÚ encode_boolÚ decode_boolÚintÚ encode_intÚ decode_intrÚencode_referenceÚdecode_referencerrÚencode_passwordÚdecode_passwordrÚencode_datetimeÚdecode_datetimeÚtype_maprÚPY2Ú encode_longÚ decode_longÚlong)Úselfr©r"ú@/usr/lib/python3/dist-packages/boto/sdb/db/manager/xmlmanager.pyÚ__init__+s      ûÿzXMLConverter.__init__cCs*d}|jD] }|j|jkr||j7}q|S)NÚ)Ú childNodesÚnodeTypeÚ TEXT_NODEÚdata)r!Ú parent_nodeÚvalueÚnoder"r"r#Úget_text_value6s    €zXMLConverter.get_text_valuecCs$||jvr|j|d}||ƒS|S©Nr)r)r!Ú item_typer+Úencoder"r"r#r0=s zXMLConverter.encodecCs.||jvr|j|d}||ƒS| |¡}|S)Né)rr-)r!r/r+Údecoder"r"r#r2Cs  zXMLConverter.decodecCsdt|tƒr+t|dƒr)g}|D]}t|dƒ}t| ¡vrt}| | ||¡¡q|S|S| |j|¡S)Nr/) Ú isinstanceÚlistÚhasattrÚgetattrrÚmroÚappendr0Ú data_type)r!Úpropr+Ú new_valueÚvr/r"r"r#Ú encode_propKs    zXMLConverter.encode_propcCst|jtkr3t|dƒr.t|dƒ}t| ¡vrt}g}| d¡D] }| ||¡}| |¡q|S|  |¡S| |j|¡S)Nr/Úitem) r9r4r5r6rr7ÚgetElementsByTagNamer2r8r-)r!r:r+r/ÚvaluesÚ item_noder"r"r#Ú decode_propZs       zXMLConverter.decode_propcCót|ƒ}d|S©Nz%d)r©r!r+r"r"r#rjózXMLConverter.encode_intcCs"| |¡}|r t|ƒ}|Sd}|Sr)r-rrEr"r"r#rns ÿzXMLConverter.decode_intcCrCrD)r rEr"r"r#rvrFzXMLConverter.encode_longcCó| |¡}t|ƒSr)r-r rEr"r"r#rzó zXMLConverter.decode_longcCs|dkrdSdS)NTÚtrueÚfalser"rEr"r"r#r~szXMLConverter.encode_boolcCs| |¡}| ¡dkr dSdS)NrITF)r-ÚlowerrEr"r"r#r„s  zXMLConverter.decode_boolcCs | t¡Sr)ÚstrftimeÚISO8601rEr"r"r#r‹s zXMLConverter.encode_datetimecCs&| |¡}zt |t¡WSYdSr)r-rÚstrptimerMrEr"r"r#rŽs zXMLConverter.decode_datetimecCsXt|tjƒr|S|durdS|jj d¡}| d|j¡| dd|jj |jj f¡|S)Nr%ÚobjectÚidÚclassú%s.%s) r3rÚ string_typesrÚdocÚ createElementÚ setAttributerPÚ __class__Ú __module__Ú__name__)r!r+Úval_noder"r"r#r•s zXMLConverter.encode_referencecCsH|sdSz|jd}| d¡}| d¡}t|ƒ}| |¡WSYdS)NrrQrP)r&Ú getAttributerÚ get_by_ids)r!r+Ú class_namerPÚclsr"r"r#r s    zXMLConverter.decode_referencecCs|r t|ƒdkr t|ƒSdSr.)ÚlenÚstrrEr"r"r#r¬szXMLConverter.encode_passwordcCrGr)r-rrEr"r"r#r²rHzXMLConverter.decode_passwordN)rYrXÚ __qualname__Ú__doc__r$r-r0r2r=rBrrrrrrrrrrrrr"r"r"r#r s(    r c@seZdZdd„Zdd„Zd>dd„Zdd „Zd d „Zd d „Zdd„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zd>dd„Zd d!„Zd"d#„Zd$d%„Zd?d&d'„Zd?d(d)„Zd>d*d+„Zd>d,d-„Zd.d/„Zd0d1„Zd2d3„Zd4d5„Zd6d7„Zd8d9„Zd:d;„Zd|jr|jr ddlm}nddlm}||j|jƒ|_dSdS)Nr)ÚHTTPSConnection)ÚHTTPConnection)rirrÚhttplibrvrwrjrq)r!Ú Connectionr"r"r#Ú_connectÒs  úzXMLManager._connectNcCsd|js| ¡z|j ¡WnY|j ¡i}|jr"|j|d<|j ||||¡|j ¡}|S)z3 Make a request on this connection Ú Authorization)rqrzÚcloseÚconnectrsÚrequestÚ getresponse)r!ÚmethodÚurlÚ post_dataÚbodyÚheadersÚrespr"r"r#Ú _make_requestÛs   zXMLManager._make_requestcCs|j ddd¡S)Nrd)rorp©r!r"r"r#Únew_docíszXMLManager.new_docc csª| d¡D]L}|s| d¡}t|ƒ}| d¡}||ƒ}| d¡D]-}| d¡}| |¡} | rNt| dƒr<| || j¡} n | | |¡} |  | ¡} t || j | ƒq!|VqdS)NrOrQrPÚpropertyÚnamer/) r?r[rÚ find_propertyr5Úget_listr/Ú decode_valueÚmake_value_from_datastoreÚsetattrrŠ) r!r^rTÚobj_noder]rPÚobjÚ prop_nodeÚ prop_namer:r+r"r"r#Ú_object_listerðs&€       €ðzXMLManager._object_listercCs | ¡dSr)rzr‡r"r"r#Úresets zXMLManager.resetcCs|jSr)rTr‡r"r"r#Úget_docszXMLManager.get_doccCó|j ||¡Sr)rnr=©r!r:r+r"r"r#Ú encode_value ózXMLManager.encode_valuecCr—r)rnrBr˜r"r"r#r ršzXMLManager.decode_valuecCs|js t |j|j¡|_|jSr)rmÚbotoÚ connect_s3Úaws_access_key_idÚaws_secret_access_keyr‡r"r"r#Úget_s3_connectionszXMLManager.get_s3_connectioncCsRg}z | d¡d}WngYS| d¡D]}|j ||¡}| |¡q|S)NÚitemsrr>)r?rnr2r8)r!r’r/r@Ú items_noderAr+r"r"r#rŒs zXMLManager.get_listc Csž| d¡d}|s| d¡}t|ƒ}|s| d¡}||ƒ}| d¡D]*}| d¡}| |¡} | | |¡} |  | ¡} | durLz t|| j| ƒWq"Yq"q"|S)NrOrrQrPr‰rŠ)r?r[rr‹rrŽrrŠ) r!r^rPrTrr]r‘r’r“r:r+r"r"r#Úget_object_from_docs&      üzXMLManager.get_object_from_docc CsŒ| d¡d}|s| d¡}t|ƒ}|s| d¡}i}| d¡D] }| d¡}| |¡} | | |¡} |  | ¡} | dur@| || j<q |||fS)zµ Pull out the properties from this document Returns the class, the properties in a hash, and the id if provided as a tuple :return: (cls, props, id) rOrrQrPr‰rŠN)r?r[rr‹rrŽrŠ) r!r^rPrTrr]Úpropsr’r“r:r+r"r"r#Úget_props_from_doc3s        € zXMLManager.get_props_from_doccCsf|js| ¡|jstdƒ‚d|j|f}| d|¡}|jdkr%t|ƒ}ntd|jƒ‚| |||¡S)Nú)Can't query without a database connectionú/%s/%sÚGETéÈú Error: %s) rqrzÚNotImplementedErrorrfr†Ústatusr Ú Exceptionr¢)r!r^rPrr…rTr"r"r#Ú get_objectJs   zXMLManager.get_objectc Csœ|js| ¡|jstdƒ‚ddlm}t| ||||¡ƒ}|r,d|j|d|iƒf}nd|j}| d|¡}|j dkrAt |ƒ} nt d |j ƒ‚|  || ¡S) Nr¥r)Ú urlencodez/%s?%sÚqueryú/%sr§r¨r©) rqrzrªÚurllibr®r`Ú _build_queryrfr†r«r r¬r”) r!r^ÚfiltersÚlimitÚorder_byr®r¯rr…rTr"r"r#r¯Xs      zXMLManager.queryc Cs.ddl}t|ƒdkrtdƒ‚g}|jdd}|D][\}} | ¡ ¡\} } d} |D]B} | j| krjd} | | ¡tkrZg}| D]}|  | |¡}|  d| | |f¡q<|  dd   |¡¡q(|  | | ¡} |  d | | | f¡q(| sstd | ƒ‚q|r’|  d ¡r„|d d…}d}n|}d}|  d|||f¡d  |¡S)NrézToo many filters, max is 4F©ÚhiddenTz '%s' %s '%s'z[%s]z OR z['%s' %s '%s']z%s is not a valid fieldú-r1ÚdescÚascz"['%s' starts-with ''] sort '%s' %sz intersection ) Útypesr_r¬Ú propertiesÚstripÚsplitrŠÚTypeTyper4r™r8ÚjoinÚ startswith)r!r^r³r´rµr¼Úpartsr½Úfilterr+rŠÚopÚfoundr‰Ú filter_partsÚvalÚkeyÚtyper"r"r#r²ms>      € ÿ   zXMLManager._build_querycOstdƒ‚)Nz GQL queries not supported in XML)rª)r!Ú query_stringÚargsÚkwdsr"r"r#Ú query_gqlŽszXMLManager.query_gqlcCs`| d¡}| |¡|D]!}| d¡}| |¡t|tƒr#| |¡q | |¡}| |¡q dS)Nr r>)rUÚ appendChildr3r ÚcreateTextNode)r!rTr r’r¡r>rAÚ text_noder"r"r#Ú save_list‘s        ùzXMLManager.save_listc Cs²| |¡}|jrd|j|jf}nd|j}|jd|| ¡d}| |jdt|ƒ¡}|j|_| ¡D]$}z|j }Wn t yDd}Ynw|rVt ||j ƒ} | rVt ||j | ƒq2|S)z1 Marshal the object and do a PUT r¦r°ÚPUT)rƒN) Úmarshal_objectrPrfr†Útoxmlr¢rWr r½rŠÚAttributeErrorr6r) r!r‘Úexpected_valuerTrr…Únew_objr:Úpropnamer+r"r"r#Ú save_objects&     ÿ €zXMLManager.save_objectc Cs|s| ¡}|s |j}| d¡}|jr| d|j¡| dd|jj|jjf¡|j}|  |¡|j ddD]R}| d¡}| d|j ¡| d |j ¡|  |¡}|durƒ| ||¡}t|tƒrg| |||¡nt|tƒrr|  |¡n| t |¡ d d ¡¡}|  |¡|  |¡q6|S) NrOrPrQrRFr·r‰rŠrÊÚasciiÚignore)rˆrTrUrPrVrWrXrYÚdocumentElementrÏr½rŠÚ type_nameÚget_value_for_datastorer™r3r4rÒr rÐrÚ text_typer0) r!r‘rTrÚrootr‰r’r+rÑr"r"r#rÔµs6 ÿ         zXMLManager.marshal_objectcCó,t|tjƒr t|ƒ}nt|ƒ}| |||¡Sr)r3rrSr r r¢©r!Úfpr^rPrTr"r"r#Úunmarshal_objectÕs  zXMLManager.unmarshal_objectcCrâ)zf Same as unmarshalling an object, except it returns from "get_props_from_doc" )r3rrSr r r¤rãr"r"r#Úunmarshal_propsÜs  zXMLManager.unmarshal_propscCsd|j|jf}| d|¡S)Nr¦ÚDELETE)rfrPr†)r!r‘rr"r"r#Ú delete_objectçs zXMLManager.delete_objectcCs|jj|j||idddS)NT)Úreplace)ÚdomainÚput_attributesrP)r!r‘rŠr+r"r"r#Ú set_key_valueëszXMLManager.set_key_valuecCs|j |j|¡dSr)rêÚdelete_attributesrP)r!r‘rŠr"r"r#Údelete_key_valueîszXMLManager.delete_key_valuecCs$|j |j|¡}||vr||SdSr)rêÚget_attributesrP)r!r‘rŠÚar"r"r#Ú get_key_valueñszXMLManager.get_key_valuecCs|j |j¡Sr)rêÚget_itemrP©r!r‘r"r"r#Ú get_raw_itemøršzXMLManager.get_raw_itemcCódSrr")r!r:r‘rŠr+r"r"r#Ú set_propertyûózXMLManager.set_propertycCrõrr")r!r:r‘rŠr"r"r#Ú get_propertyþr÷zXMLManager.get_propertycCs|js | |j¡}d|_|S)NT)Ú_loadedÚ get_by_idrPrór"r"r#Ú load_objects zXMLManager.load_object)NNr)!rYrXrar$rzr†rˆr”r•r–r™rrŸrŒr¢r¤r­r¯r²rÎrÒrÚrÔrårærèrìrîrñrôrörørûr"r"r"r#rc·s>   !    rc)r›Ú boto.utilsrrÚboto.sdb.db.keyrÚboto.sdb.db.modelrÚ boto.compatrrrÚxml.dom.minidomr r r r rMrOr rcr"r"r"r#Ús