-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhostentry.py
More file actions
30 lines (20 loc) · 914 Bytes
/
hostentry.py
File metadata and controls
30 lines (20 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from typing import List
class HostEntry(object):
def __init__(self, ip: str, hostnames: List[str]) -> None:
self.ip = str(ip)
self.hostnames = list(hostnames)
def __eq__(self, other):
return self.ip.strip().lower() == other.ip.strip().lower()
def parse_from_hostfile_row(row: str) -> (bool, HostEntry):
if row is None:
return False, 'No row provided'
split_row = (' '.join(row.strip().replace('\t', ' ').split(' '))).split(' ')
if len(split_row) < 2:
return False, 'The row must at least contain 2 columns (IP and Hostname)'
if '#' in split_row:
return False, 'Provided row seems to be (or contain) a comment'
ip = split_row[0]
hostnames = [hn for hn in split_row[1:] if hn != ""]
if len(hostnames) == 0:
return False, 'There is not enough entries containing Hostnames'
return True, HostEntry(ip, hostnames)