Пример #1
0
def _conform_value(value, convert_size=False):
    '''
    Ensure value always conform to what zfs expects
    '''
    # NOTE: salt breaks the on/off/yes/no properties
    if isinstance(value, bool):
        return 'on' if value else 'off'

    if isinstance(value, six.text_type) or isinstance(value, str):
        # NOTE: handle whitespaces
        if ' ' in value:
            # NOTE: quoting the string may be better
            #       but it is hard to know if we already quoted it before
            #       this can be improved in the future
            return "'{0}'".format(value.strip("'"))

        # NOTE: handle ZFS size conversion
        match_size = re_zfs_size.match(value)
        if convert_size and match_size:
            v_size = float(match_size.group(1))
            v_unit = match_size.group(2).upper()[0]
            v_power = math.pow(
                1024, ['K', 'M', 'G', 'T', 'P', 'E', 'Z'].index(v_unit) + 1)
            value = v_size * v_power
            return int(value) if int(value) == value else value

        # NOTE: convert to numeric if needed
        return str_to_num(value)

    # NOTE: passthrough
    return value
Пример #2
0
def from_numeric(value):
    '''
    Convert zfs numeric to python int
    '''
    if value == 'none':
        value = None
    elif value:
        value = str_to_num(value)
    return value
Пример #3
0
def from_numeric(value):
    """
    Convert zfs numeric to python int
    """
    if value == "none":
        value = None
    elif value:
        value = str_to_num(value)
    return value