def addProgress(self, message): if self.no_progress: self.progress = 0 return if self.gui_progress: if ClProgressDialog: if not self.progressbar: self.progressbar = ClProgressDialog() self.progressbar.setCancelButton(None) self.progressbar.adjustSize() self.progressbar.setWindowTitle("") self.progressbar.setAutoClose(False) self.progressbar.setAutoReset(False) self.progressbar.setLabelText(message.decode("utf-8")) self.progressbar.setMaximum(0) self.progressbar.setTextVisible(False) else: self.progressbar = None else: widgets = ["", "", Bar(), "", Percentage(), " ", ETA()] self.progressbar = ProgressBar(widgets=widgets, maxval=100) self.progressbar.start() self.progress = 0 self.addMessage(type="progress", id=id)
def get_Progress(client, sid, pid, id): widgets = ['','', Bar(), '', Percentage(),' ', ETA()] pbar = ProgressBar(widgets=widgets, maxval=100) # maybe do something pbar.start() """ get progress for the current job """ returnProgr = client.service.get_progress(sid, pid, id) temp_progress = -1 last_message = '' percent = returnProgr.percent while percent <= 100 and percent >= 0: if temp_progress != percent: last_message = print_progressbar(returnProgr, pbar, last_msg = last_message) # pbar.update(returnProgr.percent) if percent == 100: print return temp_progress = percent else: pbar.update(percent) time.sleep(1) returnProgr = client.service.get_progress(sid, pid, id) percent = returnProgr.percent if percent < 0: # print_progress(returnProgr, error = True) pbar.update(0)# - returnProgr.percent) pbar.finish() else: # print_progress(returnProgr) pbar.update(100)#returnProgr.percent) pbar.finish()
class Common(CommonMethods): """ class to interact with the processes """ reClearTags = re.compile("<.*?>") def __init__(self, no_progress, gui_progress, gui_warning): self.pid = 0 self.Num = 100000 self.color_print = color_print() self.no_progress = no_progress self.gui_progress = gui_progress self.gui_warning = gui_warning self.progress = None self.progressbar = None self.last_progress_msg = "" self.error = [] self.warning = [] def writeFile(self): pass def setProgress(self, perc, short_message=None, long_message=None): if self.no_progress: self.progress = perc return if perc >= 100 or perc < 0: if self.progressbar: if not self.gui_progress: self.progressbar.finish() return self.last_progress_msg = self.print_progressbar( perc, short_message, long_message, last_msg=self.last_progress_msg ) def print_progressbar(self, percent, short_message, long_message, last_msg=None, error=False): if not self.progressbar: return if long_message: if self.gui_progress: self.progressbar.setTextLabel(long_message) self.progressbar.setMaximum(100) self.progressbar.setTextVisible(True) if percent == 100: self.progressbar.setValue(99) self.progressbar.setValue(percent) else: if last_msg != long_message: cout_progress() for line in long_message.splitlines(): self.color_print.printSUCCESS(line) self.progressbar.update(percent) return long_message elif short_message: if self.gui_progress: self.progressbar.setTextLabel(short_message) self.progressbar.setMaximum(100) self.progressbar.setTextVisible(True) if percent == 100: self.progressbar.setValue(99) self.progressbar.setValue(percent) else: if last_msg != short_message: cout_progress() for line in short_message.splitlines(): self.color_print.printSUCCESS(line) self.progressbar.update(percent) return short_message else: if self.gui_progress: self.progressbar.setMaximum(100) self.progressbar.setTextVisible(True) if percent == 100: self.progressbar.setValue(99) self.progressbar.setValue(percent) else: self.progressbar.update(percent) return last_msg def setStatus(self, stat): pass def setData(self, dat): self.data_list = dat def getStatus(self): pass def getProgress(self): if self.no_progress: return self.progress if self.progressbar: if self.gui_progress: return self.progressbar.value() else: return self.progressbar.currval return 100 def addProgress(self, message): if self.no_progress: self.progress = 0 return if self.gui_progress: if ClProgressDialog: if not self.progressbar: self.progressbar = ClProgressDialog() self.progressbar.setCancelButton(None) self.progressbar.adjustSize() self.progressbar.setWindowTitle("") self.progressbar.setAutoClose(False) self.progressbar.setAutoReset(False) self.progressbar.setLabelText(message.decode("utf-8")) self.progressbar.setMaximum(0) self.progressbar.setTextVisible(False) else: self.progressbar = None else: widgets = ["", "", Bar(), "", Percentage(), " ", ETA()] self.progressbar = ProgressBar(widgets=widgets, maxval=100) self.progressbar.start() self.progress = 0 self.addMessage(type="progress", id=id) def printTable(self, table_name, head, body, fields=None, onClick=None, addAction=None): self.printSUCCESS(message=table_name) print printTable(body, head) def addMessage(self, type="normal", message=None, id=None): self.printSUCCESS(message) def printDefault(self, message=""): message = self.clearTags(message) self.color_print.defaultPrint(message + "\n") def printPre(self, message=""): for line in message.split("<br/>"): self.printDefault(line) def clearTags(self, s): """ Remove tags from text """ reColor = re.compile("color=['\"](.*?)['\"]") def fontFunc(part): attr, text = part.groups() color = reColor.search(attr) if color: color = color.group(1) if "light" in color or "dark gray" in color: bg = "1" else: bg = "0" color = color.replace("light", "").strip() colorMap = { "red": "31", "green": "32", "brown": "33", "blue": "34", "purple": "35", "cyan": "36", "gray": "37", "dark gray": "30", } if color in colorMap: fg = colorMap[color] return "\033[%s;%sm%s\033[0m" % (bg, fg, text) return text reFont = re.compile("<font (.*?)>(.*?)</font>") return self.reClearTags.sub("", reFont.sub(fontFunc, s)).replace(" ", " ") # return self.reClearTags.sub("",s) def printSUCCESS(self, message="", type=None, id=None): if message: if self.progressbar and not self.gui_progress: if not self.progressbar.finished: cout_progress() for line in message.splitlines(): self.color_print.printSUCCESS(self.clearTags(line)) return for line in message.splitlines(): self.color_print.printSUCCESS(self.clearTags(line)) def printWARNING(self, message): if self.progressbar and not self.no_progress and not self.gui_progress: if not self.progressbar.finished: cout_progress() for line in message.splitlines(): line = self.clearTags(line) self.color_print.printWARNING(line) return if message: for line in message.splitlines(): line = self.clearTags(line) self.color_print.printWARNING(line) if message: self.warning.append(message) def printERROR(self, message=""): perc = self.getProgress() if perc == 0: self.setProgress(100) elif self.getProgress() > 0: # finish progress self.setProgress(0 - self.getProgress()) else: # self.setProgress(-100) self.setProgress(perc) if message: for line in message.splitlines(): line = self.clearTags(line) self.color_print.printERROR(line) if message: self.error.append(message) def startTask(self, message, progress=False, num=1): self.printSUCCESS(message=message) if progress: self.addProgress(message) def setTaskNumber(self, number=None): pass def endTask(self, result=None, progress_message=None): if result: self.color_print.printOK(self.clearTags(result)) self.setProgress(100, progress_message) def askConfirm(self, message): colorPrint = color_print() print while True: try: ask = raw_input(message + "? (yes/no): ") except KeyboardInterrupt: ask = "no" print if ask.lower() in ["n", "no"]: colorPrint.printERROR(_("Manually interrupted")) return "no" if ask.lower() in ["y", "yes"]: return "yes" def isInteractive(self): """ Check interactive ability """ return sys.stdin.isatty() def askChoice(self, message, answers=[("yes", "Yes"), ("no", "No")]): colorPrint = color_print() print # ability answer by first letter FIRSTLETTER = 0 VALUE, COMMENT = 0, 1 answerByChar = map(lambda x: x[VALUE][FIRSTLETTER], answers) if filter(lambda x: answerByChar.count(x) > 1, answerByChar): useGetChar = False else: useGetChar = True while True: outMessage = message + " (%s): " % ( "/".join(map(lambda x: "%s[%s]" % (x[COMMENT], x[VALUE][FIRSTLETTER]), answers)) ) if useGetChar: colorPrint.defaultPrint(outMessage) ask = getch() print else: try: ask = raw_input(outMessage) except KeyboardInterrupt: ask = answers[-1][COMMENT] print ask = ask.lower() likeAnswers = filter(lambda x: x[VALUE].startswith(ask), answers) if not likeAnswers: colorPrint.printERROR(_("The answer is not understood")) continue if len(likeAnswers) == 1: return likeAnswers[VALUE][FIRSTLETTER] else: colorPrint.printERROR(_("Ambiguous answer:") + ",".join(map(lambda x: x[COMMENT], likeAnswers))) def askQuestion(self, message): print return raw_input(message + ":") def askPassword(self, message, twice=False): from calculate.lib.utils.common import getpass if os.readlink("/proc/self/fd/0") == "/dev/console": os.system("chvt 1 &>/dev/null") text1 = "%s:" % message if not twice: return getpass.getpass(text1) if twice: text2 = _("Repeat: ") pass1 = "password" pass2 = "repeat" try: while pass1 != pass2: pass1 = getpass.getpass(text1) pass2 = getpass.getpass(text2) if pass1 != pass2: print _("Passwords do not match") except KeyboardInterrupt: return None passwd = pass1 if (pass1 and pass1 == pass2) else None return passwd def beginFrame(self, message=None): self.printSUCCESS(message=message) def endFrame(self): self.printSUCCESS(type="endFrame") if self.gui_progress and self.progressbar: self.progressbar.quit() if self.warning and not self.error and self.gui_warning: ClMessageBox().warning(("\n".join(self.warning)).decode("utf-8")) if self.error and self.gui_progress: if self.warning and self.gui_warning: error = "\n".join(self.warning + self.error) else: error = "\n".join(self.error) ClMessageBox().critical(error.decode("utf-8")) def startGroup(self, message): self.printSUCCESS(type="startGroup", message=message) def endGruop(self): pass def briefParams(self, view_name): pass