Revision 545c6c29 kamaki/cli/commands/pithos.py

b/kamaki/cli/commands/pithos.py
39 39
from kamaki.cli.command_tree import CommandTree
40 40
from kamaki.cli.errors import raiseCLIError, CLISyntaxError
41 41
from kamaki.cli.utils import (
42
    format_size, to_bytes, print_dict, print_items, pretty_keys,
42
    format_size, to_bytes, print_dict, print_items, pretty_keys, pretty_dict,
43 43
    page_hold, bold, ask_user, get_path_size, print_json)
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
47
from kamaki.cli.commands import _command_init, errors, _optional_output_cmd
47
from kamaki.cli.commands import _command_init, errors
48
from kamaki.cli.commands import _optional_output_cmd, _optional_json
48 49
from kamaki.clients.pithos import PithosClient, ClientError
49 50
from kamaki.clients.astakos import AstakosClient
50 51

  
......
290 291

  
291 292

  
292 293
@command(pithos_cmds)
293
class file_list(_file_container_command):
294
class file_list(_file_container_command, _optional_json):
294 295
    """List containers, object trees or objects in a directory
295 296
    Use with:
296 297
    1 no parameters : containers in current account
......
329 330
        exact_match=FlagArgument(
330 331
            'Show only objects that match exactly with path',
331 332
            '--exact-match'),
332
        enum=FlagArgument('Enumerate results', '--enumerate'),
333
        json_output=FlagArgument('show output in json', ('-j', '--json'))
333
        enum=FlagArgument('Enumerate results', '--enumerate')
334 334
    )
335 335

  
336 336
    def print_objects(self, object_list):
......
407 407
                if_unmodified_since=self['if_unmodified_since'],
408 408
                until=self['until'],
409 409
                show_only_shared=self['shared'])
410
            self.print_containers(r.json)
410
            self._print(r.json, self.print_containers)
411 411
        else:
412 412
            prefix = self.path or self['prefix']
413 413
            r = self.client.container_get(
......
421 421
                until=self['until'],
422 422
                meta=self['meta'],
423 423
                show_only_shared=self['shared'])
424
            self.print_objects(r.json)
424
            self._print(r.json, self.print_objects)
425 425

  
426 426
    def main(self, container____path__=None):
427 427
        super(self.__class__, self)._run(container____path__)
......
1418 1418

  
1419 1419

  
1420 1420
@command(pithos_cmds)
1421
class file_hashmap(_file_container_command):
1421
class file_hashmap(_file_container_command, _optional_json):
1422 1422
    """Get the hash-map of an object"""
1423 1423

  
1424 1424
    arguments = dict(
1425 1425
        if_match=ValueArgument('show output if ETags match', '--if-match'),
1426 1426
        if_none_match=ValueArgument(
1427
            'show output if ETags match',
1428
            '--if-none-match'),
1427
            'show output if ETags match', '--if-none-match'),
1429 1428
        if_modified_since=DateArgument(
1430
            'show output modified since then',
1431
            '--if-modified-since'),
1429
            'show output modified since then', '--if-modified-since'),
1432 1430
        if_unmodified_since=DateArgument(
1433
            'show output unmodified since then',
1434
            '--if-unmodified-since'),
1431
            'show output unmodified since then', '--if-unmodified-since'),
1435 1432
        object_version=ValueArgument(
1436
            'get the specific version',
1437
            ('-O', '--object-version')),
1438
        json_output=FlagArgument('show headers in json', ('-j', '--json'))
1433
            'get the specific version', ('-O', '--object-version'))
1439 1434
    )
1440 1435

  
1441 1436
    @errors.generic.all
......
1443 1438
    @errors.pithos.container
1444 1439
    @errors.pithos.object_path
1445 1440
    def _run(self):
1446
        data = self.client.get_object_hashmap(
1441
        self._print(self.client.get_object_hashmap(
1447 1442
            self.path,
1448 1443
            version=self['object_version'],
1449 1444
            if_match=self['if_match'],
1450 1445
            if_none_match=self['if_none_match'],
1451 1446
            if_modified_since=self['if_modified_since'],
1452
            if_unmodified_since=self['if_unmodified_since'])
1453
        printer = print_json if self['json_output'] else print_dict
1454
        printer(data)
1447
            if_unmodified_since=self['if_unmodified_since']), print_dict)
1455 1448

  
1456 1449
    def main(self, container___path):
1457 1450
        super(self.__class__, self)._run(
......
1614 1607
    """
1615 1608

  
1616 1609

  
1610
def print_permissions(permissions_dict):
1611
    expected_keys = ('read', 'write')
1612
    if set(permissions_dict).issubset(expected_keys):
1613
        print_dict(permissions_dict)
1614
    else:
1615
        invalid_keys = set(permissions_dict.keys()).difference(expected_keys)
1616
        raiseCLIError(
1617
            'Illegal permission keys: %s' % ', '.join(invalid_keys),
1618
            importance=1, details=[
1619
                'Valid permission types: %s' % ' '.join(expected_keys)])
