def loadConfFile(self): #load a redirect.conf from default conf directory first, if it exisits, #it contains the real directory path to the main configuration file #if it doesn't exist, then system will load main config from default dir try: redirectConf=codecs.open(os.path.join(common.getConfDir(self.uname),"redirection.conf"),"r","utf-8") confdir=redirectConf.read().strip() redirectConf.close() except: confdir=common.getConfDir(self.uname) self.confPath=os.path.join(confdir,self.confFName) try: fp=codecs.open(self.confPath,"r","utf-8") readfList=json.load(fp,"utf-8") except Exception as Err: readfList=[] for cgroup in self.confList: for conf in cgroup[1]: if conf[0] in readfList: conf[1]['value']=readfList[conf[0]]
def OnOK(self,evt): r=self.toDict() if r['confDir']!=os.path.dirname(self.confPath): dlg=wx.MessageDialog(self,_("Configuration directory has changed, do you want to copy the config files to the new directory?"), _("copy the files?",),wx.YES_NO|wx.YES_DEFAULT) if dlg.ShowModal()==wx.ID_YES: try: common.copyConfigFiles(os.path.dirname(self.confPath),r['confDir'],self.uname) except Exception as Err: wx.MessageBox(_("Configureation files copy failed! copy the files manually\n")+unicode(Err),_("Error"),0|wx.ICON_ERROR,self) try: fp=codecs.open(os.path.join(common.getConfDir(self.uname),"redirection.conf"),"w","utf-8") fp.write(r['confDir']) fp.close() fp=codecs.open(os.path.join(r['confDir'],self.confFName),"w","utf-8") del r['confDir'] json.dump(r,fp,ensure_ascii=False,encoding='utf-8') fp.close() except Exception as Err: wx.MessageBox(_("Failed to save configurations!\n")+unicode(Err),_("Error"),0|wx.ICON_ERROR,self) evt.Skip()
def OnOK(self,evt): pass1=self.text_ctrl_upass1.GetValue().strip() pass2=self.text_ctrl_upass2.GetValue().strip() self.uname=self.text_ctrl_uname.GetValue().strip() b=evt.GetEventObject() orig_label=b.GetLabel() b.SetLabel(_("Creating...")) self.disableMe() confdir=common.getConfDir(self.uname) if os.path.isdir(confdir): wx.MessageBox(_("User already exisits, choose a different username"),_("Error"),0|wx.ICON_ERROR,self) b.SetLabel(orig_label) self.enableMe() return try: goodName(self.uname) except Exception as Err: wx.MessageBox(_("Not a good username, choose a different username\n")+unicode(Err),_("Error"),0|wx.ICON_ERROR,self) b.SetLabel(orig_label) self.enableMe() return if pass1!=pass2: wx.MessageBox(_("Password of two typing doesn't match!"),_("Error"),0|wx.ICON_ERROR,self) b.SetLabel(orig_label) self.enableMe() return try: goodPass(pass1,self.uname) except Exception as Err: wx.MessageBox(_("Not a good password, choose a different password\n")+unicode(Err),_("Error"),0|wx.ICON_ERROR,self) b.SetLabel(orig_label) self.enableMe() return waitbox=wx.BusyInfo(_("Creating new user, please wait...")) uport=common.getNewPort() exename=common.getManpassdExeName() cmd=exename+" -username={uname} -create=true -pipepass=true -svrport={port}".format(uname=self.uname,port=uport) args=shlex.split(cmd) if platform.system()=="Windows": startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags|= subprocess.STARTF_USESHOWWINDOW else: startupinfo = None ON_POSIX = 'posix' in sys.builtin_module_names p=subprocess.Popen(args,executable=exename,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,startupinfo=startupinfo,close_fds=ON_POSIX) def enqueue_output(out, queue): for line in iter(out.readline, b''): queue.put(line) out.close() outq=Queue.Queue() errq=Queue.Queue() t1=EnQThread(p.stdout,outq) t2=EnQThread(p.stderr,errq) t1.daemon=True t2.daemon=True t1.start() t2.start() p.stdin.write(pass1+"\n") p.stdin.close() def check_output(outq,errq): while True: wx.GetApp().Yield() try: outline=outq.get_nowait() except Queue.Empty: pass else: #print "stdout:",outline if outline.find("new user created") !=-1: break try: errline=errq.get_nowait() except Queue.Empty: pass else: #print "some err:",errline break t1.stop() t2.stop() fp=open(os.path.join(confdir,"manpass.conf"),"w") fp.write('{"port": '+str(uport)+" }") fp.close() check_output(outq,errq) del waitbox self.Close() evt.Skip()