Ejemplo n.º 1
0
    def print_topics(self, shell):
        addl = []
        for topic in self.topics:
            if topic.content or not topic.commands:
                addl.append(topic)

            if topic.commands:
                self.print_topic_commands(shell, topic)
                print()

        if self.uncat.commands:
            self.print_topic_commands(shell, self.uncat)
            print()

        if addl:
            print(
                AnsiStderr.yellow,
                title_str("Additional Topics", shell.width),
                AnsiStderr.reset,
                sep=''
            )
            tbl = FixedColumnTable([shell.width // 3] * 3)
            for topic in addl:
                tbl.add_cell(sys.stdout, topic.id)
            tbl.flush(sys.stdout)
            print()
Ejemplo n.º 2
0
    def run(self, shell):
        self.old_completer = readline.get_completer()
        readline.set_completer(self.complete)
        print(
            title_str("Entering " + self.name + " Wizard", width=shell.width, box=True, align='center'),
            '\n',
            self.description, '\n\n',
            word_wrap(
                "To exit, enter either Ctrl+C, Ctrl+D, or 'quit'. For help "
                "about the current step, enter 'help' or '?'.", shell.width
            ),
            sep=''
        )

        running = True
        for step in self.steps:
            self.active_step = step
            valid = False
            while not valid:
                raw = None
                prompt = step.name
                if step.default is not None:
                    d = step.default
                    if callable(d):
                        d = d(self.values)
                    prompt += ' [{}]'.format(d)
                prompt += ': '
                try:
                    raw = input(prompt)
                except (KeyboardInterrupt, EOFError):
                    print()
                    print(AnsiStdout.red, "Wizard canceled", AnsiStdout.reset, sep='')
                    readline.set_completer(self.old_completer)
                    return None

                if raw.lower() == 'quit':
                    print(AnsiStdout.red, "Exiting wizard", AnsiStdout.reset, sep='')
                    readline.set_completer(self.old_completer)
                    return None
                elif raw.lower() in ('?', 'help'):
                    print(word_wrap(step.help, shell.width))
                else:
                    if not raw and step.default is not None:
                        raw = step.default

                    try:
                        value = step.validate(self.values, raw)
                    except ValueError as e:
                        print(AnsiStdout.red, "Error: ", str(e), AnsiStdout.reset, sep='')
                        print(AnsiStdout.yellow, step.name, ": ", step.help, sep='')
                    else:
                        self.values[step.id] = value
                        valid = True

        readline.set_completer(self.old_completer)
        return self.values
Ejemplo n.º 3
0
    def run(self, shell, args, ctx):
        try:
            ns = self.parser.parse_args(args)
        except CommandShortCircuit as e:
            return e.code

        rc = 0
        if ns.name:
            if ns.delete:
                if ns.name in shell.ctx.macros:
                    del shell.ctx.macros[ns.name]
                    #It gets registered as a command too. See line 202 in this file and register() in shell.py
                    del shell.commands[ns.name]
                else:
                    self.error(shell, "unknown macro ", ns.name)
                    rc = -1
            elif ns.show:
                if ns.name in shell.ctx.macros:
                    print("macro ", ns.name, sep='')
                    for line in shell.ctx.macros[ns.name]:
                        print("    ", line, sep='')
                    print("end")
                else:
                    self.error(shell, "unknown macro ", ns.name)
                    rc = -1
            elif ns.list:
                self.usage_error(shell, "list option does not take an argument")
            else:
                if ns.name in shell.commands.keys() and ns.name not in shell.ctx.macros:
                    self.error(shell, "A macro cannot have the same name as an existing command.")
                    return -1

                self.macro_name = ns.name
                self.begin_block(shell)
                if sys.stdin.isatty():
                    print("Beginning macro, use the '", shell.ctx.block.end_cmd,
                          "' command to save.", sep='')

                shell.ctx.macro_orig_eof_is_sigint = shell.eof_is_sigint
                shell.eof_is_sigint = True
        elif ns.list:
            tbl = FixedColumnTable(widths=[shell.width//3]*3)
            print(title_str("Registered Macros", shell.width))
            for name in shell.ctx.macros:
                tbl.add_cell(sys.stdout, name)
            tbl.flush(sys.stdout)
        else:
            self.usage_error(shell, "missing required argument: NAME")
            rc = 1

        return rc
Ejemplo n.º 4
0
 def print_topic_commands(self, shell, topic, title=None):
     print(AnsiStderr.yellow,
           title_str(title or topic.name or topic.id, shell.width),
           AnsiStderr.reset,
           sep='')
     print(AnsiStderr.yellow, end='')
     Table(columns=(Column(''), Column('', Column.Grow)),
           spacing=4,
           header=False,
           width=shell.width).extend(
               *[(' Name',
                  'Description'), (' ----', '-----------')]).extend(
                      *[(' ' + c.name, c.brief or '')
                        for c in topic.commands]).write(sys.stdout)
     print(AnsiStderr.reset, end='')
Ejemplo n.º 5
0
 def print_topic_commands(self,
                          shell,
                          topic,
                          title=None,
                          name_col_width=20):
     print(AnsiCodes.yellow,
           title_str(title or topic.name or topic.id, shell.width),
           AnsiCodes.reset,
           sep='')
     print(AnsiCodes.yellow, end='')
     Table(columns=(Column(''), Column('', Column.Grow)),
           spacing=4,
           header=False,
           width=shell.width).extend(
               *[(' ' + c.name.ljust(name_col_width - 1), c.brief or '')
                 for c in topic.commands]).write(sys.stdout)
     print(AnsiCodes.reset, end='')
Ejemplo n.º 6
0
    def print_topic(self, shell, id):
        if id not in self.lookup:
            if id in shell.commands:
                cmd = shell.commands[id]
                print(AnsiStderr.yellow, cmd.usage, AnsiStderr.reset, sep='')
                return 0

            self.error(shell, "unknown topic: ", id)
            return -1

        topic = self.lookup[id]
        if topic.content:
            print(title_str(topic.name or topic.id, shell.width))
            print(word_wrap(topic.content, shell.width))
            print()

        if topic.commands:
            self.print_topic_commands(shell, topic, "Commands")
        return 0
Ejemplo n.º 7
0
    def print_topic(self, shell, id):
        if id not in self.lookup:
            if id in shell.commands:
                cmd = shell.commands[id]
                print(AnsiStderr.yellow, cmd.usage, AnsiStderr.reset, sep='')
                return 0

            self.error(shell, "unknown topic: ", id)
            return -1

        topic = self.lookup[id]
        if topic.content:
            print(title_str(topic.name or topic.id, shell.width))
            print(word_wrap(topic.content, shell.width))
            print()

        if topic.commands:
            self.print_topic_commands(shell, topic, "Commands")
        return 0
Ejemplo n.º 8
0
Archivo: help.py Proyecto: cfm/pypsi
 def print_topic_commands(self, shell, topic, title=None,
                          name_col_width=20):
     print(
         AnsiCodes.yellow,
         title_str(title or topic.name or topic.id, shell.width),
         AnsiCodes.reset,
         sep=''
     )
     print(AnsiCodes.yellow, end='')
     Table(
         columns=(Column(''), Column('', Column.Grow)),
         spacing=4,
         header=False,
         width=shell.width
     ).extend(*[
         (' ' + c.name.ljust(name_col_width - 1), c.brief or '')
         for c in topic.commands
     ]).write(sys.stdout)
     print(AnsiCodes.reset, end='')
Ejemplo n.º 9
0
Archivo: help.py Proyecto: lnenov/pypsi
    def print_topics(self, shell):
        max_name_width = 0
        for topic in shell.ctx.topics:
            for c in topic.commands:
                max_name_width = max(len(c.name), max_name_width)

        for c in shell.ctx.uncat_topic.commands:
            max_name_width = max(len(c.name), max_name_width)

        addl = []
        for topic in shell.ctx.topics:
            if topic.content or not topic.commands:
                addl.append(topic)

            if topic.commands:
                self.print_topic_commands(shell, topic,
                                          name_col_width=max_name_width)
                print()

        if shell.ctx.uncat_topic.commands:
            self.print_topic_commands(shell, shell.ctx.uncat_topic,
                                      name_col_width=max_name_width)
            print()

        if addl:
            addl = sorted(addl, key=lambda x: x.id)
            print(
                AnsiCodes.yellow,
                title_str("Additional Topics", shell.width),
                sep=''
            )
            Table(
                columns=(Column(''), Column('', Column.Grow)),
                spacing=4,
                header=False,
                width=shell.width
            ).extend(*[
                (' ' + topic.id.ljust(max_name_width - 1), topic.name or '')
                for topic in addl
            ]).write(sys.stdout)
            print(AnsiCodes.reset)
Ejemplo n.º 10
0
    def run(self, shell, args, ctx):
        ns = self.parser.parse_args(shell, args)
        if self.parser.rc is not None:
            return self.parser.rc

        rc = 0
        if ns.name:
            if ns.delete:
                if ns.name in shell.ctx.macros:
                    del shell.ctx.macros[ns.name]
                else:
                    self.error(shell, "unknown macro ", ns.name)
                    rc = -1
            elif ns.show:
                if ns.name in shell.ctx.macros:
                    print("macro ", ns.name, sep='')
                    for line in shell.ctx.macros[ns.name]:
                        print("    ", line, sep='')
                    print("end")
                else:
                    self.error(shell, "unknown macro ", ns.name)
                    rc = -1
            elif ns.list:
                self.usage_error(shell,
                                 "list option does not take an argument")
            else:
                self.macro_name = ns.name
                self.begin_block(shell)
        elif ns.list:
            tbl = FixedColumnTable(widths=[shell.width // 3] * 3)
            print(title_str("Registered Macros", shell.width))
            for name in shell.ctx.macros:
                tbl.add_cell(sys.stdout, name)
            tbl.flush(sys.stdout)
        else:
            self.usage_error(shell, "missing required argument: NAME")
            rc = 1

        return rc
Ejemplo n.º 11
0
 def print_topic_commands(self, shell, topic, title=None):
     print(
         AnsiStderr.yellow,
         title_str(title or topic.name or topic.id, shell.width),
         AnsiStderr.reset,
         sep=''
     )
     print(AnsiStderr.yellow, end='')
     Table(
         columns=(Column(''), Column('', Column.Grow)),
         spacing=4,
         header=False,
         width=shell.width
     ).extend(
         *[
             (' Name', 'Description'),
             (' ----', '-----------')
         ]
     ).extend(
         *[(' '+c.name, c.brief or '') for c in topic.commands]
     ).write(sys.stdout)
     print(AnsiStderr.reset, end='')
Ejemplo n.º 12
0
Archivo: help.py Proyecto: lnenov/pypsi
    def print_topic(self, shell, id):
        if id not in shell.ctx.topic_lookup:
            if id in shell.commands:
                cmd = shell.commands[id]
                print(AnsiCodes.yellow, cmd.usage, AnsiCodes.reset, sep='')
                return 0

            self.error(shell, "unknown topic: ", id)
            return -1

        topic = shell.ctx.topic_lookup[id]
        if topic.content:
            print(title_str(topic.name or topic.id, shell.width))
            try:
                cnt = topic.content.format(**self.vars)
            except:
                cnt = topic.content

            print(cnt)
            print()

        if topic.commands:
            self.print_topic_commands(shell, topic, "Commands")
        return 0
Ejemplo n.º 13
0
    def print_topics(self, shell):
        addl = []
        for topic in self.topics:
            if topic.content or not topic.commands:
                addl.append(topic)

            if topic.commands:
                self.print_topic_commands(shell, topic)
                print()

        if self.uncat.commands:
            self.print_topic_commands(shell, self.uncat)
            print()

        if addl:
            print(AnsiStderr.yellow,
                  title_str("Additional Topics", shell.width),
                  AnsiStderr.reset,
                  sep='')
            tbl = FixedColumnTable([shell.width // 3] * 3)
            for topic in addl:
                tbl.add_cell(sys.stdout, topic.id)
            tbl.flush(sys.stdout)
            print()
Ejemplo n.º 14
0
    def run(self, shell, args):
        try:
            ns = self.parser.parse_args(args)
        except CommandShortCircuit as e:
            return e.code

        rc = 0
        if ns.show:
            if ns.delete or ns.name:
                self.usage_error(shell,
                                 'incompatible arguments: -s/--show and ',
                                 '-d/--delete' if ns.delete else 'NAME')
                return -1
            if ns.list or ns.name:
                self.usage_error(shell,
                                 'incompatible arguments: -s/--show and ',
                                 '-l/--list' if ns.list else 'NAME')
                return -1

            if ns.show in shell.ctx.macros:
                print("macro ", ns.show, sep='')
                for line in shell.ctx.macros[ns.show]:
                    print("    ", line, sep='')
                print("end")
            else:
                self.error(shell, "unknown macro ", ns.show)
                rc = -1
        elif ns.delete:
            if ns.list or ns.name:
                self.usage_error(shell,
                                 'incompatible arguments: -d/--delete and ',
                                 '-l/--list' if ns.list else 'NAME')
                return -1

            if ns.delete in shell.ctx.macros:
                del shell.ctx.macros[ns.delete]
                # It gets registered as a command too. See line 230 in this
                # file and register() in shell.py
                del shell.commands[ns.delete]
            else:
                self.error(shell, "unknown macro ", ns.delete)
                rc = -1
        elif ns.name:
            if ns.list:
                self.usage_error(shell,
                                 "list option does not take an argument")
            else:
                if (ns.name in shell.commands.keys() and
                        ns.name not in shell.ctx.macros):
                    self.error(
                        shell, "A macro cannot have the same name as an ",
                        "existing command."
                    )
                    return -1

                self.macro_name = ns.name
                self.begin_block(shell)
                if sys.stdin.isatty():
                    print("Beginning macro, use the '",
                          shell.ctx.block.end_cmd, "' command to save.",
                          sep='')

                shell.ctx.macro_orig_eof_is_sigint = shell.eof_is_sigint
                shell.eof_is_sigint = True
        elif ns.list:
            # Left justified table
            print(title_str("Registered Macros", shell.width))
            chunk_size = 3

            tbl = Table(
                columns=(Column(''), Column(''), Column('', Column.Grow)),
                spacing=4,
                header=False,
                width=shell.width
            )

            macro_names = list(shell.ctx.macros.keys())
            for i in range(0, len(macro_names), chunk_size):
                chunk = macro_names[i:i + chunk_size]
                tbl.extend(chunk)
            tbl.write(sys.stdout)
        else:
            self.usage_error(shell, "missing required argument: NAME")
            rc = 1

        return rc
Ejemplo n.º 15
0
    def run(self, shell, print_header=True):
        '''
        Execute the wizard, prompting the user for input.

        :param pypsi.shell.Shell shell: the active shell
        :returns: a :class:`~pypsi.namespace.Namespace` object containing all
         the answers on success, :const:`None` if the user exited the wizard
        '''

        self.old_completer = readline.get_completer()
        readline.set_completer(self.complete)

        if print_header:
            print(title_str("Entering " + self.name + " Wizard",
                            width=shell.width,
                            box=True,
                            align='center'),
                  '\n',
                  self.description,
                  '\n\n',
                  "To exit, enter either Ctrl+C, Ctrl+D, or 'quit'. For help "
                  "about the current step, enter 'help' or '?'.",
                  sep='')

        for step in self.steps:
            self.active_step = step
            valid = False
            while not valid:
                print()
                raw = None
                prompt = step.name
                if step.default is not None:
                    d = step.default
                    if callable(d):
                        d = d(self.values)
                    prompt += ' [{}]'.format(d)
                prompt += ': '
                try:
                    raw = input(prompt)
                except (KeyboardInterrupt, EOFError):
                    print()
                    print(AnsiCodes.red,
                          "Wizard canceled",
                          AnsiCodes.reset,
                          sep='')
                    readline.set_completer(self.old_completer)
                    return None

                if raw.lower() == 'quit':
                    print(AnsiCodes.red,
                          "Exiting wizard",
                          AnsiCodes.reset,
                          sep='')
                    readline.set_completer(self.old_completer)
                    return None

                if raw.lower() in ('?', 'help'):
                    print(step.help)
                else:
                    if not raw.strip() and step.default is not None:
                        raw = step.default

                    try:
                        value = step.validate(self.values, raw)
                    except ValueError as e:
                        print(AnsiCodes.red,
                              "Error: ",
                              str(e),
                              AnsiCodes.reset,
                              sep='')
                        print(AnsiCodes.yellow,
                              step.name,
                              ": ",
                              step.help,
                              AnsiCodes.reset,
                              sep='')
                    else:
                        self.values[step.id] = value
                        valid = True

        readline.set_completer(self.old_completer)
        return self.values
Ejemplo n.º 16
0
    def run(self, shell, print_header=True):
        '''
        Execute the wizard, prompting the user for input.

        :param pypsi.shell.Shell shell: the active shell
        :returns: a :class:`~pypsi.namespace.Namespace` object containing all
         the answers on success, :const:`None` if the user exited the wizard
        '''

        self.old_completer = readline.get_completer()
        readline.set_completer(self.complete)

        if print_header:
            print(
                title_str("Entering " + self.name + " Wizard",
                          width=shell.width, box=True, align='center'),
                '\n',
                self.description, '\n\n',
                "To exit, enter either Ctrl+C, Ctrl+D, or 'quit'. For help "
                "about the current step, enter 'help' or '?'.", sep=''
            )

        for step in self.steps:
            self.active_step = step
            valid = False
            while not valid:
                print()
                raw = None
                prompt = step.name
                if step.default is not None:
                    d = step.default
                    if callable(d):
                        d = d(self.values)
                    prompt += ' [{}]'.format(d)
                prompt += ': '
                try:
                    raw = input(prompt)
                except (KeyboardInterrupt, EOFError):
                    print()
                    print(AnsiCodes.red, "Wizard canceled", AnsiCodes.reset,
                          sep='')
                    readline.set_completer(self.old_completer)
                    return None

                if raw.lower() == 'quit':
                    print(AnsiCodes.red, "Exiting wizard", AnsiCodes.reset,
                          sep='')
                    readline.set_completer(self.old_completer)
                    return None

                if raw.lower() in ('?', 'help'):
                    print(step.help)
                else:
                    if not raw.strip() and step.default is not None:
                        raw = step.default

                    try:
                        value = step.validate(self.values, raw)
                    except ValueError as e:
                        print(AnsiCodes.red, "Error: ", str(e),
                              AnsiCodes.reset, sep='')
                        print(AnsiCodes.yellow, step.name, ": ", step.help,
                              AnsiCodes.reset, sep='')
                    else:
                        self.values[step.id] = value
                        valid = True

        readline.set_completer(self.old_completer)
        return self.values
Ejemplo n.º 17
0
    def run(self, shell, args):
        try:
            ns = self.parser.parse_args(args)
        except CommandShortCircuit as e:
            return e.code

        rc = 0
        if ns.show:
            if ns.delete or ns.name:
                self.usage_error(shell,
                                 'incompatible arguments: -s/--show and ',
                                 '-d/--delete' if ns.delete else 'NAME')
                return -1
            if ns.list or ns.name:
                self.usage_error(shell,
                                 'incompatible arguments: -s/--show and ',
                                 '-l/--list' if ns.list else 'NAME')
                return -1

            if ns.show in shell.ctx.macros:
                print("macro ", ns.show, sep='')
                for line in shell.ctx.macros[ns.show]:
                    print("    ", line, sep='')
                print("end")
            else:
                self.error(shell, "unknown macro ", ns.show)
                rc = -1
        elif ns.delete:
            if ns.list or ns.name:
                self.usage_error(shell,
                                 'incompatible arguments: -d/--delete and ',
                                 '-l/--list' if ns.list else 'NAME')
                return -1

            if ns.delete in shell.ctx.macros:
                del shell.ctx.macros[ns.delete]
                # It gets registered as a command too. See line 230 in this
                # file and register() in shell.py
                del shell.commands[ns.delete]
            else:
                self.error(shell, "unknown macro ", ns.delete)
                rc = -1
        elif ns.name:
            if ns.list:
                self.usage_error(shell,
                                 "list option does not take an argument")
            else:
                if (ns.name in shell.commands.keys()
                        and ns.name not in shell.ctx.macros):
                    self.error(shell,
                               "A macro cannot have the same name as an ",
                               "existing command.")
                    return -1

                self.macro_name = ns.name
                self.begin_block(shell)
                if sys.stdin.isatty():
                    print("Beginning macro, use the '",
                          shell.ctx.block.end_cmd,
                          "' command to save.",
                          sep='')

                shell.ctx.macro_orig_eof_is_sigint = shell.eof_is_sigint
                shell.eof_is_sigint = True
        elif ns.list:
            # Left justified table
            print(title_str("Registered Macros", shell.width))
            chunk_size = 3

            tbl = Table(columns=(Column(''), Column(''),
                                 Column('', Column.Grow)),
                        spacing=4,
                        header=False,
                        width=shell.width)

            macro_names = list(shell.ctx.macros.keys())
            for i in range(0, len(macro_names), chunk_size):
                chunk = macro_names[i:i + chunk_size]
                tbl.extend(chunk)
            tbl.write(sys.stdout)
        else:
            self.usage_error(shell, "missing required argument: NAME")
            rc = 1

        return rc