'Turn a netmask/hostmask string into a prefix length.
Args:
ip_str: A netmask or hostmask, formatted as an IP address.
Returns:
The prefix length as an integer.
Raises:
NetmaskValueError: If the input is not a netmask or hostmask.'

def _prefix_from_ip_string(self, ip_str):

    try:
        ip_int = self._ip_int_from_string(ip_str)
    except AddressValueError:
        raise NetmaskValueError(('%s    is    not    a    valid    netmask' % ip_str))
    try:
        return self._prefix_from_ip_int(ip_int)
    except NetmaskValueError:
        pass
    ip_int ^= self._ALL_ONES
    try:
        return self._prefix_from_ip_int(ip_int)
    except NetmaskValueError:
        raise NetmaskValueError(('%s    is    not    a    valid    netmask' % ip_str))
