Merge branch 'master' of git.calculate.ru:/calculate-lib

develop
Самоукин Алексей 14 years ago
commit 22567b7b74

@ -64,7 +64,10 @@ def printERROR(errMessage):
errMessage = str(errMessage)
errMessage += "\n"
sys.stderr.write(errMessage)
sys.stderr.flush()
try:
sys.stderr.flush()
except:
exit(1)
def printSUCCESS(message,printBR=True):
"""Вывод сообщения о успехе"""
@ -74,4 +77,7 @@ def printSUCCESS(message,printBR=True):
if printBR:
message += "\n"
sys.stdout.write(message)
sys.stdout.flush()
try:
sys.stdout.flush()
except IOError:
exit(1)

@ -17,6 +17,7 @@
import sys, struct, termios, fcntl
from cl_utils import _toUNICODE
import cl_overriding
class color_print(object):
_printSysOut = sys.stdout
@ -115,7 +116,10 @@ class color_print(object):
def defaultPrint(self, string):
self._printSysOut.write(string)
self._printSysOut.flush()
try:
self._printSysOut.flush()
except IOError:
cl_overriding.exit(1)
def printLine(self, argL, argR, offsetL=0, printBR=True):
"""Печатает справа и слева консоли цветные сообщения"""
@ -156,7 +160,10 @@ class color_print(object):
colorDict[''](rightString)
if printBR:
self._printSysOut.write("\n")
self._printSysOut.flush()
try:
self._printSysOut.flush()
except IOError:
cl_overriding.exit(1)
def printNotOK(self, string, offsetL=0, printBR=True):
"""Вывод на печать в случае сбоя"""

@ -232,10 +232,14 @@ class process:
def read(self):
"""Read all data"""
self._open()
if self.cacheresult is None:
self.cacheresult = self.pipe.communicate()[0]
return self.cacheresult
try:
self._open()
if self.cacheresult is None:
self.cacheresult = self.pipe.communicate()[0]
return self.cacheresult
except KeyboardInterrupt:
self.kill()
raise KeyboardInterrupt
def readlines(self):
"""Read lines"""
@ -247,15 +251,18 @@ class process:
self.iter = iter(self.readlines())
return self.iter
def kill(self):
"""Kill this process"""
if self.pipe:
self.pipe.kill()
def next(self):
"""Next string from stdout"""
return self.__iter__().next()
def returncode(self):
"""Get return code"""
self._open()
if self.pipe.returncode is None:
self.cacheresult = self.pipe.communicate()[0]
self.read()
return self.pipe.returncode
def success(self):
@ -266,6 +273,61 @@ class process:
"""Failed or not"""
return self.returncode() != 0
class processProgress(process):
"""Execute system command by Popen for parse stdout."""
def __init__(self,command,*params,**kwarg):
process.__init__(self,command,*params,**kwarg)
self.readsize = kwarg.get("readsize",10)
self.init(**kwarg)
def init(self,**kwarg):
pass
def read(self):
"""Read data with parsing ability"""
try:
self.processInit()
self._open()
if self.cacheresult is None:
self.cacheresult = []
self.buf = ""
part = self.pipe.stdout.read(1)
while part:
if self.buf:
self.buf += part
else:
self.buf = part
if self.processStdout():
self.processDraw()
self.cacheresult.append(part)
part = self.pipe.stdout.read(self.readsize)
self.pipe.poll()
self.processEnd(self.success())
except KeyboardInterrupt:
self.cacheresult = "".join(self.cacheresult)
self.pipe.kill()
self.processEnd(False)
raise KeyboardInterrupt()
self.cacheresult = "".join(self.cacheresult)
return self.cacheresult
def processInit(self):
"""Called when read first byte"""
pass
def processDraw(self):
"""Called when processStdout return True"""
pass
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):
"""Выполняет внешнюю программу

Loading…
Cancel
Save