[z @sxdZddddddddd d d d d g ZddlZddlZddlZddlZddlZddlmZddl Z ddl Z ddl Z ddl Z ddl mZddlmZejdddeZejejGdddeZGdddeZejddZGdddeZddZddZddZddZ Gd ddZ!d!Z"e j#d"Z$d#d$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/Gd7d8d8e0Z1id9d:6d;d<6d=d>6d?d@6Z2GdAdBdBZ3dCdDZ4GdEdFdFe5Z6dGdHZ7iedIe/dJe,dKe.e86edIe7dJe3dKe6e96Z:dLddMdNdOedPd Z;dLddMdNdOedQd Z<dLe8dRdNdSdTdUd Z=dLe8dSdTdRdNdVd Z>dS)Waplistlib.py -- a tool to generate and parse MacOSX .plist files. The property list (.plist) file format is a simple XML pickle supporting basic object types, like dictionaries, lists, numbers and strings. Usually the top level object is a dictionary. To write out a plist file, use the dump(value, file) function. 'value' is the top level object, 'file' is a (writable) file object. To parse a plist from a file, use the load(file) function, with a (readable) file object as the only argument. It returns the top level object (again, usually a dictionary). To work with plist data in bytes objects, you can use loads() and dumps(). Values can be strings, integers, floats, booleans, tuples, lists, dictionaries (but only with string keys), Data, bytes, bytearray, or datetime.datetime objects. Generate Plist example: pl = dict( aString = "Doodah", aList = ["A", "B", 12, 32.1, [1, 2, 3]], aFloat = 0.1, anInt = 728, aDict = dict( anotherString = "", aUnicodeValue = "M\xe4ssig, Ma\xdf", aTrueValue = True, aFalseValue = False, ), someData = b"", someMoreData = b"" * 10, aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())), ) with open(fileName, 'wb') as fp: dump(pl, fp) Parse Plist example: with open(fileName, 'rb') as fp: pl = load(fp) print(pl["aKey"]) readPlist writePlistreadPlistFromByteswritePlistToBytesPlistDataDictFMT_XML FMT_BINARYloaddumploadsdumpsN)BytesIO)warn) ParserCreate PlistFormatzFMT_XML FMT_BINARYmodulec@s:eZdZfZddZddZddZdS) _InternalDictc CsCy||}Wntk r.t|YnXtdtd|S)NzLAttribute access from plist dicts is deprecated, use d[key] notation instead)KeyErrorAttributeErrorrDeprecationWarning)selfattrvaluer/usr/lib/python3.4/plistlib.py __getattr__Vs  z_InternalDict.__getattr__cCstdtd|||t|d}t|}WdQX|}|j||S)z,Deprecated. Use the load() function instead.rbN)r/r update)clsr,r.rZplistrrrfromFiles   zPlist.fromFilec Cs)t|d}t||WdQXdS)z,Deprecated. Use the dump() function instead.wbN)r/r )rr,r.rrrwritesz Plist.write)r!r"r#__doc__r& classmethodr3r5rr)r(rrs  cCsHtdtdt|d#}t|dddddtSWdQXdS) z Read a .plist from a path or file. pathOrFile should either be a file name, or a readable binary file object. This function is deprecated, use load instead. z8The readPlist function is deprecated, use load() insteadrr0fmtNuse_builtin_typesF dict_type)rrr/r r)r,r.rrrrs  cCsKtdtdt|d&}t||dtddddWd QXd S) z Write 'value' to a .plist file. 'pathOrFile' may either be a file name or a (writable) file object. This function is deprecated, use dump instead. z9The writePlist function is deprecated, use dump() insteadrr4r8 sort_keysTskipkeysFN)rrr/r r)rr,r.rrrrs cCs2tdtdtt|dddddtS)z} Read a plist data from a bytes object. Return the root object. This function is deprecated, use loads instead. zBThe readPlistFromBytes function is deprecated, use loads() insteadrr8Nr9Fr:)rrr rr)datarrrrs c CsBtdtdt}t||dtdddd|jS)zp Return 'value' as a plist-formatted bytes object. This function is deprecated, use dumps instead. zAThe writePlistToBytes function is deprecated, use dumps() insteadrr8r;Tr<F)rrrr rgetvalue)rfrrrrs   c@s[eZdZdZddZeddZdddZd d Zd d Z d S)rz] Wrapper for binary data. This class is deprecated, use a bytes object instead. cCs+t|tstdn||_dS)Nzdata must be as bytes)r)bytes TypeErrorr=)rr=rrrr&sz Data.__init__cCs|t|S)N)_decode_base64)r2r=rrr fromBase64szData.fromBase64LcCst|j|S)N)_encode_base64r=)r maxlinelengthrrrasBase64sz Data.asBase64cCsXt||jr"|j|jkSt|tr>|j|kSt|t|kSdS)N)r)r(r=r*id)rotherrrr__eq__s  z Data.__eq__cCsd|jjt|jfS)Nz%s(%s))r(r!reprr=)rrrr__repr__sz Data.__repr__N) r!r"r#r6r&r7rCrGrJrLrrrrrs   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]rDcCsn|dd}g}xJtdt||D]0}||||}|jtj|q-Wdj|S)Nr)rangelenappendbinasciiZ b2a_base64join)srFZ maxbinsizepiecesichunkrrrrEs rEcCs6t|tr%tj|jdStj|SdS)Nzutf-8)r)r*rSZ a2b_base64encode)rUrrrrB srBz{(?P\d\d\d\d)(?:-(?P\d\d)(?:-(?P\d\d)(?:T(?P\d\d)(?::(?P\d\d)(?::(?P\d\d))?)?)?)?)?ZcCsld}tj|j}g}x;|D]3}||}|dkrHPn|jt|q(Wtj|S)Nyearmonthdayhourminutesecond)zyearzmonthzdayzhourzminutezsecond) _dateParsermatch groupdictrRintdatetime)rUZorderZgdZlstkeyvalrrr_date_from_strings   rgcCs,d|j|j|j|j|j|jfS)Nz%04d-%02d-%02dT%02d:%02d:%02dZ)rZr[r\r]r^r_)drrr_date_to_string&sricCstj|}|dk r*tdn|jdd}|jdd}|jdd}|jdd}|jd d }|S) Nzz>)_controlCharPatsearch ValueErrorreplace)textmrrr_escape,s ruc@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'S)( _PlistParsercCs1g|_d|_d|_||_||_dS)N)stack current_keyroot_use_builtin_types _dict_type)rr9r:rrrr&9s     z_PlistParser.__init__cCsPt|_|j|j_|j|j_|j|j_|jj||j S)N) rparserhandle_begin_elementZStartElementHandlerhandle_end_elementZEndElementHandler handle_dataZCharacterDataHandlerZ ParseFilery)rZfileobjrrrparse@s  z_PlistParser.parsecCs<g|_t|d|d}|dk r8||ndS)NZbegin_)r=getattr)relementattrshandlerrrrr}Hs  z!_PlistParser.handle_begin_elementcCs0t|d|d}|dk r,|ndS)NZend_)r)rrrrrrr~Ns z_PlistParser.handle_end_elementcCs|jj|dS)N)r=rR)rr=rrrrSsz_PlistParser.handle_datacCs|jdk rdt|jdtisDtd|jjn||jd|jr)rwrRwritelnr)rrrrr begin_elementsz_DumbXMLWriter.begin_elementcCsT|jdkst|jj|ks0t|jd8_|jd|dS)Nrrz)rAssertionErrorrwrr)rrrrr end_elementsz_DumbXMLWriter.end_elementNcCsJ|dk r5t|}|jd|||fn|jd|dS)Nz <%s>%sz<%s/>)rur)rrrrrrsimple_elements  z_DumbXMLWriter.simple_elementcCsh|rTt|tr'|jd}n|jj|j|j|jj|n|jjddS)Nzutf-8s )r)r*rYrr5rr)rlinerrrrs z_DumbXMLWriter.writeln)r!r"r#r&rrrrrrrrrs   rc@sseZdZdddddddZdd Zd d Zd d ZddZddZddZ dS) _PlistWriterrs rTFcCsB|r|jtntj||||||_||_dS)N)r5 PLISTHEADERrr& _sort_keys _skipkeys)rrrrZ writeHeaderr;r<rrrr&s  z_PlistWriter.__init__cCs+|jd|j||jddS)Nzz)r write_value)rrrrrr5s  z_PlistWriter.writecCst|tr"|jd|nz|dkr>|jdn^|dkrZ|jdnBt|trd|kodknr|jd d |qt|nt|tr|jd t|nt|tr|j|nt|t r|j |nt|t t fr6|j |nft|tjra|jd t|n;t|ttfr|j|ntd t|dS)NstringTZtrueFZfalser?@Zintegerz%drealZdatezunsupported type: %srll)r)r*rrc OverflowErrorrrKdict write_dictr write_datar@ bytearray write_bytesrdrituplelist write_arrayrAr)rrrrrrs.  z_PlistWriter.write_valuecCs|j|jdS)N)rr=)rr=rrrrsz_PlistWriter.write_datacCs|jd|jd8_tddt|jjdd |j}x6t||jdD]}|rd|j|qdqdW|jd7_|j ddS) Nr=rrDs  s s ) rrmaxrQrrrrEsplitrr)rr=rFrrrrrs )z_PlistWriter.write_bytescCs|r|jd|jr1t|j}n |j}x^|D]V\}}t|ts}|jrnqDntdn|jd||j |qDW|j dn |jddS)Nrzkeys must be stringsre) rrsorteditemsr)r*rrArrr)rrhrrerrrrr s    z_PlistWriter.write_dictcCsR|rA|jdx|D]}|j|qW|jdn |jddS)Narray)rrrr)rrrrrrrs   z_PlistWriter.write_arrayN) r!r"r#r&r5rrrrrrrrrrs    %  rcCsd }x!|D]}|j|r dSq WxtjdftjdftjdffD]n\}}|j|svqUnxJ|D]B}||jdj|}|dt||kr}dSq}WqUWdS) N6xBBQQQi)_fpseekosSEEK_ENDreadrQrstructunpack _ref_size _read_ints_object_offsets _read_objectOSError IndexErrorerror)rr.trailer offset_size num_objects top_objectoffset_table_offsetrrrr]s  $z_BinaryPlistParser.parsecCsb|dkr^|jjddd@}d|>}dt|}tj||jj|dS|S)z$ return the size of the next object.rrrNrn)rr_BINARY_FORMATrr)rtokenLrtrUr?rrr _get_sizets    z_BinaryPlistParser._get_sizecsr|jj|tkr>tjdt|Stfddtd|DSdS)Nrnc3s/|]%}tj||dVqdS)bigN)rc from_bytes).0rW)r=sizerr sz0_BinaryPlistParser._read_ints..r)rrrrrrrP)rnrr)r=rrr~s  z_BinaryPlistParser._read_intscCs|j||jS)N)rr)rrrrr _read_refssz_BinaryPlistParser._read_refsc sjj|jjdd}|d@|d@}}|dkrKdS|dkr[dS|dkrkd S|dkr{d S|d krtjjjd|>d d |dkS|dkrtjdjjddS|dkr tjdjjddS|dkrMtjdjjdd}tjj|d$S|dkrj |}j rjj|St jj|SnF|dkrj |}jj|j d}|S|dkr j |}jj|dj dS|dkrMj |}j |}fdd |DS|d!krj |}j |} j |}j}xGt| |D]6\} } jj| |jj| frM#z>d3imiQrPr`rzutf-16becs&g|]}jj|qSr)rr)rx)rrr s z3_BinaryPlistParser._read_object..i3,i;,iO:)rrrrcrrrrdZutcfromtimestamprrzrrrr{ziprrr) roffsettokenZtokenHrr?rUresultZobj_refsZkey_refskor)rrrs^          "       .z_BinaryPlistParser._read_objectN) r!r"r#r6r&rrrrrrrrrrQs     rcCs:|dkrdS|dkr dS|d>d>r2dSdSdS) NrrrrrrMir)countrrr_count_to_sizes  r c@sXeZdZddZddZddZddZd d Zd d Zd S)_BinaryPlistWritercCs||_||_||_dS)N)rrr)rr.r;r<rrrr&s  z_BinaryPlistWriter.__init__c Cs3g|_i|_i|_|j|t|j}dg||_t||_t|j|_ |j j dx|jD]}|j |qW|j |}|j j}t|}dt||}|j j tj||jd}|||j|||f} |j j tjd| dS)Nrsbplist00rnz >5xBBBQQQ)_objlist _objtable _objidtable_flattenrQrr rr _ref_formatrr5 _write_object _getrefnumtellrpack) rrrobjrrrZ offset_formatZ sort_versionrrrrr5s(      z_BinaryPlistWriter.writec Cst|ttttjttfrFt||f|jkr}dSn7t|t r}t|j |j f|jkr}dSnt |j }|j j |yNt|t r||jt|j |j fBrrz>BBBrz>BBHrz>BBLz>BBQril)rr5rr)rrrrrr _write_sizeUs # ) ) )z_BinaryPlistWriter._write_sizec sj|}jjj|<|dkrDjjdn|dkrcjjdn|dkrjjdnt|tr|dkry#jjtjdd|Wqtj k rt |dYqXq|d&krjjtjd d |q|d'krFjjtjd d|q|d(krtjjtjdd|q|d)krjjtjdd|q|d*krjjd|j d dddqt |n(t|t rjjtjdd|nt|t j rj|t j dd d j}jjtjdd|nt|trjdt|jjj|jnft|ttfrjdt|jj|n(t|trky)|jd}jdt|Wn7tk rW|jd}jdt|YnXjj|nt|ttfrfd d!|D}t|}jd"|jjtjd#j||n/t|trgg}}jrt|j} n |j} xm| D]e\} } t| tsejrVq,nt d$n|j!j| |j!j| q,Wt|}jd%|jjtjd#j||jjtjd#j||n t |dS)+NsFsTs rz>Bqrrrz>BBrz>BHrrz>BLrrz>BQrsrrz>Bdrirrrzutf-16bercsg|]}j|qSr)r)rr)rrrrs z4_BinaryPlistWriter._write_object..rrnzkeys must be stringsrrilll)"rrrrr5r)rcrrrrto_bytesrrdZ total_secondsrrrQr=r@rr*rYUnicodeEncodeErrorrrrrrrrrrArR) rrrefr?tZrefsrUZkeyRefsZvalRefsZ rootItemsrrr)rrres    # " " " " )""  *     '*z _BinaryPlistWriter._write_objectN) r!r"r#r&r5rrrrrrrrr s   - / r cCs|dddkS)Nrsbplist00r)rrrr_is_fmt_binarysr#detectr|writerr8r9Tr:cCs|dkri|jd}|jdxLtjD]$}|d|r5|d}Pq5q5Wtnt|d}|d|d|}|j|S)zRead a .plist file. 'fp' should be (readable) file object. Return the unpacked root object (which usually is a dictionary). Nrrr$r|r9r:)rr_FORMATSrrr)r.r8r9r:rinfoPprrrr s    cCs(t|}t|d|d|d|S)zqRead a .plist file from a bytes object. Return the unpacked root object (which usually is a dictionary). r8r9r:)rr )rr8r9r:r.rrrr s r;r<FcCsS|tkr"td|fnt|d|d|d|}|j|dS)zQWrite 'value' to a .plist file. 'fp' should be a (writable) file object. zUnsupported format: %rr%r;r<N)r&rqr5)rr.r8r;r<r%rrrr s  c Cs2t}t||d|d|d||jS)z?Return a bytes object with the contents for a .plist file. r8r<r;)rr r>)rr8r<r;r.rrrr s )?r6__all__rSr contextlibrdZenumiorrrrerwarningsrZxml.parsers.expatrZEnumr!rglobalsr1Z __members__rrrcontextmanagerr/rrrrrrrcompilerorErBASCIIr`rgrirurvrrrrqrrrr objectr r#rr r&r r r r rrrr/st              0    h&d !"