Exemplo n.º 1
0
def call_method(metaObject, args, unknown_args, colorPrint):
    """
    Function for call method through metaObject and args
    """
    method_name = args.method
    stdin_passwd = args.stdin_passwd
    method_view_name = method_name + "_view"
    metaObject.no_progress = args.no_progress
    metaObject.gui_progress = args.gui_progress
    metaObject.gui_warning = args.gui_warning
    view_obj = ViewInfo()
    view_obj.step = None
    view_obj.expert = True
    view_obj.brief = None
    view_obj.onlyhelp = False

    try:
        view = getattr(metaObject, method_view_name)(0, view_obj)
    except AttributeError:
        colorPrint.printERROR(_("Method not found: ") + method_name)
        return None
    method_parser = get_method_argparser(view, args, cl_core=True)
    param_object = create_param_object(view)
    try:
        unknown_args = method_parser.fixBoolVariables(unknown_args)
        args, unknown_args = method_parser.parse_known_args(unknown_args)
        no_questions = args.no_questions
    except SystemExit as e:
        return 1
    except Exception as e:
        import traceback

        for i in apply(traceback.format_exception, sys.exc_info()):
            sys.stderr.write(i)
        sys.stderr.flush()
        raise
    for i in unknown_args:
        if i.startswith("-"):
            if i in parse(True).parse_known_args()[1]:
                _print(_("Unknown parameter"), i)
                return 1
        else:
            _print(_("Unknown argument"), i)
            return 1

    param_object, steps = collect_object(None, param_object, view, args, stdin_passwd=stdin_passwd)
    if view.has_brief:
        setattr(param_object, "CheckOnly", True)
        check_res = {}
        while True:
            method_result = getattr(metaObject, method_name)(0, param_object)
            if not method_result:
                print _("Method not available")
                return None

            if method_result[0].type and method_result[0].type != "pid":
                check_res = check_result_msg(method_result, view, check_res)
                if not check_res:
                    return None
                else:
                    param_object = get_param_pwd(check_res, view, param_object, stdin_passwd=stdin_passwd)
            else:
                break

        view_obj = ViewInfo()
        view_obj.step = None
        view_obj.expert = True
        view_obj.brief = True
        view_obj.onlyhelp = False
        try:
            view = getattr(metaObject, method_view_name)(0, view_obj)
        except AttributeError:
            colorPrint.printERROR(_("Method not found: ") + method_name)

        print_brief(view, steps.label)
        if not no_questions:
            while True:
                try:
                    ask = raw_input("\n" + _("Run process? (yes/no): "))
                except KeyboardInterrupt:
                    ask = "no"
                    print
                if ask.lower() in ["n", "no"]:
                    colorPrint.printERROR(_("Manually interrupted"))
                    return None
                if ask.lower() in ["y", "yes"]:
                    break

    setattr(param_object, "CheckOnly", False)
    try:
        method_result = getattr(metaObject, method_name)(0, param_object)
    except VariableError, e:
        _print(e)
        return None
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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)