Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.4 kB)

1 5e383dd4 Stavros Sachtouris
# Copyright 2012-2014 GRNET S.A. All rights reserved.
2 73e0914d Stavros Sachtouris
#
3 73e0914d Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 73e0914d Stavros Sachtouris
# without modification, are permitted provided that the following
5 73e0914d Stavros Sachtouris
# conditions are met:
6 73e0914d Stavros Sachtouris
#
7 73e0914d Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 73e0914d Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 73e0914d Stavros Sachtouris
#      disclaimer.
10 73e0914d Stavros Sachtouris
#
11 73e0914d Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 73e0914d Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 73e0914d Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 73e0914d Stavros Sachtouris
#      provided with the distribution.
15 73e0914d Stavros Sachtouris
#
16 73e0914d Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 73e0914d Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 73e0914d Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 73e0914d Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 73e0914d Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 73e0914d Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 73e0914d Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 73e0914d Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 73e0914d Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 73e0914d Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 73e0914d Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 73e0914d Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 73e0914d Stavros Sachtouris
#
29 73e0914d Stavros Sachtouris
# The views and conclusions contained in the software and
30 73e0914d Stavros Sachtouris
# documentation are those of the authors and should not be
31 73e0914d Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 73e0914d Stavros Sachtouris
# or implied, of GRNET S.A.
33 73e0914d Stavros Sachtouris
34 d486baec Stavros Sachtouris
from kamaki.cli.command_tree import CommandTree
35 ddc97a10 Stavros Sachtouris
from kamaki.cli.argument import ValueArgument
36 73e0914d Stavros Sachtouris
from kamaki.cli.history import History
37 73e0914d Stavros Sachtouris
from kamaki.cli import command
38 dc6fc88e Stavros Sachtouris
from kamaki.cli.commands import _command_init, errors
39 73e0914d Stavros Sachtouris
40 234954d1 Stavros Sachtouris
41 a29d2f88 Stavros Sachtouris
history_cmds = CommandTree('history', 'Kamaki command history')
42 db950b10 Stavros Sachtouris
_commands = [history_cmds]
43 234954d1 Stavros Sachtouris
44 234954d1 Stavros Sachtouris
45 5eae854d Stavros Sachtouris
class _init_history(_command_init):
46 a03ade9e Stavros Sachtouris
    @errors.generic.all
47 dc6fc88e Stavros Sachtouris
    @errors.history.init
48 dc6fc88e Stavros Sachtouris
    def _run(self):
49 534e7bbb Stavros Sachtouris
        self.history = History(self.config.get('global', 'history_file'))
50 50a32c37 Stavros Sachtouris
        self.history.limit = self.config.get('global', 'history_limit')
51 234954d1 Stavros Sachtouris
52 dc6fc88e Stavros Sachtouris
    def main(self):
53 dc6fc88e Stavros Sachtouris
        self._run()
54 dc6fc88e Stavros Sachtouris
55 73e0914d Stavros Sachtouris
56 d486baec Stavros Sachtouris
@command(history_cmds)
57 d53062bd Stavros Sachtouris
class history_show(_init_history):
58 ddc97a10 Stavros Sachtouris
    """Show history
59 ddc97a10 Stavros Sachtouris
        Featutes:
60 ddc97a10 Stavros Sachtouris
        - slice notation (cmd numbers --> N or :N or N: or N1:N2)
61 ddc97a10 Stavros Sachtouris
        - text matching (--match)
62 c5bcc3e3 Stavros Sachtouris
    """
63 234954d1 Stavros Sachtouris
64 746bd77f Stavros Sachtouris
    arguments = dict(
65 ddc97a10 Stavros Sachtouris
        match=ValueArgument('Show lines matching this', '--match'),
66 746bd77f Stavros Sachtouris
    )
67 73e0914d Stavros Sachtouris
68 dc6fc88e Stavros Sachtouris
    @errors.generic.all
69 ddc97a10 Stavros Sachtouris
    def _run(self, cmd_slice):
70 5e383dd4 Stavros Sachtouris
        c = self.history.counter
71 5e383dd4 Stavros Sachtouris
        lines = ['%s.\t%s' % (i + c, l) for i, l in enumerate(
72 5e383dd4 Stavros Sachtouris
            self.history[:])][cmd_slice]
73 ddc97a10 Stavros Sachtouris
        if not isinstance(cmd_slice, slice):
74 ddc97a10 Stavros Sachtouris
            lines = [lines, ]
75 ddc97a10 Stavros Sachtouris
        if self['match']:
76 2f302751 Stavros Sachtouris
            lines = [l for l in lines if self.history._match(l, self['match'])]
77 ddc97a10 Stavros Sachtouris
        self.print_items([l[:-1] for l in lines])
78 ddc97a10 Stavros Sachtouris
79 ddc97a10 Stavros Sachtouris
    def main(self, cmd_numbers=''):
80 dc6fc88e Stavros Sachtouris
        super(self.__class__, self)._run()
81 ddc97a10 Stavros Sachtouris
        sl_args = [
82 ddc97a10 Stavros Sachtouris
            int(x) if x else None for x in cmd_numbers.split(':')] if (
83 ddc97a10 Stavros Sachtouris
                cmd_numbers) else [None, None]
84 ddc97a10 Stavros Sachtouris
        slice_cmds = slice(*sl_args) if len(sl_args) > 1 else sl_args[0]
85 ddc97a10 Stavros Sachtouris
        self._run(slice_cmds)
86 dc6fc88e Stavros Sachtouris
87 73e0914d Stavros Sachtouris
88 d486baec Stavros Sachtouris
@command(history_cmds)
89 73e0914d Stavros Sachtouris
class history_clean(_init_history):
90 c5bcc3e3 Stavros Sachtouris
    """Clean up history (permanent)"""
91 73e0914d Stavros Sachtouris
92 dc6fc88e Stavros Sachtouris
    @errors.generic.all
93 dc6fc88e Stavros Sachtouris
    def _run(self):
94 edaf3ba6 Stavros Sachtouris
        self.history.empty()
95 304c90b5 Stavros Sachtouris
96 dc6fc88e Stavros Sachtouris
    def main(self):
97 dc6fc88e Stavros Sachtouris
        super(self.__class__, self)._run()
98 dc6fc88e Stavros Sachtouris
        self._run()