apple / altEdit.py
t5
apple
8cd12eb
# -*- coding: utf-8 -*-
##import sys;sys.path.append("N:\\afilen\\program\\python\\Mjmoji");from wr import *
import sys
import time
from ctypes import *
import subprocess as p
from wr import *
def scroll(*ww):
import edit_
#
#
def ctockai( text ):
import os
logfile=[fr"{tfolder}/1.txt",fr"{tfolder}/2er.txt"]
## for f in logfile: os.path.exists(f) and os.remove(f)
ip = p.Popen( ['cmd'], stdin=p.PIPE, shell=True )
#text="%s > %s 2>&1 \n"%(text,erfile)#write
text="{} 1> {} 2> {} \n".format(text,*logfile)#write
## orror(text);
ip.stdin.write( text.encode("cp932"))
shuttown(ip.stdin)
retcode = ip.wait()
#
lir=list(open(f,"rb").read() for f in logfile)
## lir=[te.replace(b"\r\r\n",b"\n").replace(b"\r\n",b"\n").decode("mbcs") for te in lir]
lir=[te.replace(b"\r\r\n",b"\n").replace(b"\r\n",b"\n").decode("utf8") for te in lir]
return (retcode,lir[0],lir[1])
def ctod( text ,sh=True,cp="cp932",si=None):
ip = p.Popen( ['cmd']if(sh)else text, stdin=p.PIPE, stdout=p.PIPE,stderr=p.PIPE,shell=sh )
if 0:#no
ip.stdin.write("echo 1\n".encode())
for x in iter(ip.stdout.readline, b''):x
if 0:#no
ip.stdin.write("chcp 65001\n".encode("cp932"))
ip.stdin.flush()
ip.stdout.flush()
print(ip.stdout.read());
if(sh):
text+="\n";
ip.stdin.write( text.encode("cp932"))
shuttown(ip.stdin)
elif(si):
ip.stdin.write(si)
shuttown(ip.stdin)
retcode=None
try:
## retcode = ip.wait(timeout=0)
retcode = ip.wait()
res=(ip.stdout.read()[96:],
ip.stderr.read())
## for f in range(212): er=ip.stdout.read(2)
except p.TimeoutExpired as e:
## return ip.stdout.readline(21)
res=(ip.stdout.read()[96:],
ip.stderr.read())
fds
## cp="utf8"
## try:
## return retcode,r.decode(cp),er.decode(cp),ip
## except:
## cp="cp932"
## return retcode,r.decode(cp),er.decode(cp),ip
res=[te.replace(b"\r\r\n",b"\n").replace(b"\r\n",b"\n") for te in res]
from wr import vcs
res=[vcs(te) for te in res]
return retcode,*res,ip
def Runw(li): ctod(li,sh=False)
##(ctockai(jw(" ",["node",r"C:\v\P\electron\electron-quick-start\or1.js"])))
##print(ctockai("dir ."))
##print(ctod(jw(" ",["node",r"C:\v\P\electron\electron-quick-start\or1.js"])))
import os
import th
class cltof():
def __init__(zl):
#zl.mutex=fwe
zl.ev=th.Event()
zl.lock=th.Lock()
os.environ['PYTHONIOENCODING'] = 'utf-8'
def ctof(zl,cb,to, text ,null=""):
from wr import vcs, tp, wfd
zl.ev.clear()
if 0==to:to=3600*12
time0=time.perf_counter()
def fppc():
return time.perf_counter()-time0
def fpin(cb,text):
date=tp()
if isinstance(text,(list,tuple)): c=os.path.basename((text[0]))
elif isinstance(text,(str)): c= "C"
logfile=zl.logfile=[fr"{tfolder}/ctof_{c}_{x}_{date}.txt" for x in ("1","2")]
## print(text,cb)
for f in logfile: os.path.exists(f) and os.remove(f)
## ip = p.Popen( [("echo ff"),"1>",logfile[0],"2>",logfile[1]], shell=True )
if isinstance(text,(list,tuple)): te= [*text,"1>",logfile[0],"2>",logfile[1]]
elif isinstance(text,(str)):te= text+" 1> {} 2> {}".format(*logfile)
if not null or 1:
zl.ip=ip=p.Popen(text,shell=True, stdout=open(logfile[0],"w"), stderr=open(logfile[1],"w"), stdin=p.PIPE)
#
while any([False==os.path.exists(f) for f in logfile]):
print("not ready;",[os.path.exists(f) for f in logfile],te)
sleep(0.1)
while 1:
## try:
#with open(logfile[0],"rb") as fo:
lifo=[open(f,"rb") for f in logfile]
while 1:
poll=zl.poll=ip.poll()
for x,fo in enumerate(lifo):
bit=fo.read()
## print("p;",poll,bit)#lwt
if bit and cb: cb(x+1, vcs(bit))
if poll!=None: break
sleep(0.2)
if poll!=None: break
if to<(d:=fppc()):return
## print(d)
## except 2: f
if ip.poll()!=None:break
if to<(d:=fppc()):break
def fpin1(*w):
cb,*_=w
try:
res=fpin(*w)
## except Exception as e:
## wfd("r:\\logEdit.txt").t(str(e))
finally:
zl.ev.set()
return res
t=th.run(fpin1,(cb,text))
return t
@property
def te(zl):
from wr import vcs
if 1:
logfile=zl.logfile
lir=list(open(f,"rb").read() for f in logfile)
## lir=[te.replace(b"\r\r\n",b"\n").replace(b"\r\n",b"\n").decode("mbcs") for te in lir]
## print(lir[0].decode("cp932"),poll)
lir=[vcs(te.replace(b"\r\r\n",b"\n").replace(b"\r\n",b"\n"))for te in lir]
return (zl.poll,lir[0],lir[1])
@property
def wa(zl):
try:zl.ev.wait()
except KeyboardInterrupt as e:e
return zl
@staticmethod
def cbpr(std,b): print(f"{std} cblen({len(b)}) {b}")
def put(p,te):
p.ip.stdin.write(te.encode())
p.ip.stdin.flush()
def ctof(*w):
p=cltof()
p.ctof(*w)
return p
##def watofin(*w):
## p=ctof(*w).wa
## return p
##def returnwat():
def watof(*w):
p=ctof(*w)
te=p.wa.te
return te[1]+"\n"+te[2]
def tofu(*w):
return ctof(*w).wa.te
import subprocess, shlex
from threading import Timer
def run(cmd, timeout_sec):
proc = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
kill_proc = lambda p: p.kill()
timer = Timer(timeout_sec, kill_proc, [proc])
try:
timer.start()
stdout,stderr = proc.communicate()
finally:
timer.cancel()
#
import time
import signal,os
def reloadFull(timeout=2.2, me=[sys.argv[0],"-r"]):
ip=p.Popen(me,shell=True)
try:
ri=ip.wait(timeout=timeout)#idea
print("eof. continue")
except subprocess.TimeoutExpired as e:
print("reload",e)
sys.exit(0)
def r1reloadFull(dure,me):
def handler(signum, frame):
print("reload,handler",signum)
def handlerth(signum, frame):
print("reload",signum)
## sys.exit(0)#no
os.kill(os.getpid(),signal.SIGINT)
## os.kill(os.getpid(),signal.SIGTERM)#ideaLk
signal.signal(signal.SIGTERM, handler)
signal.signal(signal.SIGINT, handler)
timer=Timer(dure-0.2,handlerth,args=[1,2])
#
try:
timer.start()
ip=p.Popen( me,bufsize=2, stdin=p.PIPE, stdout=p.PIPE,stderr=p.PIPE,shell=True )
while ip.poll() is None:
sleep(0.2)
print("while_poll",ip.poll())
pass
## print(ip.stdout.read())
print("poll2",ip.poll())
try:
## ri=ip.wait(timeout=dure)#idea
ri=ip.wait()
except subprocess.TimeoutExpired as e:
print(e)
## sys.exit(0)
## ri
finally:
timer.cancel()
if __name__ == '__main__':
import edit_
acs()
fil=sys.argv
c1=fil[1]if(len(fil)>=2)else None
def reheasal():
import multiprocessing as mlti
sys.stdin.conn
if(c1=="-out"):
for j in fil[2]:
sleep(0.3)
sys.stdout.write(j)
if(c1=="-c"):
for f in ":苍天\n:":
print(f)
## sys.stdout.write(f)
if(c1=="-e"): fwf
if(c1=="-lf"):
for f in "123\n234":
print(f)
print(" start ",end="")
## input("pw")
sys.stdout.flush()
print("", flush=True)#,file=stdout
sleep(1)
if not c1:
print("c1;")
## reloadFull([r"dir"])
## reloadFull(2,[r"N:\afilen\program\python\work\ClipArg.py"] + args)
## r1reloadFull(5,[r"N:\afilen\program\python\qt\ClipProcess2.pyw"] + args)
## r1reloadFull(5,[fil[0],"-out", jw(".",[str(j)for j in list(range(2))])] )
## ctof(__file__,"-out","21212")
## print(";;",ctof(f'{__file__} -out 21\n\n\n'))
#? print(";;",ctof(f'{__file__}',"-out", '"21\n21"'))
## print(";;",ctof(f'{__file__}',"-out", "21"))
#lwt
def cb(std,b): print(f"{std} cblen({len(b)}) {b}")
## print(";",ctof(cb,'ffmpeg',"-y","-i",r"r:\\p9.mkv","r:\\fw.mp4"))
## print(";;",ctof(f'{__file__}',"-c",''))
## print(";;",ctof(f'{__file__}',"-e"))
## print(";;",watof(cb,10,(f'{__file__}',"-lf")))
print(";;",watof(cb,10,(f'{__file__}'+" -lf")))
## print(";;",watof(f'{__file__}',"-out",'21212苍天\nf'))#?