# -*- coding: utf-8 -*- # # by Michel Claveau Informatique # http://mclaveau.com # from __future__ import with_statement GlobalPVersion='0.61' import time,os,os.path,sys,types import ponx class the(object): def __init__(self): """ import win32com.client self.pv = win32com.client.Dispatch('Ponxv.Mci') self.pd = win32com.client.Dispatch('Ponxd.Mci') self.px = win32com.client.Dispatch('Ponx.Mci') """ self.px=ponx.ponx() self.g = None #import perthe #self.perthe=perthe.the(self) #execfile('D:\\dev\\python\\the\\perthe.py',globals(),globals()) #ponx.msginfo("35",ponx.ponxdir()+'\\the\\perthe.py') execfile(ponx.ponxdir()+'\\the\\perthe.py',globals(),globals()) self.perthe=the(self) self.text = "" self.filenamecurrent = "" self.scriptdir = "" self.workdir = "" self.macro=[] self.gmacro=None self.pcherche=[] self.chainecherche="" self.template={} self.tclefs={} def setscriptdir(self, scriptdir="D:\\dev\\autoit\\the"): self.scriptdir = str(scriptdir) #ponx.msginfo("55",scriptdir) def safter(txt,debut): try: vret= txt[len(debut):] except: vret="" return vret self.template={} self.tclefs={} source="" ligs=iter(open(self.scriptdir+'\\the.tmpl.txt','rb').readlines()) while True: try: lig=ligs.next() except: break if lig.startswith('@@@'): if source!="": self.template[tnom]=source for clef in tclefs: if clef in self.tclefs: self.tclefs[clef].append(tnom) else: self.tclefs[clef]=[tnom] #print tnom #print tclefs #print source #print "-"*55 source="" if lig.startswith('@@@nom:'): tnom = safter(lig,'@@@nom:').strip() elif lig.startswith('@@@classement:'): temp=safter(lig,'@@@classement:') source="" tclefs=[ clef.strip() for clef in temp.split(',')] else: source = source+lig """ for t in self.template: print '-'*55 print t print self.template[t] print "*"*66 for c in self.tclefs: print c,self.tclefs[c] """ def setworkdir(self, workdir="D:\\dev\\autoit\\the"): self.workdir = str(workdir) #ponx.msginfo("110",self.workdir) def gettemplateclefs(self): #retourne la liste des clefs des templates return self.tclefs.keys() def gettemplateclef(self,clef): #retourne la liste des templates d'une clef return self.tclefs[clef] def gettemplate(self,nom,seldeb,selfin): """Retourne un template (source), après remplacement de la sélection s'il y a lieu """ temp=self.template[nom] #ponx.msginfo(str(seldeb),str(selfin)) selection="" if selfin>seldeb: selection=self.text[seldeb:selfin] #ponx.msginfo("122",str(selection)) vret=temp.replace('--SELECTION--',selection) #ponx.msginfo("124",vret) return vret def version(self): return 'agui '+GlobalPVersion def u1252(self, chu): try: if type(chu) is unicode: return chu.encode('cp1252','replace') else: return chu except: return repr(chu) def alert(self,txt1="TITRE",txt2="Texte."): ponx.msginfo(txt1,txt2) def sendtxt(self,txt=""): try: self.text=txt.replace('\n','\r\n') except: self.text=str(txt) def gettxt(self): return self.text def setcurrentfile(self,filename=''): if filename!='': self.filenamecurrent=filename def openfile(self,filename): if os.path.exists(filename): txt=open(filename,"rb").read() else: txt="" self.text=txt self.filenamecurrent=filename return(self.text) def openfile1252(self,filename): if os.path.exists(filename): txt=open(filename,"rb").read() else: txt="" self.text=txt.decode('cp1252','replace').encode('utf-8','replace') self.filenamecurrent=filename return(self.text) def savefile(self,filename='',prefixsupprimer=''): if filename=='': filename=self.filenamecurrent txt=self.text if prefixsupprimer != '': prefixsupprimer = prefixsupprimer.replace("\\","/") txt = txt.replace("file:///"+prefixsupprimer,"") #ponx.msginfo("file:///"+prefixsupprimer,txt) txt = txt.replace("the.htm","") #ponx.msginfo("the.htm",txt) open(filename,"wb").write(txt.encode('cp1252','replace')) self.filenamecurrent=filename return(self.text) def openmacro(self,filename): self.macro=[] if os.path.exists(filename): def tparam(ch): s=ch t=s.replace('","','|') lt=t.split(",") lret=[ l.replace('|',',') for l in lt] return lret lmac=open(filename,"rb").readlines() itermac=iter(lmac) while True: try: l=itermac.next()[:-2] if l<>"": while l[-1]=="_": l = l[:-1] +'\r\n'+ itermac.next()[:-2] except: break if l.startswith("#") or l==(" "*len(l)): pass else: self.macro.append(tparam(l)) self.gmacro=iter(self.macro) return(len(self.macro)) def nextligmacro(self): try: return(self.gmacro.next(),) except: self.gmacro=iter(self.macro) return(['END'],) def lanceurconsole(self,typelancement="POSTMORTEM", reptravail=''): """Lance un script Python dans une console orpheline faisant parler les morts. 'orpheline': qui n'a pas de parent (pour une console indépendante) 'faire parler les morts': avec déboguage 'post mortem' """ if reptravail=='': if self.workdir!='': reptravail=self.workdir fichierlong=self.filenamecurrent pathcomplet=os.path.abspath(fichierlong) cdir=os.path.dirname(pathcomplet).replace('\\','\\\\') os.chdir(cdir) #print "typelancement",typelancement #os.system('start "" cmd /kthe_lanceur.py '+typelancement+' '+fichierlong) if reptravail=='': os.system('start "'+fichierlong+'" cmd /k'+self.scriptdir+'\\the_lanceur.py '+typelancement+' '+fichierlong) else: os.system('start "'+fichierlong+'" /D '+reptravail+' /I cmd /k'+self.scriptdir+'\\the_lanceur.py '+typelancement+' '+fichierlong) #os.system('start "'+fichierlong+'" /I cmd /k'+self.scriptdir+'\\z_lanceur.py '+typelancement+' '+fichierlong) def runconsole(self, reptravail=''): """Lance un script Python dans une console orpheline , SANS lanceur 'orpheline': qui n'a pas de parent (pour une console indépendante) """ if reptravail=='': if self.workdir!='': reptravail=self.workdir fichierlong=self.filenamecurrent pathcomplet=os.path.abspath(fichierlong) cdir=os.path.dirname(pathcomplet).replace('\\','\\\\') os.chdir(cdir) if reptravail=='': os.system('start "'+fichierlong+'" cmd /k'+fichierlong) else: os.system('start "'+fichierlong+'" /D '+reptravail+' /I cmd /k'+fichierlong) #os.system('start "'+fichierlong+'" /I cmd /k'+fichierlong) def lanceur(self, reptravail=''): """Lance un script Python en récupérant le résultat. Attention : impossible de faire de l'interactif par ce biais. """ if reptravail=='': if self.workdir!='': reptravail=self.workdir fichierlong=self.filenamecurrent pathcomplet=os.path.abspath(fichierlong) cdir=os.path.dirname(pathcomplet).replace('\\','\\\\') exdir= self.scriptdir.replace('\\','\\\\') r = self.px.PRun('cmdpp4(["cmd /c'+exdir+'\\\\the_lanceur.py normal '+fichierlong+'",],"'+reptravail+'")') #vret=r.encode('cp1252','replace') vret=r return vret def search(self,rtxt=u"",pos=0): """ recherche une chaîne à partir de la position indiquée (ou courante par défaut) la recherche respecte la casse """ self.pcherche=[] rtxt=rtxt.decode('cp1252','replace') #ponx.msginfo(str(type(rtxt)),rtxt) self.chainecherche=rtxt.encode('utf-8','replace') if pos<1: vret=self.text.find(self.chainecherche) else: vret=self.text.find(self.chainecherche,pos) self.pcherche.append(vret) return vret def searchnext(self): """ recherche l'occurence suivante """ if len(self.pcherche)<1: return self.search(self.chainecherche) else: derposcherche=self.pcherche[-1] vret=self.text.find(self.chainecherche,derposcherche+1) self.pcherche.append(vret) return vret def searchprior(self): """ recherche l'occurence suivante """ if len(self.pcherche)>0: self.pcherche.pop() if len(self.pcherche)>0: derposcherche=self.pcherche[-1] else: derposcherche=-1 else: derposcherche=-1 return derposcherche def searchinfiles(self,dir,pattern,cherche): lret=ponx.findfinfiles(dir,pattern,cherche) return lret def pyperso(self,*param): #ponx.msginfo("142",str(param)) if param[0]=='F5': if self.g: vret = self.g.send(param) if vret[0]=='END': self.g=None return(vret,) else: vret=self.perthe.f5(*param) if type(vret) in (types.GeneratorType,): self.g=vret vret = self.g.next() if vret[0]=='END': self.g=None return(vret,) else: self.g=None if vret is None: vret=['END'] if type(vret) in (types.ListType,types.TupleType): return(vret,) else: return((vret),) elif param[0]=='F6': if self.g: vret = self.g.send(param) if vret[0]=='END': self.g=None return(vret,) else: vret=self.perthe.f6(*param) if type(vret) in (types.GeneratorType,): self.g=vret vret = self.g.next() if vret[0]=='END': self.g=None return(vret,) else: self.g=None if vret is None: vret=['END'] if type(vret) in (types.ListType,types.TupleType): return(vret,) else: return((vret),) elif param[0]=='F7': if self.g: vret = self.g.send(param) if vret[0]=='END': self.g=None return(vret,) else: vret=self.perthe.f7(*param) if type(vret) in (types.GeneratorType,): self.g=vret vret = self.g.next() if vret[0]=='END': self.g=None return(vret,) else: self.g=None if vret is None: vret=['END'] if type(vret) in (types.ListType,types.TupleType): return(vret,) else: return((vret),) elif param[0]=='F8': if self.g: vret = self.g.send(param) if vret[0]=='END': self.g=None return(vret,) else: vret=self.perthe.f8(*param) if type(vret) in (types.GeneratorType,): self.g=vret vret = self.g.next() if vret[0]=='END': self.g=None return(vret,) else: self.g=None if vret is None: vret=['END'] if type(vret) in (types.ListType,types.TupleType): return(vret,) else: return((vret),) elif param[0]=='OPENFILE': if self.g: vret = self.g.send(param) if vret[0]=='END' or vret[0]=='ABANDON': self.g=None return(vret,) else: vret=self.perthe.openfile(*param) if type(vret) in (types.GeneratorType,): self.g=vret vret = self.g.next() if vret[0]=='END' or vret[0]=='ABANDON': self.g=None return(vret,) else: self.g=None if vret is None: vret=['END'] if type(vret) in (types.ListType,types.TupleType): return(vret,) else: return((vret),) elif param[0]=='SAVEFILE': if self.g: vret = self.g.send(param) if vret[0]=='END' or vret[0]=='ABANDON': self.g=None return(vret,) else: vret=self.perthe.savefile(*param) if type(vret) in (types.GeneratorType,): self.g=vret vret = self.g.next() if vret[0]=='END' or vret[0]=='ABANDON': self.g=None return(vret,) else: self.g=None if vret is None: vret=['END'] if type(vret) in (types.ListType,types.TupleType): return(vret,) else: return((vret),) else: vret=['END'] self.g=None return(vret,)