本文整理汇总了Python中template.Template.processString方法的典型用法代码示例。如果您正苦于以下问题:Python Template.processString方法的具体用法?Python Template.processString怎么用?Python Template.processString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类template.Template
的用法示例。
在下文中一共展示了Template.processString方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Expect
# 需要导入模块: from template import Template [as 别名]
# 或者: from template.Template import processString [as 别名]
def Expect(self, data, tproc=None, vars=None):
vars = vars or {}
data = re.sub(r"(?m)^#.*\n", "", data)
match = re.search(r"\s*--\s*start\s*--\s*", data)
if match:
data = data[match.end():]
match = re.search(r"\s*--\s*stop\s*--\s*", data)
if match:
data = data[:match.start()]
tests = re.split(r"(?mi)^\s*--\s*test\s*--\s*", data)
if not tests[0]:
tests.pop(0)
ttprocs = None
if isinstance(tproc, dict):
tproc = Template(tproc)
elif isinstance(tproc, (tuple, list)):
ttprocs = dict(tproc)
tproc = tproc[0][1]
elif not isinstance(tproc, Template):
tproc = Template()
for count, test in enumerate(tests):
match = re.search(r"(?mi)^\s*-- name:? (.*?) --\s*\n", test)
if match:
name = match.group(1)
test = test[:match.start()] + test[match.end():]
else:
name = "template text %d" % (count + 1)
match = re.search(r"(?mi)^\s*--\s*expect\s*--\s*\n", test)
if match:
input, expect = test[:match.start()], test[match.end():]
else:
input, expect = test, ""
match = re.match(r"(?mi)^\s*--\s*use\s+(\S+)\s*--\s*\n", input)
if match:
ttname = match.group(1)
ttlookup = ttprocs.get(ttname)
if ttlookup:
tproc = ttlookup
else:
self.fail("no such template object to use: %s\n" % ttname)
input = input[:match.start()] + input[match.end():]
try:
out = tproc.processString(input, vars)
except Exception, e:
self.fail("Test #%d: %s process FAILED: %s\n%s" % (
count + 1, name, subtext(input), e))
match = re.match(r"(?i)\s*--+\s*process\s*--+\s*\n", expect)
if match:
expect = expect[match.end():]
try:
expect = tproc.processString(expect, vars)
except TemplateException, e:
self.fail("Test #%d: Template process failed (expect): %s" % (
count + 1, e))
示例2: writeVhost
# 需要导入模块: from template import Template [as 别名]
# 或者: from template.Template import processString [as 别名]
def writeVhost(settings, domain, templateName, values):
"""Writes a site's vhost configuration file"""
# Open the file
conf = str(os.path.join(settings['confdir'], domain))
outFile = open(conf, 'w')
# Write the configuration
t = Template()
try:
tplDir = os.path.join(settings['templates'], templateName)
templateFilename = os.path.join(tplDir, templateName + '.tpl')
inFile = open(templateFilename, 'r')
source = inFile.read()
# While there are variables left unprocessed, process the string
expression = r'\[\%.+\%\]'
while re.search(expression, source) != None:
source = t.processString(source, values)
# Write what we have
outFile.write(source)
except TemplateException, e:
print "ERROR: %s" % e
示例3: makeDirectories
# 需要导入模块: from template import Template [as 别名]
# 或者: from template.Template import processString [as 别名]
def makeDirectories(settings, domain, templateName, values):
"""Creates the directories for the web site"""
t = Template()
try:
tplDir = os.path.join(settings['templates'], templateName)
dirsFile = os.path.join(tplDir, templateName + '.dirs')
inFile = open(dirsFile, 'r')
source = inFile.read()
# Substitute any variables
expression = '\[\%.+\%\]'
while re.search(expression, source) != None:
source = t.processString(source, values)
# Go through each substituted line
lines = source.split('\n')
for line in lines:
if len(line) > 0 and not line.strip().startswith('#'):
# Split the line up into its pieces
l = line.split()
directory = l[0]
user = pwd.getpwnam(l[1])[0]
group = grp.getgrnam(l[2])[0]
mode = l[3]
uid = pwd.getpwnam(l[1])[2]
gid = grp.getgrnam(l[2])[2]
# Create the directory
if os.path.exists(directory):
print('warning: ' + directory + ' exists')
else:
os.mkdir(directory, int(mode, 8))
# chown and chmod it
os.chown(directory, uid, gid)
subprocess.call('chmod ' + mode + ' ' + directory, shell=True)
except TemplateException, e:
print "ERROR: %s" % e
示例4: Template
# 需要导入模块: from template import Template [as 别名]
# 或者: from template.Template import processString [as 别名]
#
# Simple "Hello world!" program.
#
from template import Template
from template.util import TemplateException
TEMPLATE_TEXT = "Hello [% thing %]!"
t = Template()
try:
print t.processString(TEMPLATE_TEXT, { "thing": "world" })
except TemplateException, e:
print "ERROR: %s" % e