Ejemplo n.º 1
0
	def _processFile(self, filePath, fileName, outFilePath):
		inputFullPath = os.path.join(filePath, fileName)
		inputBasenamePath, ext = os.path.splitext(inputFullPath)
		outputFullPath = os.path.join(outFilePath, fileName)
		
		if ext == ".html":
			if fileName[0] != "_":
				print "  Processing file %s..." % inputFullPath
			
				template = CaduceusTemplateParser.parseTemplateFile(inputFullPath, self.rootPath, self.caduceusPath)	
				if template:
					CaduceusHelper.ensurePathExists(outFilePath)
					
					outputFile = open(outputFullPath, 'w')
					try:
						# Load controller for file
						dictGlob = {}
						dictLoc = {}
						
						try:
							controllerName, _ext = os.path.splitext(fileName)
							controllerName = controllerName + "Test"
							
							sys.path.append(os.path.dirname(inputFullPath))
							exec ("from %s import %s" % (controllerName, controllerName)) in dictGlob, dictLoc

							# We must copy local dictionnary into global, to allow some symbols resolution
							dictGlob.update(dictLoc)
							
							# Instanciate a controller in parsing context 
							exec ("__caduceus_controler__ = %s()" % controllerName) in dictGlob, dictLoc
							
							# Bind all controller public methods as global methods 
							for key in dir(dictLoc[controllerName]):
								if key[0] != "_":
									proc = eval("__caduceus_controler__.%s" % key, dictGlob, dictLoc)
									dictLoc[key] = proc
						except IOError:
							print "Warning: No controller file for '%s'" % inputFullPath
						except ImportError:
							print "Warning: No controller file for '%s'" % inputFullPath
							
						# Render template using context
						tmplResults = CaduceusTemplateResults(outputFullPath)
						result = template.render(dictGlob, dictLoc, tmplResults)
						self.results.addTemplateResult(tmplResults)
						
						outputFile.write(result)
					finally:
						outputFile.close()
			else:
				print "  Skipping partial %s" % inputFullPath
		elif ext not in [".py", ".pyc"]:
			# File may be stylesheet, javasript or other resource
			# copy as it
			print "  Copy file %s..." % inputFullPath
			
			CaduceusHelper.ensurePathExists(outFilePath)		
			shutil.copyfile(inputFullPath, outputFullPath)