Ejemplo n.º 1
0
def human_to_bytes(size, default_unit=None, isbits=False):
    ''' Return bytes count from a human readable string '''
    try:
        return formatters.human_to_bytes(size, default_unit, isbits)
    except Exception:
        raise AnsibleFilterError(
            "human_to_bytes() can't interpret following string: %s" % size)
Ejemplo n.º 2
0
def check_type_bytes(value):
    """Convert a human-readable string value to bytes

    Raises TypeError if unable to covert the value
    """
    try:
        return human_to_bytes(value)
    except ValueError:
        raise TypeError('%s cannot be converted to a Byte value' % type(value))
Ejemplo n.º 3
0
def check_type_bits(value):
    """Convert a human-readable string bits value to bits in integer.

    Example: check_type_bits('1Mb') returns integer 1048576.

    Raises TypeError if unable to covert the value.
    """
    try:
        return human_to_bytes(value, isbits=True)
    except ValueError:
        raise TypeError('%s cannot be converted to a Bit value' % type(value))
Ejemplo n.º 4
0
def test_human_to_bytes_wrong_unit(test_input):
    """Test of human_to_bytes function, wrong units."""
    with pytest.raises(ValueError, match="The suffix must be one of"):
        human_to_bytes(test_input)
Ejemplo n.º 5
0
def test_human_to_bytes_wrong_number(test_input):
    """Test of human_to_bytes function, number param is invalid string / number."""
    with pytest.raises(ValueError, match="can't interpret"):
        human_to_bytes(test_input)
Ejemplo n.º 6
0
def test_human_to_bytes_number_unit(input_data, unit):
    """Test of human_to_bytes function, number and default_unit args are passed."""
    assert human_to_bytes(input_data, default_unit=unit) == NUM_IN_METRIC.get(
        unit[0], 1024)
Ejemplo n.º 7
0
def test_human_to_bytes_number(input_data, expected):
    """Test of human_to_bytes function, only number arg is passed."""
    assert human_to_bytes(input_data) == expected
Ejemplo n.º 8
0
def test_human_to_bytes_isbits_wrong_default_unit(test_input, unit, isbits):
    """Test of human_to_bytes function, default_unit is in an invalid format for isbits value."""
    with pytest.raises(ValueError, match="Value is not a valid string"):
        human_to_bytes(test_input, default_unit=unit, isbits=isbits)
Ejemplo n.º 9
0
def test_human_to_bytes_isbits_default_unit(input_data, unit):
    """Test of human_to_bytes function, isbits = True and default_unit args are passed."""
    assert human_to_bytes(input_data, default_unit=unit,
                          isbits=True) == NUM_IN_METRIC.get(unit[0], 1024)
Ejemplo n.º 10
0
def test_human_to_bytes_isbits(input_data, expected):
    """Test of human_to_bytes function, isbits = True."""
    assert human_to_bytes(input_data, isbits=True) == expected