예제 #1
0
파일: variable.py 프로젝트: jeinstos/pypsi
    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.list:
            tbl = Table(
                columns=(Column("Variable"), Column("Value", Column.Grow)),
                width=shell.width,
                spacing=4,
            )
            for name in shell.ctx.vars:
                if name == '_':
                    continue
                s = shell.ctx.vars[name]
                if callable(s):
                    s = s()
                elif isinstance(s, ManagedVariable):
                    s = s.getter(shell)
                tbl.append(name, obj_str(s))
            tbl.write(sys.stdout)
        elif ns.delete:
            if ns.delete in shell.ctx.vars:
                s = shell.ctx.vars[ns.delete]
                if isinstance(s, ManagedVariable):
                    self.error(shell, "variable is managed and cannot be deleted")
                    rc = -1
                else:
                    del shell.ctx.vars[ns.delete]
            else:
                self.error(shell, "unknown variable: ", ns.delete)
        elif ns.exp and '=' in ''.join(args):
            (remainder, exp) = Expression.parse(args)
            if remainder or not exp:
                self.error(shell, "invalid expression")
                return 1
            shell.ctx.vars[exp.operand] = exp.value
        elif ns.exp:
            if len(args) == 1:
                if args[0] in shell.ctx.vars:
                    s = shell.ctx.vars[args[0]]
                    if callable(s):
                        s = s()
                    elif isinstance(s, ManagedVariable):
                        s = s.getter(shell)
                    print(obj_str(s))
                else:
                    self.error(shell, "unknown variable: ", args[0])
                    return 1
            else:
                self.error(shell, "invalid expression")
                return 1
        else:
            self.usage_error(shell, "missing required EXPRESSION")
            rc =1

        return rc
예제 #2
0
    def print_stocks(self, shell, exchange_id: str = None):
        if exchange_id:
            columns = [
                Column('Id'),
                Column('Name'),
                Column('Ups'),
                Column('Downs')
            ]
            cursor = shell.ctx.db.stocks.find(
                {'exchange_id': ObjectId(exchange_id)})
        else:
            columns = [
                Column('Id'),
                Column('Exchange Id'),
                Column('Name'),
                Column('Ups'),
                Column('Downs')
            ]
            cursor = shell.ctx.db.stocks.find()

        table = Table(columns, spacing=4)
        for row in cursor:
            if exchange_id:
                table.append(row['_id'], row['name'], row['ups'], row['downs'])
            else:
                table.append(row['_id'], row['exchange_id'], row['name'],
                             row['ups'], row['downs'])

        table.write(sys.stdout)
        return 0
예제 #3
0
    def run(self, shell, args):
        ns = ConfigWizard.run(shell)
        if ns:
            print()
            # Create a table with optimally sized columns.
            Table(
                columns=(
                    # FIrst column is the configuration ID. This column will be
                    # the minimum width possible without wrapping
                    Column('Config ID', Column.Shrink),
                    # Second column is the configuration value. This column will
                    # grow to a maximum width possible.
                    Column('Config Value', Column.Grow)),
                # Number of spaces between each column.
                spacing=4).extend(
                    # Each tuple is a row
                    ('ip_addr', ns.ip_addr),
                    ('port', ns.port),
                    ('path', ns.path),
                    ('mode', ns.mode)).write(
                        sys.stdout)  # Write the table to stdout
        else:
            pass

        return 0
예제 #4
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.list:
            tbl = Table(
                columns=(Column("Variable"), Column("Value", Column.Grow)),
                width=shell.width,
                spacing=4,
            )
            for name in shell.ctx.vars:
                if name == '_':
                    continue
                s = shell.ctx.vars[name]
                if callable(s):
                    s = s()
                elif isinstance(s, ManagedVariable):
                    s = s.getter(shell)
                tbl.append(name, obj_str(s))
            tbl.write(sys.stdout)
        elif ns.delete:
            if ns.delete in shell.ctx.vars:
                s = shell.ctx.vars[ns.delete]
                if isinstance(s, ManagedVariable):
                    self.error(shell,
                               "variable is managed and cannot be deleted")
                    rc = -1
                else:
                    del shell.ctx.vars[ns.delete]
            else:
                self.error(shell, "unknown variable: ", ns.delete)
        elif ns.exp and '=' in ''.join(args):
            (remainder, exp) = Expression.parse(args)
            if remainder or not exp:
                self.error(shell, "invalid expression")
                return 1
            shell.ctx.vars[exp.operand] = exp.value
        elif ns.exp:
            if len(args) == 1:
                if args[0] in shell.ctx.vars:
                    s = shell.ctx.vars[args[0]]
                    if callable(s):
                        s = s()
                    elif isinstance(s, ManagedVariable):
                        s = s.getter(shell)
                    print(obj_str(s))
                else:
                    self.error(shell, "unknown variable: ", args[0])
                    return 1
            else:
                self.error(shell, "invalid expression")
                return 1
        else:
            self.usage_error(shell, "missing required EXPRESSION")
            rc = 1

        return rc
예제 #5
0
파일: help.py 프로젝트: jnashold/pypsi
 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='')
