def testTemplateFileString(self): t=TemplateFileOldFormat(content=template1) self.assertEqual(t.getString({"x":-1}),"This should be 1\n") fName=mktemp() t.writeToFile(fName,{"x":"1+sqrt(4)"}) result=open(fName).read() self.assertEqual(result,"This should be 9.0\n")
def setValsOnIter(self, i): if (i > len(self.__vals)): print "ParameterVertex is out of values. Exit" exit() baseFile = str(self.__templateFile) relativeDir = (baseFile.split("/"))[-2] baseFile = (baseFile.split("/"))[-1] saveFile = (baseFile.split("."))[0] savePath = self.__currentPath + "/" + relativeDir + "/" + saveFile self.__templateFile = self.__currentPath + "/" + relativeDir + "/" + baseFile baseFile = (baseFile.split("."))[0] t = TemplateFileOldFormat(name=self.__templateFile) for key, value in self.__parDict.items(): j = self.__vars.index(key) self.__parDict[key] = self.__vals[i][j] sys.stdout.write(key + "=" + str(self.__vals[i][j]) + " ") sys.stdout.write("\n") sys.stdout.flush() t.writeToFile(savePath, self.__parDict)
def testTemplateFileLongMath(self): t=TemplateFileOldFormat(content=templateMath) self.assertEqual(t.getString({"x":4}),"sqrt(x) = 2.0\n")
def testTemplateFileLongVars(self): t=TemplateFileOldFormat(content=template2) self.assertEqual(int(t.getString({"x":1})),36)
def testTemplateFileFile(self): fName=mktemp() open(fName,"w").write(template1) t=TemplateFileOldFormat(name=fName) self.assertEqual(t.getString({"x":-1}),"This should be 1\n")
def run(self): if self.opts.template == "stdin" and self.opts.pickledFileRead == "stdin": self.error( "Can't simultanously read pickled data and the tempalte from the standard input" ) content = None if self.opts.template == "stdin": content = sys.stdin.read() data = None if self.opts.pickledFileRead: data = self.readPickledData() fName = None if len(self.parser.getArgs()) == 2: if self.opts.pickledFileRead: self.error("old-format mode does not work with pickled input") if self.opts.outputFile: self.error("--output-file is not valid for the old format") # old school implementation fName = self.parser.getArgs()[0] vals = eval(self.parser.getArgs()[1]) if type(vals) == str: # fix a problem with certain shells vals = eval(vals) if self.opts.template == None: template = fName + ".template" else: template = self.opts.template if content: t = TemplateFileOldFormat(content=content) else: t = TemplateFileOldFormat(name=template) elif len(self.parser.getArgs()) == 0: if self.opts.template == None and self.opts.outputFile != None and self.opts.outputFile != "stdin": self.opts.template = self.opts.outputFile + ".template" self.warning("Automatically setting template to", self.opts.template) vals = {} if self.opts.useDefaults and self.opts.template != None and self.opts.template != "stdin": name, ext = path.splitext(self.opts.template) defaultName = name + ".defaults" if path.exists(defaultName): self.warning("Reading default values from", defaultName) vals = ParsedParameterFile( defaultName, noHeader=True, doMacroExpansion=True).getValueDict() vals.update(self.parameters) if self.opts.values: vals.update(eval(self.opts.values)) elif self.opts.valuesDict: vals.update( ParsedParameterFile(self.opts.valuesDict, noHeader=True, doMacroExpansion=True).getValueDict()) elif data: vals.update(data["values"]) elif len(self.parameters) == 0: self.error( "Either specify the values with --values-string or --values-dictionary or in the pickled input data" ) if self.opts.dumpUsed: maxLen = max([len(k) for k in vals.keys()]) formatString = " %%%ds | %%s" % maxLen print_("Used values") print_(formatString % ("Name", "Value")) print_("-" * (maxLen + 30)) for k, v in iteritems(vals): print_(formatString % (k, v)) if content: t = TemplateFile( content=content, tolerantRender=self.opts.tolerantRender, allowExec=self.opts.allowExec, expressionDelimiter=self.opts.expressionDelimiter, assignmentLineStart=self.opts.assignmentLineStart) elif data: t = TemplateFile( content=data["template"], tolerantRender=self.opts.tolerantRender, allowExec=self.opts.allowExec, expressionDelimiter=self.opts.expressionDelimiter, assignmentLineStart=self.opts.assignmentLineStart) elif self.opts.template: t = TemplateFile( name=self.opts.template, tolerantRender=self.opts.tolerantRender, allowExec=self.opts.allowExec, expressionDelimiter=self.opts.expressionDelimiter, assignmentLineStart=self.opts.assignmentLineStart) else: self.error("Template unspecified") if self.opts.outputFile: fName = self.opts.outputFile else: self.error( "Either specify 2 arguments (file and values) for old format or no arguments for the new format" ) if self.opts.stdout: print_(t.getString(vals)) elif fName: try: t.writeToFile(fName, vals) except (NameError, SyntaxError): e = sys.exc_info()[ 1] # Needed because python 2.5 does not support 'as e' print_("While processing file", fName) raise e else: self.error("No destination for the output specified")
def run(self): if self.opts.template=="stdin" and self.opts.pickledFileRead=="stdin": self.error("Can't simultanously read pickled data and the tempalte from the standard input") content=None if self.opts.template=="stdin": content=sys.stdin.read() data=None if self.opts.pickledFileRead: data=self.readPickledData() fName=None if len(self.parser.getArgs())==2: if self.opts.pickledFileRead: self.error("old-format mode does not work with pickled input") if self.opts.outputFile: self.error("--output-file is not valid for the old format") # old school implementation fName=self.parser.getArgs()[0] vals=eval(self.parser.getArgs()[1]) if type(vals)==str: # fix a problem with certain shells vals=eval(vals) if self.opts.template==None: template=fName+".template" else: template=self.opts.template if content: t=TemplateFileOldFormat(content=content) else: t=TemplateFileOldFormat(name=template) elif len(self.parser.getArgs())==0: if self.opts.template==None and self.opts.outputFile!=None and self.opts.outputFile!="stdin": self.opts.template=self.opts.outputFile+".template" self.warning("Automatically setting template to",self.opts.template) vals={} if self.opts.useDefaults and self.opts.template!=None and self.opts.template!="stdin": name,ext=path.splitext(self.opts.template) defaultName=name+".defaults" if path.exists(defaultName): self.warning("Reading default values from",defaultName) vals=ParsedParameterFile(defaultName, noHeader=True, doMacroExpansion=True).getValueDict() vals.update(self.parameters) if self.opts.values: vals.update(eval(self.opts.values)) elif self.opts.valuesDict: vals.update(ParsedParameterFile(self.opts.valuesDict, noHeader=True, doMacroExpansion=True).getValueDict()) elif data: vals.update(data["values"]) elif len(self.parameters)==0: self.error("Either specify the values with --values-string or --values-dictionary or in the pickled input data") if self.opts.dumpUsed: maxLen=max([len(k) for k in vals.keys()]) formatString=" %%%ds | %%s" % maxLen print_("Used values") print_(formatString % ("Name","Value")) print_("-"*(maxLen+30)) for k,v in iteritems(vals): print_(formatString % (k,v)) if content: t=TemplateFile(content=content, tolerantRender=self.opts.tolerantRender, allowExec=self.opts.allowExec, expressionDelimiter=self.opts.expressionDelimiter, assignmentLineStart=self.opts.assignmentLineStart) elif data: t=TemplateFile(content=data["template"], tolerantRender=self.opts.tolerantRender, allowExec=self.opts.allowExec, expressionDelimiter=self.opts.expressionDelimiter, assignmentLineStart=self.opts.assignmentLineStart) elif self.opts.template: t=TemplateFile(name=self.opts.template, tolerantRender=self.opts.tolerantRender, allowExec=self.opts.allowExec, expressionDelimiter=self.opts.expressionDelimiter, assignmentLineStart=self.opts.assignmentLineStart) else: self.error("Template unspecified") if self.opts.outputFile: fName=self.opts.outputFile else: self.error("Either specify 2 arguments (file and values) for old format or no arguments for the new format") if self.opts.stdout: print_(t.getString(vals)) elif fName: try: t.writeToFile(fName,vals) except (NameError,SyntaxError): e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e' print_("While processing file",fName) raise e else: self.error("No destination for the output specified")