3 0f@s^dddddddddd d d d d gZddlZddlZddlZddlZddlZddlmZddlZddl Z ddl Z ddl Z ddl m Z ddlmZejddedZejejGdddeZGdddeZejddZGdddeZddZddZddZddZGd ddZ d!Z!e j"d"Z#dOd$d%Z$d&d'Z%e j"d(e j&Z'd)d*Z(d+d,Z)d-d.Z*Gd/d0d0Z+Gd1d2d2Z,Gd3d4d4e,Z-d5d6Z.Gd7dde/Z0d8d9d:d;d<Z1e2Z3Gd=d>d>Z4d?d@Z5e6e7e8eje9fZ:GdAdBdBe2Z;dCdDZee)rr>rrrr's z Data.__init__cCs |t|S)N)_decode_base64)r4r>rrr fromBase64szData.fromBase64LcCs t|j|S)N)_encode_base64r>)r maxlinelengthrrrasBase64sz Data.asBase64cCs4t||jr|j|jkSt|tr,|j|kStSdS)N)r+r)r>rANotImplemented)rotherrrr__eq__s     z Data.__eq__cCsd|jjt|jfS)Nz%s(%s))r)r"reprr>)rrrr__repr__sz Data.__repr__N)rE) r"r#r$r'r8rDrHrKrMrrrrrs   s zv[\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0b\x0c\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f]rEcCsT|dd}g}x8tdt||D]$}||||}|jtj|q"Wdj|S)Nr)rangelenappendbinasciiZ b2a_base64join)srGZ maxbinsizepiecesichunkrrrrFs  rFcCs(t|trtj|jdStj|SdS)Nzutf-8)r+r,rTZ a2b_base64encode)rVrrrrC s rCz{(?P\d\d\d\d)(?:-(?P\d\d)(?:-(?P\d\d)(?:T(?P\d\d)(?::(?P\d\d)(?::(?P\d\d))?)?)?)?)?ZcCsNd}tj|j}g}x,|D]$}||}|dkr2P|jt|qWtj|S)Nyearmonthdayhourminutesecond)r[r\r]r^r_r`) _dateParsermatch groupdictrSintdatetime)rVorderZgdZlstkeyvalrrr_date_from_strings ricCs d|j|j|j|j|j|jfS)Nz%04d-%02d-%02dT%02d:%02d:%02dZ)r[r\r]r^r_r`)drrr_date_to_string&s rkcCsZtj|}|dk rtd|jdd}|jdd}|jdd}|jdd}|jd d }|S) Nzz>)_controlCharPatsearch ValueErrorreplace)textmrrr_escape,s      rwc@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)S)* _PlistParsercCs"g|_d|_d|_||_||_dS)N)stack current_keyroot_use_builtin_types _dict_type)rr:r;rrrr'9s z_PlistParser.__init__cCsBt|_|j|j_|j|j_|j|j_|j|j_ |jj ||j S)N) rparserhandle_begin_elementZStartElementHandlerhandle_end_elementZEndElementHandler handle_dataZCharacterDataHandlerhandle_entity_declZEntityDeclHandlerZ ParseFiler{)rZfileobjrrrparse@s     z_PlistParser.parsecCs tddS)Nz8XML entity declarations are not supported in plist files)r)rZ entity_nameZis_parameter_entityrbaseZ system_idZ public_idZ notation_namerrrrIsz_PlistParser.handle_entity_declcCs*g|_t|d|d}|dk r&||dS)NZbegin_)r>getattr)relementattrshandlerrrrrOsz!_PlistParser.handle_begin_elementcCs"t|d|d}|dk r|dS)NZend_)r)rrrrrrrUsz_PlistParser.handle_end_elementcCs|jj|dS)N)r>rS)rr>rrrrZsz_PlistParser.handle_datacCs|jdk rFt|jdtis.td|jj||jd|j<d|_nB|jsT||_n4t|jdtgsxtd|jj|jdj|dS)Nzunexpected element at line %drrr) rzr+rytypersr~CurrentLineNumberr{rS)rrrrr add_object]s   z_PlistParser.add_objectcCsdj|j}g|_|S)N)rUr>)rr>rrrget_datams z_PlistParser.get_datacCs"|j}|j||jj|dS)N)r}rryrS)rrrjrrr begin_dictts z_PlistParser.begin_dictcCs*|jrtd|j|jjf|jjdS)Nz%missing value for key '%s' at line %d)rzrsr~rrypop)rrrrend_dictysz_PlistParser.end_dictcCs:|jst|jdti r,td|jj|j|_dS)Nrzunexpected key at line %dr)rzr+ryrrsr~rr)rrrrend_keys z_PlistParser.end_keycCsg}|j||jj|dS)N)rryrS)rrarrr begin_arrays z_PlistParser.begin_arraycCs|jjdS)N)ryr)rrrr end_arraysz_PlistParser.end_arraycCs|jddS)NT)r)rrrrend_truesz_PlistParser.end_truecCs|jddS)NF)r)rrrr end_falsesz_PlistParser.end_falsecCs|jt|jdS)N)rrdr)rrrr end_integersz_PlistParser.end_integercCs|jt|jdS)N)rfloatr)rrrrend_realsz_PlistParser.end_realcCs|j|jdS)N)rr)rrrr end_stringsz_PlistParser.end_stringcCs2|jr|jt|jn|jtj|jdS)N)r|rrCrrrD)rrrrend_datasz_PlistParser.end_datacCs|jt|jdS)N)rrir)rrrrend_datesz_PlistParser.end_dateN)r"r#r$r'rrrrrrrrrrrrrrrrrrrrrrrrx8s( rxc@s8eZdZdddZddZddZdd d Zd d Zd S)_DumbXMLWriterr cCs||_g|_||_||_dS)N)filery _indent_levelindent)rr indent_levelrrrrr'sz_DumbXMLWriter.__init__cCs,|jj||jd||jd7_dS)Nz<%s>r)ryrSwritelnr)rrrrr begin_elements z_DumbXMLWriter.begin_elementcCs |jd8_|jd|dS)Nrz)rr)rrrrr end_elementsz_DumbXMLWriter.end_elementNcCs8|dk r&t|}|jd|||fn|jd|dS)Nz <%s>%sz<%s/>)rwr)rrrrrrsimple_elementsz_DumbXMLWriter.simple_elementcCsH|r8t|tr|jd}|jj|j|j|jj||jjddS)Nzutf-8 )r+r,rZrr7rr)rlinerrrrs    z_DumbXMLWriter.writeln)rr)N)r"r#r$r'rrrrrrrrrs   rc@sFeZdZdddZdd Zd d Zd d ZddZddZddZ dS) _PlistWriterr rTFcCs.|r|jttj||||||_||_dS)N)r7 PLISTHEADERrr' _sort_keys _skipkeys)rrrrZ writeHeaderr<r=rrrr's  z_PlistWriter.__init__cCs"|jd|j||jddS)Nzz)r write_value)rrrrrr7s  z_PlistWriter.writecCs4t|tr|jd|n|dkr0|jdn|dkrD|jdnt|trd|ko`dknrx|jd d |nt|nt|tr|jd t|nt|tr|j|n|t|t r|j |nft|t t fr|j |nLt|tjr|jd t|n,t|ttfr |j|ntd t|dS)NstringTtrueFZfalser?@Zintegerz%drealZdatezunsupported type: %srll)r+r,rrd OverflowErrorrrLdict write_dictr write_datarA bytearray write_bytesrerktuplelist write_arrayrBr)rrrrrrs.           z_PlistWriter.write_valuecCs|j|jdS)N)rr>)rr>rrrrsz_PlistWriter.write_datacCs~|jd|jd8_tddt|jjdd |j}x&t||jdD]}|rL|j|qLW|jd7_|j ddS) Nr>rrEr rs ) rrmaxrRrrtrFsplitrr)rr>rGrrrrrs z_PlistWriter.write_bytescCs|rx|jd|jr"t|j}n|j}x@|D]8\}}t|tsR|jrJq0td|jd||j |q0W|j dn |jddS)Nrzkeys must be stringsrg) rrsorteditemsr+r,rrBrrr)rrjrrgrrrrrs    z_PlistWriter.write_dictcCs@|r2|jdx|D]}|j|qW|jdn |jddS)Narray)rrrr)rrrrrrr&s    z_PlistWriter.write_arrayN)rrrTF) r"r#r$r'r7rrrrrrrrrrs % rcCsd }x|D]}|j|r dSq WxntjdftjdftjdffD]N\}}|j|sRq>x8|D]0}||jdj|}|dt||krXdSqXWq>WdS) N6xBBQQQi)_fpseekosSEEK_ENDreadrRrstructunpack _ref_size _read_ints_object_offsets _undefined_objects _read_objectOSError IndexErrorerrorrrs)rr0trailer offset_size num_objects top_objectoffset_table_offsetrrrrfs      z_BinaryPlistParser.parsecCsL|dkrH|jjddd@}d|>}dt|}tj||jj|dS|S)NrrrOrp)rr_BINARY_FORMATrr)rtokenLrvrVr@rrr _get_sizes  z_BinaryPlistParser._get_sizecsv|jj|tkr2tjd|tS sHt|krNttfddtd|DSdS)Nrpc3s&|]}tj||dVqdS)bigN)rd from_bytes).0rX)r>sizerr sz0_BinaryPlistParser._read_ints..r) rrrrrrRrrrQ)rnrr)r>rrrsz_BinaryPlistParser._read_intscCs|j||jS)N)rr)rrrrr _read_refssz_BinaryPlistParser._read_refsc sj|}|tk r|Sj|}jj|jjdd}|d@|d@}}|dkr^d}n|dkrnd}n||dkr~d}nl|dkrd }n\|d krtjjjd|>d |d kd }n0|dkrtj djjdd}n |dkrtj djjdd}n|dkrDtj djjdd}t j dddt j |d}n|dkrj |}jj|}t ||krxtjst|}n^|dkrΈj |}jj|} t | |krt| jd}n|dkrj |d}jj|} t | |krt| jd}n|dkrXj |}j|} g}|j|<|jfdd| Dn|dkrj |}j|} j|} j}|j|<y2x,t| | D]\} } j| |j| <qWWntk rtYnXnt|j|<|S) NrrrrF TrPrrrO)signed"z>frN#z>d3i)ZsecondsrPr`rzutf-16bec3s|]}j|VqdS)N)r)rx)rrrrsz2_BinaryPlistParser._read_object..)rrrrrrrdrrrreZ timedeltarrRrr|rrrextendr}ziprrB)rrefresultoffsettokenZtokenHrr@rVr>Zobj_refsZkey_refskor)rrrs                          z_BinaryPlistParser._read_objectN) r"r#r$r'rrrrrrrrrrZs   rcCs0|dkr dS|dkrdS|d>d>r(dSdSdS) NrrrrrrNir)countrrr_count_to_sizes rc@s<eZdZddZddZddZddZd d Zd d Zd S)_BinaryPlistWritercCs||_||_||_dS)N)rrr)rr0r<r=rrrr' sz_BinaryPlistWriter.__init__c Csg|_i|_i|_|j|t|j}dg||_t||_t|j|_ |j j dx|jD]}|j |q\W|j |}|j j}t|}dt||}|j j tj|f|jd}|||j|||f} |j j tjd| dS)Nrsbplist00rp >5xBBBQQQ)r)_objlist _objtable _objidtable_flattenrRrrrr _ref_formatrr7 _write_object _getrefnumtellrpack) rrrobjrrrZ offset_formatZ sort_versionrrrrr7s(          z_BinaryPlistWriter.writec Cs~t|tr"t||f|jkrZdSn8t|trHt|j|jf|jkrZdSnt||jkrZdSt|j }|j j |t|tr||jt||f<n0t|tr||jt|j|jf<n||jt|<t|t rPg}g}|j }|j rt|}xB|D]:\}}t|ts|jr qtd|j ||j |qWxLtj||D]}|j|q:Wn*t|ttfrzx|D]}|j|qfWdS)Nzkeys must be strings)r+_scalarsrrrr>idrrRrrSrrrrr,rrB itertoolschainrrr) rrZrefnumkeysvaluesrrvrrrrr>sB          z_BinaryPlistWriter._flattencCsNt|tr|jt||fSt|tr<|jt|j|jfS|jt|SdS)N)r+r rrrr>rr!)rrrrrrns   z_BinaryPlistWriter._getrefnumcCs|dkr"|jjtjd||Bn|dkrH|jjtjd|dBd|nh|dkrn|jjtjd|dBd|nB|dkr|jjtjd |dBd |n|jjtjd |dBd |dS)Nrz>Brrz>BBBrz>BBHrz>BBLz>BBQril)rr7rr)rrrrrr _write_sizevsz_BinaryPlistWriter._write_sizec sj|}jjj|<|dkr2jjdn|dkrJjjdn|dkrbjjdnt|trl|dkryjjtjdd|Wn tj k rt |dYnXn|d'krԈjjtjd d |n|d(krjjtjd d|nt|d)krjjtjdd|nR|d*kr8jjtjdd|n0|d+kr`jjd|j d dddnt |nxt|t rjjtjdd|nRt|t j r|t j dd d j}jjtjdd|nt|trjdt|jjj|jnt|ttfr0jdt|jj|nt|try|jd}jdt|Wn4tk r|jd}jdt|d YnXjj|nDt|ttfrfd!d"|D}t|}jd#|jjtjd$j|f|nt|trgg}}jr&t|j} n|j} xR| D]J\} } t| ts\jrTq4t d%|j!j| |j!j| q4Wt|}jd&|jjtjd$j|f|jjtjd$j|f|nt |dS),NFTrrz>Bqr)rrz>BBrz>BHr'rz>BLr(rz>BQrr)rz>Bdrirrrzutf-16berrcsg|]}j|qSr)r)rr)rrr sz4_BinaryPlistWriter._write_object..rrpzkeys must be stringsrrilll)"rrrrr7r+rdrrrrto_bytesrreZ total_secondsrr*rRr>rArr,rZUnicodeEncodeErrorrrrrrrrrrBrS) rrr r@tZrefsrVZkeyRefsZvalRefsZ rootItemsrr&r)rrrs            $    "$z _BinaryPlistWriter._write_objectN) r"r#r$r'r7rrr*rrrrrr s -0rcCs|dddkS)Nrsbplist00r)rrrr_is_fmt_binarysr2)detectr~writerT)r9r:r;cCsn|dkrL|jd}|jdx:tjD]}|d|r&|d}Pq&Wtn t|d}|||d}|j|S)Nrrr3r~)r:r;)rr_FORMATSr%rr)r0r9r:r;rinfoPprrrr s     cCst|}t||||dS)N)r9r:r;)rr )rr9r:r;r0rrrr sF)r9r<r=cCs:|tkrtd|ft|d|||d}|j|dS)NzUnsupported format: %rr4)r<r=)r5rsr7)rr0r9r<r=r4rrrr s)r9r=r<cCs t}t|||||d|jS)N)r9r=r<)rr r?)rr9r=r<r0rrrrs)rE)D__all__rTr contextlibreenumiorr"rrerwarningsrZxml.parsers.expatrEnumr"rglobalsr3 __members__rrrcontextmanagerr1rrrrrrrcompilerqrFrCASCIIrarirkrwrxrrrrsrrobjectrrrr,rdrrAr rr2r r r5r r r rrrrr1sx          0    o&d!# S