Revision 5e383dd4
b/kamaki/cli/commands/history.py | ||
---|---|---|
1 | 1 |
#!/usr/bin/env python |
2 | 2 |
|
3 |
# Copyright 2012-2013 GRNET S.A. All rights reserved.
|
|
3 |
# Copyright 2012-2014 GRNET S.A. All rights reserved.
|
|
4 | 4 |
# |
5 | 5 |
# Redistribution and use in source and binary forms, with or |
6 | 6 |
# without modification, are permitted provided that the following |
... | ... | |
68 | 68 |
|
69 | 69 |
@errors.generic.all |
70 | 70 |
def _run(self, cmd_slice): |
71 |
lines = ['%s.\t%s' % (i, l) for i, l in enumerate(self.history[:])][ |
|
72 |
cmd_slice] |
|
71 |
c = self.history.counter |
|
72 |
lines = ['%s.\t%s' % (i + c, l) for i, l in enumerate( |
|
73 |
self.history[:])][cmd_slice] |
|
73 | 74 |
if not isinstance(cmd_slice, slice): |
74 | 75 |
lines = [lines, ] |
75 | 76 |
if self['match']: |
b/kamaki/cli/history.py | ||
---|---|---|
1 | 1 |
#!/usr/bin/env python |
2 | 2 |
|
3 |
# Copyright 2012-2013 GRNET S.A. All rights reserved.
|
|
3 |
# Copyright 2012-2014 GRNET S.A. All rights reserved.
|
|
4 | 4 |
# |
5 | 5 |
# Redistribution and use in source and binary forms, with or |
6 | 6 |
# without modification, are permitted provided that the following |
... | ... | |
40 | 40 |
def __init__(self, filepath, token=None): |
41 | 41 |
self.filepath = filepath |
42 | 42 |
self.token = token |
43 |
self._limit = 0 |
|
44 |
self.counter = 0 |
|
43 | 45 |
|
44 | 46 |
def __getitem__(self, cmd_ids): |
45 | 47 |
with codecs.open(self.filepath, mode='r', encoding='utf-8') as f: |
46 | 48 |
try: |
47 |
cmd_list = f.readlines() |
|
49 |
cmd_list = f.readlines()[1:]
|
|
48 | 50 |
return cmd_list[cmd_ids] |
49 | 51 |
except IndexError: |
50 | 52 |
return None |
51 | 53 |
|
54 |
@property |
|
55 |
def limit(self): |
|
56 |
return self._limit |
|
57 |
|
|
58 |
@limit.setter |
|
59 |
def limit(self, new_limit): |
|
60 |
new_limit = int(new_limit) |
|
61 |
if new_limit < 0: |
|
62 |
raise ValueError('Invalid history limit (%s)' % new_limit) |
|
63 |
old_limit, self._limit = self._limit, new_limit |
|
64 |
if self._limit and self._limit < old_limit: |
|
65 |
with codecs.open(self.filepath, mode='r', encoding='utf-8') as f: |
|
66 |
lines = f.readlines() |
|
67 |
old_len = len(lines[1:]) |
|
68 |
if old_len > new_limit: |
|
69 |
self.counter += old_len - new_limit |
|
70 |
f.write('%s\n' % self.counter) |
|
71 |
f.write(''.join(lines[(self.counter + 1):])) |
|
72 |
f.flush() |
|
73 |
|
|
52 | 74 |
@classmethod |
53 | 75 |
def _match(self, line, match_terms): |
54 | 76 |
if match_terms: |
... | ... | |
67 | 89 |
with codecs.open(self.filepath, mode='a+', encoding='utf-8') as f: |
68 | 90 |
f.write(line + '\n') |
69 | 91 |
f.flush() |
92 |
self.limit = self.limit |
|
70 | 93 |
|
71 | 94 |
def empty(self): |
72 | 95 |
with open(self.filepath, 'w') as f: |
96 |
f.write('0\n') |
|
73 | 97 |
f.flush() |
98 |
self.counter = 0 |
|
74 | 99 |
|
75 | 100 |
def clean(self): |
76 | 101 |
"""DEPRECATED since version 0.14""" |
b/kamaki/cli/test.py | ||
---|---|---|
37 | 37 |
from mock import patch, call |
38 | 38 |
from itertools import product |
39 | 39 |
|
40 |
from kamaki.cli.command_tree.test import Command, CommandTree |
|
41 |
from kamaki.cli.config.test import Config |
|
42 |
from kamaki.cli.argument.test import ( |
|
43 |
Argument, ConfigArgument, RuntimeConfigArgument, FlagArgument, |
|
44 |
ValueArgument, IntArgument, DateArgument, VersionArgument, |
|
45 |
RepeatableArgument, KeyValueArgument, ProgressBarArgument, |
|
46 |
ArgumentParseManager) |
|
47 |
from kamaki.cli.utils.test import UtilsMethods |
|
48 |
|
|
49 | 40 |
|
50 | 41 |
class History(TestCase): |
51 | 42 |
|
... | ... | |
88 | 79 |
history = self.HCLASS(self.file.name) |
89 | 80 |
history.empty() |
90 | 81 |
self.file.seek(0) |
91 |
self.assertEqual(self.file.read(), '') |
|
82 |
self.assertEqual(self.file.read(), '0\n')
|
|
92 | 83 |
|
93 | 84 |
def test_retrieve(self): |
94 | 85 |
sample_history = ( |
... | ... | |
107 | 98 |
for i in (0, len(sample_history) + 1, - len(sample_history) - 1): |
108 | 99 |
self.assertEqual(history.retrieve(i), None) |
109 | 100 |
for i in range(1, len(sample_history)): |
110 |
self.assertEqual(history.retrieve(i), sample_history[i - 1])
|
|
101 |
self.assertEqual(history.retrieve(i), sample_history[i]) |
|
111 | 102 |
self.assertEqual(history.retrieve(- i), sample_history[- i]) |
112 | 103 |
|
104 |
def test_limit(self): |
|
105 |
sample_history = ( |
|
106 |
'kamaki history show\n', |
|
107 |
'kamaki file list\n', |
|
108 |
'kamaki file create /pithos/f1\n', |
|
109 |
'kamaki file info /pithos/f1\n', |
|
110 |
'last command is always excluded') |
|
111 |
sample_len = len(sample_history) |
|
112 |
self.file.write(''.join(sample_history)) |
|
113 |
self.file.flush() |
|
114 |
history = self.HCLASS(self.file.name) |
|
115 |
|
|
116 |
for value, exp_e in ( |
|
117 |
(-2, ValueError), |
|
118 |
('non int', ValueError), |
|
119 |
(None, TypeError)): |
|
120 |
try: |
|
121 |
history.limit = value |
|
122 |
except Exception as e: |
|
123 |
self.assertTrue(isinstance(e, exp_e)) |
|
124 |
|
|
125 |
history.limit = 10 |
|
126 |
self.assertEqual(history.limit, 10) |
|
127 |
self.file.seek(0) |
|
128 |
self.assertEqual(len(self.file.readlines()), sample_len) |
|
129 |
|
|
130 |
history.limit = sample_len - 1 |
|
131 |
self.assertEqual(history.limit, sample_len - 1) |
|
132 |
self.file.seek(0) |
|
133 |
self.assertEqual(len(self.file.readlines()), sample_len) |
|
134 |
|
|
113 | 135 |
|
114 | 136 |
class LoggerMethods(TestCase): |
115 | 137 |
|
Also available in: Unified diff