Update of /cvsroot/happydoc/HappyDoc3/happydoclib/docset/docset_TAL
In directory sc8-pr-cvs1:/tmp/cvs-serv995/happydoclib/docset/docset_TAL
Added Files:
templatefile.py test_templatefile.py
Log Message:
Wrapper for TAL library, including tests.
--- NEW FILE: templatefile.py ---
#!/usr/bin/env python
#
# $Id: templatefile.py,v 1.1 2003/01/26 19:15:02 doughellmann Exp $
#
# Copyright 2003 Doug Hellmann.
#
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee 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, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
"""Class to manage a single template file.
"""
__rcs_info__ = {
#
# Creation Information
#
'module_name' : '$RCSfile: templatefile.py,v $',
'rcs_id' : '$Id: templatefile.py,v 1.1 2003/01/26 19:15:02 doughellmann Exp $',
'creator' : 'Doug Hellmann <do...@he...>',
'project' : 'HappyDoc',
'created' : 'Sun, 26-Jan-2003 13:20:27 EST',
#
# Current Information
#
'author' : '$Author: doughellmann $',
'version' : '$Revision: 1.1 $',
'date' : '$Date: 2003/01/26 19:15:02 $',
}
try:
__version__ = __rcs_info__['version'].split(' ')[1]
except:
__version__ = '0.0'
#
# Import system modules
#
from cStringIO import StringIO
from pprint import pprint
#
# Import Local modules
#
from happydoclib.docset.docset_TAL import hdExpressions
from happydoclib.docset.docset_TAL.TAL.HTMLTALParser import HTMLTALParser
from happydoclib.docset.docset_TAL.TAL.TALGenerator import TALGenerator
from happydoclib.docset.docset_TAL.TAL.TALInterpreter import TALInterpreter
from happydoclib.trace import trace
#
# Module
#
TRACE_LEVEL = 2
class TemplateFile:
"""Class to manage a single TAL template file.
"""
def __init__(self, fileName):
"""Constructor.
"""
trace.into('TemplateFile', '__init__',
fileName=fileName,
outputLevel=TRACE_LEVEL,
)
self.filename = fileName
self.engine = hdExpressions.getEngine()
self.generator = TALGenerator(self.engine, xml=0)
self.parser = HTMLTALParser(self.generator)
self.parser.parseFile(self.filename)
self.program, self.macros = self.parser.getCode()
#print 'PROGRAM:', self.program
#print 'MACROS:'
#pprint(self.macros)
trace.outof(outputLevel=TRACE_LEVEL)
return
def getContext(self):
c = {'template': self,
'options': {},
'nothing': None,
'modules': hdExpressions.DocsetModuleImporter,
}
return c
def render(self, extraContext={}):
c = self.getContext()
c.update(extraContext)
context = self.engine.getContext(c)
output = StringIO()
interpreter = TALInterpreter(self.program,
self.macros,
context,
output,
wrap=0,
)
interpreter()
return output.getvalue()
--- NEW FILE: test_templatefile.py ---
#!/usr/bin/env python
#
# $Id: test_templatefile.py,v 1.1 2003/01/26 19:15:02 doughellmann Exp $
#
# Copyright 2003 Doug Hellmann.
#
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee 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, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
"""Basic tests for templatefile module for HappyDoc.
"""
__rcs_info__ = {
#
# Creation Information
#
'module_name' : '$RCSfile: test_templatefile.py,v $',
'rcs_id' : '$Id: test_templatefile.py,v 1.1 2003/01/26 19:15:02 doughellmann Exp $',
'creator' : 'Doug Hellmann <do...@he...>',
'project' : 'HappyDoc',
'created' : 'Sun, 26-Jan-2003 13:04:06 EST',
#
# Current Information
#
'author' : '$Author: doughellmann $',
'version' : '$Revision: 1.1 $',
'date' : '$Date: 2003/01/26 19:15:02 $',
}
try:
__version__ = __rcs_info__['version'].split(' ')[1]
except:
__version__ = '0.0'
#
# Import system modules
#
import os
import unittest
#
# Import Local modules
#
from happydoclib.docset.docset_TAL.templatefile import TemplateFile
#
# Module
#
class Here:
Athens = '<p>YOU GOT ATHENS</p>'
Atlanta = 'YOU GOT <b>ATLANTA</b>'
def __init__(self, name):
self.name = name
self.dict = { 'foo':'foo'}
return
def callMe(self):
return '%s was called' % self.name
def __getitem__(self, key):
return '%s-%s' % (self.name, self.dict[key])
class TemplateFileTestCase(unittest.TestCase):
def runOneTest(self, hereName, templateFileName, macroFileNames, expectedResults):
template = TemplateFile(templateFileName)
macro_files = {}
for macro_filename in macroFileNames:
macro_file_basename = os.path.basename(macro_filename)
m = TemplateFile(macro_filename)
macro_files[macro_file_basename] = m
actual_value = template.render(
extraContext={'templates':macro_files,
'here':Here(hereName),
},
)
self.failUnlessEqual(actual_value, expectedResults)
return
default_expected_value = '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>Override title</title>
This is the head_slot part of the header.
</head>
<body>
<div>
<table border="0" cellpadding="5" cellspacing="5">
<tr>
<td>
<a href="http://my.weather.com/weather/map/30605?name=index_large&day=1" target="_blank">
<img border="0" src="http://image.weather.com/web/radar/us_atl_closeradar_small_usen.jpg">
</a>
</td>
</tr>
<tr>
<td>
<a href="http://www.weather.com/weather/local/30605" target="_blank">
<img border="1" width="270" height="140" src="http://oap.weather.com/fcgi-bin/oap/generate_magnet?loc_id=USGA0027&code=482224"></a>
</td>
</tr>
</table>
<h2>Call Me</h2>
<div>%(here_name)s was called</div>
<div>
<span>bar</span>
</div>
<div>
<span>bar as python string</span>
</div>
<ul>
<li>
<p>foo</p>
</li>
<li>
<p>bar</p>
</li>
<li>
<p>blah</p>
</li>
<li>
<p>bletch</p>
</li>
</ul>
<span>Should see this.</span>
<h2>Athens, Structure</h2>
<div><p>YOU GOT ATHENS</p></div>
<h2>Atlanta, Structure</h2>
<div>YOU GOT <b>ATLANTA</b></div>
<h2>Athens, No Structure</h2>
<div><p>YOU GOT ATHENS</p></div>
<h2>Atlanta, No Structure</h2>
<div>YOU GOT <b>ATLANTA</b></div>
<span>%(here_name)s</span>
<span>%(here_name)s-foo</span>
<span>YOU ARE HERE</span>
<span>YOU ARE HERE (function)</span>
<span>YOU ARE HERE (function)</span>
</div>
</body>
</html>
'''
def testOne(self):
here_name = 'one'
expected_value = self.default_expected_value % locals()
self.runOneTest( 'one',
'TestCases/TAL/testtal.pt',
('TestCases/TAL/header.pt',
),
expected_value,
)
return
def testTwo(self):
here_name = 'two'
expected_value = self.default_expected_value % locals()
self.runOneTest( 'two',
'TestCases/TAL/testtal.pt',
('TestCases/TAL/header.pt',
),
expected_value,
)
return
|