Commit 69e16d8f authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Merge branch 'feature-argument' into develop

parents a38eb14e b01d6eb7
......@@ -35,6 +35,7 @@ from kamaki.cli.config import Config
from kamaki.cli.errors import CLISyntaxError, raiseCLIError
from kamaki.cli.utils import split_input
from logging import getLogger
from datetime import datetime as dtm
from argparse import ArgumentParser, ArgumentError
......@@ -236,6 +237,43 @@ class IntArgument(ValueArgument):
details=['Value %s not an int' % newvalue]))
class DateArgument(ValueArgument):
"""
:value type: a string formated in an acceptable date format
:value returns: same date in first of DATE_FORMATS
"""
DATE_FORMATS = ["%a %b %d %H:%M:%S %Y",
"%A, %d-%b-%y %H:%M:%S GMT",
"%a, %d %b %Y %H:%M:%S GMT"]
INPUT_FORMATS = DATE_FORMATS + ["%d-%m-%Y", "%H:%M:%S %d-%m-%Y"]
@property
def value(self):
return getattr(self, '_value', self.default)
@value.setter
def value(self, newvalue):
if newvalue is None:
return
self._value = self.format_date(newvalue)
def format_date(self, datestr):
for format in self.INPUT_FORMATS:
try:
t = dtm.strptime(datestr, format)
except ValueError:
continue
self._value = t.strftime(self.DATE_FORMATS[0])
return
raiseCLIError(None,
'Date Argument Error',
details='%s not a valid date. correct formats:\n\t%s'\
% (datestr, self.INPUT_FORMATS))
class VersionArgument(FlagArgument):
"""A flag argument with that prints current version"""
......
......@@ -36,14 +36,13 @@ from kamaki.cli.command_tree import CommandTree
from kamaki.cli.errors import raiseCLIError, CLISyntaxError
from kamaki.cli.utils import format_size, print_dict, pretty_keys
from kamaki.cli.argument import FlagArgument, ValueArgument, IntArgument
from kamaki.cli.argument import KeyValueArgument
from kamaki.cli.argument import KeyValueArgument, DateArgument
from kamaki.cli.argument import ProgressBarArgument
from kamaki.cli.commands import _command_init
from kamaki.clients.pithos import PithosClient, ClientError
from kamaki.cli.utils import bold
from sys import stdout
from time import localtime, strftime
from datetime import datetime as dtm
from logging import getLogger
kloger = getLogger('kamaki')
......@@ -137,44 +136,6 @@ class RangeArgument(ValueArgument):
(start, end) = (int(start), int(end))
self._value = '%s-%s' % (start, end)
class DateArgument(ValueArgument):
"""
:value type: a string formated in an acceptable date format
:value returns: same date in first of DATE_FORMATS
"""
DATE_FORMATS = ["%a %b %d %H:%M:%S %Y",
"%A, %d-%b-%y %H:%M:%S GMT",
"%a, %d %b %Y %H:%M:%S GMT"]
INPUT_FORMATS = DATE_FORMATS + ["%d-%m-%Y", "%H:%M:%S %d-%m-%Y"]
@property
def value(self):
return getattr(self, '_value', self.default)
@value.setter
def value(self, newvalue):
if newvalue is None:
return
self._value = self.format_date(newvalue)
def format_date(self, datestr):
for format in self.INPUT_FORMATS:
try:
t = dtm.strptime(datestr, format)
except ValueError:
continue
self._value = t.strftime(self.DATE_FORMATS[0])
return
raiseCLIError(None,
'Date Argument Error',
details='%s not a valid date. correct formats:\n\t%s'\
% (datestr, self.INPUT_FORMATS))
# Command specs
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment