Revision ff1c0296 kamaki/cli/commands/pithos.py

b/kamaki/cli/commands/pithos.py
362 362
            prfx = (
363 363
                '%s%s. ' % (empty_space, index)) if self['enum'] else ''
364 364
            if self['detail']:
365
                self._out.writelines(u'%s%s\n' % (prfx, oname))
365
                self.writeln('%s%s' % (prfx, oname))
366 366
                print_dict(pretty_obj, exclude=('name'), out=self._out)
367
                self._out.writelines(u'\n')
367
                self.writeln()
368 368
            else:
369
                oname = u'%s%9s %s' % (prfx, size, oname)
370
                oname += u'/' if isDir else u''
371
                self._out.writelines(oname + u'\n')
369
                oname = '%s%9s %s' % (prfx, size, oname)
370
                oname += '/' if isDir else u''
371
                self.writeln(oname)
372 372

  
373 373
    def print_containers(self, container_list):
374 374
        for index, container in enumerate(container_list):
......
379 379
                self['more']) else bold(container['name'])
380 380
            cname = u'%s%s' % (prfx, _cname)
381 381
            if self['detail']:
382
                self._out.writelines(cname + u'\n')
382
                self.writeln(cname)
383 383
                pretty_c = container.copy()
384 384
                if 'bytes' in container:
385 385
                    pretty_c['bytes'] = '%s (%s)' % (container['bytes'], size)
386 386
                print_dict(pretty_c, exclude=('name'), out=self._out)
387
                self._out.writelines(u'\n')
387
                self.writeln()
388 388
            else:
389 389
                if 'count' in container and 'bytes' in container:
390
                    self._out.writelines(u'%s (%s, %s objects)\n' % (
390
                    self.writeln('%s (%s, %s objects)' % (
391 391
                        cname, size, container['count']))
392 392
                else:
393
                    self._out.writelines(cname + '\n')
393
                    self.writeln(cname)
394 394

  
395 395
    @errors.generic.all
396 396
    @errors.pithos.connection
......
456 456

  
457 457
    def main(self, container___directory):
458 458
        super(self.__class__, self)._run(
459
            container___directory,
460
            path_is_optional=False)
459
            container___directory, path_is_optional=False)
461 460
        self._run()
462 461

  
463 462

  
......
482 481
            self.client.create_object(self.path, self['content_type']))
483 482

  
484 483
    def main(self, container___path):
485
        super(file_touch, self)._run(
486
            container___path,
487
            path_is_optional=False)
484
        super(file_touch, self)._run(container___path, path_is_optional=False)
488 485
        self._run()
489 486

  
490 487

  
......
539 536

  
540 537
    def _run(self, source_container___path, path_is_optional=False):
541 538
        super(_source_destination_command, self)._run(
542
            source_container___path,
543
            path_is_optional)
539
            source_container___path, path_is_optional)
544 540
        self.dst_client = PithosClient(
545 541
            base_url=self.client.base_url,
546 542
            token=self.client.token,
......
739 735
            self, source_container___path,
740 736
            destination_container___path=None):
741 737
        super(file_copy, self)._run(
742
            source_container___path,
743
            path_is_optional=False)
738
            source_container___path, path_is_optional=False)
744 739
        (dst_cont, dst_path) = self._dest_container_path(
745 740
            destination_container___path)
746 741
        self.dst_client.container = dst_cont or self.container
......
813 808
                content_type=self['content_type'])
814 809
        if no_source_object:
815 810
            raiseCLIError('No object %s in container %s' % (
816
                self.path,
817
                self.container))
811
                self.path, self.container))
818 812
        self._optional_output(r)
