def test_exceptions(self): zero = Size(0) self.assertEqual(zero, Size("0.0")) s = Size(500) with self.assertRaises(ValueError): s.human_readable(max_places="2") self.assertEqual(s.human_readable(max_places=0), "500 B")
def test_human_readable_translation(self): s = Size("56.19 MiB") size_str = s.human_readable() for lang in TEST_LANGS: os.environ['LANG'] = lang locale.setlocale(locale.LC_ALL, '') self.assertTrue(s.human_readable().endswith("%s" % (_BS("MiB")))) self.assertEqual(s.human_readable(xlate=False), size_str)
def _add_free_space(self, itr, device_name): # Calculate the free space. disk_free = Size(self._device_tree.GetDiskFreeSpace([device_name])) if disk_free < Size("1MiB"): return # Add a new row. free_space_string = "<span foreground='grey' style='italic'>{}</span>".format( escape_markup(_("Free space"))) disk_free_string = "<span foreground='grey' style='italic'>{}</span>".format( escape_markup(disk_free.human_readable(max_places=1))) self._disk_store.append(itr, [ "", free_space_string, "", disk_free_string, NOTHING, False, TY_FREE_SPACE, None, disk_free, ]) # Update the total free space. self._initial_free_space += disk_free
def _add_partition(self, itr, device_name): # Get the device data. device_data = DeviceData.from_structure( self._device_tree.GetDeviceData(device_name) ) format_data = DeviceFormatData.from_structure( self._device_tree.GetFormatData(device_name) ) # Calculate the free size. # Devices that are not resizable are still deletable. is_shrinkable = self._device_tree.IsDeviceShrinkable(device_name) size_limits = self._device_tree.GetDeviceSizeLimits(device_name) min_size = Size(size_limits[0]) device_size = Size(device_data.size) if is_shrinkable: free_size = device_size - min_size resize_string = _("%(freeSize)s of %(devSize)s") % { "freeSize": free_size.human_readable(max_places=1), "devSize": device_size.human_readable(max_places=1) } if not device_data.protected: self._can_shrink_something = True else: free_size = device_size resize_string = "<span foreground='grey'>%s</span>" % \ escape_markup(_("Not resizeable")) # Choose the type. if device_data.protected: ty = TY_PROTECTED else: ty = TY_NORMAL # Generate the description. description = self._get_partition_description(device_data, format_data) # Add a new row. self._disk_store.append(itr, [ device_name, description, format_data.description, resize_string, _(PRESERVE), not device_data.protected, ty, self._get_tooltip(device_data), int(device_size), ]) return free_size
def _set_size(self): if self._request.container_size_policy == SIZE_POLICY_AUTO: self._sizeCombo.set_active(0) self._sizeEntry.set_text("") elif self._request.container_size_policy == SIZE_POLICY_MAX: self._sizeCombo.set_active(1) self._sizeEntry.set_text("") else: self._sizeCombo.set_active(2) size = Size(self._request.container_size_policy) self._sizeEntry.set_text(size.human_readable(max_places=2)) if not self._permissions.container_size_policy: fancy_set_sensitive(self._sizeCombo, False) self._sizeEntry.set_sensitive(False)
def _get_size_policy(self): idx = self._sizeCombo.get_active() if idx == 0: return SIZE_POLICY_AUTO if idx == 1: return SIZE_POLICY_MAX original_size = Size(self._request.container_size_policy) original_entry = original_size.human_readable(max_places=2) if self._sizeEntry.get_text() == original_entry: return self._request.container_size_policy size = get_size_from_entry(self._sizeEntry, lower_bound=self.MIN_SIZE_ENTRY, units=SIZE_UNITS_DEFAULT) if size is None: return SIZE_POLICY_MAX return size.get_bytes()
def test_human_readable_fractional_quantities(self): s = Size(0xfffffffffffff) self.assertEqual(s.human_readable(max_places=2), "4 PiB") s = Size(0xffff) # value is not exactly 64 KiB, but w/ 2 places, value is 64.00 KiB # so the trailing 0s are stripped. self.assertEqual(s.human_readable(max_places=2), "64 KiB") # since all significant digits are shown, there are no trailing 0s. self.assertEqual(s.human_readable(max_places=None), "63.9990234375 KiB") # deviation is less than 1/2 of 1% of 1024 s = Size(16384 - (1024 / 100 // 2)) self.assertEqual(s.human_readable(max_places=2), "16 KiB") # deviation is greater than 1/2 of 1% of 1024 s = Size(16384 - ((1024 / 100 // 2) + 1)) self.assertEqual(s.human_readable(max_places=2), "15.99 KiB") s = Size(0x10000000000000) self.assertEqual(s.human_readable(max_places=2), "4 PiB")
def test_human_readable(self): s = Size(58929971) self.assertEqual(s.human_readable(), "56.2 MiB") s = Size(478360371) self.assertEqual(s.human_readable(), "456.2 MiB") # human_reable output should be the same as input for big enough sizes # and enough places and integer values s = Size("12.68 TiB") self.assertEqual(s.human_readable(max_places=2), "12.68 TiB") s = Size("26.55 MiB") self.assertEqual(s.human_readable(max_places=2), "26.55 MiB") s = Size("300 MiB") self.assertEqual(s.human_readable(max_places=2), "300 MiB") # rounding should work with max_places limitted s = Size("12.687 TiB") self.assertEqual(s.human_readable(max_places=2), "12.69 TiB") s = Size("23.7874 TiB") self.assertEqual(s.human_readable(max_places=3), "23.787 TiB") s = Size("12.6998 TiB") self.assertEqual(s.human_readable(max_places=2), "12.7 TiB") # byte values close to multiples of 2 are shown without trailing zeros s = Size(0xff) self.assertEqual(s.human_readable(max_places=2), "255 B") # a fractional quantity is shown if the value deviates # from the whole number of units by more than 1% s = Size(16384 - (1024 / 100 + 1)) self.assertEqual(s.human_readable(max_places=2), "15.99 KiB") # if max_places is set to None, all digits are displayed s = Size(0xfffffffffffff) self.assertEqual( s.human_readable(max_places=None), "3.99999999999999911182158029987476766109466552734375 PiB") s = Size(0x10000) self.assertEqual(s.human_readable(max_places=None), "64 KiB") s = Size(0x10001) self.assertEqual(s.human_readable(max_places=None), "64.0009765625 KiB") # test a very large quantity with no associated abbreviation or prefix s = Size(1024**9) self.assertEqual(s.human_readable(max_places=2), "1024 YiB") s = Size(1024**9 - 1) self.assertEqual(s.human_readable(max_places=2), "1024 YiB") s = Size(1024**10) self.assertEqual(s.human_readable(max_places=2), "1048576 YiB")
def test_segative(self): s = Size("-500MiB") self.assertEqual(s.human_readable(), "-500 MiB") self.assertEqual(s.convert_to(B), -524288000)
def test_human_readable(self): s = Size(58929971) self.assertEqual(s.human_readable(), "56.2 MiB") s = Size(478360371) self.assertEqual(s.human_readable(), "456.2 MiB") # human_reable output should be the same as input for big enough sizes # and enough places and integer values s = Size("12.68 TiB") self.assertEqual(s.human_readable(max_places=2), "12.68 TiB") s = Size("26.55 MiB") self.assertEqual(s.human_readable(max_places=2), "26.55 MiB") s = Size("300 MiB") self.assertEqual(s.human_readable(max_places=2), "300 MiB") # rounding should work with max_places limitted s = Size("12.687 TiB") self.assertEqual(s.human_readable(max_places=2), "12.69 TiB") s = Size("23.7874 TiB") self.assertEqual(s.human_readable(max_places=3), "23.787 TiB") s = Size("12.6998 TiB") self.assertEqual(s.human_readable(max_places=2), "12.7 TiB") # byte values close to multiples of 2 are shown without trailing zeros s = Size(0xff) self.assertEqual(s.human_readable(max_places=2), "255 B") # a fractional quantity is shown if the value deviates # from the whole number of units by more than 1% s = Size(16384 - (1024 / 100 + 1)) self.assertEqual(s.human_readable(max_places=2), "15.99 KiB") # if max_places is set to None, all digits are displayed s = Size(0xfffffffffffff) self.assertEqual(s.human_readable(max_places=None), "3.99999999999999911182158029987476766109466552734375 PiB") s = Size(0x10000) self.assertEqual(s.human_readable(max_places=None), "64 KiB") s = Size(0x10001) self.assertEqual(s.human_readable(max_places=None), "64.0009765625 KiB") # test a very large quantity with no associated abbreviation or prefix s = Size(1024 ** 9) self.assertEqual(s.human_readable(max_places=2), "1024 YiB") s = Size(1024 ** 9 - 1) self.assertEqual(s.human_readable(max_places=2), "1024 YiB") s = Size(1024 ** 10) self.assertEqual(s.human_readable(max_places=2), "1048576 YiB")