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 __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 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"
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import sys, os import argparse import re from calculate.lib.utils.common import getpass from calculate.lib.cl_print import color_print from calculate.lib.cl_lang import setLocalTranslate setLocalTranslate('cl_core3',sys.modules[__name__]) from itertools import * colorPrint = color_print() class BoolAction(argparse.Action): reTrue = re.compile("^(?:on)$",re.I) reFalse = re.compile("^(?:off)$",re.I) def __init__(self, option_strings, dest, nargs="?", const=None, default=None, type=None, choices=None, required=False, help=None,
def print_brief_group(Fields, group_name): print_group_flag = False # if group_name: # _print ('\b'+group_name) uncompatible_count = 0 colorPrint = color_print() for field in Fields: if field.uncompatible: uncompatible_count += 1 continue if field.element in ["input", "openfile"]: value = field.value if field.value else "" if not print_group_flag: if group_name: print_group_flag = True _print("\b" + group_name) colorPrint.printSUCCESS("%s: %s" % (field.label, value)) elif field.element in ["combo", "comboEdit", "radio", "file"]: if field.choice: if not field.choice[0]: field.choice.pop(0) if field.comments: if not field.comments[0]: field.comments.pop(0) if field.comments and field.choice: if not field.value in field.choice: field.choice.append(field.value) value = map( lambda x: field.comments[x] if len(field.comments) > x else field.choice[x], map(lambda x: field.choice.index(x), filter(lambda x: x in field.choice, [field.value])), ) value = ", ".join(value) else: value = field.value if field.value else "" if not print_group_flag: if group_name: print_group_flag = True _print("\b" + group_name) colorPrint.printSUCCESS("%s: %s" % (field.label, value)) elif field.element in ["multichoice", "multichoice_add", "selecttable", "selecttable_add"]: if field.choice: if not field.choice[0]: field.choice.pop(0) if field.comments: if not field.comments[0]: field.comments.pop(0) if field.listvalue: if not field.listvalue[0]: field.listvalue.pop(0) if field.choice: value = map( lambda x: field.comments[x] if len(field.comments) > x else field.choice[x], map(lambda x: field.choice.index(x), field.listvalue), ) else: value = [] value = ", ".join(value) if field.listvalue and not value: value = ", ".join(field.listvalue) elif not value: value = field.value if field.value else "" if not print_group_flag: if group_name: print_group_flag = True _print("\b" + group_name) colorPrint.printSUCCESS("%s: %s" % (field.label, value)) # elif field.element == 'label': # print field.label elif field.element == "error": if not print_group_flag: if group_name: print_group_flag = True _print("\b" + group_name) colorPrint.printERROR(field.label) elif field.element in ["check", "check_tristate"]: if field.value == "on": value = _("yes") elif field.value == "off": value = _("no") elif field.value == "auto": value = _("auto") else: value = field.value if not print_group_flag: if group_name: print_group_flag = True _print("\b" + group_name) colorPrint.printSUCCESS("%s: %s" % (field.label, value)) elif field.element == "table" and field.type != "steps": head = field.tablevalue.head body = [] for row in field.tablevalue.body: if not row[0]: row.pop(0) body.append(row) # if empty table if not filter(None, map(lambda x: x, body)): body = [[""] * len(head)] res = printTable(body, head) sys.stdout.flush() sys.stdout.write(res + "\n") continue ChoiceValue = field.tablevalue.values for row in xrange(len(ChoiceValue)): if ChoiceValue[row].typefield in ["check", "check_tristate"]: for i in xrange(len(body)): if body[i][row] == "on": body[i][row] = _("yes") if body[i][row] == "off": body[i][row] = _("no") if body[i][row] == "auto": body[i][row] = _("auto") if "password" in ChoiceValue[row].typefield: for i in xrange(len(body)): if body[i][row]: body[i][row] = "***" data = [] for body_row in body: data.append(map(lambda x: x if x else "", body_row)) if not print_group_flag: if group_name: print_group_flag = True _print("\b" + group_name) colorPrint.printSUCCESS(field.label + ": ") res = printTable(data, head) sys.stdout.flush() sys.stdout.write(res + "\n") else: uncompatible_count += 1
def local_method(metaclass, args, unknown_args): """ Call method from metaclass, check method existing. Generate help, for method, run method by 'call_method'. """ import os sym_link = os.path.basename(sys.argv[0]) if sym_link != "cl-core": if sym_link in metaclass.Dec.conMethods.keys(): args.method = metaclass.Dec.conMethods[sym_link][0] else: _print(_("Method not found for %s") % sym_link) sys.exit(1) if args.list_methods: keys = metaclass.Dec.conMethods.keys() keys.sort() for key in keys: print metaclass.Dec.conMethods[key][0], " - ", metaclass.Dec.conMethods[key][2] return 0 colorPrint = color_print() metaObject = metaclass() method_name = args.method method_view_name = method_name + "_view" if args.method and args.help: view_obj = ViewInfo() view_obj.step = None view_obj.expert = True view_obj.brief = None view_obj.onlyhelp = True try: view = getattr(metaObject, method_view_name)(0, view_obj) except AttributeError: colorPrint.printERROR(_("Method not found: ") + method_view_name) return 1 try: method_parser = get_method_argparser(view, args, cl_core=True) except Exception as e: import traceback for i in apply(traceback.format_exception, sys.exc_info()): sys.stderr.write(i) sys.stderr.flush() metaObject.clear_cache(0, method_name) return 1 method_parser.print_help() else: try: call_method(metaObject, args, unknown_args, colorPrint) metaObject.clear_cache(0, method_name) return metaObject.method_status except (ValueError) as e: colorPrint.printERROR(str(e)) # colorPrint.printERROR(shortTraceback(*sys.exc_info())) except (KeyboardInterrupt, EOFError): colorPrint.printERROR(_("Manually interrupted")) except Exception as e: colorPrint.printERROR(shortTraceback(*sys.exc_info())) pass # print 'Error: ', e metaObject.clear_cache(0, method_name)