Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / history.py @ 3042fac1

History | View | Annotate | Download (3.4 kB)

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