def _print_quotas(self, quotas, *args, **kwargs): if not self['bytes']: for project_id, resources in quotas.items(): for r in self._to_format.intersection(resources): resources[r] = dict([(k, format_size(v)) for k, v in resources[r].items()]) self.print_dict(quotas, *args, **kwargs)
def _print_quotas(self, quotas, *args, **kwargs): if not self['bytes']: for project_id, resources in quotas.items(): for r in self._to_format.intersection(resources): resources[r] = dict( [(k, format_size(v)) for k, v in resources[r].items()]) self.print_dict(quotas, *args, **kwargs)
def _print_quotas(self, quotas, *args, **kwargs): if not self['bytes']: for category in quotas.values(): for service in self._to_format.intersection(category): for attr, v in category[service].items(): category[service][attr] = format_size(v) self.print_dict(quotas, *args, **kwargs)
def _run(self, image_id): meta = self.client.get_meta(image_id) if not self['output_format']: try: meta['owner'] += ' (%s)' % self._uuid2username(meta['owner']) except KeyError as ke: log.debug('%s' % ke) try: meta['size'] = format_size(meta['size']) except KeyError as ke: log.debug('%s' % ke) self.print_(meta, self.print_dict)
def _run(self): super(self.__class__, self)._run() if self['image_ID_for_members']: return self._members(self['image_ID_for_members']) filters = {} for arg in set([ 'container_format', 'disk_format', 'name', 'size_min', 'size_max', 'status']).intersection(self.arguments): filters[arg] = self[arg] order = self['order'] detail = any([self[x] for x in ( 'detail', 'prop', 'prop_like', 'owner', 'owner_name')]) images = self.client.list_public(detail, filters, order) if self['owner'] or self['owner_name']: images = self._filter_by_owner(images) if self['prop'] or self['prop_like']: images = self._filter_by_properties(images) images = self._filter_by_id(images) images = self._non_exact_name_filter(images) for img in [] if self['output_format'] else images: try: img['size'] = format_size(img['size']) except KeyError: continue if self['detail'] and not self['output_format']: images = self._add_owner_name(images) elif detail and not self['detail']: for img in images: for key in set(img).difference([ 'id', 'name', 'status', 'container_format', 'disk_format', 'size']): img.pop(key) kwargs = dict(with_enumeration=self['enum']) if self['limit']: images = images[:self['limit']] if self['more']: kwargs['out'] = StringIO() kwargs['title'] = () self.print_(images, **kwargs) if self['more']: pager(kwargs['out'].getvalue())
def _run(self, image_id): meta = self.client.get_meta(image_id) if not self["output_format"]: try: meta["owner"] += " (%s)" % self._uuid2username(meta["owner"]) except KeyError as ke: log.debug("%s" % ke) try: meta["size"] = format_size(meta["size"]) except KeyError as ke: log.debug("%s" % ke) self.print_(meta, self.print_dict)
def _raise(self, *args, **kwargs): size = kwargs.get('size', None) start = kwargs.get('start', 0) end = kwargs.get('end', 0) if size: try: size = int(size) except ValueError as ve: raise CLIError( 'Invalid file size %s ' % size, details=['size must be a positive integer', '%s' % ve], importance=1) else: try: start = int(start) except ValueError as e: raise CLIError( 'Invalid start value %s in range' % start, details=['size must be a positive integer', '%s' % e], importance=1) try: end = int(end) except ValueError as e: raise CLIError( 'Invalid end value %s in range' % end, details=['size must be a positive integer', '%s' % e], importance=1) if start > end: raise CLIError('Invalid range %s-%s' % (start, end), details=['size must be a positive integer'], importance=1) size = end - start try: return func(self, *args, **kwargs) except ClientError as ce: if size and ce.status in (416, 400): raise CLIError( 'Remote object %s:%s <= %s %s' % (self.container, self.path, format_size(size), ('(%sB)' % size) if size >= 1024 else ''), details=['%s %s' % (getattr(ce, 'status', ''), ce)]) raise
def _raise(self, *args, **kwargs): size = kwargs.get('size', None) start = kwargs.get('start', 0) end = kwargs.get('end', 0) if size: try: size = int(size) except ValueError as ve: raise CLIError( 'Invalid file size %s ' % size, details=['size must be a positive integer', '%s' % ve], importance=1) else: try: start = int(start) except ValueError as e: raise CLIError( 'Invalid start value %s in range' % start, details=['size must be a positive integer', '%s' % e], importance=1) try: end = int(end) except ValueError as e: raise CLIError( 'Invalid end value %s in range' % end, details=['size must be a positive integer', '%s' % e], importance=1) if start > end: raise CLIError( 'Invalid range %s-%s' % (start, end), details=['size must be a positive integer'], importance=1) size = end - start try: return func(self, *args, **kwargs) except ClientError as ce: if size and ce.status in (416, 400): raise CLIError( 'Remote object %s:%s <= %s %s' % ( self.container, self.path, format_size(size), ('(%sB)' % size) if size >= 1024 else ''), details=['%s %s' % (getattr(ce, 'status', ''), ce)]) raise
def _raise(self, *args, **kwargs): size = kwargs.get('size', None) start = kwargs.get('start', 0) end = kwargs.get('end', 0) if size: try: size = int(size) except ValueError as ve: msg = 'Invalid file size %s ' % size details = ['size must be a positive integer'] raiseCLIError(ve, msg, details=details, importance=1) else: try: start = int(start) except ValueError as e: msg = 'Invalid start value %s in range' % start, details = ['size must be a positive integer'], raiseCLIError(e, msg, details=details, importance=1) try: end = int(end) except ValueError as e: msg = 'Invalid end value %s in range' % end details = ['size must be a positive integer'] raiseCLIError(e, msg, details=details, importance=1) if start > end: raiseCLIError( 'Invalid range %s-%s' % (start, end), details=['size must be a positive integer'], importance=1) size = end - start try: return func(self, *args, **kwargs) except ClientError as ce: err_msg = ('%s' % ce).lower() expected = 'object length is smaller than range length' if size and ( ce.status == 416 or ( ce.status == 400 and expected in err_msg)): raiseCLIError(ce, 'Remote object %s:%s <= %s %s' % ( self.container, self.path, format_size(size), ('(%sB)' % size) if size >= 1024 else '')) raise
def _run(self): super(self.__class__, self)._run() if self["image_ID_for_members"]: return self._members(self["image_ID_for_members"]) filters = {} for arg in set(["container_format", "disk_format", "name", "size_min", "size_max", "status"]).intersection( self.arguments ): filters[arg] = self[arg] order = self["order"] detail = any([self[x] for x in ("detail", "prop", "prop_like", "owner", "owner_name")]) images = self.client.list_public(detail, filters, order) if self["owner"] or self["owner_name"]: images = self._filter_by_owner(images) if self["prop"] or self["prop_like"]: images = self._filter_by_properties(images) images = self._filter_by_id(images) images = self._non_exact_name_filter(images) for img in [] if self["output_format"] else images: try: img["size"] = format_size(img["size"]) except KeyError: continue if self["detail"] and not self["output_format"]: images = self._add_owner_name(images) elif detail and not self["detail"]: for img in images: for key in set(img).difference(["id", "name", "status", "container_format", "disk_format", "size"]): img.pop(key) kwargs = dict(with_enumeration=self["enum"]) if self["limit"]: images = images[: self["limit"]] if self["more"]: kwargs["out"] = StringIO() kwargs["title"] = () self.print_(images, **kwargs) if self["more"]: pager(kwargs["out"].getvalue())
def _run(self, image_id): meta = self.client.get_meta(image_id) if self['hashmap']: print meta['location'] location = meta['location'].split('pithos://')[1] location = location.split('/') uuid, container = location[0], location[1] pithos = self.get_client(PithosClient, 'pithos') pithos.account, pithos.container = uuid, container path = '/'.join(location[2:]) meta = pithos.get_object_hashmap(path) elif not self['output_format']: try: meta['owner'] += ' (%s)' % self._uuid2username(meta['owner']) except KeyError as ke: log.debug('%s' % ke) try: meta['size'] = format_size(meta['size']) except KeyError as ke: log.debug('%s' % ke) self.print_(meta, self.print_dict)
def test_format_size(self): from kamaki.cli.utils import format_size from kamaki.cli import CLIError for v in ('wrong', {1: '1', 2: '2'}, ('tuples', 'not OK'), [1, 2]): self.assertRaises(CLIError, format_size, v) for step, B, K, M, G, T in ( (1000, 'B', 'KB', 'MB', 'GB', 'TB'), (1024, 'B', 'KiB', 'MiB', 'GiB', 'TiB')): Ki, Mi, Gi = step, step * step, step * step * step for before, after in ( (0, '0' + B), (512, '512' + B), ( Ki - 1, '%s%s' % (step - 1, B)), (Ki, '1' + K), (42 * Ki, '42' + K), ( Mi - 1, '%s.99%s' % (step - 1, K)), (Mi, '1' + M), (42 * Mi, '42' + M), ( Ki * Mi - 1, '%s.99%s' % (step - 1, M)), (Gi, '1' + G), (42 * Gi, '42' + G), ( Mi * Mi - 1, '%s.99%s' % (step - 1, G)), (Mi * Mi, '1' + T), (42 * Mi * Mi, '42' + T), ( Mi * Gi - 1, '%s.99%s' % (step - 1, T)), ( 42 * Mi * Gi, '%s%s' % (42 * Ki, T))): self.assertEqual(format_size(before, step == 1000), after)