root / kamaki / cli / history.py @ edaf3ba6
History | View | Annotate | Download (3.1 kB)
1 | 73e0914d | Stavros Sachtouris | #!/usr/bin/env python
|
---|---|---|---|
2 | 73e0914d | Stavros Sachtouris | |
3 | 34e4cf61 | Stavros Sachtouris | # Copyright 2012-2013 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 | c49188b7 | Stavros Sachtouris | import codecs |
37 | c49188b7 | Stavros Sachtouris | |
38 | fd5db045 | Stavros Sachtouris | |
39 | 73e0914d | Stavros Sachtouris | class History(object): |
40 | edaf3ba6 | Stavros Sachtouris | def __init__(self, filepath, token=None, max_lines=0): |
41 | fd5db045 | Stavros Sachtouris | self.filepath = filepath
|
42 | 2d1f5058 | Stavros Sachtouris | self.token = token
|
43 | edaf3ba6 | Stavros Sachtouris | self.max_lines = max_lines
|
44 | edaf3ba6 | Stavros Sachtouris | |
45 | edaf3ba6 | Stavros Sachtouris | def __getitem__(self, cmd_id): |
46 | edaf3ba6 | Stavros Sachtouris | cmd_id = int(cmd_id)
|
47 | edaf3ba6 | Stavros Sachtouris | if not cmd_id: |
48 | edaf3ba6 | Stavros Sachtouris | return None |
49 | edaf3ba6 | Stavros Sachtouris | with open(self.filepath) as f: |
50 | edaf3ba6 | Stavros Sachtouris | try:
|
51 | edaf3ba6 | Stavros Sachtouris | cmd_list = f.readlines()[:-1] # exclude current command |
52 | edaf3ba6 | Stavros Sachtouris | return cmd_list[cmd_id - (1 if cmd_id > 0 else 0)] |
53 | edaf3ba6 | Stavros Sachtouris | except IndexError: |
54 | edaf3ba6 | Stavros Sachtouris | return None |
55 | 73e0914d | Stavros Sachtouris | |
56 | fd5db045 | Stavros Sachtouris | @classmethod
|
57 | fd5db045 | Stavros Sachtouris | def _match(self, line, match_terms): |
58 | fd5db045 | Stavros Sachtouris | if match_terms is None: |
59 | fd5db045 | Stavros Sachtouris | return True |
60 | fd5db045 | Stavros Sachtouris | for term in match_terms.split(): |
61 | fd5db045 | Stavros Sachtouris | if term not in line: |
62 | fd5db045 | Stavros Sachtouris | return False |
63 | fd5db045 | Stavros Sachtouris | return True |
64 | 73e0914d | Stavros Sachtouris | |
65 | fd5db045 | Stavros Sachtouris | def get(self, match_terms=None, limit=0): |
66 | 24903c7d | Stavros Sachtouris | limit = int(limit or 0) |
67 | c49188b7 | Stavros Sachtouris | with codecs.open(self.filepath, mode='r', encoding='utf-8') as f: |
68 | c49188b7 | Stavros Sachtouris | result = [u'%s. \t%s' % (
|
69 | 99085b30 | Stavros Sachtouris | i + 1, line) for i, line in enumerate(f.readlines()) |
70 | 99085b30 | Stavros Sachtouris | if self._match(line, match_terms)] |
71 | 98093aac | Stavros Sachtouris | return result[- limit:]
|
72 | 73e0914d | Stavros Sachtouris | |
73 | fd5db045 | Stavros Sachtouris | def add(self, line): |
74 | 2d1f5058 | Stavros Sachtouris | line = line.replace(self.token, '...') if self.token else line |
75 | 98093aac | Stavros Sachtouris | with open(self.filepath, 'a+') as f: |
76 | 98093aac | Stavros Sachtouris | f.write(line + '\n')
|
77 | 73e0914d | Stavros Sachtouris | |
78 | edaf3ba6 | Stavros Sachtouris | def empty(self): |
79 | 98093aac | Stavros Sachtouris | with open(self.filepath, 'w'): |
80 | 98093aac | Stavros Sachtouris | pass
|
81 | 75a0ea7d | Stavros Sachtouris | |
82 | edaf3ba6 | Stavros Sachtouris | def clean(self): |
83 | edaf3ba6 | Stavros Sachtouris | """DEPRECATED in version 0.14"""
|
84 | edaf3ba6 | Stavros Sachtouris | return self.empty() |
85 | 75a0ea7d | Stavros Sachtouris | |
86 | edaf3ba6 | Stavros Sachtouris | def retrieve(self, cmd_id): |
87 | edaf3ba6 | Stavros Sachtouris | """DEPRECATED in version 0.14"""
|
88 | edaf3ba6 | Stavros Sachtouris | return self[cmd_id] |