;ò Ú™ú>c@s¥dkZdkZdkZdkZdkZdZdZdfd„ƒYZdefd„ƒYZdefd„ƒYZ d e fd „ƒYZ d fd „ƒYZ dS( Ns3.01s This python module defines a class named XML_document which will allow you to generate XML documents (yeah !) more easily than using print or similar functions. Here's a list of available methods: =================================== __init__(version, encoding) The instance constructor, automatically called when you create a new instance of XML_document. you can optionnally pass a version and encoding string, the defaults are "1.0" and "iso-8859-1". _indentstring(istr) istr is the new indentation string used to nicely present your XML documents. By default istr is equal to 4 space characters. _output(filearg) use it to save the XML document to a file. The optionnal filearg argument may be: None, "", or "-" which stands for sys.stdout. a file name. any file object. _text(sometext) use it to insert plain text at the current position in the document. _push() saves the current position in the XML document. use it if you're going to create a bunch of nested XML tags and want to escape from them later to continue your document at the same indentation level. you can pass an optional 'name' argument, to mark a position by its name. _pop() restores the latest saved position. use it to escape from nested tags and continue your XML document at the same indentation level than the latest time you called _push(). you can pass an optional 'name' argument, to continue at the same indentation level as when you called _push() with the same 'name' argument. _template(file, **vars) loads a template file and insert it as plain text at the current position in the document, replacing ##varname## variables in the template file with their corresponding value passed in vars[varname] _updatemapping(newmap) updates the internal mapping used for replacing some strings with others when rendering. This can be used as an easy way to do templating without the need of an external file. Pass None or no argument to reset the mapping to an empty one. This method returns the new mapping's content. Some more methods are available but not meant to be used directly, they are: __nonzero__, __getitem__, __setitem__, __delitem__, __coerce__, __add__, __radd__, __mul__, __rmul__, and __copy__. They are used automatically when doing special things, read the source for details. ANY and ALL other method you may call will be treated as an XML tag, unless it already exists as a method in XML_document or a subclass of it, or its name begins with "__". I suggest you to only add methods whose names begin with '_' to keep things simple and clear: "__" is reserved for future use. The file test/test.py is an example program which generates some documents, just play with it (use and modify) and you'll learn quickly how to use jaxml. Its source code is documented and attempts at describing and trying all jaxml's possibilities, so reading it is probably the best way to become powerful with jaxml in less than 10 minutes. Really, PLEASE READ the file test/test.py to learn all possibilities. ========================================================================= Since version 2.00, jaxml integrates the full functionnalities of the old jahtml module via the HTML_document and CGI_document classes, however the API for these two classes has changed to be cleaner and don't use any predefined set of tags. The HTML_document() and CGI_document() classes both inherit from XML_document() and all its methods (see above), but also feature some useful helper methods. Please read the jaxml module sources and the test/test.py program to learn how to use them. ========================================================================= The only difficult things are: ------------------------------ * you have to use the _push() and _pop() methods if you need to get out of a bunch of nested tags. * if you call a method (tag) with a string as the first unnamed parameter, you'll don't need _push() or _pop() because your tag will be automatically closed immediately. * if you call a method (tag) with a python mapping as the first or second unamed parameter, this mapping is used to correctly handle XML namespaces or attributes which are python reserved words (e.g. class), please look at test/test.py to see an example. s_TAGGED_documentcBsötZdZdfd„ƒYZd„Zd„Zd„Zd„Zd„Zd„Z d „Z d „Z d „Z d „Z d „Zd„Zd„Zd„Zd„Zd„Zed„Zed„Zd„Zdd„Zed„Zdd„ZRS(s$This class defines a tagged documentsTagcBs,tZdZd„Zed„Zd„ZRS(sãThis class defines a tag This is largely inspired from a post in comp.lang.python by Kragen Sitaker at the end of September 2000. Many thanks to him !!! cCs||_||_dS(s4Save a link to the parent and the name of the tag for future reference parent The parent object, probably a _TAGGED_document instance. tagname The name of this tag N(sparentsselfs _Tag__parentstagnames _Tag__tagname(sselfsparentstagname((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys__init__bs c OsÝt|ƒthƒjo|f}t}nd}t|ƒ}|djotdt|dƒ‚nÚ|oÒ|d} y›x”| i ƒD]†}| |}yFx?|i ƒD]1}|d||od|t||ƒf}q§WWq‡tj o!|d|t|ƒf}q‡Xq‡WWq?tj otdt| ƒ‚q?Xn|o+tit|d„|i ƒƒdƒ} nd} |tj o4|iid |i| |t|ƒ|ifƒn|ii|i| |ƒ|iSd S( sWInserts the tag and its attributes in the document _text_ eventually a string to be enclosed in the tag. the name _text_ was chosen to not conflict with a probable user's attribute called 'text' sisjaxml: Invalid attributes %sis %s%s%s="%s"s:s %s="%s"cCsd|t||ƒfS(Ns %s="%s"(sxsstrsa(sxsa((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys’ss <%s%s>%sN(stypes_text_s nsattributessNonesnsargsslenslgs ValueErrorsstrsnsattrskeyssnsstagsstagsAttributeErrors attributessstringsjoinsmapsargsselfs _Tag__parents_texts _Tag__tagnames_tag__( sselfs_text_s nsattributess attributesslgsnsstagssnsargsstagsargsnsattr((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys__call__ns:        3'+ 4cCs'|ii|id|i|fƒSdS(s)Handles naming spaces (Space:Tag) name The name of the (sub)tag part The current tag's name becomes the naming space's name. name becomes the new tag's name. s%s:%sN(sselfs _Tag__parentsTags _Tag__tagnamesname(sselfsname((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys __getattr__Ÿs(s__name__s __module__s__doc__s__init__sNones__call__s __getattr__(((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pysTag[s  1cCs<g|_g|_h|_d|_|iƒ|iƒdS(sInitialize local datasiN(sselfs_TAGGED_document__pages_TAGGED_document__pusheds_TAGGED_document__pusheddicts_TAGGED_document__positions_updatemappings _indentstring(sself((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys__init__ªs     cCs×|iƒ}|i|_|i|_|iiƒ|_|i|_|i|_|i iƒ|_ xj|i i ƒD]Y\}}|d djo%|ddjott||ƒƒ ot|||ƒqrqrW|SdS(s&Creates a copy of the current documentis__iþÿÿÿN(sselfs __class__snews_TAGGED_document__pages_TAGGED_document__pusheds_TAGGED_document__pusheddictscopys_TAGGED_document__positions_TAGGED_document__indentstrings_TAGGED_document__mappings__dict__sitemsskeysvaluescallablesgetattrssetattr(sselfsvalueskeysnew((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys__copy__¹s      9cCs”t|ƒtdƒjo td‚n|djo td‚n|djo|iƒSn6|iƒ}x"t|dƒD]}||}qxW|SdS(sÅAllows a document to be repeated number The number of times to repeat the document allows constructs like: mydoc * 3 is<jaxml.py: __mul__ operation not permitted on these operands.is=jaxml.py: can't repeat a document a negative number of times.N( stypesnumbers TypeErrors ValueErrorsselfs __class__s__copy__snewsrangesi(sselfsnumbersisnew((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys__mul__Ìs     cCs ||SdS(sÄAllows a document to be repeated number The number of times to repeat the document allows construts like: 3 * mydoc N(sselfsnumber(sselfsnumber((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys__rmul__ãscCsxt|tƒ ot|ƒtdƒjo td‚n|iƒ}|ii |iƒ|i ti |ƒd ƒ|SdS(s¢Allows two documents to be concatenated other The document or string of text to concatenate to self This is not a real concatenation: the second document (other) is in fact inserted at the current position in the first one (self). Also allows constructs like: mydoc + "some text" ss<jaxml.py: __add__ operation not permitted on these operands.iÿÿÿÿN( s isinstancesothers_TAGGED_documentstypes TypeErrorsselfs__copy__snews_TAGGED_document__mappingsupdates_texts__str__(sselfsothersnew((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys__add__ís *  cCs ||SdS(s®Allows two documents to be concatenated other The document or string of text to which self will be concatenated This is not a real concatenation: the first document (self) is in fact inserted at the current position in the second one (other). Also allows constructs like: "some text" + mydoc N(sothersself(sselfsother((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys__radd__ s cCst|tƒo||fSnlt|ƒtdƒjo'|iƒ}|i|ƒ||fSn,t|ƒtdƒjo||fSntSdS(s-Try to convert two documents to a common typesiN( s isinstancesothers_TAGGED_documentsselfstypes __class__snews_textsNone(sselfsothersnew((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys __coerce__s  cCs)|d djo|i||ƒSndS(s’Here's the magic: we create tags on demand name The name of the tag we want to create is__N(snamesselfsTag(sselfsname((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys __getattr__*scCs|iodSndSdS(sAFor truth value testing, returns 1 when the document is not emptyiiN(sselfs_TAGGED_document__page(sself((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys __nonzero__5s cCs|i|SdS(s+returns key's value in the internal mappingN(sselfs_TAGGED_document__mappingskey(sselfskey((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys __getitem__<scCs||i| iþÿÿÿis /> s %s<%s%s> s%s%s N(s cStringIOsStringIOsoutstrs indentationslensselfs_TAGGED_document__indentstringslgindentsNones lastopeneds_TAGGED_document__pagestextsargsoffsetswritesseeksflushsgetvaluesretvalscloses_TAGGED_document__mappingsitemsskeysvaluesstringsreplace( sselfskeys lastopenedsargstextslgindents indentationsoutstrsvaluesretvalsoffset((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys__str__Hs6               cCst|ƒSdS(sIReturns a printable representation of the document, same as str() for nowN(sstrsself(sself((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys__repr__kscCsA|io3|iiƒ\}}|ii|||fƒndS(sŠAdjust the stack of pushed positions. offset offset by which adjust the stack N(sselfs_TAGGED_document__pushedspopsposs oldoffsetsappendsoffset(sselfsoffsets oldoffsetspos((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys__adjust_stackts cCs_|ii|i||dfƒ|id|_|ii|i|tdfƒ|idƒdS(Niiÿÿÿÿi(sselfs_TAGGED_document__pagesinserts_TAGGED_document__positionstagsargsNones_TAGGED_document__adjust_stack(sselfstagsarg((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys_tag__~scCs>|ot|iƒ|i|t|ƒthƒjo|ii|ƒ|iSn td‚dS(s}Updates the internal mapping for the new templating facility, and returns the new mapping's content newmap a Python mapping object to initialise or extend the mapping. If None then the mapping is reset to an empty dictionnary which is the default value. sEjaxml.py: _updatemapping's parameter must be a Python mapping object.N(snewmapsNonesselfs_TAGGED_document__mappingstypesupdates TypeError(sselfsnewmap((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys_updatemapping¶s    s-cCs¦d}t|ƒtdƒjp |tjo:|o |djot|dƒ}d}qlti}n|}|idt |ƒƒ|i ƒ|o|i ƒndS(sÒOuput the page, with indentation. file the optional file object or filename to output to ("-" or None or "" means sys.stdout) iss-swis%sN( sisopenstypesfilesNonesopensoutfssyssstdoutswritesstrsselfsflushsclose(sselfsfilesoutfsisopen((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys_outputÊs&   (s__name__s __module__s__doc__sTags__init__s__copy__s__mul__s__rmul__s__add__s__radd__s __coerce__s __getattr__s __nonzero__s __getitem__s __setitem__s __delitem__s__str__s__repr__s_TAGGED_document__adjust_stacks_tag__sNones_pushs_pops_texts _indentstrings_updatemappings_output(((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys_TAGGED_documentYs0 O           #    s XML_documentcBs;tZdZddd„Zd„Zd„Zdd„ZRS(s"This class defines an XML documents1.0s iso-8859-1cCs#ti|ƒ||_||_dS(s²Initialize local datas. arguments: version: xml version string encoding: xml encoding language N(s_TAGGED_documents__init__sselfsversions __version__sencodings __encoding__(sselfsversionsencoding((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys__init__ás  cCs:ti|ƒ}|od|i|if|SndSdS(s,returns the XML document as a string of texts# sN(s_TAGGED_documents__str__sselfs tagdocstrs __version__s __encoding__(sselfs tagdocstr((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys__str__ìs c KsVdk}|idƒ}x7|D]/}x|i|ƒdjoyt||idƒƒ} Wn0t j o$td|idƒdƒ} nX||i dd }||i dd} ti|ƒd jo[ti| d ƒ}|dg}x#|dD]}|i||ƒqøWti|d ƒ} nd || | f}q(W|i|ƒqWdS( sBSubstitues var names with their values. parts of this function come from the Whiz package THANKS TO Neale Pickett ! Here follows the original license terms for Whiz: ## Author: Neale Pickett ## Time-stamp: <99/02/11 10:45:42 neale> ## This software and ancillary information (herein called "SOFTWARE") ## called html.py made avaiable under the terms described here. The ## SOFTWARE has been approved for release with associated LA-CC Number ## 89-47. ## Unless otherwise indicated, this SOFTWARE has been authored by an ## employee or employees of the University of California, operator of ## the Los Alamos National Laboratory under contract No. W-7405-ENG-36 ## with the U.S. Department of Energy. The U.S. Government has rights ## to use, reproduce, and distribute this SOFTWARE. The public may ## copy, distribute, prepare derivative works and publicly display this ## SOFTWARE without charge, provided that this Notice and any statement ## of authorship are reproduced on all copies. Neither the Government ## nor the University makes any warranty, express or implied, or assumes ## any liability or responsibility for the use of this SOFTWARE. ## If SOFTWARE is modified to produce derivative works, such modified ## SOFTWARE should be clearly marked, so as not to confuse it with the ## version available from LANL. Ns)\(\)?iÿÿÿÿisiiss s%s%s%s(sregexscompiles containerslinesslinessearchsstrsvarssgroups replacementsKeyErrorsregssprespostsstringsstripssplitsnewslsappendsjoinsselfs_text( sselfslinessvarssregexspres containerslsnewslinesposts replacement((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys __subst_linesôs, "  s-cKs°|tjpt|ƒtdƒjo4|o |djot|dƒ}q`ti}n|}td„|iƒƒ}|tijo|i ƒnt |i |f|ƒdS(sƒInclude an external file in the current doc and replaces ##vars## with their values. Parts of this function come from the Whiz package THANKS TO Neale Pickett ! Here follows the original license terms for Whiz: ## Author: Neale Pickett ## Time-stamp: <99/02/11 10:45:42 neale> ## This software and ancillary information (herein called "SOFTWARE") ## called html.py made avaiable under the terms described here. The ## SOFTWARE has been approved for release with associated LA-CC Number ## 89-47. ## Unless otherwise indicated, this SOFTWARE has been authored by an ## employee or employees of the University of California, operator of ## the Los Alamos National Laboratory under contract No. W-7405-ENG-36 ## with the U.S. Department of Energy. The U.S. Government has rights ## to use, reproduce, and distribute this SOFTWARE. The public may ## copy, distribute, prepare derivative works and publicly display this ## SOFTWARE without charge, provided that this Notice and any statement ## of authorship are reproduced on all copies. Neither the Government ## nor the University makes any warranty, express or implied, or assumes ## any liability or responsibility for the use of this SOFTWARE. ## If SOFTWARE is modified to produce derivative works, such modified ## SOFTWARE should be clearly marked, so as not to confuse it with the ## version available from LANL. ss-srcCs|d S(Niÿÿÿÿ(sl(sl((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pysJsN(sfilesNonestypesopensinfssyssstdinsmaps readlinesslinessclosesapplysselfs_XML_document__subst_linessvars(sselfsfilesvarsslinessinf((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys _template&s& (s__name__s __module__s__doc__s__init__s__str__s_XML_document__subst_liness _template(((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys XML_documentßs   2s HTML_documentcBs›tZdZdd„Zed„Zd„Zd„Zd„Zd„Z d„Z d „Z d „Z d „Z d „Zd „Zd„Zd„Zd„ZRS(sÖThis class defines a useful method to output a default header, as well as some methods defined for easying the use of this module and keep porting from the old jahtml module easy too. sJAXML Default HTML DocumentcKsœ|iƒ|iƒ|iƒ|i|ƒx`|iƒD]R}||tjo;|iƒ|i dt i |ƒd||ƒ|i ƒq8q8W|i ƒdS(s-Begins a normal document. title the title of the document modifiers usual meta name= content= tags (keywords, description, etc...) WARNING: doesn't work with other meta tags snamescontentN( sselfshtmls_pushsheadstitles modifiersskeyssmodsNonesmetasstringsuppers_pop(sselfstitles modifierssmod((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys_default_headerTs      #cKs4|iƒt|itf|ƒ}|iƒ|SdS(N(sselfs_pushsapplysinputsNonesargssretcodes_pop(sselfs_text_sargssretcode((s=/mnt/gmirror/ports/textproc/py-jaxml/work/jaxml-3.01/jaxml.pys __fake_inputks  cKs$d|d