def mapToFile(self,costMap): #file = open('/root/Desktop/framework/testfileeditor/OutputFukle.yml', 'w') file=open('YamlFile.yml','w') keyList=costMap.keys() keyListLength=len(keyList) i=0; while i<keyListLength: yaml.dumpToFile(file,{keyList[i]:costMap[keyList[i]]}) i=i+1 file.close()
def run(self): mod = self.mod # read config information from imported template module if hasattr(mod, "scriptfile") and mod.scriptfile: self.scriptfile = mod.scriptfile elif hasattr(mod, "templatefile") and mod.templatefile: self.templatefile = mod.templatefile elif hasattr(mod, "inipickle") and mod.inipickle: self.inipickle = mod.inipickle elif hasattr(mod, "picklefile") and mod.picklefile: self.picklefile = mod.picklefile elif hasattr(mod, "outputfile") and mod.outputfile: self.outputfile = mod.outputfile elif hasattr(mod, "yamlfile") and mod.yamlfile: self.yamlfile = mod.yamlfile try: import yaml except: self.yamlfile = "" if self.picklefile: try: f = file(self.picklefile) import pickle self.values = pickle.load(f) f.close() except: self.log.traceback() elif self.inipickle: try: import obj2ini self.values = obj2ini.load(self.inipickle) except: self.log.traceback() elif self.yamlfile: try: import yaml self.values = yaml.loadFile(self.yamlfile).next() except: self.log.traceback() self.easy = easy = self.create_easy(mod, self.values) if easy.ShowModal() == wx.ID_OK: self.values = easy.GetValue() if self.picklefile: try: import pickle f = file(self.picklefile, "wb") pickle.dump(self.values, f) f.close() except: self.log.traceback() elif self.inipickle: try: import obj2ini obj2ini.dump(self.values, self.inipickle) except: self.log.traceback() elif self.yamlfile: try: import yaml yaml.dumpToFile(file(self.yamlfile, "wb"), self.values) except: self.log.traceback() if self.scriptfile: # cal scriptpath SCRIPTPATH = "" if hasattr(mod, "__file__"): SCRIPTPATH = os.path.dirname( os.path.abspath(os.path.join(os.path.dirname(mod.__file__), self.scriptfile)) ) # add scriptpath # self.values['SCRIPTPATH'] = SCRIPTPATH oldworkpath = os.getcwd() try: # os.chdir(SCRIPTPATH) # from meteor import TemplateScript, Template # from StringIO import StringIO # buf = StringIO() # template = Template() # template.load(os.path.basename(self.scriptfile), 'text') # buf.write(template.value('text', EasyUtils.str_object(self.values, self.outputencoding))) # buf.seek(0) # ts = TemplateScript() # ts.run(buf, self.values, True) if SCRIPTPATH: os.chdir(SCRIPTPATH) from meteor import TemplateScript ts = TemplateScript() ts.run(self.scriptfile, self.values, True) finally: os.chdir(oldworkpath) # if self.verbose: # print buf.getvalue() elif self.templatefile: from meteor import Template template = Template() template.load(self.templatefile) if isinstance(self.outputfile, (str, unicode)): f = file(self.outputfile, "wb") elif not self.outputfile: f = sys.stdout else: f = self.outputfile f.write(template.value(values=EasyUtils.str_object(self.values, self.outputencoding))) return True else: return False
def run(self): mod = self.mod #read config information from imported template module if hasattr(mod, 'scriptfile') and mod.scriptfile: self.scriptfile = mod.scriptfile elif hasattr(mod, 'templatefile') and mod.templatefile: self.templatefile = mod.templatefile elif hasattr(mod, 'inipickle') and mod.inipickle: self.inipickle = mod.inipickle elif hasattr(mod, 'picklefile') and mod.picklefile: self.picklefile = mod.picklefile elif hasattr(mod, 'outputfile') and mod.outputfile: self.outputfile = mod.outputfile elif hasattr(mod, 'yamlfile') and mod.yamlfile: self.yamlfile = mod.yamlfile try: import yaml except: self.yamlfile = '' if self.picklefile: try: f = file(self.picklefile) import pickle self.values = pickle.load(f) f.close() except: self.log.traceback() elif self.inipickle: try: import obj2ini self.values = obj2ini.load(self.inipickle) except: self.log.traceback() elif self.yamlfile: try: import yaml self.values = yaml.loadFile(self.yamlfile).next() except: self.log.traceback() self.easy = easy = self.create_easy(mod, self.values) if easy.ShowModal() == wx.ID_OK: self.values = easy.GetValue() if self.picklefile: try: import pickle f = file(self.picklefile, 'wb') pickle.dump(self.values, f) f.close() except: self.log.traceback() elif self.inipickle: try: import obj2ini obj2ini.dump(self.values, self.inipickle) except: self.log.traceback() elif self.yamlfile: try: import yaml yaml.dumpToFile(file(self.yamlfile, 'wb'), self.values) except: self.log.traceback() if self.scriptfile: #cal scriptpath SCRIPTPATH = '' if hasattr(mod, '__file__'): SCRIPTPATH = os.path.dirname( os.path.abspath( os.path.join(os.path.dirname(mod.__file__), self.scriptfile))) #add scriptpath #self.values['SCRIPTPATH'] = SCRIPTPATH oldworkpath = os.getcwd() try: # os.chdir(SCRIPTPATH) # from meteor import TemplateScript, Template # from StringIO import StringIO # buf = StringIO() # template = Template() # template.load(os.path.basename(self.scriptfile), 'text') # buf.write(template.value('text', EasyUtils.str_object(self.values, self.outputencoding))) # buf.seek(0) # ts = TemplateScript() # ts.run(buf, self.values, True) if SCRIPTPATH: os.chdir(SCRIPTPATH) from meteor import TemplateScript ts = TemplateScript() ts.run(self.scriptfile, self.values, True) finally: os.chdir(oldworkpath) # if self.verbose: # print buf.getvalue() elif self.templatefile: from meteor import Template template = Template() template.load(self.templatefile) if isinstance(self.outputfile, (str, unicode)): f = file(self.outputfile, 'wb') elif not self.outputfile: f = sys.stdout else: f = self.outputfile f.write( template.value(values=EasyUtils.str_object( self.values, self.outputencoding))) return True else: return False