819 813

  
820 814
    def main(
......
841 835

  
842 836
    arguments = dict(
843 837
        progress_bar=ProgressBarArgument(
844
            'do not show progress bar',
845
            ('-N', '--no-progress-bar'),
838
            'do not show progress bar', ('-N', '--no-progress-bar'),
846 839
            default=False)
847 840
    )
848 841

  
......
853 846
    def _run(self, local_path):
854 847
        (progress_bar, upload_cb) = self._safe_progress_bar('Appending')
855 848
        try:
856
            f = open(local_path, 'rb')
857
            self._optional_output(
858
                self.client.append_object(self.path, f, upload_cb))
859
        except Exception:
860
            self._safe_progress_bar_finish(progress_bar)
861
            raise
849
            with open(local_path, 'rb') as f:
850
                self._optional_output(
851
                    self.client.append_object(self.path, f, upload_cb))
862 852
        finally:
863 853
            self._safe_progress_bar_finish(progress_bar)
864 854

  
......
897 887

  
898 888
    arguments = dict(
899 889
        progress_bar=ProgressBarArgument(
900
            'do not show progress bar',
901
            ('-N', '--no-progress-bar'),
890
            'do not show progress bar', ('-N', '--no-progress-bar'),
902 891
            default=False)
903 892
    )
904 893

  
905
    def _open_file(self, local_path, start):
906
        f = open(path.abspath(local_path), 'rb')
907
        f.seek(0, 2)
908
        f_size = f.tell()
909
        f.seek(start, 0)
910
        return (f, f_size)
911

  
912 894
    @errors.generic.all
913 895
    @errors.pithos.connection
914 896
    @errors.pithos.container
915 897
    @errors.pithos.object_path
916 898
    @errors.pithos.object_size
917 899
    def _run(self, local_path, start, end):
918
        (start, end) = (int(start), int(end))
919
        (f, f_size) = self._open_file(local_path, start)
900
        start, end = int(start), int(end)
920 901
        (progress_bar, upload_cb) = self._safe_progress_bar(
921 902
            'Overwrite %s bytes' % (end - start))
922 903
        try:
923
            self._optional_output(self.client.overwrite_object(
924
                obj=self.path,
925
                start=start,
926
                end=end,
927
                source_file=f,
928
                upload_cb=upload_cb))
904
            with open(path.abspath(local_path), 'rb') as f:
905
                self._optional_output(self.client.overwrite_object(
906
                    obj=self.path,
907
                    start=start,
908
                    end=end,
909
                    source_file=f,
910
                    upload_cb=upload_cb))
929 911
        finally:
930 912
            self._safe_progress_bar_finish(progress_bar)
931 913

  
......
1065 1047
                            'Object %s exists but it is not a dir' % rpath,
1066 1048
                            importance=1, details=['Use -f to overwrite'])
1067 1049
                except ClientError as ce:
1068
                    if ce.status != 404:
1050
                    if ce.status not in (404, ):
1069 1051
                        raise
1070 1052
            self._check_container_limit(lpath)
1071 1053
            prev = ''
......
1076 1058
                        rel_path = rpath + top.split(lpath)[1]
1077 1059
                    except IndexError:
1078 1060
                        rel_path = rpath
1079
                    print('mkdir %s:%s' % (self.client.container, rel_path))
1061
                    self.error('mkdir %s:%s' % (
1062
                        self.client.container, rel_path))
1080 1063
                    self.client.create_directory(rel_path)
1081 1064
                for f in files:
1082 1065
                    fpath = path.join(top, f)
......
1085 1068
                        pathfix = f.replace(path.sep, '/')
1086 1069
                        yield open(fpath, 'rb'), '%s/%s' % (rel_path, pathfix)
1087 1070
                    else:
1088
                        print('%s is not a regular file' % fpath)
1071
                        self.error('%s is not a regular file' % fpath)
1089 1072
        else:
1090 1073
            if not path.isfile(lpath):
