Deneb
deneb at unixwave.org
Sun May 27 22:12:38 EST 2001
Hi, all just yesterday i finished well a Template Class in order to use it with modpython, higly ispired from PHPlib Template class, which is suitable for all purpose. Actually i use it in some of my projects. A correct example on how it works, can be found in the PHPlib docs, at phplib.netuse.de. I hope that you find this useful. The code is in the attach. Cheers, deneb. -------------- next part -------------- #!/usr/bin/python """ /* * PyTemplate - a Simple Python Template Class * Copyright (C) 2001 Giorgio Zoppi <deneb at unixwave.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ This Template file was strictly ported form PHPLIB Template class. Begin from the port: 28 May 2001 """ import sys import re import os import string from stat import * from types import * class PyTemplate: """A simple Template class""" __classname='PyTemplate' """0 -> False, 1-> True""" __debug = 1 __root = os.getcwd(); __unknowns = "remove" __file = {} __varkeys = {} __varvals = {} __halt_on_error = "yes" __last_error = "" def __init__(self,root,unknows="remove"): self.set_root(root) self.set_unknowns(unknows) def set_root(self,root): """I check if the directory exists""" mode = os.stat(root)[ST_MODE] if S_ISDIR(mode): """0 -> False, 1-> True""" self.__root=root return 1 return 0 def set_unknowns(self,unk): self.__unknowns=unk def set_file(self,handle,myfile=''): if not (type(handle) is DictType): if (myfile==''): return 0 else: self.__file[handle]=self.__filename(myfile) else: for (h,f) in handle.items(): self.__file[h]=self.__filename(f) def set_block(self,parent,handle,name=''): if not(self.__loadfile(parent)): return 0 if (name==''): name=handle vstr=self.get_var(parent) str2 = '' for value in vstr: str2 = str2 + value reg=r"<!--\s+BEGIN "+handle+"\s+-->(.*)<!--\s+END "+ handle+ "\s+-->" matchreg=re.compile(reg,re.S|re.M) m=matchreg.findall(str2) vname='{' + name + '}' vstr2=matchreg.sub(vname,str2) self.set_var(name,'') self.set_var(handle,m[0]) self.set_var(parent,vstr2) def set_var(self,varname1,value=''): if not (type(varname1) is DictType): self.__varkeys[varname1]=self.__varname(varname1) self.__varvals[varname1]=value else: for vname in varname1.keys(): self.__varkeys[vname]=self.__varname(vname) self.__varvals[vname]=varname1[vname] def subst(self,handle): if not(self.__loadfile(handle)): return 0 vstr=self.get_var(handle) regstr = '' for values in vstr: regstr= regstr + values vvals= self.__varvals.keys() kvals= self.__varkeys.keys() key1,key2='','' i,j=0,0 len1,len2=len(vvals),len(kvals) while ((i<len1) and (j<len2)): key2 = vvals[i] key1 = kvals[j] vareg=re.compile(self.__varkeys[key1]) regstr=vareg.sub(self.__varvals[key2],regstr) i,j=i+1,j+1 return regstr def psubst(self,handle): print self.subst(handle) return 0 def tmpl2str(self,varname): return self.finish(self.get_var(varname)) def parse(self,target,handle, append): if not (type(handle) is DictType): vstr=self.subst(handle) if (append==1): mvar = self.get_var(target) + vstr self.set_var(target,mvar) else: self.set_var(target,vstr) else: vstr = '' for (hname,hvalue) in handle.items(): vstr=self.subst(hvalue) self.set_var(target,vstr) #print t.varvals return vstr def get_vars(self): for kname in self.__varkeys.keys(): result[kname]=self.__varvals[kname] return result def get_var(self,varname): if not (type(varname) is DictType): return self.__varvals[varname] else: for vname in varname.keys(): result[vname]=self.__varvals[vname] return result """def get_undefined --> not implemented""" def finish(self,vstr): regstr1=re.compile('{\[^}+}') if (self.__unknowns=="comment"): regstr1.sub("<!--Template not defined -->",vstr) elif (self.__unknowns=="remove"): regstr1.sub("",vstr); else: self.__unknowns= "keep" """Nothing to do""" return vstr def p(self,varname): print self.finish(self.get_var(varname)) def get(self,varname): return self.finish(self.get_var(varname)) def __filename(self,filename): slice=string.find(filename,'/',0,1) if (slice==-1): filename= self.__root + '/' + filename if not (os.path.isfile(filename)): filename = 0 return filename def __varname(self,varname1): var = '{' + varname1 + '}' return var def __loadfile(self,handle): try: if ((self.__varkeys[handle]) and (not (self.__varvals[handle]==''))): return 1 except KeyError: new=1 try: name=self.__file[handle] except KeyError: return 0 filename= self.__filename(name) file=open(filename,'r') vstr=file.readlines() self.set_var(handle,vstr) return 1
|