ParseEventHandler

Event handler for parsing PSP tokens.

This module is called when the Parser encounters psp tokens. It creates a generator to handle the PSP token. When the PSP source file is fully parsed, this module calls all of the generators in turn to output their source code.

Copyright (c) by Jay Love, 2000 (mailto:jsliv@jslove.org)

Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee or royalty is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation or portions thereof, including modifications, that you make.

This software is based in part on work done by the Jakarta group.

class PSP.ParseEventHandler.ParseEventHandler(ctxt, parser)

Bases: object

This is a key class.

It implements the handling of all the parsing elements. Note: This files JSP cousin is called ParseEventListener, I don’t know why, but Handler seemed more appropriate to me.

__init__(ctxt, parser)
addGenerator(gen)
aspace = ' '
beginProcessing()
defaults = {'BASE_CLASS': 'Page', 'BASE_METHOD': 'writeHTML', 'formatter': 'str', 'gobbleWhitespace': True, 'imports': {'filename': 'classes'}, 'indent': 4, 'instanceSafe': 'yes', 'threadSafe': 'no'}
directiveHandlers = {'BaseClass': <function ParseEventHandler.extendsHandler>, 'extends': <function ParseEventHandler.extendsHandler>, 'formatter': <function ParseEventHandler.formatterHandler>, 'gobbleWhitespace': <function ParseEventHandler.gobbleWhitespaceHandler>, 'import': <function ParseEventHandler.importHandler>, 'imports': <function ParseEventHandler.importHandler>, 'indentSpaces': <function ParseEventHandler.indentSpacesHandler>, 'indentType': <function ParseEventHandler.indentTypeHandler>, 'isInstanceSafe': <function ParseEventHandler.instanceSafeHandler>, 'isThreadSafe': <function ParseEventHandler.threadSafeHandler>, 'method': <function ParseEventHandler.mainMethodHandler>}
endProcessing()
extendsHandler(bases, start, stop)

Extends is a page directive.

It sets the base class (or multiple base classes) for the class that this class will generate. The choice of base class affects the choice of a method to override with the BaseMethod page directive. The default base class is PSPPage. PSPPage inherits from Page.py.

formatterHandler(value, start, stop)

Set an alternate formatter function to use instead of str().

generateAll(phase)
generateDeclarations()
generateFooter()
generateHeader()
generateInitPSP()
generateMainMethod()
gobbleWhitespace()

Gobble up whitespace.

This method looks for a character block between two PSP blocks that contains only whitespace. If it finds one, it deletes it.

This is necessary so that a write() line can’t sneek in between a if/else, try/except etc.

gobbleWhitespaceHandler(value, start, stop)

Declare whether whitespace between script tags are gobble up.

handleCharData(start, stop, chars)

Flush character data into a CharDataGenerator.

handleComment(start, stop)

Comments get swallowed into nothing.

handleDirective(directive, start, stop, attrs)

Flush any template data and create a new DirectiveGenerator.

handleEndBlock()
handleExpression(start, stop, attrs)

Flush any template data and create a new ExpressionGenerator.

handleInclude(attrs, param)

This is for includes of the form <psp:include …>

This function essentially forwards the request to the specified URL and includes that output.

handleInsert(attrs, param)

This is for includes of the form <psp:insert …>

This type of include is not parsed, it is just inserted into the output stream.

handleMethod(start, stop, attrs)
handleMethodEnd(start, stop, attrs)
handleScript(start, stop, attrs)

Handle scripting elements

handleScriptClass(start, stop, attrs)

Python script that goes at the class level

handleScriptFile(start, stop, attrs)

Python script that goes at the file/module level

importHandler(imports, start, stop)
indentSpacesHandler(amount, start, stop)

Set number of spaces used to indent in generated source.

indentTypeHandler(indentType, start, stop)

Declare whether tabs are used to indent source code.

instanceSafeHandler(value, start, stop)

Handle isInstanceSafe.

isInstanceSafe tells the Servlet engine whether it is safe to use object instances of this page multiple times. The default is “yes”.

Saying “no” here hurts performance.

mainMethodHandler(method, start, stop)

BaseMethod is a page directive.

It sets the class method that the main body of this PSP page over-rides. The default is WriteHTML. This value should be set to either WriteHTML or writeBody. See the PSPPage.py and Page.py servlet classes for more information.

optimizeCharData()

Optimize the CharData.

Too many char data generators make the servlet slow. If the current Generator and the next are both CharData type, merge their data.

setTemplateInfo(start, stop)

Mark non code data.

threadSafeHandler(value, start, stop)

Handle isThreadSage.

isThreadSafe is a page directive. The value can be “yes” or “no”. Default is no because the default base class, Page.py, isn’t thread safe.

PSP.ParseEventHandler.checkForTextHavingOnlyGivenChars(text, whitespace=None)

Checks whether text contains only whitespace (or other chars).

Does the given text contain anything other than the whitespace characters? Return true if text is only whitespace characters.