Revision 7b109aa7 kamaki/cli/argument/__init__.py
b/kamaki/cli/argument/__init__.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
from kamaki.cli.config import Config |
35 | 35 |
from kamaki.cli.errors import CLISyntaxError, raiseCLIError |
36 |
from kamaki.cli.utils import split_input |
|
36 |
from kamaki.cli.utils import split_input, to_bytes
|
|
37 | 37 |
|
38 | 38 |
from datetime import datetime as dtm |
39 | 39 |
from time import mktime |
... | ... | |
228 | 228 |
details=['Value %s not an int' % newvalue])) |
229 | 229 |
|
230 | 230 |
|
231 |
class DataSizeArgument(ValueArgument): |
|
232 |
"""Input: a string of the form <number><unit> |
|
233 |
Output: the number of bytes |
|
234 |
Units: B, KiB, KB, MiB, MB, GiB, GB, TiB, TB |
|
235 |
""" |
|
236 |
|
|
237 |
@property |
|
238 |
def value(self): |
|
239 |
return getattr(self, '_value', self.default) |
|
240 |
|
|
241 |
def _calculate_limit(self, user_input): |
|
242 |
limit = 0 |
|
243 |
try: |
|
244 |
limit = int(user_input) |
|
245 |
except ValueError: |
|
246 |
index = 0 |
|
247 |
digits = [str(num) for num in range(0, 10)] + ['.'] |
|
248 |
while user_input[index] in digits: |
|
249 |
index += 1 |
|
250 |
limit = user_input[:index] |
|
251 |
format = user_input[index:] |
|
252 |
try: |
|
253 |
return to_bytes(limit, format) |
|
254 |
except Exception as qe: |
|
255 |
msg = 'Failed to convert %s to bytes' % user_input, |
|
256 |
raiseCLIError(qe, msg, details=[ |
|
257 |
'Syntax: containerlimit set <limit>[format] [container]', |
|
258 |
'e.g.,: containerlimit set 2.3GB mycontainer', |
|
259 |
'Valid formats:', |
|
260 |
'(*1024): B, KiB, MiB, GiB, TiB', |
|
261 |
'(*1000): B, KB, MB, GB, TB']) |
|
262 |
return limit |
|
263 |
|
|
264 |
@value.setter |
|
265 |
def value(self, new_value): |
|
266 |
if new_value: |
|
267 |
self._value = self._calculate_limit(new_value) |
|
268 |
|
|
269 |
|
|
231 | 270 |
class DateArgument(ValueArgument): |
232 | 271 |
|
233 | 272 |
DATE_FORMAT = '%a %b %d %H:%M:%S %Y' |
Also available in: Unified diff