Commit f17d6cb5 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Fully test argument.Argument

Refs: #4058
parent 5286e2c3
......@@ -64,29 +64,22 @@ class Argument(object):
def __init__(self, arity, help=None, parsed_name=None, default=None):
self.arity = int(arity) = '%s' % help or ''
self.parsed_name = parsed_name
self.default = default or (None if self.arity else False)
def parsed_name(self):
:returns: (str) of the form --smth or -s is recognised as a call to an
argument instance
return getattr(self, '_parsed_name', [])
def parsed_name(self, newname):
assert newname, 'No parsed name for argument %s' % self
self._parsed_name = getattr(self, '_parsed_name', [])
if isinstance(newname, list) or isinstance(newname, tuple):
self._parsed_name += list(newname)
self._parsed_name.append('%s' % newname)
assert parsed_name, 'No parsed name for argument %s' % self
self.parsed_name = list(parsed_name) if isinstance(
parsed_name, list) or isinstance(parsed_name, tuple) else (
'%s' % parsed_name).split()
for name in self.parsed_name:
assert name.count(' ') == 0, '%s: Invalid parse name "%s"' % (
self, name)
msg = '%s: Invalid parse name "%s" should start with a "-"' % (
self, name)
assert name.startswith('-'), msg
self.default = default or (None if self.arity else False)
def value(self):
"""the value of the argument"""
return getattr(self, '_value', self.default)
......@@ -95,19 +88,11 @@ class Argument(object):
def update_parser(self, parser, name):
"""Update argument parser with self info"""
action = 'append' if self.arity < 0\
else 'store_true' if self.arity == 0\
else 'store'
action = 'append' if self.arity < 0 else (
'store' if self.arity else 'store_true')
def main(self):
"""Overide this method to give functionality to your args"""
raise NotImplementedError
dest=name, action=action, default=self.default,
class ConfigArgument(Argument):
......@@ -153,6 +138,7 @@ class ConfigArgument(Argument):
def get_cloud(self, cloud, option):
return self.value.get_cloud(cloud, option)
_config_arg = ConfigArgument(
1, 'Path to configuration file', ('-c', '--config'))
......@@ -305,11 +291,7 @@ class VersionArgument(FlagArgument):
def value(self, newvalue):
self._value = newvalue
def main(self):
"""Print current version"""
if self.value:
if newvalue:
import kamaki
print('kamaki %s' % kamaki.__version__)
......@@ -33,6 +33,7 @@
#from mock import patch, call
from unittest import TestCase
from StringIO import StringIO
#from itertools import product
from kamaki.cli import argument
......@@ -43,6 +44,9 @@ class Argument(TestCase):
def test___init__(self):
self.assertRaises(ValueError, argument.Argument, 'non-integer')
self.assertRaises(AssertionError, argument.Argument, 1)
self.assertRaises(AssertionError, argument.Argument, 0, 'noname')
self.assertRaises(AssertionError, argument.Argument, 0, '--no name')
self.assertRaises(AssertionError, argument.Argument, 0, ['-n', 'n m'])
for arity, help, parsed_name, default in (
(0, 'help 0', '--zero', None),
(1, 'help 1', ['--one', '-o'], 'lala'),
......@@ -60,6 +64,26 @@ class Argument(TestCase):
exp_default = default or (None if arity else False)
self.assertEqual(exp_default, a.default)
def test_value(self):
a = argument.Argument(1, parsed_name='--value')
for value in (None, '', 0, 0.1, -12, [1, 'a', 2.8], (3, 'lala'), 'pi'):
a.value = value
self.assertEqual(value, a.value)
def test_update_parser(self):
for i, arity in enumerate((-1, 0, 1)):
arp = argument.ArgumentParser()
pname, aname = '--pname%s' % i, 'a_name_%s' % i
a = argument.Argument(arity, 'args', pname, 42)
a.update_parser(arp, aname)
f = StringIO()
usage, exp = f.readline(), '[%s%s]\n' % (
pname, (' %s' % aname.upper()) if arity else '')
self.assertEqual(usage[-len(exp):], exp)
del arp
if __name__ == '__main__':
from sys import argv
......@@ -450,14 +450,13 @@ class file_list(_file_container_command, _optional_json):
class file_mkdir(_file_container_command, _optional_output_cmd):
"""Create a directory"""
__doc__ += '\n. '.join([
'Kamaki hanldes directories the same way as OOS Storage and Pithos+:',
'A directory is an object with type "application/directory"',
'An object with path dir/name can exist even if dir does not exist',
'or even if dir is a non directory object. Users can modify dir',
'without affecting the dir/name object in any way.'])
"""Create a directory
Kamaki hanldes directories the same way as OOS Storage and Pithos+:
A directory is an object with type "application/directory"
An object with path dir/name can exist even if dir does not exist
or even if dir is a non directory object. Users can modify dir '
without affecting the dir/name object in any way.
......@@ -533,7 +532,7 @@ class file_create(_file_container_command, _optional_output_cmd):
class _source_destination_command(_file_container_command):
arguments = dict(
destination_account=ValueArgument('', ('a', '--dst-account')),
destination_account=ValueArgument('', ('-a', '--dst-account')),
recursive=FlagArgument('', ('-R', '--recursive')),
prefix=FlagArgument('', '--with-prefix', default=''),
suffix=ValueArgument('', '--with-suffix', default=''),
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