1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
#! /usr/bin/env python
# Test program for jaxml
#
# (C) Jerome Alet <alet@librelogiciel.com> 2000
# You're welcome to redistribute this software under the
# terms of the GNU General Public Licence version 2.0
# or, at your option, any higher version.
#
# You can read the complete GNU GPL in the file COPYING
# which should come along with this software, or visit
# the Free Software Foundation's WEB site http://www.fsf.org
#
# $Id: test.py,v 1.13 2003/02/13 14:36:13 jerome Exp $
#
import sys
# import the jaxml module from the parent directory
sys.path.insert(0, "..")
import jaxml
print "\n\n==== TESTING XML ====\n"
# now we create an instance
# we may optionally pass a version and an encoding arguments.
x = jaxml.XML_document()
# first tag, with different attributes
# numeric values are automatically quoted
x.sometag(yes = "NO", some = "a bit", value = 5)
# this one, and till the end will be inside the previous one
x.anothertag("this tag and till the end will be inside the <sometag> ... </sometag>")
# here we save the current position
x._push()
# here some nested tags
x.whatever()
x.ilikepython()
x._text("Here we are inside <whatever><ilikepython> ... </ilikepython></whatever>")
# the following tag has nothing but attributes, we must save and restore it's
# position because it is followed by another tag (we doesn't want to enclose the following tag)
x._push()
x.someattributetag(attr = "Hey ! I'm the attribute !")
x._pop()
x.justatest("This is just a test", dummy="YES")
# here we want to continue our document
# at the same indentation level than <whatever>
x._pop()
x.dummytag("we have just escaped", value = "Fine !")
x.dummytwo("Since the previous tag and this one were called with an unnamed first parameter\nwe didn't need _push() nor _pop()")
# here we insert plain text
x._text("Near the end")
# here we insert some text just like:
# <mytag>Some dummy text</mytag>
x.mytag("Some dummy text, and no tag attributes")
# here some beautiful tag nesting
x.onetag(message="This is").anotherone("a beautiful").deeper(message = "tag nesting possibility")
# here the naming space notation for <Space:Tag>...</Space:Tag>
x.namingspace.onetag("This is how to use the naming space notation Space:Tag", wonderful="YES")
# here just a tag with attributes, but nothing in it
# we don't need to _push() and _pop() because it isn't followed by anything
x.attributetag(content = "I've got nothing enclosed in me", index = 9)
# here we save to a file
x._output("sampleXML.xml")
# but we may as well output it to the screen
print x
# test the new templating facility
# I urge you to read the following lines and look carefully at the result
# to see how this beautiful thing works !
x._text("Now we will replace some content with the new possibility of using a document as a mapping.")
x._text("This may be useful for templating without a template file, or replacing some chars with their equivalent SGML entities for example:")
x._text("Here are three accented characters, two of them which will be replaced\nwith their equivalent SGML entities: ")
x["nothing enclosed"] = "something enclosed"
x["SGML"] = "XML"
x["attributetag"] = "modifiedattributename"
x[""] = "é";
x[""] = "è";
x[""] = "à";
# this is also available as readable attributes
sys.stderr.write('x[""] = %s\n' % x[""])
# and we can also delete them
del x[""]
# or use the str() or repr() builtin functions
mydoc = "With str() or repr(), my modified document looks like:\n" + str(x) + "And that's all folks !"
print mydoc
# Now we want to test the HTML output
print "\n\n==== TESTING HTML ====\n"
page = jaxml.HTML_document()
# here we begin our html document
page.html()
# we must do a push and a pop in order for the <body> tags
# to not be enclosed between <head> and </head>
page._push()
# build the head of the document
page.head()
#
#
# Other meta tags should work fine
page._meta(name="GENERATOR", content="jaxml.py v2.24 from Jerome Alet - alet@librelogiciel.com")
page._meta(name="DESCRIPTION", content="A CGI document, to test jaxml.py")
page._meta(name="KEYWORDS", content="python, jaxml, linux")
page.title("A CGI test document")
# here we exit from the <head> ... </head>
page._pop()
# we begin the body
page.body(bgcolor="pink")
# here we insert a dumb text
page._text("A small text")
# we do a push to be able to exit from the <form> ... </form>
page._push()
page.form(action="/cgi-bin/jerome/ok.py", method="POST")
page.h1("Form's title")
# to be able to exit from <select> ... </select>
page._push()
page.select(name="choice", size="1", multiple="multiple")
page.option("Choice number 1")
page.option("Choice number 2", selected="selected")
page.option("Choice number 3")
# exit from <select> ... </select>
page._pop()
page.h3("Second part of the Form")
page._br()
page._textinput(name="dumbstring", size="50")
page._submit()
page._reset()
# here we exit from the <form> ... </form>
page._pop()
page._text("here we should be outside of the form")
page._text("and there we should be one the same line visually but on two different lines in the html file")
page.a("Click on Me", href="http://www.slashdot.org")
page.pre("Hello !!!\n\t\tBye Bye\n\n")
page._text("Here we should be outside of the PRE.../PRE tag")
# then we insert some text
page._text("Just below you will see some lines of text which are included from a template file, with variables substitution:")
page._br()
# then we include the template file
page._template("template.htt", font_color='red', link_to_my_homepage="<a href='http://www.librelogiciel.com/'>My website</a>", another_variable="<br /><center>Thank you for trying</center>")
# then some separation
page.hr(width="33%", noshade="noshade")
# here we do the output to the screen
page._output()
# and here we do the output to a file
page._output("sampleHTML.html")
# Now we want to test the CGI/HTML output
print "\n\n==== TESTING CGI ====\n"
# just some dummy values
page = jaxml.CGI_document(encoding = "utf-8", content_type="text/html", version = "3.0")
# to do a redirection, just do
# page.set_redirect("http://www.librelogiciel.com/")
# then just call page.output("")
# here again we can do that whenever we want (before output)
# text/html is the default for _set_content_type()
#page._set_content_type("application/pdf")
# to define a pragma, just use:
# page._set_pragma("pragma_name")
# we can do that whenever we want, (before output)
# to define an expiration date, just use:
# page._set_expires("expiration_date")
# we can do that whenever we want, (before output)
# Maybe this should be done by the class's __init__ function
# but I don't think so in order for us to have more control
page._default_header(title = 'a CGI document')
# we begin the body
page.body(bgcolor="pink")
# here we insert a dumb text
page._text("A small text")
# we do a push to be able to exit from the <form> ... </form>
page._push()
page.form(action="/cgi-bin/jerome/ok.py", method="POST")
page.h1("Form's title")
# to be able to exit from <select> ... </select>
page._push()
page.select(name="choice", size="1")
page.option("Choice number 1")
page.option("Choice number 2")
page.option("Choice number 3", selected="selected")
# exit from <select> ... </select>
page._pop()
page.h3("Second part of the Form")
page._br()
page._textinput(name="dumbstring", size="50")
page._submit()
page._reset()
# here we exit from the <form> ... </form>
page._pop()
page._text("here we should be outside of the form")
page._text("and there we should be one the same line visually but on two different lines in the html file")
page.a("Click on Me", href="http://www.slashdot.org")
page.pre("Hello !!!\n\t\tBye Bye\n\n")
page._text("Here we should be outside of the PRE.../PRE tag")
# here we define a debug file which will receive the CGI output too
page._set_debug("CGI_debug.html")
# here we do the output
# for a CGI script, give an empty string (for stdout)
# or None, or nothing, unless you want to debug (give a filename) or a file object
page._output("")
# Now we want to test the arithmetic operations
print "\n\n==== TESTING ARITHMETIC ====\n"
print "page + page = %s" % (page + page)
print "page + 'string' = %s" % (page + 'string')
print "'string' + page = %s" % ('string' + page)
print "page * 2 = %s" % (page * 2)
print "2 * page = %s" % (2 * page)
# new name spaces support
x = jaxml.XML_document()
x.tag("hello", name="blah")
x.another("bloum",
{ "xmlns": { "tal" : "http://xml.zope.org/namespaces/tal",
"metal": "http://xml.zope.org/namespaces/metal"},
"metal": {"use-macro" : "here/StandardLookAndFeel/macros/master"},
"" : { "class" : "header"}
})
x._push("save")
x.otherone({ "xmlns": { "tal" : "http://xml.zope.org/namespaces/tal",
"metal": "http://xml.zope.org/namespaces/metal"},
"metal": {"use-macro" : "here/StandardLookAndFeel/macros/master"},
"" : { "class" : "header"}
})
x._push()
x.inside(name="inside")
x.atag()
x._text("blah")
x._pop("save")
x.outside(attrib="None")
print x
|