Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / history.py @ b8220825

History | View | Annotate | Download (3.4 kB)

1
# Copyright 2012-2014 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from kamaki.cli.command_tree import CommandTree
35
from kamaki.cli.argument import ValueArgument
36
from kamaki.cli.history import History
37
from kamaki.cli import command
38
from kamaki.cli.commands import _command_init, errors
39

    
40

    
41
history_cmds = CommandTree('history', 'Kamaki command history')
42
_commands = [history_cmds]
43

    
44

    
45
class _init_history(_command_init):
46
    @errors.generic.all
47
    @errors.history.init
48
    def _run(self):
49
        self.history = History(self.config.get('global', 'history_file'))
50
        self.history.limit = self.config.get('global', 'history_limit')
51

    
52
    def main(self):
53
        self._run()
54

    
55

    
56
@command(history_cmds)
57
class history_show(_init_history):
58
    """Show history
59
        Featutes:
60
        - slice notation (cmd numbers --> N or :N or N: or N1:N2)
61
        - text matching (--match)
62
    """
63

    
64
    arguments = dict(
65
        match=ValueArgument('Show lines matching this', '--match'),
66
    )
67

    
68
    @errors.generic.all
69
    def _run(self, cmd_slice):
70
        c = self.history.counter
71
        lines = ['%s.\t%s' % (i + c, l) for i, l in enumerate(
72
            self.history[:])][cmd_slice]
73
        if not isinstance(cmd_slice, slice):
74
            lines = [lines, ]
75
        if self['match']:
76
            lines = [l for l in lines if self.history._match(l, self['match'])]
77
        self.print_items([l[:-1] for l in lines])
78

    
79
    def main(self, cmd_numbers=''):
80
        super(self.__class__, self)._run()
81
        sl_args = [
82
            int(x) if x else None for x in cmd_numbers.split(':')] if (
83
                cmd_numbers) else [None, None]
84
        slice_cmds = slice(*sl_args) if len(sl_args) > 1 else sl_args[0]
85
        self._run(slice_cmds)
86

    
87

    
88
@command(history_cmds)
89
class history_clean(_init_history):
90
    """Clean up history (permanent)"""
91

    
92
    @errors.generic.all
93
    def _run(self):
94
        self.history.empty()
95

    
96
    def main(self):
97
        super(self.__class__, self)._run()
98
        self._run()