# Can only test in the invalid domain case
# because of the settings global variable.
- @mock.patch("updateHostsFile.raw_input", side_effect=["foo", "no"])
+ @mock.patch("updateHostsFile.input", side_effect=["foo", "no"])
@mock.patch("updateHostsFile.is_valid_domain_format", return_value=False)
def test_basic(self, *_):
gather_custom_exclusions("foo", [])
output = sys.stdout.getvalue()
self.assertIn(expected, output)
- @mock.patch("updateHostsFile.raw_input", side_effect=["foo", "yes",
+ @mock.patch("updateHostsFile.input", side_effect=["foo", "yes",
"bar", "no"])
@mock.patch("updateHostsFile.is_valid_domain_format", return_value=False)
def test_multiple(self, *_):
for invalid_default in ["foo", "bar", "baz", 1, 2, 3]:
self.assertRaises(ValueError, query_yes_no, "?", invalid_default)
- @mock.patch("updateHostsFile.raw_input", side_effect=["yes"] * 3)
+ @mock.patch("updateHostsFile.input", side_effect=["yes"] * 3)
def test_valid_default(self, _):
for valid_default, expected in [(None, "[y/n]"), ("yes", "[Y/n]"),
("no", "[y/N]")]:
self.assertIn(expected, output)
- @mock.patch("updateHostsFile.raw_input", side_effect=([""] * 2))
+ @mock.patch("updateHostsFile.input", side_effect=([""] * 2))
def test_use_valid_default(self, _):
for valid_default in ["yes", "no"]:
expected = (valid_default == "yes")
self.assertEqual(actual, expected)
- @mock.patch("updateHostsFile.raw_input", side_effect=["no", "NO", "N",
+ @mock.patch("updateHostsFile.input", side_effect=["no", "NO", "N",
"n", "No", "nO"])
def test_valid_no(self, _):
self.assertFalse(query_yes_no("?", None))
- @mock.patch("updateHostsFile.raw_input", side_effect=["yes", "YES", "Y",
+ @mock.patch("updateHostsFile.input", side_effect=["yes", "YES", "Y",
"yeS", "y", "YeS",
"yES", "YEs"])
def test_valid_yes(self, _):
self.assertTrue(query_yes_no("?", None))
- @mock.patch("updateHostsFile.raw_input", side_effect=["foo", "yes",
+ @mock.patch("updateHostsFile.input", side_effect=["foo", "yes",
"foo", "no"])
def test_invalid_then_valid(self, _):
expected = "Please respond with 'yes' or 'no'"
# This Python script will combine all the host files you provide
# as sources into one, unique host file to keep you internet browsing happy.
-from __future__ import (absolute_import, division, print_function, unicode_literals)
-
import argparse
import fnmatch
import json
if PY3:
from urllib.request import urlopen
- raw_input = input
-else: # Python 2
+else:
raise Exception('We do not support Python 2 anymore.')
# Syntactic sugar for "sudo" command in UNIX / Linux
remove_old_hosts_file(settings["backup"])
if settings["compress"]:
# Another mode is required to read and write the file in Python 3
- final_file = open(path_join_robust(settings["outputpath"], "hosts"), "w+b" if PY3 else "w+")
+ final_file = open(path_join_robust(settings["outputpath"], "hosts"), "w+b")
compressed_file = tempfile.NamedTemporaryFile()
remove_dups_and_excl(merge_file, exclusion_regexes, compressed_file)
compress_file(compressed_file, settings["targetip"], final_file)
elif settings["minimise"]:
- final_file = open(path_join_robust(settings["outputpath"], "hosts"), "w+b" if PY3 else "w+")
+ final_file = open(path_join_robust(settings["outputpath"], "hosts"), "w+b")
minimised_file = tempfile.NamedTemporaryFile()
remove_dups_and_excl(merge_file, exclusion_regexes, minimised_file)
minimise_file(minimised_file, settings["targetip"], final_file)
# says that they have no more domains to exclude.
while True:
domain_prompt = ("Enter the domain you want to exclude (e.g. facebook.com): ")
- user_domain = raw_input(domain_prompt)
+ user_domain = input(domain_prompt)
if is_valid_domain_format(user_domain):
exclusion_regexes = exclude_domain(user_domain, exclusion_pattern, exclusion_regexes)
if output_file is None:
# Another mode is required to read and write the file in Python 3
- final_file = open(path_join_robust(settings["outputpath"], "hosts"), "w+b" if PY3 else "w+")
+ final_file = open(path_join_robust(settings["outputpath"], "hosts"), "w+b")
else:
final_file = output_file
The data to write to the file.
"""
- if PY3:
- f.write(bytes(data, "UTF-8"))
- else:
- try:
- f.write(str(data))
- except UnicodeEncodeError:
- f.write(str(data.encode("UTF-8")))
+ f.write(bytes(data, "UTF-8"))
def list_dir_no_hidden(path):
def query_yes_no(question, default="yes"):
"""
- Ask a yes/no question via raw_input() and get answer from the user.
+ Ask a yes/no question via input() and get answer from the user.
Inspired by the following implementation:
while not reply:
sys.stdout.write(colorize(question, Colors.PROMPT) + prompt)
- choice = raw_input().lower()
+ choice = input().lower()
reply = None
if default and not choice: