Revision f17d6cb5
b/kamaki/cli/argument/__init__.py | ||
---|---|---|
64 | 64 |
def __init__(self, arity, help=None, parsed_name=None, default=None): |
65 | 65 |
self.arity = int(arity) |
66 | 66 |
self.help = '%s' % help or '' |
67 |
self.parsed_name = parsed_name |
|
68 |
self.default = default or (None if self.arity else False) |
|
69 | 67 |
|
70 |
@property |
|
71 |
def parsed_name(self): |
|
72 |
""" |
|
73 |
:returns: (str) of the form --smth or -s is recognised as a call to an |
|
74 |
argument instance |
|
75 |
""" |
|
76 |
return getattr(self, '_parsed_name', []) |
|
77 |
|
|
78 |
@parsed_name.setter |
|
79 |
def parsed_name(self, newname): |
|
80 |
assert newname, 'No parsed name for argument %s' % self |
|
81 |
self._parsed_name = getattr(self, '_parsed_name', []) |
|
82 |
if isinstance(newname, list) or isinstance(newname, tuple): |
|
83 |
self._parsed_name += list(newname) |
|
84 |
else: |
|
85 |
self._parsed_name.append('%s' % newname) |
|
68 |
assert parsed_name, 'No parsed name for argument %s' % self |
|
69 |
self.parsed_name = list(parsed_name) if isinstance( |
|
70 |
parsed_name, list) or isinstance(parsed_name, tuple) else ( |
|
71 |
'%s' % parsed_name).split() |
|
72 |
for name in self.parsed_name: |
|
73 |
assert name.count(' ') == 0, '%s: Invalid parse name "%s"' % ( |
|
74 |
self, name) |
|
75 |
msg = '%s: Invalid parse name "%s" should start with a "-"' % ( |
|
76 |
self, name) |
|
77 |
assert name.startswith('-'), msg |
|
78 |
|
|
79 |
self.default = default or (None if self.arity else False) |
|
86 | 80 |
|
87 | 81 |
@property |
88 | 82 |
def value(self): |
89 |
"""the value of the argument""" |
|
90 | 83 |
return getattr(self, '_value', self.default) |
91 | 84 |
|
92 | 85 |
@value.setter |
... | ... | |
95 | 88 |
|
96 | 89 |
def update_parser(self, parser, name): |
97 | 90 |
"""Update argument parser with self info""" |
98 |
action = 'append' if self.arity < 0\ |
|
99 |
else 'store_true' if self.arity == 0\ |
|
100 |
else 'store' |
|
91 |
action = 'append' if self.arity < 0 else ( |
|
92 |
'store' if self.arity else 'store_true') |
|
101 | 93 |
parser.add_argument( |
102 | 94 |
*self.parsed_name, |
103 |
dest=name, |
|
104 |
action=action, |
|
105 |
default=self.default, |
|
106 |
help=self.help) |
|
107 |
|
|
108 |
def main(self): |
|
109 |
"""Overide this method to give functionality to your args""" |
|
110 |
raise NotImplementedError |
|
95 |
dest=name, action=action, default=self.default, help=self.help) |
|
111 | 96 |
|
112 | 97 |
|
113 | 98 |
class ConfigArgument(Argument): |
... | ... | |
153 | 138 |
def get_cloud(self, cloud, option): |
154 | 139 |
return self.value.get_cloud(cloud, option) |
155 | 140 |
|
141 |
|
|
156 | 142 |
_config_arg = ConfigArgument( |
157 | 143 |
1, 'Path to configuration file', ('-c', '--config')) |
158 | 144 |
|
... | ... | |
305 | 291 |
@value.setter |
306 | 292 |
def value(self, newvalue): |
307 | 293 |
self._value = newvalue |
308 |
self.main() |
|
309 |
|
|
310 |
def main(self): |
|
311 |
"""Print current version""" |
|
312 |
if self.value: |
|
294 |
if newvalue: |
|
313 | 295 |
import kamaki |
314 | 296 |
print('kamaki %s' % kamaki.__version__) |
315 | 297 |
|
b/kamaki/cli/argument/test.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
#from mock import patch, call |
35 | 35 |
from unittest import TestCase |
36 |
from StringIO import StringIO |
|
36 | 37 |
#from itertools import product |
37 | 38 |
|
38 | 39 |
from kamaki.cli import argument |
... | ... | |
43 | 44 |
def test___init__(self): |
44 | 45 |
self.assertRaises(ValueError, argument.Argument, 'non-integer') |
45 | 46 |
self.assertRaises(AssertionError, argument.Argument, 1) |
47 |
self.assertRaises(AssertionError, argument.Argument, 0, 'noname') |
|
48 |
self.assertRaises(AssertionError, argument.Argument, 0, '--no name') |
|
49 |
self.assertRaises(AssertionError, argument.Argument, 0, ['-n', 'n m']) |
|
46 | 50 |
for arity, help, parsed_name, default in ( |
47 | 51 |
(0, 'help 0', '--zero', None), |
48 | 52 |
(1, 'help 1', ['--one', '-o'], 'lala'), |
... | ... | |
60 | 64 |
exp_default = default or (None if arity else False) |
61 | 65 |
self.assertEqual(exp_default, a.default) |
62 | 66 |
|
67 |
def test_value(self): |
|
68 |
a = argument.Argument(1, parsed_name='--value') |
|
69 |
for value in (None, '', 0, 0.1, -12, [1, 'a', 2.8], (3, 'lala'), 'pi'): |
|
70 |
a.value = value |
|
71 |
self.assertEqual(value, a.value) |
|
72 |
|
|
73 |
def test_update_parser(self): |
|
74 |
for i, arity in enumerate((-1, 0, 1)): |
|
75 |
arp = argument.ArgumentParser() |
|
76 |
pname, aname = '--pname%s' % i, 'a_name_%s' % i |
|
77 |
a = argument.Argument(arity, 'args', pname, 42) |
|
78 |
a.update_parser(arp, aname) |
|
79 |
|
|
80 |
f = StringIO() |
|
81 |
arp.print_usage(file=f), f.seek(0) |
|
82 |
usage, exp = f.readline(), '[%s%s]\n' % ( |
|
83 |
pname, (' %s' % aname.upper()) if arity else '') |
|
84 |
self.assertEqual(usage[-len(exp):], exp) |
|
85 |
del arp |
|
86 |
|
|
63 | 87 |
|
64 | 88 |
if __name__ == '__main__': |
65 | 89 |
from sys import argv |
b/kamaki/cli/commands/pithos.py | ||
---|---|---|
450 | 450 |
|
451 | 451 |
@command(pithos_cmds) |
452 | 452 |
class file_mkdir(_file_container_command, _optional_output_cmd): |
453 |
"""Create a directory""" |
|
454 |
|
|
455 |
__doc__ += '\n. '.join([ |
|
456 |
'Kamaki hanldes directories the same way as OOS Storage and Pithos+:', |
|
457 |
'A directory is an object with type "application/directory"', |
|
458 |
'An object with path dir/name can exist even if dir does not exist', |
|
459 |
'or even if dir is a non directory object. Users can modify dir', |
|
460 |
'without affecting the dir/name object in any way.']) |
|
453 |
"""Create a directory |
|
454 |
Kamaki hanldes directories the same way as OOS Storage and Pithos+: |
|
455 |
A directory is an object with type "application/directory" |
|
456 |
An object with path dir/name can exist even if dir does not exist |
|
457 |
or even if dir is a non directory object. Users can modify dir ' |
|
458 |
without affecting the dir/name object in any way. |
|
459 |
""" |
|
461 | 460 |
|
462 | 461 |
@errors.generic.all |
463 | 462 |
@errors.pithos.connection |
... | ... | |
533 | 532 |
class _source_destination_command(_file_container_command): |
534 | 533 |
|
535 | 534 |
arguments = dict( |
536 |
destination_account=ValueArgument('', ('a', '--dst-account')), |
|
535 |
destination_account=ValueArgument('', ('-a', '--dst-account')),
|
|
537 | 536 |
recursive=FlagArgument('', ('-R', '--recursive')), |
538 | 537 |
prefix=FlagArgument('', '--with-prefix', default=''), |
539 | 538 |
suffix=ValueArgument('', '--with-suffix', default=''), |
Also available in: Unified diff