Ejemplo n.º 1
0
def decimal_valid(edit_field, disk_win=None):
    '''Check text to see if it is a decimal number of precision no
    greater than the tenths place.
    
    '''
    text = edit_field.get_text().lstrip()
    if text.endswith(" "):
        raise UIMessage(_('Only the digits 0-9 and "." are valid.'))
    vals = text.split(".")
    if len(vals) > 2:
        raise UIMessage(_('A number can only have one "."'))
    try:
        if len(vals[0]) > 0:
            int(vals[0])
        if len(vals) > 1 and len(vals[1]) > 0:
            int(vals[1])
    except ValueError:
        raise UIMessage(_('Only the digits 0-9 and "." are valid.'))
    if len(vals) > 1 and len(vals[1]) > 1:
        raise UIMessage(_("Size can be specified to only one decimal place."))
    if disk_win is not None:
        text = text.rstrip(".")
        if not text:
            text = "0"
        new_size = DiskSpace(text + "gb")
        max_size = edit_field.data_obj.get_max_size(disk_win.disk_info)

        # When comparing sizes, check only to the first decimal place,
        # as that is all the user sees. (Rounding errors that could
        # cause the partition/slice layout to be invalid get cleaned up
        # prior to target instantiation)
        new_size_rounded = round(new_size.size_as("gb"), 1)
        max_size_rounded = round(max_size, 1)
        if new_size_rounded > max_size_rounded:
            raise UIMessage(
                _("The new size (%(size).1f) is greater than "
                  "the available space (%(avail).1f)") % {
                      "size": new_size_rounded,
                      "avail": max_size_rounded
                  })
        size_diff = abs(new_size.size_as("gb") - edit_field.data_obj.orig_size)
        if size_diff > DiskWindow.SIZE_PRECISION:
            edit_field.data_obj.size = new_size
            edit_field.data_obj.adjust_offset(disk_win.disk_info)
        else:
            edit_field.data_obj.size = "%fgb" % edit_field.data_obj.orig_size
        disk_win.update_avail_space()
        disk_win.no_ut_refresh()
        part_field = disk_win.find_part_field(edit_field.data_obj)[1]
        disk_win.mark_if_destroyed(part_field)
    return True
Ejemplo n.º 2
0
    def modified(self, off_by=UI_PRECISION):
        '''Returns False if and only if this SliceInfo was instantiated from
        a tgt.Slice, and this SliceInfo does not differ in substance
        from the tgt.Slice from which it was instantiated.
        
        Size, offset, type and number are compared to determine
        whether this slice has been modified.
        
        off_by - A string or DiskSpace indicating a rounding factor. Any size
        data (offset, size) that differs by less than the given amount is
        assumed to be unchanged. e.g., if the tgt.Slice indicates a size
        of 10.05GB and this SliceInfo has a size of 10.1GB, and off_by
        is the default of 0.1GB, then it is assumed that the represented
        slice has not changed. (The original tgt.Slice size should be
        used, for accuracy)
        
        '''
        if self._tgt_slice is None:
            return True

        if not isinstance(off_by, DiskSpace):
            off_by = DiskSpace(off_by)
        off_by_bytes = off_by.size_as("b")

        if self.number != self._tgt_slice.number:
            return True

        if self.type[0] != self._tgt_slice.type:
            return True

        if self.type[1] != self._tgt_slice.user:
            return True

        tgt_size = self._tgt_slice.blocks * self._tgt_slice.geometry.blocksz
        if abs(tgt_size - self.size.size_as("b")) > off_by_bytes:
            return True

        tgt_offset = self._tgt_slice.offset * self._tgt_slice.geometry.blocksz
        if abs(tgt_offset - self.offset.size_as("b")) > off_by_bytes:
            return True

        return False