1620

  
1621

  
1617 1622
@command(pithos_cmds)
1618
class file_permissions_get(_file_container_command):
1623
class file_permissions_get(_file_container_command, _optional_json):
1619 1624
    """Get read and write permissions of an object"""
1620 1625

  
1621 1626
    @errors.generic.all
......
1623 1628
    @errors.pithos.container
1624 1629
    @errors.pithos.object_path
1625 1630
    def _run(self):
1626
        r = self.client.get_object_sharing(self.path)
1627
        print_dict(r)
1631
        self._print(
1632
            self.client.get_object_sharing(self.path), print_permissions)
1628 1633

  
1629 1634
    def main(self, container___path):
1630 1635
        super(self.__class__, self)._run(
......
1697 1702

  
1698 1703

  
1699 1704
@command(pithos_cmds)
1700
class file_info(_file_container_command):
1705
class file_info(_file_container_command, _optional_json):
1701 1706
    """Get detailed information for user account, containers or objects
1702 1707
    to get account info:    /file info
1703 1708
    to get container info:  /file info <container>
......
1707 1712
    arguments = dict(
1708 1713
        object_version=ValueArgument(
1709 1714
            'show specific version \ (applies only for objects)',
1710
            ('-O', '--object-version')),
1711
        json_output=FlagArgument('show headers in json', ('-j', '--json'))
1715
            ('-O', '--object-version'))
1712 1716
    )
1713 1717

  
1714 1718
    @errors.generic.all
......
1724 1728
            r = self.client.get_object_info(
1725 1729
                self.path,
1726 1730
                version=self['object_version'])
1727
        printer = print_json if self['json_output'] else print_dict
1728
        printer(r)
1731
        self._print(r, print_dict)
1729 1732

  
1730 1733
    def main(self, container____path__=None):
1731 1734
        super(self.__class__, self)._run(container____path__)
......
1740 1743

  
1741 1744

  
1742 1745
@command(pithos_cmds)
1743
class file_metadata_get(_file_container_command):
1746
class file_metadata_get(_file_container_command, _optional_json):
1744 1747
    """Get metadata for account, containers or objects"""
1745 1748

  
1746 1749
    arguments = dict(
......
1748 1751
        until=DateArgument('show metadata until then', '--until'),
1749 1752
        object_version=ValueArgument(
1750 1753
            'show specific version \ (applies only for objects)',
1751
            ('-O', '--object-version')),
1752
        json_output=FlagArgument('show headers in json', ('-j', '--json'))
1754
            ('-O', '--object-version'))
1753 1755
    )
1754 1756

  
1755 1757
    @errors.generic.all
......
1758 1760
    @errors.pithos.object_path
1759 1761
    def _run(self):
1760 1762
        until = self['until']
1763
        r = None
1761 1764
        if self.container is None:
1762 1765
            if self['detail']:
1763 1766
                r = self.client.get_account_info(until=until)
1764 1767
            else:
1765 1768
                r = self.client.get_account_meta(until=until)
1766 1769
                r = pretty_keys(r, '-')
1767
            print(bold(self.client.account))
1768 1770
        elif self.path is None:
1769 1771
            if self['detail']:
1770 1772
                r = self.client.get_container_info(until=until)
......
1787 1789
                    version=self['object_version'])
1788 1790
                r = pretty_keys(pretty_keys(r, '-'))
1789 1791
        if r:
1790
            printer = print_json if self['json_output'] else print_dict
1791
            printer(r)
1792
            self._print(r, print_dict)
1792 1793

  
1793 1794
    def main(self, container____path__=None):
1794 1795
        super(self.__class__, self)._run(container____path__)
......
1844 1845

  
1845 1846

  
1846 1847
@command(pithos_cmds)
1847
class file_quota(_file_account_command):
1848
class file_quota(_file_account_command, _optional_json):
1848 1849
    """Get account quota"""
1849 1850

  
1850 1851
    arguments = dict(
......
1854 1855
    @errors.generic.all
1855 1856
    @errors.pithos.connection
1856 1857
    def _run(self):
1857
        reply = self.client.get_account_quota()
1858
        if not self['in_bytes']:
1859
            for k in reply:
1860
                reply[k] = format_size(reply[k])
1861
        print_dict(pretty_keys(reply, '-'))
1858

  
1859
        def pretty_print(output):
1860
            if not self['in_bytes']:
1861
                for k in output:
1862
                    output[k] = format_size(output[k])
1863
            pretty_dict(output, '-')
1864

  
1865
        self._print(self.client.get_account_quota(), pretty_print)
1862 1866

  
1863 1867
    def main(self, custom_uuid=None):
1864 1868
        super(self.__class__, self)._run(custom_account=custom_uuid)
......
1871 1875

  
1872 1876

  
1873 1877
@command(pithos_cmds)
1874
class file_containerlimit_get(_file_container_command):
1878
class file_containerlimit_get(_file_container_command, _optional_json):
1875 1879
    """Get container size limit"""
1876 1880

  
1877 1881
    arguments = dict(
......
1881 1885
    @errors.generic.all
1882 1886
    @errors.pithos.container
1883 1887
    def _run(self):
1884
        reply = self.client.get_container_limit(self.container)
1885
        if not self['in_bytes']:
1886
            for k, v in reply.items():
1887
                reply[k] = 'unlimited' if '0' == v else format_size(v)
1888
        print_dict(pretty_keys(reply, '-'))
1888

  
1889
        def pretty_print(output):
1890
            if not self['in_bytes']:
1891
                for k, v in output.items():
1892
                    output[k] = 'unlimited' if '0' == v else format_size(v)
1893
            pretty_dict(output, '-')
1894

  
1895
        self._print(
1896
            self.client.get_container_limit(self.container), pretty_print)
1889 1897

  
1890 1898
    def main(self, container=None):
1891 1899
        super(self.__class__, self)._run()
......
1894 1902

  
1895 1903

  
1896 1904
@command(pithos_cmds)
1897
class file_containerlimit_set(_file_account_command):
1905
class file_containerlimit_set(_file_account_command, _optional_output_cmd):
1898 1906
    """Set new storage limit for a container
1899 1907
    By default, the limit is set in bytes
1900 1908
    Users may specify a different unit, e.g:
......
1948 1956

  
1949 1957

  
1950 1958
@command(pithos_cmds)
1951
class file_versioning_get(_file_account_command):
1959
class file_versioning_get(_file_account_command, _optional_json):
1952 1960
    """Get  versioning for account or container"""
1953 1961

  
1954 1962
    @errors.generic.all
1955 1963
    @errors.pithos.connection
1956 1964
    @errors.pithos.container
1957 1965
    def _run(self):
1958
        if self.container:
1959
            r = self.client.get_container_versioning(self.container)
1960
        else:
1961
            r = self.client.get_account_versioning()
1962
        print_dict(r)
1966
        #if self.container:
1967
        #    r = self.client.get_container_versioning(self.container)
1968
        #else:
1969
        #    r = self.client.get_account_versioning()
1970
        self._print(
1971
            self.client.get_container_versioning(self.container) if (
1972
                self.container) else self.client.get_account_versioning(),
1973
            print_dict)
1963 1974

  
1964 1975
    def main(self, container=None):
1965 1976
        super(self.__class__, self)._run()
......
1999 2010

  
2000 2011

  
2001 2012
@command(pithos_cmds)
2002
class file_group_get(_file_account_command):
2003
    """Get groups and group members"""
2013
class file_group_list(_file_account_command, _optional_json):
2014
    """list all groups and group members"""
2004 2015

  
2005 2016
    @errors.generic.all
2006 2017
    @errors.pithos.connection
2007 2018
    def _run(self):
2008
        r = self.client.get_account_group()
2009
        print_dict(pretty_keys(r, '-'))
2019
        self._print(self.client.get_account_group(), pretty_dict, delim='-')
2010 2020

  
2011 2021
    def main(self):
2012 2022
        super(self.__class__, self)._run()
......
2045 2055

  
2046 2056

  
2047 2057
@command(pithos_cmds)
2048
class file_sharers(_file_account_command):
2058
class file_sharers(_file_account_command, _optional_json):
2049 2059
    """List the accounts that share objects with current user"""
2050 2060

  
2051 2061
    arguments = dict(
......
2057 2067
    @errors.pithos.connection
2058 2068
    def _run(self):
2059 2069
        accounts = self.client.get_sharing_accounts(marker=self['marker'])
2060
        if self['detail']:
2061
            print_items(accounts)
2070
        if self['json_output'] or self['detail']:
2071
            self._print(accounts)
2062 2072
        else:
2063
            print_items([acc['name'] for acc in accounts])
2073
            self._print([acc['name'] for acc in accounts])
2064 2074

  
2065 2075
    def main(self):
2066 2076
        super(self.__class__, self)._run()
2067 2077
        self._run()
2068 2078

  
2069 2079

  
2080
def version_print(versions):
2081
    print_items([dict(id=vitem[0], created=strftime(
2082
        '%d-%m-%Y %H:%M:%S',
2083
        localtime(float(vitem[1])))) for vitem in versions])
2084

  
2085

  
2070 2086
@command(pithos_cmds)
2071
class file_versions(_file_container_command):
2087
class file_versions(_file_container_command, _optional_json):
2072 2088
    """Get the list of object versions
2073 2089
    Deleted objects may still have versions that can be used to restore it and
2074 2090
    get information about its previous state.
......
2082 2098
    @errors.pithos.container
2083 2099
    @errors.pithos.object_path
2084 2100
    def _run(self):
2085
        versions = self.client.get_object_versionlist(self.path)
2086
        print_items([dict(id=vitem[0], created=strftime(
2087
            '%d-%m-%Y %H:%M:%S',
2088
            localtime(float(vitem[1])))) for vitem in versions])
2101
        self._print(
2102
            self.client.get_object_versionlist(self.path), version_print)
2089 2103

  
2090 2104
    def main(self, container___path):
2091 2105
        super(file_versions, self)._run(

Also available in: Unified diff