next up previous Python Library Reference contents modules index

Wpre - Using the Wpre module

There are two objects within the Wpre system: Wpre and WpreFile. The Wpre object allows you to define a string that contains the HTML or XML tags along with Python variables that will be replaced by Wpre. The WpreFile  object allows you to define all of your special HTML or XML tags and Python variables in a separate file and then process the information once all of the variables have been defined.
 

Wpre

Let us consider how you could easily emit code for a hidden variable in HTML without having to hard code HTML tags directly in a print statement (all examples here are in Python).
 

# First import the appropriate object
from wpre import Wpre
 

def testWpre():

# create the Wpre object
w = Wpre()

# define a string that will contain the HTML tags
raw = '<input type="hidden" name="`name" value="`value">'

# compile the string into an "internal" representation for preprocessing
w.compile( raw )

# define all variables that are referenced in the the line (name and value)
name = 'person'
value = 'george'

# substitute the variables to obtain a version with no variables
outputLine = w.evaluate(vars())

# send the output to standard output
print outputLine


def Main():

testWpre()


Main()


When you run this program, you'd get the following output:

<input type="hidden" name="person" value="george">
Again, keep in mind that this example is aimed at demonstrating the raw processing capabilities on a single string but has, in fact, been generalized to process entire files at a time. As you can see, however, even if you were to process one line of text at a time using this interface, it would still be better than hard-coding HTML directly in print statements.

In addition to variables, there are other "specially defined sequences" that can be used:

`(expr) - This allows you to put any valid Python expression (involving defined variables) in the string and have it be evaluated. This is mostly true; however, if you make a mistake and have nested parentheses that are not balanced, an exception will occur. This feature exists in case there is some quick calculation you wish to perform and should not generally be used as an alternative to defining a variable.

`` - This will expand to a single back-quote.

`( - This will expand to a left-parenthesis

') - You can guess this one!

`anything else - expands to whatever is there, if it is not part of an identifier or one of the special forms.


WpreFile

Once you have mastered the basic use of Wpre, you will probably only work with WpreFile. This class allows you to process an entire file. You can include other files. You can define "custom" rules to be applied when you encounter a certain file (either directly or by inclusion) with a particular variable defined on a
particular line. It is all very customizable!

WpreFile in its most basic form requires the following:

fileName - the name of the file to be directly processed
searchPath - a Python list containing candidate directories to "look" for included files.
env - an environment in which the variables are defined. This has already been discussed in the first example.
Let's now consider how we could use WpreFile to process an entire file of text. The listing below is taken from our example file named test1.html:
<html><title>`docTitle</title>

<h1>Welcome to `company's home page.</h1>

</html>

Now we define the variables used in the document and then use the WpreFile object to replace the variables with there values and send the output to the standard output stream:
 
# import the WpreFile object
from WpreFile import WpreFile

def testWpreFile():

# define the variable used in the HTML document
docTitle = "Happy Days"
company = "Tools of Computing"

# create the WpreFile object
wpf = WpreFile('test1.html', [], vars())

# process the internal representation and print to the standard output
wpf.process()


def Main():

testWpreFile()
Main()
That's it. The variables would be substituted into the lines of the file, one at a time, until the file processing had been completed. The output is currently sent to standard output. We can easily extend this example to send the output to a list as follows:
 
# import the WpreFile object
from WpreFile import WpreFile

def testWpreFileList():

# define the variable used in the HTML document
docTitle = "Happy Days"
company = "Tools of Computing"

# create the WpreFile object
wpf = WpreFile('test1.html', [], vars())

# process the internal representation and put the output into a list
myList = []
wpf.processList( myList )


def Main():

testWpreFileList()
Main()


To capture the output as a list, we first defined an empty list, myList, and pass it to the processList method. An empty list is required since lists maintain state and a populated list will have the new information appended to it as opposed to replacing it. After the method call, you can use the list to retrieve each line of the processed output.

Using lists, you can process the HTML file and capture the output inside another HTML file. This is mostly useful when you would like to create HTML files that will be used in the future, but are not needed at the time of processing as is the case with CGI application.


next up previous Python Library Reference contents modules index

Send comments on this document to pshafae@leibniz.jhpc.cs.depaul.edu

Copyright(C) Java and High-Performance Computing Laboratory (JHPC) at DePaul University Chicago
Documentation template borrowed from python.org