1091 1074
                raiseCLIError(('%s is not a regular file' % lpath) if (
......
1101 1084
                        importance=1,
1102 1085
                        details=['use -f to overwrite or resume'])
1103 1086
            except ClientError as ce:
1104
                if ce.status != 404:
1087
                if ce.status not in (404, ):
1105 1088
                    raise
1106 1089
            self._check_container_limit(lpath)
1107 1090
            yield open(lpath, 'rb'), rpath
......
1112 1095
    @errors.pithos.object_path
1113 1096
    @errors.pithos.local_path
1114 1097
    def _run(self, local_path, remote_path):
1115
        poolsize = self['poolsize']
1116
        if poolsize > 0:
1117
            self.client.MAX_THREADS = int(poolsize)
1098
        if self['poolsize'] > 0:
1099
            self.client.MAX_THREADS = int(self['poolsize'])
1118 1100
        params = dict(
1119 1101
            content_encoding=self['content_encoding'],
1120 1102
            content_type=self['content_type'],
......
1124 1106
        uploaded = []
1125 1107
        container_info_cache = dict()
1126 1108
        for f, rpath in self._path_pairs(local_path, remote_path):
1127
            print('%s --> %s:%s' % (f.name, self.client.container, rpath))
1109
            self.error('%s --> %s:%s' % (f.name, self.client.container, rpath))
1128 1110
            if not (self['content_type'] and self['content_encoding']):
1129 1111
                ctype, cenc = guess_mime_type(f.name)
1130 1112
                params['content_type'] = self['content_type'] or ctype
......
1162 1144
                finally:
1163 1145
                    self._safe_progress_bar_finish(progress_bar)
1164 1146
        self._optional_output(uploaded)
1165
        print('Upload completed')
1147
        self.error('Upload completed')
1166 1148

  
1167 1149
    def main(self, local_path, container____path__=None):
1168 1150
        super(self.__class__, self)._run(container____path__)
......
1193 1175
    @errors.pithos.object_path
1194 1176
    def _run(self):
1195 1177
        self.client.download_object(
1196
            self.path,
1197
            self._out,
1178
            self.path, self._out,
1198 1179
            range_str=self['range'],
1199 1180
            version=self['object_version'],
1200 1181
            if_match=self['if_match'],
......
1236 1217
            'get the specific version', ('-O', '--object-version')),
1237 1218
        poolsize=IntArgument('set pool size', '--with-pool-size'),
1238 1219
        progress_bar=ProgressBarArgument(
1239
            'do not show progress bar',
1240
            ('-N', '--no-progress-bar'),
1220
            'do not show progress bar', ('-N', '--no-progress-bar'),
1241 1221
            default=False),
1242 1222
        recursive=FlagArgument(
1243
            'Download a remote path and all its contents',
1244
            ('-R', '--recursive'))
1223
            'Download a remote path and its contents', ('-R', '--recursive'))
1245 1224
    )
1246 1225

  
1247 1226
    def _outputs(self, local_path):
......
1311 1290
                    makedirs(lpath)
1312 1291
                elif path.exists(lpath):
1313 1292
                    if not self['resume']:
1314
                        print('File %s exists, aborting...' % lpath)
1293
                        self.error('File %s exists, aborting...' % lpath)
1315 1294
                        continue
1316 1295
                    with open(lpath, 'rwb+') as f:
1317 1296
                        yield (f, rpath)
......
1388 1367
                    finally:
1389 1368
                        self._out.flush()
1390 1369
                        timeout += 0.1
1391
            print('\nDownload canceled by user')
1370
            self.error('\nDownload canceled by user')
1392 1371
            if local_path is not None:
1393
                print('to resume, re-run with --resume')
1372
                self.error('to resume, re-run with --resume')
1394 1373
        except Exception:
1395 1374
            self._safe_progress_bar_finish(progress_bar)
1396 1375
            raise
......
1433 1412

  
1434 1413
    def main(self, container___path):
1435 1414
        super(self.__class__, self)._run(
1436
            container___path,
1437
            path_is_optional=False)
