예제 #1
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
예제 #2
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
예제 #3
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()
예제 #4
0
 def test_obj_str_list_notrunc(self):
     assert fmt.obj_str(['1', '2', '3', '4', '5'],
                        max_children=0) == 'list( 1, 2, 3, 4, 5 )'
예제 #5
0
 def test_obj_str_list_short(self):
     assert fmt.obj_str(['1', '2', '3']) == 'list( 1, 2, 3 )'
예제 #6
0
 def test_obj_str_list_trunc(self):
     assert fmt.obj_str(['1', '2', '3', '4']) == 'list( 1, 2, 3, ... )'
예제 #7
0
 def test_obj_str_float(self):
     assert fmt.obj_str(1.561) == 'float( 1.561 )'
예제 #8
0
 def test_obj_str_str(self):
     assert fmt.obj_str('hello') == 'hello'
예제 #9
0
 def test_obj_str_unknown(self):
     now = datetime.now()
     assert fmt.obj_str(now) == str(now)
예제 #10
0
파일: test_format.py 프로젝트: ameily/pypsi
 def test_obj_str_list_short(self):
     assert fmt.obj_str(['1', '2', '3']) == 'list( 1, 2, 3 )'
예제 #11
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
예제 #12
0
파일: test_format.py 프로젝트: ameily/pypsi
 def test_obj_str_simple(self, type_cls):
     val = type_cls(0)
     assert fmt.obj_str(val) == type_cls.__name__ + '( ' + str(val) + ' )'
예제 #13
0
파일: test_format.py 프로젝트: ameily/pypsi
 def test_obj_str_none(self):
     assert fmt.obj_str(None) == '<null>'
예제 #14
0
파일: test_format.py 프로젝트: ameily/pypsi
 def test_obj_str_unknown(self):
     now = datetime.now()
     assert fmt.obj_str(now) == str(now)
예제 #15
0
파일: test_format.py 프로젝트: ameily/pypsi
 def test_obj_str_list_type(self):
     assert fmt.obj_str([1, 2]) == 'list( int( 1 ), int( 2 ) )'
예제 #16
0
파일: test_format.py 프로젝트: ameily/pypsi
 def test_obj_str_list_notrunc(self):
     assert fmt.obj_str(['1', '2', '3', '4', '5'], max_children=0) == 'list( 1, 2, 3, 4, 5 )'
예제 #17
0
파일: test_format.py 프로젝트: ameily/pypsi
 def test_obj_str_list_trunc(self):
     assert fmt.obj_str(['1', '2', '3', '4']) == 'list( 1, 2, 3, ... )'
예제 #18
0
 def test_obj_str_list_type(self):
     assert fmt.obj_str([1, 2]) == 'list( int( 1 ), int( 2 ) )'
예제 #19
0
파일: test_format.py 프로젝트: ameily/pypsi
 def test_obj_str_float(self):
     assert fmt.obj_str(1.561) == 'float( 1.561 )'
예제 #20
0
 def test_obj_str_none(self):
     assert fmt.obj_str(None) == '<null>'
예제 #21
0
 def test_obj_str_simple(self, type_cls):
     val = type_cls(0)
     assert fmt.obj_str(val) == type_cls.__name__ + '( ' + str(val) + ' )'
예제 #22
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
예제 #23
0
파일: test_format.py 프로젝트: ameily/pypsi
 def test_obj_str_str(self):
     assert fmt.obj_str('hello') == 'hello'