
from __future__ import absolute_import

import dataproperty
import six

from ._error import NetworkInterfaceNotFoundError


ANYWHERE_NETWORK = "0.0.0.0/0"


def verify_network_interface(device):
    try:
        import netifaces
    except ImportError:
        return

    if device not in netifaces.interfaces():
        raise NetworkInterfaceNotFoundError(
            "network interface not found: " + device)


def sanitize_network(network):
    """
    :return: Network string
    :rtype: str
    :raises ValueError: if the network string is invalid.
    """

    import ipaddress

    if dataproperty.is_empty_string(network):
        return ""

    if network.lower() == "anywhere":
        return ANYWHERE_NETWORK

    try:
        ipaddress.IPv4Address(six.u(network))
        return network + "/32"
    except ipaddress.AddressValueError:
        pass

    ipaddress.IPv4Network(six.u(network))  # validate network str

    return network


from __future__ import absolute_import

import dataproperty
import six

from ._error import NetworkInterfaceNotFoundError


ANYWHERE_NETWORK = "0.0.0.0/0"


def verify_network_interface(device):
    try:
        import netifaces
    except ImportError:
        return

    if device not in netifaces.interfaces():
        raise NetworkInterfaceNotFoundError(
            "network interface not found: {}".format(device))


def sanitize_network(network):
    """
    :return: Network string
    :rtype: str
    :raises ValueError: if the network string is invalid.
    """

    import ipaddress

    if dataproperty.is_empty_string(network):
        return ""

    if network.lower() == "anywhere":
        return ANYWHERE_NETWORK

    try:
        ipaddress.IPv4Address(six.u(network))
        return network + "/32"
    except ipaddress.AddressValueError:
        pass

    ipaddress.IPv4Network(six.u(network))  # validate network str

    return network

Change to use format method
Change to use format method

  
  from __future__ import absolute_import
  
  import dataproperty
  import six
  
  from ._error import NetworkInterfaceNotFoundError
  
  
  ANYWHERE_NETWORK = "0.0.0.0/0"
  
  
  def verify_network_interface(device):
      try:
          import netifaces
      except ImportError:
          return
  
      if device not in netifaces.interfaces():
          raise NetworkInterfaceNotFoundError(
-             "network interface not found: " + device)
+             "network interface not found: {}".format(device))
  
  
  def sanitize_network(network):
      """
      :return: Network string
      :rtype: str
      :raises ValueError: if the network string is invalid.
      """
  
      import ipaddress
  
      if dataproperty.is_empty_string(network):
          return ""
  
      if network.lower() == "anywhere":
          return ANYWHERE_NETWORK
  
      try:
          ipaddress.IPv4Address(six.u(network))
          return network + "/32"
      except ipaddress.AddressValueError:
          pass
  
      ipaddress.IPv4Network(six.u(network))  # validate network str
  
      return network
  
Change to use format method
## Code Before:

from __future__ import absolute_import

import dataproperty
import six

from ._error import NetworkInterfaceNotFoundError


ANYWHERE_NETWORK = "0.0.0.0/0"


def verify_network_interface(device):
    try:
        import netifaces
    except ImportError:
        return

    if device not in netifaces.interfaces():
        raise NetworkInterfaceNotFoundError(
            "network interface not found: " + device)


def sanitize_network(network):
    """
    :return: Network string
    :rtype: str
    :raises ValueError: if the network string is invalid.
    """

    import ipaddress

    if dataproperty.is_empty_string(network):
        return ""

    if network.lower() == "anywhere":
        return ANYWHERE_NETWORK

    try:
        ipaddress.IPv4Address(six.u(network))
        return network + "/32"
    except ipaddress.AddressValueError:
        pass

    ipaddress.IPv4Network(six.u(network))  # validate network str

    return network

## Instruction:
Change to use format method
## Code After:

from __future__ import absolute_import

import dataproperty
import six

from ._error import NetworkInterfaceNotFoundError


ANYWHERE_NETWORK = "0.0.0.0/0"


def verify_network_interface(device):
    try:
        import netifaces
    except ImportError:
        return

    if device not in netifaces.interfaces():
        raise NetworkInterfaceNotFoundError(
            "network interface not found: {}".format(device))


def sanitize_network(network):
    """
    :return: Network string
    :rtype: str
    :raises ValueError: if the network string is invalid.
    """

    import ipaddress

    if dataproperty.is_empty_string(network):
        return ""

    if network.lower() == "anywhere":
        return ANYWHERE_NETWORK

    try:
        ipaddress.IPv4Address(six.u(network))
        return network + "/32"
    except ipaddress.AddressValueError:
        pass

    ipaddress.IPv4Network(six.u(network))  # validate network str

    return network
