root / kamaki / cli / history.py @ 304c90b5
History | View | Annotate | Download (2.9 kB)
1 | 73e0914d | Stavros Sachtouris | #!/usr/bin/env python
|
---|---|---|---|
2 | 73e0914d | Stavros Sachtouris | |
3 | 73e0914d | Stavros Sachtouris | # Copyright 2012 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 | fd5db045 | Stavros Sachtouris | |
37 | 73e0914d | Stavros Sachtouris | def order_free_contains(containing, contained): |
38 | fd5db045 | Stavros Sachtouris | superset = containing.split() |
39 | fd5db045 | Stavros Sachtouris | for term in contained.split(): |
40 | fd5db045 | Stavros Sachtouris | if term not in superset: |
41 | fd5db045 | Stavros Sachtouris | return False |
42 | fd5db045 | Stavros Sachtouris | return True |
43 | fd5db045 | Stavros Sachtouris | |
44 | 73e0914d | Stavros Sachtouris | |
45 | 73e0914d | Stavros Sachtouris | class History(object): |
46 | fd5db045 | Stavros Sachtouris | def __init__(self, filepath): |
47 | fd5db045 | Stavros Sachtouris | self.filepath = filepath
|
48 | 73e0914d | Stavros Sachtouris | |
49 | fd5db045 | Stavros Sachtouris | @classmethod
|
50 | fd5db045 | Stavros Sachtouris | def _match(self, line, match_terms): |
51 | fd5db045 | Stavros Sachtouris | if match_terms is None: |
52 | fd5db045 | Stavros Sachtouris | return True |
53 | fd5db045 | Stavros Sachtouris | for term in match_terms.split(): |
54 | fd5db045 | Stavros Sachtouris | if term not in line: |
55 | fd5db045 | Stavros Sachtouris | return False |
56 | fd5db045 | Stavros Sachtouris | return True |
57 | 73e0914d | Stavros Sachtouris | |
58 | fd5db045 | Stavros Sachtouris | def get(self, match_terms=None, limit=0): |
59 | fd5db045 | Stavros Sachtouris | f = open(self.filepath, 'r') |
60 | fd5db045 | Stavros Sachtouris | result = ['%s. \t%s' % (index + 1, line)\ |
61 | fd5db045 | Stavros Sachtouris | for index, line in enumerate(f.readlines())\ |
62 | fd5db045 | Stavros Sachtouris | if self._match(line, match_terms)] |
63 | fd5db045 | Stavros Sachtouris | offset = len(result) - limit if limit and len(result) > limit else 0 |
64 | fd5db045 | Stavros Sachtouris | return result[offset:]
|
65 | 73e0914d | Stavros Sachtouris | |
66 | fd5db045 | Stavros Sachtouris | def add(self, line): |
67 | fd5db045 | Stavros Sachtouris | f = open(self.filepath, 'a+') |
68 | fd5db045 | Stavros Sachtouris | f.write(line + '\n')
|
69 | fd5db045 | Stavros Sachtouris | f.close() |
70 | 73e0914d | Stavros Sachtouris | |
71 | fd5db045 | Stavros Sachtouris | def clean(self): |
72 | fd5db045 | Stavros Sachtouris | f = open(self.filepath, 'w') |
73 | fd5db045 | Stavros Sachtouris | f.close() |
74 | 75a0ea7d | Stavros Sachtouris | |
75 | 75a0ea7d | Stavros Sachtouris | def retrieve(self, cmd_id): |
76 | 75a0ea7d | Stavros Sachtouris | """
|
77 | 75a0ea7d | Stavros Sachtouris | :param cmd_id: (int) the id of the command to retrieve
|
78 | 75a0ea7d | Stavros Sachtouris |
|
79 | 75a0ea7d | Stavros Sachtouris | :returns: (str) the stored command record without the id
|
80 | 75a0ea7d | Stavros Sachtouris | """
|
81 | 304c90b5 | Stavros Sachtouris | cmd_id = int(cmd_id)
|
82 | 75a0ea7d | Stavros Sachtouris | with open(self.filepath) as f: |
83 | 304c90b5 | Stavros Sachtouris | try:
|
84 | 304c90b5 | Stavros Sachtouris | return f.readlines()[cmd_id - 1] |
85 | 304c90b5 | Stavros Sachtouris | except IndexError: |
86 | 304c90b5 | Stavros Sachtouris | return None |