# Setup logging
log = logging.getLogger(__name__)
+HOST_PREFIXES = set((
+ "0.0.0.0 ",
+ "127.0.0.1 ",
+ "::1 ",
+))
+
class Format(enum.Enum):
PLAIN = 1
HOSTS = 2
return Format.ADBLOCKPLUS
# Is this a hosts file?
- elif line.startswith("0.0.0.0 "):
+ elif any(line.startswith(prefix) for prefix in HOST_PREFIXES):
return Format.HOSTS
# Check for a plain FQDN
"""
Parses a line of a hosts file.
"""
- return line.removeprefix("0.0.0.0 ")
+ # Strip any comments
+ line, _, comment = line.partition("#")
+
+ for prefix in HOST_PREFIXES:
+ if line.startswith(prefix):
+ return line.removeprefix(prefix)
+
+ # If none of the prefixes matched, we return the entire line
+ return line
def _process_plain(self, line):
"""