Thread: [cgkit-commits] cgkit2/cgkit slparams.py,1.2,1.3 simplecpp.py,1.1,1.2
Brought to you by:
mbaas
From: Matthias B. <mb...@us...> - 2006-01-27 22:11:18
|
Update of /cvsroot/cgkit/cgkit2/cgkit In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24366 Modified Files: slparams.py simplecpp.py Log Message: slparams() also accepts a file-like object in addition to the shader file name. Index: simplecpp.py =================================================================== RCS file: /cvsroot/cgkit/cgkit2/cgkit/simplecpp.py,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** simplecpp.py 21 Apr 2005 17:27:56 -0000 1.1 --- simplecpp.py 27 Jan 2006 22:11:09 -0000 1.2 *************** *** 21,24 **** --- 21,26 ---- # $Id$ + import types, os.path + # PreProcessor class PreProcessor: *************** *** 36,47 **** self.inside_comment = False # filter def filter(self, s): """Filter a string (which may contain multiple lines). """ reslist = map(lambda x: self.filterLine(x), s.split("\n")) return "\n".join(reslist) ! ! # filterLine def filterLine(self, s): --- 38,75 ---- self.inside_comment = False + def __call__(self, source, errstream=None): + """Preprocess a file or a file-like object. + + source may either be the name of a file or a file-like object. + """ + if isinstance(source, types.StringTypes): + filename = source + # Read the input file + f = open(filename) + s = f.read() + f.close() + else: + s = source.read() + filename = getattr(source, "name", "<?>") + + # Filter + s = self.filter(s) + # Return the filtered text + res = '# 1 "%s"\n%s'%(os.path.basename(filename), s) + return res + # filter def filter(self, s): """Filter a string (which may contain multiple lines). + + s may either be a string or a file-like object. """ + # If s is no string it must be a file-like object + if not isinstance(s, types.StringTypes): + s = s.read() + reslist = map(lambda x: self.filterLine(x), s.split("\n")) return "\n".join(reslist) ! # filterLine def filterLine(self, s): Index: slparams.py =================================================================== RCS file: /cvsroot/cgkit/cgkit2/cgkit/slparams.py,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** slparams.py 5 Jul 2005 17:21:50 -0000 1.2 --- slparams.py 27 Jan 2006 22:11:09 -0000 1.3 *************** *** 30,34 **** """Extract shader parameters from a RenderMan shader source file.""" ! import os, os.path, sys, string, StringIO, sltokenize import cgtypes, math, sl, simplecpp import _slparser --- 30,34 ---- """Extract shader parameters from a RenderMan shader source file.""" ! import os, os.path, sys, string, StringIO, sltokenize, types import cgtypes, math, sl, simplecpp import _slparser *************** *** 198,234 **** - # _preprocessSource - def _preprocessSource(filename): - """Simple substitution for a C preprocessor. - """ - # Read the input file - f = file(filename) - s = f.read() - f.close() - # Filter - cpp = simplecpp.PreProcessor() - s = cpp.filter(s) - # Return the filtered text - res = '# 1 "%s"\n%s'%(os.path.basename(filename), s) - return res - - # slparams ! def slparams(slname, cpp=None, cpperrstream=sys.stderr): """Extracts the shader parameters from a Shading Language source file. ! The argument slname is the name of the shader source file (*.sl). ! cpp determines how the shader source is preprocessed. It can be ! either a string containing the name of an external preprocessor ! tool (such as 'cpp') that must take the file name as parameter and ! dump the preprocessed output to stdout. If the preprocessor ! doesn't produce any data a PreprocessorNotFound exception is ! thrown. The error stream of the preprocessor is written to the ! object that's specified by cpperrstream which must have a write() method. If cpperrstream is None, the error stream is ignored. ! cpp can also be a callable object that takes a filename as input ! and returns the filtered contents as a string. If cpp is None a ! simple internal preprocessor based on the simplecpp module is ! used. The function returns a list of 3-tuples, one for each shader found --- 198,223 ---- # slparams ! def slparams(slfile=None, cpp=None, cpperrstream=sys.stderr, slname=None): """Extracts the shader parameters from a Shading Language source file. ! The argument slfile is either the name of the shader source file ! (*.sl) or it is a file-like object that provides the shader ! sources. cpp determines how the shader source is preprocessed. It ! can either be a string containing the name of an external ! preprocessor tool (such as 'cpp') that must take the file name as ! parameter and dump the preprocessed output to stdout or it can be ! a callable that takes slfile and cpperrstream as input and returns ! the preprocessed sources as a string. If the external ! preprocessor does not produce any data a PreprocessorNotFound ! exception is thrown. ! The error stream of the preprocessor is written to the object ! that's specified by cpperrstream which must have a write() method. If cpperrstream is None, the error stream is ignored. ! ! If cpp is None a simple internal preprocessor based on the ! simplecpp module is used. ! The slname argument is an alias for slfile, it's only available ! for backwards compatibility. The function returns a list of 3-tuples, one for each shader found *************** *** 247,280 **** """ ! # Check if the file exists and can be accessed (by trying to open it) ! # If the file doesn't exist, an exception is thrown. ! dummy = file(slname) ! dummy.close() ! # Run the preprocessor on the input file... if cpp==None: ! cpp = _preprocessSource ! # Is cpp a callable then use it to preprocess the shader source ! if callable(cpp): ! slsrc = cpp(slname) ! f = StringIO.StringIO(slsrc) ! # no callable, then it must be a string containing the preprocessor command ! else: ! # The preprocessed data will be in slsrc. ! cmd = "%s %s"%(cpp, slname) ! stdin, stdout, stderr = os.popen3(cmd) ! slsrc = stdout.read() ! stdout.close() ! if cpperrstream!=None: ! errs = stderr.read() ! cpperrstream.write(errs) ! # No data? Then it's assumed that the preprocessor couldn't be found ! if len(slsrc)==0: ! raise PreprocessorNotFound("Calling '%s' didn't produce any data."%cmd) ! f = StringIO.StringIO(slsrc) ! # else: ! # f = file(slname) ! # ...and filter it, so that only the shader and function # definitions remain... --- 236,252 ---- """ ! if slname!=None: ! slfile = slname ! if slfile==None: ! return [] ! # Run the preprocessor on the input file... if cpp==None: ! cpp = simplecpp.PreProcessor() ! ! slsrc = preprocess(cpp, slfile, cpperrstream) ! f = StringIO.StringIO(slsrc) ! # ...and filter it, so that only the shader and function # definitions remain... *************** *** 317,320 **** --- 289,355 ---- raise exc + # preprocess + def preprocess(cpp, file, cpperrstream=sys.stderr): + """Preprocess an input file. + + cpp is either a string containing the name of an external preprocessor + command (e.g. 'cpp') or a callable function that takes file and + cpperrstream as input and returns the preprocessed source code as + a string. + If the external preprocessor produces no data it is assumed that + it was not found a a PreprocessorNotFound exception is thrown. + + file is either a string containing the input file name or it's a + file-like object. + If the input file doesn't exist, an IOError is thrown. + + cpperrstream is a stream that receives any error messages from + the preprocessor. If it's None, error messages are ignored. + + The function returns the preprocessed sources as a string. + """ + + # Is cpp a callable then invoke it and return the result + if callable(cpp): + return cpp(file, cpperrstream) + + # no callable, so cpp contains the name of an external preprocessor tool + + # Variables: + # + # cmd: The command that is used to execute the preprocessor. + # It either contains only the cpp name or the cpp name + input file + # slsrc: (string) Unprocessed SL source if it should be passed via stdin + # otherwise it's None. + # ppslsrc: Preprocessed SL source (this is the result). + + # Is file a string? Then it's a file name... + if isinstance(file, types.StringTypes): + cmd = "%s %s"%(cpp, file) + slsrc = None + # Check if the file exists and can be accessed (by trying to open it) + # If the file doesn't exist, an exception is thrown. + dummy = open(file) + dummy.close() + # ...otherwise it's a file-like object + else: + cmd = "%s"%cpp + slsrc = file.read() + + # The preprocessed data will be in slsrc. + stdin, stdout, stderr = os.popen3(cmd) + if slsrc!=None: + stdin.write(slsrc) + stdin.close() + ppslsrc = stdout.read() + stdout.close() + if cpperrstream!=None: + errs = stderr.read() + cpperrstream.write(errs) + # No data? Then it's assumed that the preprocessor couldn't be found + if len(ppslsrc)==0: + raise PreprocessorNotFound("Calling '%s' didn't produce any data."%cmd) + + return ppslsrc *************** *** 386,390 **** if arraylen==None: ! res = res[0] return res --- 421,428 ---- if arraylen==None: ! if len(res)==0: ! return None ! else: ! res = res[0] return res |