def CreateNewClient(root, mech, edict, pathn=None, internal=0): "create a new client id" if internal == 0: if mech.buttonInhibit: return deselect(mech, edict, reset=0) mech._set_study_info(root) mech.buttonInhibit = 1 # don't let user punch buttons during reload client = clients.CreateNewClient(root, edict, rebuild=0) if client: mech.buttonInhibit = 0 proto = CreateClientProtocol(root, mech, edict, client, internal=1) if proto: if proto == 'EXP': mech.buttonInhibit = 1 # don't let user punch buttons during reload pathing = edict['DATAPATH'] + '\\' + client[ 'client.clientid'] + '\\SMR\\stages.5' if os.path.exists(pathing): mech.buttonInhibit = 0 return _copyProtocolFiles(root, edict, client, 'SMR') clients.NewClientIndexNeeded(root, edict, 1) mech.buttonInhibit = 0
def ArchiveInactive(root,edict,rpt=0): " archive clients unchanged in last year" #get year today gatedate=time.time() gt1=list(time.localtime(gatedate)) # back up one year gt1[0] -= 1 gstamp=time.mktime(gt1) # where I mark from busy=BusyBox.BusyBox(root,"Archiving Inactive") #make list of clients clist=_getinact(root,edict,gstamp,busy,rpt) if len(clist) == 0: busy.destroy() return busy.newstatus('') text='Found %d inactive clients\nDo you really want to archive them?' %len(clist) ans = tkMessageBox.askyesno('Archiving Inactive',text) if ans == 0: busy.destroy() return #allow to UNselect some ##lst=[] ##for cid in clist: ## pass #archive those clients top=edict['DATAPATH'] toplen=len(top)+1 for cid,plist in clist: tmppath=edict['ARCHIVES']+'\\'+cid+'.BAK.ZIP' dpath=edict['ARCHIVES']+'\\'+cid+'.ZIP' nodel=0 zf=zipfile.ZipFile(tmppath,'w',zipfile.ZIP_DEFLATED) for fn in plist: aname=fn[toplen:] if aname.find('.') < 0: aname=aname+'.' # force append of dot zf.write(fn,aname,zipfile.ZIP_DEFLATED) busy.newstatus('Archiving '+aname) zf.close() if 1: try: os.remove(dpath) except: pass os.rename(tmppath,dpath) shutil.rmtree(edict['DATAPATH']+'\\'+cid,1) try: gc.collect() except: pass busy.destroy() clients.NewClientIndexNeeded(root,edict,0) # rebuild on next access
def SelectCustomPlan(root, mech, edict, client=None, pro=None): # somehow select a client first????? if mech.buttonInhibit: return if client == None: clientgui.deselect(nmech, edict) mech._set_study_info(root) mech.buttonInhibit = 1 client = clients.SelectClient(root, 0, edict) if client == None: mech.buttonInhibit = 0 return proto = clients.SelectProtocol(root) if proto == None: mech.buttonInhibit = 0 return ask = None else: client = mech.client proto = pro ask = mech.layout pathing = edict['DATAPATH'] + '\\' + client[ 'client.clientid'] + '\\' + proto pathn = _selectcustom(root, client, proto, pathing, mech, edict, ask=ask) if pathn: ####mech.title=stitle mech.st_save_path = pathn try: mech.canvas.itemconfigure(mech.canvlayout, text=clients.GetLayoutTitle(layout)) except: pass mech.set_topwarnings(root) mech.buttonInhibit = 0 # ask - Do you want a specific default plan md = Pmw.MessageDialog( root, message_text='Do you want to load a custom session plan', defaultbutton=0, title='Option to select a custom session plan', buttons=['Yes', 'No']) ans = md.activate() if ans != None and ans == 'Yes': # if yes _selectcustom(root, client, proto, pathing, mech, edict) clients.NewClientIndexNeeded(root, edict, 1) mech.buttonInhibit = 0 mech._set_study_info(root) return proto
def MakeNormal(root, interface): cl = clients.SelectClient(root, None, edict, shamselect=1) # make selection list from these if not cl: return #get GUID fpath = edict['DATAPATH'] + '\\' + cl['client.xguid'] # mark that folder as normal _SetAttributes(fpath, 0) opath = edict['DATAPATH'] + '\\' + cl['client.clientid'] clo = IniAid.LoadConfig(opath + '\\CLIENT.') clo['client.stdyclient'] = 0 try: clo['client.shamclient'] = 0 except: pass clients.WriteClientFile(clo, edict) clients.NewClientIndexNeeded(root, edict, 0)
def DeleteEntireClient(root,edict,rpt=0): # first, get confirmation!!! ans = tkMessageBox.askyesno('Delete Client???','Do you REALLY want to permanently and forever delete a client???') if not ans: return # select client (all protos) cd = clients.SelectClient(root,0,edict,1,'Old client name') if cd == None: return # never mind # get 2nd confirmation ans=tkMessageBox.askyesno('Delete Client???','Do you REALLY want to permanently and forever delete '+cd['client.clientid']+'???') if not ans: return # now remove the entire tree shutil.rmtree(edict['DATAPATH']+ '\\'+cd['client.clientid'],1) clients.NewClientIndexNeeded(root,edict,0) try: gc.collect() except: pass
def _rebuildindex(root, mech, edict): deselect(mech, edict, reset=0) mech._set_study_info(root) mech.buttonInhibit = 1 clients.NewClientIndexNeeded(root, edict, 1)
def SyncClients(root,edict,rpt=0): if os.path.normpath(edict['RAWPATH']).upper() == r'C:\EEGER\CLIENTS': tkMessageBox.showinfo("Sync Clients",'Client path is set to local now anyway') return busy=BusyBox.BusyBox(root,'Syncing clients') rdict=_makefdict(edict['RAWPATH']) ldict=_makefdict(r'C:\EEGer\Clients') rkeys=rdict.keys() lkeys=ldict.keys() rkeys.sort() needclients=0 needfiles=0 for key in lkeys: if not key in rkeys: # here to copy an entire client # copy entire folder tree if doit: try: busy.newstatus(key) shutil.copytree('C:/EEGer/Clients/'+key,edict['RAWPATH']+'/'+key) needclients += 1 except: print 'FAILURE copy client: ',key else: print "copytree ",'C:/EEGer/Clients/'+key,edict['RAWPATH'] needclients += 1 continue # here if maybe just some files missing rlist,slist,xlist=rdict[key] hasrlist,hasslist,hasxlist=ldict[key] for cand,has in ((hasrlist,rlist),(hasslist,slist),(hasxlist,xlist)): for f in cand: if not f in has: folder,proto=f.split('/')[0:2] # copy local to remote c:\EEGer\Clients\(clientkey)\filenamewithfolder [rawpath]\key\foldername if not os.path.exists(edict['RAWPATH']+'/'+folder+'/'+proto): if doit > 1: try: os.mkdir(edict['RAWPATH']+'/'+folder+'/'+proto) except: pass else: print 'mkdir ',edict['RAWPATH']+'/'+folder+'/'+proto if doit > 1: try: busy.newstatus(f) shutil.copy2('C:/EEGER/Clients/'+f,edict['RAWPATH']+'/'+folder+'/'+proto) needfiles += 1 except: print 'FAILURE file copy: ',f else: needfiles += 1 print 'copy2 ','C:/EEGER/Clients/'+f,edict['RAWPATH']+'/'+folder+'/'+proto if needclients: clients.NewClientIndexNeeded(root,edict,0) busy.destroy() if needfiles or needclients: tkMessageBox.showinfo('Sync Clients','Transfered %d clients and %d separate files'% (needclients,needfiles)) else: tkMessageBox.showinfo('Sync Clients','Nothing needed to be done')
def RenameClient(root,edict,rpt=0): # select source client (all protos) cdold = clients.SelectClient(root,0,edict,1,'Old client name') if cdold == None: return # never mind # Create new client cd=clients.CreateNewClient(root,edict,title='Creating new client name ',oldclient=cdold) if cd == None: return # again, never mind # but use XGUID from old client!!!! cd['client.xguid']=cdold['client.xguid'] clients.WriteClientFile(cd,edict) oldsrcroot=edict['DATAPATH']+ '\\'+cdold['client.clientid']+'\\' newroot=edict['DATAPATH']+ '\\'+cd['client.clientid']+'\\' # move old edit file efile=oldsrcroot+cdold['client.clientid']+'.txt' if os.path.exists(efile): shutil.copy2(efile,newroot+cd['client.clientid']+'.txt') # for all classes sdlst=glob.glob(oldsrcroot+'*') # move all contents errors=0 for each in sdlst: if os.path.isdir(each): # here on subdirs dest_proto=os.path.basename(each) spath=each dpath=newroot+dest_proto os.mkdir(dpath) stgsx=['lastset.*','stages.*','lastlayout.ini'] stgs=[] for x in stgsx: stgs.extend(glob.glob(spath+'/'+x)) for fnx in stgs: fn=os.path.basename(fnx) filename=spath+'\\'+fn newname=dpath+'\\'+fn if os.path.exists(filename): # must change internal names matching the OLD name to the NEW name if _renameinside(filename,newname,cd,cdold): # if failed or no change needed, use bulk copier shutil.copy(filename,dpath) shutil.copystat(filename,dpath+'\\'+fn) ##print oldsrcroot,dest_proto flst=simpletree.RawSumFilelist(oldsrcroot[:-1],dest_proto) ##print flst for entry in flst: # each file if entry[0]: continue f=entry[3] destfilename=dpath+'\\'+cd['client.clientid']+os.path.basename(f)[-10:] while 1: if not os.path.exists(destfilename): break num=int(destfilename[-6:-4]) num = num+1 if num >= 100: _dialoghelper('File rename failure','Too many files for one day',root=root) destfilename=None errors += 1 break destfilename='%s%02d%s' %(destfilename[:-6],num,destfilename[-4:]) if destfilename == None: continue # no place for this guy srcfilename=spath+'\\'+os.path.basename(f) shutil.copy(srcfilename,destfilename) # must know if RAW or SUM since headers differ!!!! if destfilename.upper()[-4:] == '.RAW': # update raw header rawlist.writeheader(destfilename,cd['client.clientid'],cd['client.fullname'],dest_proto) else: # update sum header sumfile.writeheader(destfilename,cd['client.clientid'],cd['client.fullname'],dest_proto) shutil.copystat(srcfilename,destfilename) # move date os.remove(srcfilename) # remove old client if no errors if errors == 0: shutil.rmtree(oldsrcroot[:-1],1) clients.NewClientIndexNeeded(root,edict,0) try: gc.collect() except: pass
def MoveFiles(root,edict,rpt=0): "move files from one client/protocol to another" cd = clients.SelectClient(root,1,edict,1,'Source client') if cd == None: if rpt: print 'No client selected' return cpath= edict['DATAPATH']+'\\'+cd['client.clientid'] lst=simpletree.RawSumFilelist(cpath,cd['pcode']) flst=simpletree.SelectedFiles(root,cpath,lst) try: gc.collect() except: pass if flst == None: if rpt: print 'No selected files' return cd2=clients.SelectClient(root,0,edict,1,'Destination client') if cd2 == None: return dest_proto=clients.SelectProtocol(root,'Select class for destination') if dest_proto == None: return if cd['client.clientid'] == cd2['client.clientid'] and cd['pcode'] == dest_proto: _dialoghelper('Invalid operation','Sorry, you cannot move to the source class',root=root) return # things to consider # if I have to create the destination protocol directory # must also copy across lastset.ini and stages.ini # copy file with new 'name' after checking for no conflict # update header data for protocol and client names # make new file have 'old' date spath=edict['DATAPATH']+ '\\'+cd['client.clientid']+'\\'+ cd['pcode'] dpath=edict['DATAPATH']+'\\'+cd2['client.clientid']+'\\'+dest_proto allyes=0 for item in flst: if item[0]: continue # just handle files themselves if item[1]: delme=0 if not allyes: ans=_ask_about(root,item[2],item[3],dpath) if ans == 'all': allyes=1 delme=1 elif ans == 'none': break elif ans == 'yes': delme=1 if delme or allyes: # here we ACTUALLY move the file if not os.path.exists(dpath): os.mkdir(dpath) stgsx=['lastset.*','stages.*','lastlayout.ini'] stgs=[] for x in stgsx: stgs.extend(glob.glob(spath+'\\'+x)) for fnx in stgs: fn=os.path.basename(fnx) filename=spath+'\\'+fn newname=dpath+'\\'+fn if os.path.exists(filename): # must change internal names matching the OLD name to the NEW name if _renameinside(filename,newname,cd,cd2): # if failed or no change needed, use bulk copier shutil.copy(filename,dpath) shutil.copystat(filename,dpath+'\\'+fn) destfilename=dpath+'\\'+cd2['client.clientid']+os.path.basename(item[3])[-10:] while 1: if not os.path.exists(destfilename): break num=int(destfilename[-6:-4]) num = num+1 if num >= 100: _dialoghelper('File move failure','Too many files for one day',root=root) destfilename=None break destfilename='%s%02d%s' %(destfilename[:-6],num,destfilename[-4:]) if destfilename == None: continue # no place for this guy srcfilename=spath+'\\'+os.path.basename(item[3]) shutil.copy(srcfilename,destfilename) # must know if RAW or SUM since headers differ!!!! if destfilename.upper()[-4:] == '.RAW': # update raw header rawlist.writeheader(destfilename,cd2['client.clientid'],cd2['client.fullname'],dest_proto) else: # update sum header sumfile.writeheader(destfilename,cd2['client.clientid'],cd2['client.fullname'],dest_proto) shutil.copystat(srcfilename,destfilename) # move date os.remove(srcfilename) clients.NewClientIndexNeeded(root,edict,0) try: gc.collect() except: pass
def _most_unarchive(root,edict,zf,cd,flst,overwrite=0,rpt=0,title="Unarchiving"): busy=BusyBox.BusyBox(root,title) #3. If current client does not exist, must create it and retrieve basic files also lpath=edict['DATAPATH']+'\\'+cd['client.clientid'] prefix=cd['client.clientid']+'/' if not os.path.exists(lpath): os.mkdir(lpath) if not os.path.exists(lpath+'/client.'): try: rd=zf.read(prefix+'client.') rdinfo=zf.getinfo(prefix+'client.') except: rd=zf.read(prefix+'client') rdinfo=zf.getinfo(prefix+'client') _make_file(edict['DATAPATH'],rdinfo,rd) notes=prefix+cd['client.clientid']+'.txt' try: rd=zf.read(notes) except: rd=None if rd: rdinfo=zf.getinfo(notes) _make_file(edict['DATAPATH'],rdinfo,rd) error='No files selected' #4. Restore selected files (If a file already exists, ask if overwrite is desired. Remember selection if OVERWRITE ALL or NEVER OVERWRITE is selected.) subpath='' for item in flst: if item[0]: continue # only do files themselves if item[1]: if error == 'No files selected': error=None # want to extract THIS item nm=prefix+item[3] namepieces=nm.split('/') directory=edict['DATAPATH']+'\\'+cd['client.clientid']+'\\'+namepieces[1] ##print 'dir=',directory,item if not os.path.exists(directory): os.mkdir(directory) if namepieces[1] != subpath: subpath=namepieces[1] other=zf.namelist() part=cd['client.clientid']+'\\'+namepieces[1] part=os.path.normpath(part) for x in other: if os.path.normpath(x).find(part) < 0: continue ext= os.path.splitext(x)[1] if ext.lower() == '.raw' or ext.lower() == '.sum': continue fn=directory+'/'+os.path.basename(x) exists=os.path.exists(directory+'/'+fn) if overwrite or not exists: if exists: pass # always overwrite for now continue # just ignore for now gotcha=0 fn=x rd=zf.read(fn) busy.newstatus(fn) rdinfo=zf.getinfo(fn) _make_file(edict['DATAPATH'],rdinfo,rd) exists=os.path.exists(edict['DATAPATH']+nm) if overwrite or not exists: # ask if overwrite desired if exists: continue # later - ask # not overwrite existing rdinfo=zf.getinfo(nm) rd=zf.read(nm) busy.newstatus(nm) _make_file(edict['DATAPATH'],rdinfo,rd) clients.NewClientIndexNeeded(root,edict,0) # rebuild on next access try: gc.collect() except: pass zf.close() busy.destroy() return error
def Archive(root,edict,rpt=1): " handle archive process" #1. Select a client from the current (active) client list (I plan on displaying the most recent file activity date) cd = clients.SelectClient(root,0,edict,1) if cd == None: if rpt: print 'No client selected' return #2. Choose files to archive (some or all of each protocol) cpath= edict['DATAPATH']+'\\'+cd['client.clientid'] lst=simpletree.RawSumFilelist(cpath) flst=simpletree.SelectedFiles(root,cpath,lst) try: gc.collect() except: pass if flst == None: if rpt: print 'No selected files' return #3. Create archive client (compressed file) if it doesn't exist busy=BusyBox.BusyBox(root,'Archiving '+cd['client.clientid']) dpath=edict['ARCHIVES']+'\\'+cd['client.clientid']+'.ZIP' tmppath=edict['ARCHIVES']+'\\'+str(os.getpid())+'.ZIP' arcpath=cd['client.clientid']+'\\' if os.path.exists(dpath): zfold=zipfile.ZipFile(dpath,'r',zipfile.ZIP_DEFLATED) oldflag=1 oldlist=zfold.namelist() copylist=[] upperlist=[] for v in oldlist: # keep list of sum/raw files already there u=v.upper() if u.find('.RAW') > 0 or u.find('.SUM') > 0: copylist.append(v) upperlist.append(os.path.normpath(edict['DATAPATH'].upper()+'\\'+u)) else: oldflag=0 zf=zipfile.ZipFile(tmppath,'w',zipfile.ZIP_DEFLATED) #4. Move selected files there #5. Make sure that basic client files exist in the archive # add client file s1=cpath+'\\client.' if os.path.exists(s1): zf.write(s1,arcpath+'client.',zipfile.ZIP_DEFLATED) else: if rpt: print 'could not find ',s1 s1=cpath+'\\'+cd['client.clientid']+'.txt' if os.path.exists(s1): zf.write(s1,arcpath+cd['client.clientid']+'.txt',zipfile.ZIP_DEFLATED) # loop through selected files subpath='' nodel=0 for i in range(len(flst)): if flst[i][1]: if flst[i][0]: continue # all the work is done by selected files!!! # copy lastset.ini and stages.ini if exist ap=os.path.split(flst[i][3]) apath=os.path.split(ap[0])[1] if apath != subpath: subpath=apath gotstages=0 # make up list of all possible stages in src stgsx=['lastset.*','stages.*','lastlayout.ini'] stgs=[] for x in stgsx: stgs.extend(glob.glob(ap[0]+'/'+x)) #print ap,flst[i][3],stgs for fn in stgs: fn=os.path.basename(fn) # just the name part tpath=arcpath+apath+'/'+fn s1=cpath+'\\'+apath+'\\'+fn if os.path.exists(s1): if fn[:6].lower() == 'stages': gotstages=1 zf.write(s1,tpath,zipfile.ZIP_DEFLATED) busy.newstatus(tpath) if oldflag: # must also remove it from list of files to copy try: k=upperlist.index(os.path.normpath(s1.upper())) del copylist[k] del upperlist[k] except: pass if gotstages == 0: oops=Pmw.MessageDialog(root,title="Archive Error",message_text="No stages file found during archive.\nPlease contact Customer Support for assistance") oops.activate() nodel=1 bname=os.path.basename(flst[i][3]) tpath=arcpath+apath+'\\'+bname zf.write(flst[i][3],tpath,zipfile.ZIP_DEFLATED) if oldflag: look=flst[i][3].upper() looker=os.path.normpath(look) try: k=upperlist.index(looker) del copylist[k] del upperlist[k] except: pass if oldflag: if len(copylist): for v in copylist: #print 'copying',v of=zfold.read(v) ofinfo=zfold.getinfo(v) zf.writestr(ofinfo,of) busy.newstatus(of) zfold.close() zf.close() if oldflag: os.remove(dpath) os.rename(tmppath,dpath) try: gc.collect() except: pass if nodel: busy.destroy() return #don't delete anything if error! #6. If all files were selected, delete current client entries after archive # loop through selected files allsel=1 for i in range(len(flst)): if flst[i][0]: # here for special subdir files if flst[i][1]: # delete entire protocol set downem=1 spath=flst[i][3]+'\\*' xtra=glob.glob(spath) for fn in xtra: try: os.remove(fn) busy.newstatus('removing '+fn) except: if rpt: print 'unable to delete ',fn downem=0 allsel=0 if downem: try: os.rmdir(flst[i][3]) clients.NewClientIndexNeeded(None,edict,0) # rebuild on next access except: allsel=0 if rpt: print 'error removing dir',flst[i][3] else: allsel=0 else: if flst[i][1]: if os.path.exists(flst[i][3]): try: os.remove(flst[i][3]) busy.newstatus('removing '+fn) except: allsel=0 if rpt: print 'error removing ',flst[i][3] if allsel: try: os.remove(cpath+'\\client.') except: allsel=0 if rpt: print 'error removing ',cpath+'\\client.' if os.path.exists(cpath+'\\'+cd['client.clientid']+'.txt'): try: os.remove(cpath+'\\'+cd['client.clientid']+'.txt') except: allsel=0 if rpt: print 'error removing notes file' if allsel: try: os.rmdir(cpath) except: pass busy.destroy()