예제 #6
0
 def run(self, shell, args):
     print("\nConfigured minions:\n")
     table = Table(columns=(Column('Name', Column.Shrink),
                            Column('IP Address', Column.Shrink),
                            Column('Plugins', Column.Grow)),
                   spacing=4)
     for name in shell.minions:
         table.append(name, shell.minions[name]['ip'],
                      obj_str(shell.minions[name]['plugins']))
     table.write(sys.stdout)
     print()
예제 #7
0
    def print_vote_labels(self, shell):
        cursor = shell.ctx.db.vote_labels.find()

        table = Table(
            [Column('Id'), Column('Name'),
             Column('Symbol')], spacing=4)
        for row in cursor:
            table.append(row['_id'], row['name'], row['symbol'])

        table.write(sys.stdout)
        return 0
예제 #8
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='')
예제 #9
0
파일: help.py 프로젝트: 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)
예제 #10
0
 def print_exchanges(self, shell):
     table = Table([Column('Id'), Column('Name')], spacing=4)
     for row in shell.ctx.db.exchanges.find():
         table.append(row['_id'], row['name'])
     table.write(sys.stdout)
     return 0
예제 #11
0
파일: macro.py 프로젝트: ameily/pypsi
    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
예제 #12
0
파일: macro.py 프로젝트: twisted-fun/pypsi
    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
예제 #13
0
파일: variable.py 프로젝트: ameily/pypsi
    def run(self, shell, args):
        try:
            ns = self.parser.parse_args(args)
        except CommandShortCircuit as e:
            return e.code

        rc = 0
        if ns.list:
            tbl = Table(
                columns=(Column("Variable"), Column("Value", Column.Grow)),
                width=shell.width,
                spacing=4,
            )
            for name in shell.ctx.vars:
                s = shell.ctx.vars[name]
                if callable(s):
                    s = s()
                elif isinstance(s, ManagedVariable):
                    s = s.get(shell)
                tbl.append(name, obj_str(s))
            tbl.write(sys.stdout)
        elif ns.delete:
            if ns.delete in shell.ctx.vars:
                s = shell.ctx.vars[ns.delete]
                if isinstance(s, ManagedVariable):
                    self.error(shell,
                               "variable is managed and cannot be deleted")
                    rc = -1
                else:
                    del shell.ctx.vars[ns.delete]
            else:
                self.error(shell, "unknown variable: ", ns.delete)
                rc = -1
        elif ns.exp and '=' in ''.join(args):
            (remainder, exp) = Expression.parse(args)
            if remainder or not exp:
                self.error(shell, "invalid expression")
                return 1
            if isinstance(shell.ctx.vars[exp.operand], ManagedVariable):
                try:
                    shell.ctx.vars[exp.operand].set(shell, exp.value)
                except ValueError as e:
                    self.error(shell, "could not set variable: ", str(e))
                    return -1
            else:
                shell.ctx.vars[exp.operand] = exp.value
        elif ns.exp:
            if len(args) == 1:
                if args[0] in shell.ctx.vars:
                    s = shell.ctx.vars[args[0]]
                    if callable(s):
                        s = s()
                    elif isinstance(s, ManagedVariable):
                        s = s.getter(shell)
                    print(obj_str(s))
                else:
                    self.error(shell, "unknown variable: ", args[0])
                    return 1
            else:
                self.error(shell, "invalid expression")
                return 1
        else:
            self.usage_error(shell, "missing required EXPRESSION")
            rc = 1

        return rc
예제 #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.list:
            tbl = Table(
                columns=(Column("Variable"), Column("Value", Column.Grow)),
                width=shell.width,
                spacing=4,
            )
            for name in shell.ctx.vars:
                s = shell.ctx.vars[name]
                if callable(s):
                    s = s()
                elif isinstance(s, ManagedVariable):
                    s = s.get(shell)
                tbl.append(name, obj_str(s))
            tbl.write(sys.stdout)
        elif ns.delete:
            if ns.delete in shell.ctx.vars:
                s = shell.ctx.vars[ns.delete]
                if isinstance(s, ManagedVariable):
                    self.error(shell,
                               "variable is managed and cannot be deleted")
                    rc = -1
                else:
                    del shell.ctx.vars[ns.delete]
            else:
                self.error(shell, "unknown variable: ", ns.delete)
                rc = -1
        elif ns.exp and '=' in ''.join(args):
            (remainder, exp) = Expression.parse(args)
            if remainder or not exp:
                self.error(shell, "invalid expression")
                return 1
            if isinstance(shell.ctx.vars[exp.operand], ManagedVariable):
                try:
                    shell.ctx.vars[exp.operand].set(shell, exp.value)
                except ValueError as e:
                    self.error(shell, "could not set variable: ", str(e))
                    return -1
            else:
                shell.ctx.vars[exp.operand] = exp.value
        elif ns.exp:
            if len(args) == 1:
                if args[0] in shell.ctx.vars:
                    s = shell.ctx.vars[args[0]]
                    if callable(s):
                        s = s()
                    elif isinstance(s, ManagedVariable):
                        s = s.getter(shell)
                    print(obj_str(s))
                else:
                    self.error(shell, "unknown variable: ", args[0])
                    return 1
            else:
                self.error(shell, "invalid expression")
                return 1
        else:
            self.usage_error(shell, "missing required EXPRESSION")
            rc = 1

        return rc