Revision 76f58e2e kamaki/cli/commands/pithos.py
b/kamaki/cli/commands/pithos.py | ||
---|---|---|
34 | 34 |
from time import localtime, strftime |
35 | 35 |
from os import path, makedirs, walk |
36 | 36 |
from io import StringIO |
37 |
from pydoc import pager |
|
37 | 38 |
|
38 | 39 |
from kamaki.cli import command |
39 | 40 |
from kamaki.cli.command_tree import CommandTree |
40 | 41 |
from kamaki.cli.errors import raiseCLIError, CLISyntaxError, CLIBaseUrlError |
41 | 42 |
from kamaki.cli.utils import ( |
42 |
format_size, to_bytes, print_dict, print_items, pager, bold, ask_user, |
|
43 |
get_path_size, guess_mime_type) |
|
43 |
format_size, to_bytes, bold, get_path_size, guess_mime_type) |
|
44 | 44 |
from kamaki.cli.argument import FlagArgument, ValueArgument, IntArgument |
45 | 45 |
from kamaki.cli.argument import KeyValueArgument, DateArgument |
46 | 46 |
from kamaki.cli.argument import ProgressBarArgument |
... | ... | |
363 | 363 |
'%s%s. ' % (empty_space, index)) if self['enum'] else '' |
364 | 364 |
if self['detail']: |
365 | 365 |
self.writeln('%s%s' % (prfx, oname)) |
366 |
print_dict(pretty_obj, exclude=('name'), out=self._out)
|
|
366 |
self.print_dict(pretty_obj, exclude=('name'))
|
|
367 | 367 |
self.writeln() |
368 | 368 |
else: |
369 | 369 |
oname = '%s%9s %s' % (prfx, size, oname) |
... | ... | |
383 | 383 |
pretty_c = container.copy() |
384 | 384 |
if 'bytes' in container: |
385 | 385 |
pretty_c['bytes'] = '%s (%s)' % (container['bytes'], size) |
386 |
print_dict(pretty_c, exclude=('name'), out=self._out)
|
|
386 |
self.print_dict(pretty_c, exclude=('name'))
|
|
387 | 387 |
self.writeln() |
388 | 388 |
else: |
389 | 389 |
if 'count' in container and 'bytes' in container: |
... | ... | |
1408 | 1408 |
if_match=self['if_match'], |
1409 | 1409 |
if_none_match=self['if_none_match'], |
1410 | 1410 |
if_modified_since=self['if_modified_since'], |
1411 |
if_unmodified_since=self['if_unmodified_since']), print_dict) |
|
1411 |
if_unmodified_since=self['if_unmodified_since']), self.print_dict)
|
|
1412 | 1412 |
|
1413 | 1413 |
def main(self, container___path): |
1414 | 1414 |
super(self.__class__, self)._run( |
... | ... | |
1451 | 1451 |
@errors.pithos.object_path |
1452 | 1452 |
def _run(self): |
1453 | 1453 |
if self.path: |
1454 |
if self['yes'] or ask_user( |
|
1455 |
'Delete %s:%s ?' % (self.container, self.path), |
|
1456 |
out=self._out, user_in=self._in): |
|
1454 |
if self['yes'] or self.ask_user( |
|
1455 |
'Delete %s:%s ?' % (self.container, self.path)): |
|
1457 | 1456 |
self._optional_output(self.client.del_object( |
1458 | 1457 |
self.path, |
1459 | 1458 |
until=self['until'], |
... | ... | |
1465 | 1464 |
ask_msg = 'Delete container contents' |
1466 | 1465 |
else: |
1467 | 1466 |
ask_msg = 'Delete container' |
1468 |
if self['yes'] or ask_user( |
|
1469 |
'%s %s ?' % (ask_msg, self.container), |
|
1470 |
out=self._out, user_in=self._in): |
|
1467 |
if self['yes'] or self.ask_user( |
|
1468 |
'%s %s ?' % (ask_msg, self.container)): |
|
1471 | 1469 |
self._optional_output(self.client.del_container( |
1472 | 1470 |
until=self['until'], |
1473 | 1471 |
delimiter='/' if self['recursive'] else self['delimiter'])) |
... | ... | |
1501 | 1499 |
@errors.pithos.connection |
1502 | 1500 |
@errors.pithos.container |
1503 | 1501 |
def _run(self): |
1504 |
if self['yes'] or ask_user( |
|
1505 |
'Purge container %s?' % self.container, |
|
1506 |
out=self._out, user_in=self._in): |
|
1502 |
if self['yes'] or self.ask_user( |
|
1503 |
'Purge container %s?' % self.container): |
|
1507 | 1504 |
try: |
1508 | 1505 |
r = self.client.purge_container() |
1509 | 1506 |
except ClientError as ce: |
... | ... | |
1571 | 1568 |
""" |
1572 | 1569 |
|
1573 | 1570 |
|
1574 |
def print_permissions(permissions_dict, out): |
|
1575 |
expected_keys = ('read', 'write') |
|
1576 |
if set(permissions_dict).issubset(expected_keys): |
|
1577 |
print_dict(permissions_dict, out=out) |
|
1578 |
else: |
|
1579 |
invalid_keys = set(permissions_dict.keys()).difference(expected_keys) |
|
1580 |
raiseCLIError( |
|
1581 |
'Illegal permission keys: %s' % ', '.join(invalid_keys), |
|
1582 |
importance=1, details=[ |
|
1583 |
'Valid permission types: %s' % ' '.join(expected_keys)]) |
|
1584 |
|
|
1585 |
|
|
1586 | 1571 |
@command(pithos_cmds) |
1587 | 1572 |
class file_permissions_get(_file_container_command, _optional_json): |
1588 | 1573 |
"""Get read and write permissions of an object""" |
1589 | 1574 |
|
1575 |
def print_permissions(self, permissions_dict, out): |
|
1576 |
expected_keys = ('read', 'write') |
|
1577 |
if set(permissions_dict).issubset(expected_keys): |
|
1578 |
self.print_dict(permissions_dict, out) |
|
1579 |
else: |
|
1580 |
invalid_keys = set(permissions_dict.keys()).difference( |
|
1581 |
expected_keys) |
|
1582 |
raiseCLIError( |
|
1583 |
'Illegal permission keys: %s' % ', '.join(invalid_keys), |
|
1584 |
importance=1, details=[ |
|
1585 |
'Valid permission types: %s' % ' '.join(expected_keys)]) |
|
1586 |
|
|
1590 | 1587 |
@errors.generic.all |
1591 | 1588 |
@errors.pithos.connection |
1592 | 1589 |
@errors.pithos.container |
1593 | 1590 |
@errors.pithos.object_path |
1594 | 1591 |
def _run(self): |
1595 | 1592 |
self._print( |
1596 |
self.client.get_object_sharing(self.path), print_permissions) |
|
1593 |
self.client.get_object_sharing(self.path), self.print_permissions)
|
|
1597 | 1594 |
|
1598 | 1595 |
def main(self, container___path): |
1599 | 1596 |
super(self.__class__, self)._run( |
... | ... | |
1686 | 1683 |
else: |
1687 | 1684 |
r = self.client.get_object_info( |
1688 | 1685 |
self.path, version=self['object_version']) |
1689 |
self._print(r, print_dict) |
|
1686 |
self._print(r, self.print_dict)
|
|
1690 | 1687 |
|
1691 | 1688 |
def main(self, container____path__=None): |
1692 | 1689 |
super(self.__class__, self)._run(container____path__) |
... | ... | |
1742 | 1739 |
self.path, |
1743 | 1740 |
version=self['object_version']) |
1744 | 1741 |
if r: |
1745 |
self._print(r, print_dict) |
|
1742 |
self._print(r, self.print_dict)
|
|
1746 | 1743 |
|
1747 | 1744 |
def main(self, container____path__=None): |
1748 | 1745 |
super(self.__class__, self)._run(container____path__) |
... | ... | |
1813 | 1810 |
if not self['in_bytes']: |
1814 | 1811 |
for k in output: |
1815 | 1812 |
output[k] = format_size(output[k]) |
1816 |
print_dict(output, '-', out=self._out)
|
|
1813 |
self.print_dict(output, '-')
|
|
1817 | 1814 |
|
1818 | 1815 |
self._print( |
1819 | 1816 |
self.client.get_account_quota(), pretty_print, out=self._out) |
... | ... | |
1844 | 1841 |
if not self['in_bytes']: |
1845 | 1842 |
for k, v in output.items(): |
1846 | 1843 |
output[k] = 'unlimited' if '0' == v else format_size(v) |
1847 |
print_dict(output, '-') |
|
1844 |
self.print_dict(output, '-')
|
|
1848 | 1845 |
|
1849 | 1846 |
self._print( |
1850 | 1847 |
self.client.get_container_limit(self.container), pretty_print) |
... | ... | |
1918 | 1915 |
@errors.pithos.container |
1919 | 1916 |
def _run(self): |
1920 | 1917 |
self._print( |
1921 |
self.client.get_container_versioning(self.container), print_dict) |
|
1918 |
self.client.get_container_versioning(self.container), |
|
1919 |
self.print_dict) |
|
1922 | 1920 |
|
1923 | 1921 |
def main(self, container): |
1924 | 1922 |
super(self.__class__, self)._run() |
... | ... | |
1961 | 1959 |
@errors.generic.all |
1962 | 1960 |
@errors.pithos.connection |
1963 | 1961 |
def _run(self): |
1964 |
self._print(self.client.get_account_group(), print_dict, delim='-') |
|
1962 |
self._print( |
|
1963 |
self.client.get_account_group(), self.print_dict, delim='-') |
|
1965 | 1964 |
|
1966 | 1965 |
def main(self): |
1967 | 1966 |
super(self.__class__, self)._run() |
... | ... | |
2027 | 2026 |
self._run() |
2028 | 2027 |
|
2029 | 2028 |
|
2030 |
def version_print(versions, out): |
|
2031 |
print_items( |
|
2032 |
[dict(id=vitem[0], created=strftime( |
|
2033 |
'%d-%m-%Y %H:%M:%S', |
|
2034 |
localtime(float(vitem[1])))) for vitem in versions], |
|
2035 |
out=out) |
|
2036 |
|
|
2037 |
|
|
2038 | 2029 |
@command(pithos_cmds) |
2039 | 2030 |
class file_versions(_file_container_command, _optional_json): |
2040 | 2031 |
"""Get the list of object versions |
... | ... | |
2045 | 2036 |
/file info -h |
2046 | 2037 |
""" |
2047 | 2038 |
|
2039 |
def version_print(self, versions, out): |
|
2040 |
self.print_items( |
|
2041 |
[dict(id=vitem[0], created=strftime( |
|
2042 |
'%d-%m-%Y %H:%M:%S', |
|
2043 |
localtime(float(vitem[1])))) for vitem in versions], |
|
2044 |
out=out) |
|
2045 |
|
|
2048 | 2046 |
@errors.generic.all |
2049 | 2047 |
@errors.pithos.connection |
2050 | 2048 |
@errors.pithos.container |
2051 | 2049 |
@errors.pithos.object_path |
2052 | 2050 |
def _run(self): |
2053 | 2051 |
self._print( |
2054 |
self.client.get_object_versionlist(self.path), version_print) |
|
2052 |
self.client.get_object_versionlist(self.path), self.version_print)
|
|
2055 | 2053 |
|
2056 | 2054 |
def main(self, container___path): |
2057 | 2055 |
super(file_versions, self)._run( |
Also available in: Unified diff