1415
            container___path, path_is_optional=False)
1438 1416
        self._run()
1439 1417

  
1440 1418

  
......
1474 1452
    def _run(self):
1475 1453
        if self.path:
1476 1454
            if self['yes'] or ask_user(
1477
                    'Delete %s:%s ?' % (self.container, self.path)):
1455
                    'Delete %s:%s ?' % (self.container, self.path),
1456
                    out=self._out, user_in=self._in):
1478 1457
                self._optional_output(self.client.del_object(
1479 1458
                    self.path,
1480 1459
                    until=self['until'],
1481 1460
                    delimiter='/' if self['recursive'] else self['delimiter']))
1482 1461
            else:
1483
                print('Aborted')
1462
                self.error('Aborted')
1484 1463
        elif self.container:
1485 1464
            if self['recursive']:
1486 1465
                ask_msg = 'Delete container contents'
1487 1466
            else:
1488 1467
                ask_msg = 'Delete container'
1489
            if self['yes'] or ask_user('%s %s ?' % (ask_msg, self.container)):
1468
            if self['yes'] or ask_user(
1469
                    '%s %s ?' % (ask_msg, self.container),
1470
                    out=self._out, user_in=self._in):
1490 1471
                self._optional_output(self.client.del_container(
1491 1472
                    until=self['until'],
1492 1473
                    delimiter='/' if self['recursive'] else self['delimiter']))
1493 1474
            else:
1494
                print('Aborted')
1475
                self.error('Aborted')
1495 1476
        else:
1496 1477
            raiseCLIError('Nothing to delete, please provide container[:path]')
1497 1478

  
......
1520 1501
    @errors.pithos.connection
1521 1502
    @errors.pithos.container
1522 1503
    def _run(self):
1523
        if self['yes'] or ask_user('Purge container %s?' % self.container):
1504
        if self['yes'] or ask_user(
1505
                'Purge container %s?' % self.container,
1506
                out=self._out, user_in=self._in):
1524 1507
            try:
1525 1508
                r = self.client.purge_container()
1526 1509
            except ClientError as ce:
......
1534 1517
                    raise
1535 1518
            self._optional_output(r)
1536 1519
        else:
1537
            print('Aborted')
1520
            self.error('Aborted')
1538 1521

  
1539 1522
    def main(self, container=None):
1540 1523
        super(self.__class__, self)._run(container)
......
1554 1537
    @errors.pithos.container
1555 1538
    @errors.pithos.object_path
1556 1539
    def _run(self):
1557
        print self.client.publish_object(self.path)
1540
        self.println(self.client.publish_object(self.path))
1558 1541

  
1559 1542
    def main(self, container___path):
1560 1543
        super(self.__class__, self)._run(
......
1631 1614

  
1632 1615
    @errors.generic.all
1633 1616
    def format_permission_dict(self, permissions):
1634
        read = False
1635
        write = False
1617
        read, write = False, False
1636 1618
        for perms in permissions:
1637 1619
            splstr = perms.split('=')
1638 1620
            if 'read' == splstr[0]:
......
1831 1813
            if not self['in_bytes']:
1832 1814
                for k in output:
1833 1815
                    output[k] = format_size(output[k])
1834
            print_dict(output, '-')
1816
            print_dict(output, '-', out=self._out)
1835 1817

  
1836
        self._print(self.client.get_account_quota(), pretty_print)
1818
        self._print(
1819
            self.client.get_account_quota(), pretty_print, out=self._out)
1837 1820

  
1838 1821
    def main(self, custom_uuid=None):
1839 1822
        super(self.__class__, self)._run(custom_account=custom_uuid)
......
2072 2055

  
2073 2056
    def main(self, container___path):
2074 2057
        super(file_versions, self)._run(
2075
            container___path,
2076
            path_is_optional=False)
2058
            container___path, path_is_optional=False)
2077 2059
        self._run()

Also available in: Unified diff