示例#1
0
    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")
示例#2
0
    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")