U e: @sjdZddlmZdZeZddlmZzddlmZ Wn e k rTddl mZ YnXddl Z ddl Z ddlmZe dZzeWneZYnXzeWneZYnXd3d d Zd dZgZd4ddZddZddZddZddZddZddZdd Z d!d"Z!d5d$d%Z"d&d'Z#d(d)Z$d6d*d+Z%d7d,d-Z&d8d.d/Z'd0d#d0d d0e!d d0d1d0f d2dZ(dS)9a Converts a Python dictionary or other native data type into a valid XML string. Supports item (`int`, `float`, `long`, `decimal.Decimal`, `bool`, `str`, `unicode`, `datetime`, `none` and other number-like objects) and collection (`list`, `set`, `tuple` and `dict`, as well as iterable and dict-like objects) data types, with arbitrary nesting for the collections. Items with a `datetime` type are converted to ISO format strings. Items with a `None` type become empty XML elements. This module works with both Python 2 and 3. )unicode_literalsz1.7.16)randint)IterableN) parseString dicttoxmlF dicttoxml.logcCsT|rBddl}td|tj|tjdtdt|jntjtj ddS)Nrz*Debug mode is on. Events are logged at: %s)filenamelevelz Logging session starts: %s)r ) datetimeprintlogging basicConfigINFOLOGinfostrtodayWARNING)debugrr r7/opt/hc_python/lib/python3.8/site-packages/dicttoxml.py set_debug,s  rcCs6td|fz t|dWSt|YSXdS)zConverts strings with non-ASCII characters to unicode for LOG. Python 3 doesn't have a `unicode()` function, so `unicode()` is an alias for `str()`, but `str()` doesn't take a second argument, hence this kludge. zInside unicode_me(). val = "%s"utf-8N)rrunicodevalrrr unicode_me8s  r順?BcCs&td|||fd|t||fS)zReturns a random integerz6Inside make_id(). element = "%s", start="%s", end="%s"%s_%s)rrr)elementstartendrrrmake_idFsr#cCsJtd|ft|}d}|rB|tkr8d}t|qt|}qtdS)z'Returns a unique id for a given elementz&Inside get_unique_id(). element = "%s"TF)rrr#idsappend)r this_idduprrr get_unique_idLs  r)cCs td|t|jft|jdkr4tddSt|jdkrPtddSt|jdkrltdd St|jd krtd d St|jd krtdd St|tjrtddSt|trtddSt|trtddStdt|jt|jS)z0Returns the data type for the xml type attributez3Inside get_xml_type(). val = "%s", type(val) = "%s"ZNoneTypez2type(val).__name__ == 'NoneType', returning 'null'nullboolz.type(val).__name__ == 'bool', returning 'bool')rrz8type(val).__name__ in ('str', unicode'), returning 'str'r)intlongz5type(val).__name__ in ('int', long'), returning 'int'r,floatz0type(val).__name__ == 'float', returning 'float'z3isinstance(val, numbers.Number), returning 'number'numberz'isinstance(val, dict), returning 'dict'dictz+isinstance(val, iterable), returning 'list'listztype not found, returning '%s') rrtype__name__ isinstancenumbersNumberr0iterablerrrr get_xml_typeZs6           r8cCsntd|t|ft|ttfkrjt|}|dd}|dd}|dd}|dd }|d d }|S) Nz0Inside escape_xml(). s = "%s" and type(s) = "%s"&z&"z"'z'z>)rrr2rrrreplace)srrr escape_xmls     r@cCs@td|fddd|D}d|dkr6dnd|fS)z2Returns an attribute string in the form key="val" z$Inside make_attstring(). attr = "%s" cSsg|]\}}d||fqS)z%s="%s"r).0kvrrr sz#make_attrstring..z%s%s)rrjoinitems)attr attrstringrrrmake_attrstringsrKcCsHtdt|d||f}zt|WdStk rBYdSXdS)z%Checks that a key is a valid XML namez'Inside key_is_valid_xml(). Testing "%s"z3<%s>fooTFN)rrrr Exception)keyZtest_xmlrrrkey_is_valid_xmls rNcCstdt|t|ft|}t|}t|r:||fSt|rRd||fSzdtt||fWStk r|YnXt| ddr| dd|fS||d<d}||fS)z)Tests an XML name and fixes it if invalidz=Inside make_valid_xml_name(). Testing key "%s" with attr "%s"zn%srA_namerM) rrrr@rNrisdigitr. ValueErrorr>)rMrIrrrmake_valid_xml_names&  rScCs,td|ft|dd}d|dS)z"Wraps a string into CDATA sectionszInside wrap_cdata(). val = "%s"z]]>z]]]]>z rrrr wrap_cdatasrTcCstd|fdS)Nz)Inside default_item_func(). parent = "%s"item)rrparentrrrdefault_item_funcsrXrootcCstdt|jt|f||}t|tkr>t||||S|dkrTt||||St|t j spt|t t fkr~t ||||St|drt ||||St|trt||||||St|trt||||||Std|t|jfdS)zcRoutes the elements of an object to the right function to convert them based on their data typez-Inside convert(). obj type is: "%s", obj="%s"N isoformatUnsupported data type: %s (%s))rrr2r3rr+ convert_bool convert_noner4r5r6rr convert_kvhasattrrZr0 convert_dictr7 convert_list TypeError)objr% attr_type item_funccdatarW item_namerrrconverts    rhc Cstdt|jt|fg}|j}||}|D]\} } tdt| t| t| jf|sjinddt|i} t| | \} } t| t kr|t | | ||| q6t | t j st| ttfkr|t| | ||| q6t| dr|t| | ||| q6t| t kr&|t | | ||| q6t | trl|rDt| | d<|d| t| t| || |||| fq6t | tr|rt| | d<|d| t| t| || |||| fq6| dkr|t| | ||| q6td | t| jfq6d |S) z#Converts a dict into an XML string.z2Inside convert_dict(): obj type is: "%s", obj="%s"zALooping inside convert_dict(): key="%s", val="%s", type(val)="%s"idz%srZr2 <%s%s>%sNr[rF)rrr2r3rr&rHr)rSr+r\r4r5r6rrr^r_rZr0r8rKr`r7rar]rbrG) rcr%rWrdrerfoutputaddlinergrMrrIrrrr`s`          r`c Cstdg}|j}||}|r(t|} t|D]\} } tdt| |t| jf|s`indd| | dfi} t| t j st| t t fkr|t || ||| q0t| dr|t || ||| q0t| tkr|t|| ||| q0t| trB|s |d|t| ||||||fn |d|t| ||||||fq0t| tr|s||d |t| t| ||||||fn&|d |t| t| ||||||fq0| d kr|t|d ||| q0td | t| jfq0d |S)z#Converts a list into an XML string.zInside convert_list()zCLooping inside convert_list(): item="%s", item_name="%s", type="%s"rirrZ <%s>%sz<%s type="dict">%sz<%s %s>%sz<%s type="list"%s>%sNr[rF)rrr&r) enumeraterr2r3r4r5r6rrr^r_rZr+r\r0r`r7rKrar]rbrG) rHr%rWrdrerfrkrlrgr'irUrIrrrra'sh           racCsxtdt|t|t|jf|dkr.i}t||\}}|rLt||d<t|}d|||dkrjt|nt ||fS)z/Converts a number or string into an XML elementz;Inside convert_kv(): key="%s", val="%s", type(val) is: "%s"Nr2rjT) rrrr2r3rSr8rKrTr@rMrrdrfrIrJrrrr^js" r^cCsltdt|t|t|jf|dkr.i}t||\}}|rLt||d<t|}d||t| |fS)z&Converts a boolean into an XML elementz=Inside convert_bool(): key="%s", val="%s", type(val) is: "%s"Nr2rj) rrrr2r3rSr8rKrlowerrqrrrr\s r\cCsftdt|t|t|t|f|dkr2i}t||\}}|rPt||d<t|}d|||fS)z)Converts a null value into an XML elementzBInside convert_none(): key="%s". val="%s", attr_type="%s", attr=%sNr2z <%s%s>)rrrrrSr8rKrqrrrr]s r]TzUTF-8c Cstdt|jt|fg} | j} |dkrx|dkrT|dkrH| dn | d| | d|t||||||d|fn| t|||||dd| dkrd| Sd| d S) ayConverts a python object into XML. Arguments: - root specifies whether the output is wrapped in an XML root element Default is True - custom_root allows you to specify a custom root element. Default is 'root' - ids specifies whether elements get unique ids. Default is False - attr_type specifies whether elements get a data type attribute. Default is True - item_func specifies what function should generate the element name for items in a list. Default is 'item' - cdata specifies whether string values should be wrapped in CDATA sections. Default is False z0Inside dicttoxml(): type(obj) is: "%s", obj="%s"TFzz$rnrVrFr) rrr2r3rr&rhrGencode) rcrYZ custom_rootZxml_declarationr%rdrerfZinclude_encodingencodingZ return_bytesrkrlrrrrs"    )Fr)rr)rY)FN)FN)FN))__doc__ __future__r __version__versionrandomrcollections.abcrr7 ImportError collectionsr5r Zxml.dom.minidomr getLoggerrrrr-r,rrr%r#r)r8r@rKrNrSrTrXrhr`rar^r\r]rrrrrsb         (    ?C