Rewrite processProgress object.

master3.3
Mike Hiretsky 12 years ago
parent e76688afca
commit 8de1736bfd

@ -307,68 +307,69 @@ class scanDirectory:
return ret
class processProgress(process):
"""Execute system command by Popen for parse stdout."""
"""
Generator like process progress
"""
def __init__(self,command,*params,**kwarg):
process.__init__(self,command,*params,**kwarg)
self.readsize = kwarg.get("readsize",10)
self.keepFilter = kwarg.get("keepFilter",lambda x:True)
self.maxLines = kwarg.get("maxLines",20)
self.init(**kwarg)
def init(self,**kwarg):
def init(self,*args,**kwarg):
pass
def read(self):
"""Read data with parsing ability"""
def processInit(self):
pass
def processEnd(self):
pass
def processString(self,strdata):
self.cacheresult.append(strdata)
return strdata
def processBuffer(self,buf):
pass
def progress(self):
try:
self.processInit()
res = self.processInit()
if res != None: yield res
self._open()
if self.cacheresult is None:
self.cacheresult = []
self.buf = ""
part = self.pipe.stdout.read(1)
localbuf = ""
while part:
if self.buf:
self.buf += part
else:
self.buf = part
localbuf += part
while "\n" in localbuf:
line,devnull,localbuf = localbuf.partition('\n')
if self.keepFilter(line):
self.cacheresult.append(line)
if len(self.cacheresult) > self.maxLines:
self.cacheresult.pop(0)
if self.processStdout():
self.processDraw()
res = self.processBuffer(part)
if res:
yield res
while "\n" in self.buf:
strdata, op, self.buf = self.buf.partition('\n')
res = self.processString(strdata)
if res:
yield res
part = self.pipe.stdout.read(self.readsize)
self.pipe.poll()
self.processEnd(self.success())
self.cacheresult = "\n".join(self.cacheresult)
self.pipe.wait()
self.pipe.poll()
res = self.processEnd()
if res != None: yield res
except KeyboardInterrupt:
self.cacheresult = "\n".join(self.cacheresult)
self.pipe.kill()
self.processEnd(False)
raise KeyboardInterrupt()
return self.cacheresult
def processInit(self):
"""Called when read first byte"""
pass
raise
finally:
self.cacheresult = "\n".join(self.cacheresult)
def processDraw(self):
"""Called when processStdout return True"""
pass
def countFiles(dirpath):
num = 1
for dirpath,dirnames,filenames in os.walk(dirpath):
num += len(set(dirnames) | set(filenames))
return num
def processStdout(self):
"""Called when read readsize byte from stdout"""
return True
def processEnd(self,res=True):
"""Called when process end"""
pass
def runOsCommand(cmd,in_str=None, env_dict=None):
"""Run system command

Loading…
Cancel
Save