Review of the notion of keepdomaincomments

This patch fix StevenBlack/hosts#777

This patch:
  * Change the default state of keepdomaincomments.
    * Indeed, comments are now displayed by default.
    * But if you don't need comments, feel free to use the argument.
  * Delete the requirement input when calling the `-k` argument.
  * Update tests case regarding the new state of keepdomaincomments.
This commit is contained in:
funilrys
2018-09-14 00:15:42 +02:00
parent ec362d81c5
commit c0d661f388
2 changed files with 17 additions and 12 deletions

View File

@@ -86,7 +86,7 @@ class TestGetDefaults(Base):
"replace": False, "replace": False,
"backup": False, "backup": False,
"skipstatichosts": False, "skipstatichosts": False,
"keepdomaincomments": False, "keepdomaincomments": True,
"extensionspath": "foo" + self.sep + "extensions", "extensionspath": "foo" + self.sep + "extensions",
"extensions": [], "extensions": [],
"compress": False, "compress": False,
@@ -753,10 +753,12 @@ class TestStripRule(Base):
self.assertEqual(output, line) self.assertEqual(output, line)
def test_strip_more_than_two(self): def test_strip_more_than_two(self):
comment = " # comments here galore"
for line in ["0.0.0.0 twitter.com", "127.0.0.1 facebook.com", for line in ["0.0.0.0 twitter.com", "127.0.0.1 facebook.com",
"8.8.8.8 google.com", "1.2.3.4 foo.bar.edu"]: "8.8.8.8 google.com", "1.2.3.4 foo.bar.edu"]:
output = strip_rule(line + " # comments here galore") output = strip_rule(line + comment)
self.assertEqual(output, line) self.assertEqual(output, line + comment)
class TestWriteOpeningHeader(BaseMockDir): class TestWriteOpeningHeader(BaseMockDir):

View File

@@ -60,7 +60,7 @@ def get_defaults():
"replace": False, "replace": False,
"backup": False, "backup": False,
"skipstatichosts": False, "skipstatichosts": False,
"keepdomaincomments": False, "keepdomaincomments": True,
"extensionspath": path_join_robust(BASEDIR_PATH, "extensions"), "extensionspath": path_join_robust(BASEDIR_PATH, "extensions"),
"extensions": [], "extensions": [],
"compress": False, "compress": False,
@@ -99,8 +99,8 @@ def main():
parser.add_argument("--ip", "-i", dest="targetip", default="0.0.0.0", parser.add_argument("--ip", "-i", dest="targetip", default="0.0.0.0",
help="Target IP address. Default is 0.0.0.0.") help="Target IP address. Default is 0.0.0.0.")
parser.add_argument("--keepdomaincomments", "-k", parser.add_argument("--keepdomaincomments", "-k",
dest="keepdomaincomments", default=False, dest="keepdomaincomments", action="store_false", default=True,
help="Keep domain line comments.") help="Do not keep domain line comments.")
parser.add_argument("--noupdate", "-n", dest="noupdate", default=False, parser.add_argument("--noupdate", "-n", dest="noupdate", default=False,
action="store_true", help="Don't update from " action="store_true", help="Don't update from "
"host data sources.") "host data sources.")
@@ -843,7 +843,10 @@ def normalize_rule(rule, target_ip, keep_domain_comments):
rule = "%s %s" % (target_ip, hostname) rule = "%s %s" % (target_ip, hostname)
if suffix and keep_domain_comments: if suffix and keep_domain_comments:
rule += " #%s" % suffix if not suffix.strip().startswith('#'):
rule += " #%s" % suffix
else:
rule += " %s" % suffix
return hostname, rule + "\n" return hostname, rule + "\n"
@@ -860,7 +863,10 @@ def normalize_rule(rule, target_ip, keep_domain_comments):
rule = "%s %s" % (target_ip, ip_host) rule = "%s %s" % (target_ip, ip_host)
if suffix and keep_domain_comments: if suffix and keep_domain_comments:
rule += " #%s" % suffix if not suffix.strip().startswith('#'):
rule += " #%s" % suffix
else:
rule += " %s" % suffix
return ip_host, rule + "\n" return ip_host, rule + "\n"
@@ -875,9 +881,6 @@ def strip_rule(line):
""" """
Sanitize a rule string provided before writing it to the output hosts file. Sanitize a rule string provided before writing it to the output hosts file.
Some sources put comments around their rules, for accuracy we need
to strip them the comments are preserved in the output hosts file.
Parameters Parameters
---------- ----------
line : str line : str
@@ -894,7 +897,7 @@ def strip_rule(line):
# just return blank # just return blank
return "" return ""
else: else:
return split_line[0] + " " + split_line[1] return " ".join(split_line)
def write_opening_header(final_file, **header_params): def write_opening_header(final_file, **header_params):