| 105 | |
| 106 | |
| 107 | class NetworkInterface(object): |
| 108 | def __init__(self, |
| 109 | provider, # type: InterfaceProvider |
| 110 | data=None, # type: Optional[Dict[str, Any]] |
| 111 | ): |
| 112 | # type: (...) -> None |
| 113 | self.provider = provider |
| 114 | self.name = "" |
| 115 | self.description = "" |
| 116 | self.network_name = "" |
| 117 | self.index = -1 |
| 118 | self.ip = None # type: Optional[str] |
| 119 | self.ips = defaultdict(list) # type: DefaultDict[int, List[str]] |
| 120 | self.type = -1 |
| 121 | self.mac = None # type: Optional[str] |
| 122 | self.dummy = False |
| 123 | if data is not None: |
| 124 | self.update(data) |
| 125 | |
| 126 | def update(self, data): |
| 127 | # type: (Dict[str, Any]) -> None |
| 128 | """Update info about a network interface according |
| 129 | to a given dictionary. Such data is provided by providers |
| 130 | """ |
| 131 | self.name = data.get('name', "") |
| 132 | self.description = data.get('description', "") |
| 133 | self.network_name = data.get('network_name', "") |
| 134 | self.index = data.get('index', 0) |
| 135 | self.ip = data.get('ip', "") |
| 136 | self.type = data.get('type', -1) |
| 137 | self.mac = data.get('mac', "") |
| 138 | self.flags = data.get('flags', 0) |
| 139 | self.dummy = data.get('dummy', False) |
| 140 | |
| 141 | for ip in data.get('ips', []): |
| 142 | if in6_isvalid(ip): |
| 143 | self.ips[6].append(ip) |
| 144 | else: |
| 145 | self.ips[4].append(ip) |
| 146 | |
| 147 | # An interface often has multiple IPv6 so we don't store |
| 148 | # a "main" one, unlike IPv4. |
| 149 | if self.ips[4] and not self.ip: |
| 150 | self.ip = self.ips[4][0] |
| 151 | |
| 152 | def __eq__(self, other): |
| 153 | # type: (Any) -> bool |
| 154 | if isinstance(other, str): |
| 155 | return other in [self.name, self.network_name, self.description] |
| 156 | if isinstance(other, NetworkInterface): |
| 157 | return self.__dict__ == other.__dict__ |
| 158 | return False |
| 159 | |
| 160 | def __ne__(self, other): |
| 161 | # type: (Any) -> bool |
| 162 | return not self.__eq__(other) |
| 163 | |
| 164 | def __hash__(self): |
no outgoing calls
no test coverage detected