diff --git a/pacman_mirrors/configfn.py b/pacman_mirrors/configfn.py index d6b3deb19c7c9b02a5e1505edafb63b1a5468bdf..3ad4336f97bb72890fb180e4df68f9e51a52471d 100644 --- a/pacman_mirrors/configfn.py +++ b/pacman_mirrors/configfn.py @@ -20,7 +20,7 @@ """Pacman-Mirrors Configuration Functions""" from . import txt -from .configuration import CONFIG_FILE, CUSTOM_FILE, MIRROR_DIR, MIRROR_FILE, MIRROR_LIST +from .configuration import CONFIG_FILE, MIRROR_DIR, MIRROR_FILE, MIRROR_LIST class ConfigFn: @@ -34,8 +34,6 @@ class ConfigFn: # is fetched from config file config = { "branch": "stable", - "config_file": CONFIG_FILE, - "custom_file": CUSTOM_FILE, "method": "rank", "mirror_dir": MIRROR_DIR, "mirror_file": MIRROR_FILE, @@ -45,7 +43,7 @@ class ConfigFn: } try: # read configuration from file - with open(config["config_file"]) as conf: + with open(CONFIG_FILE) as conf: for line in conf: line = line.strip() if line.startswith("#") or "=" not in line: diff --git a/pacman_mirrors/customfn.py b/pacman_mirrors/customfn.py index d7864e35228991f7a4af9de7d69c9c27699e4a67..89db39d7e65aa76b24797978ad94d5bf7ee80f73 100644 --- a/pacman_mirrors/customfn.py +++ b/pacman_mirrors/customfn.py @@ -56,44 +56,44 @@ class CustomFn: CustomHelper.cleanup() @staticmethod - def modify_config(configfile, customfile, onlycountry, custom=False): + def modify_config(onlycountry, custom=False): """Modify configuration""" if not custom: - if os.path.isfile(customfile): - os.remove(customfile) - CustomFn.write_custom_config(configfile, onlycountry, custom) + if os.path.isfile(CUSTOM_FILE): + os.remove(CUSTOM_FILE) + CustomFn.write_custom_config(CONFIG_FILE, onlycountry, custom) @staticmethod - def write_custom_config(configfile, selectedcountries, custom=False): + def write_custom_config(filename, selection, custom=False): """Writes the configuration to file - :param configfile: - :param selectedcountries: + :param filename: + :param selection: :param custom: """ if custom: - if selectedcountries == ["Custom"]: - selectedcountries = "OnlyCountry = Custom\n" + if selection == ["Custom"]: + new_config = "OnlyCountry = Custom\n" else: - selectedcountries = "OnlyCountry = {list}\n".format( - list=",".join(selectedcountries)) + new_config = "OnlyCountry = {list}\n".format( + list=",".join(selection)) else: - selectedcountries = "# OnlyCountry = \n" + new_config = "# OnlyCountry = \n" try: with open( - configfile) as cnf, tempfile.NamedTemporaryFile( + filename) as cnf, tempfile.NamedTemporaryFile( "w+t", dir=os.path.dirname( - configfile), delete=False) as tmp: + filename), delete=False) as tmp: replaced = False for line in cnf: if "OnlyCountry" in line: - tmp.write(selectedcountries) + tmp.write(new_config) replaced = True else: tmp.write("{}".format(line)) if not replaced: - tmp.write(selectedcountries) - os.replace(tmp.name, configfile) - os.chmod(configfile, 0o644) + tmp.write(new_config) + os.replace(tmp.name, filename) + os.chmod(filename, 0o644) except OSError as err: print(".: {} {}: {}: {}".format(txt.ERR_CLR, txt.CANNOT_READ_FILE, err.filename, err.strerror)) diff --git a/pacman_mirrors/filefn.py b/pacman_mirrors/filefn.py index 753ca9d795f351fff2abc1c621e8e6bfae29690f..5463b150b9972ef58c99f3aedb42b07c79d01e0d 100755 --- a/pacman_mirrors/filefn.py +++ b/pacman_mirrors/filefn.py @@ -23,6 +23,7 @@ import os import datetime from .configuration import FALLBACK, MIRROR_FILE, REPO_ARCH, STATUS_FILE from .jsonfn import JsonFn +from .miscfn import MiscFn from . import txt diff --git a/pacman_mirrors/pacman_mirrors.py b/pacman_mirrors/pacman_mirrors.py index 530939e73aad82e3e1d3832e18a846d0b982ebd6..6bf982c15d82abc8022fd2fcb958efc4f0c556de 100755 --- a/pacman_mirrors/pacman_mirrors.py +++ b/pacman_mirrors/pacman_mirrors.py @@ -200,14 +200,10 @@ class PacmanMirrors: worklist, quiet=self.quiet) if self.custom or self.config["only_country"] != self.mirrors.mirrorlist: - CustomFn.modify_config(self.config["config_file"], - self.config["custom_file"], - self.config["only_country"], + CustomFn.modify_config(self.config["only_country"], custom=True) else: - CustomFn.modify_config(self.config["config_file"], - self.config["custom_file"], - self.config["only_country"]) + CustomFn.modify_config(self.config["only_country"]) def build_fasttrack_mirror_list(self, number): """Fast-track the mirrorlist by aggressive sorting""" @@ -297,12 +293,9 @@ class PacmanMirrors: worklist, custom=True, quiet=self.quiet) - # always use "Custom" when interactive mode + # always use "Custom" from interactive self.config["only_country"] = ["Custom"] - CustomFn.modify_config(self.config["config_file"], - self.config["custom_file"], - self.config["only_country"], - custom=True) + CustomFn.modify_config(self.config["only_country"], custom=True) print(".: {} {}: {}".format(txt.INF_CLR, txt.MIRROR_LIST_SAVED, CUSTOM_FILE)) diff --git a/tests/test_command_line_parse.py b/tests/test_command_line_parse.py index b263f9be048a668cac129f3025d48a056d2efc79..5144404bd6377aa6cb729ecbdf85b67b2c99df67 100755 --- a/tests/test_command_line_parse.py +++ b/tests/test_command_line_parse.py @@ -29,7 +29,6 @@ class TestCommandLineParse(unittest.TestCase): "-b", "unstable"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.config["branch"] == "unstable" @@ -42,7 +41,6 @@ class TestCommandLineParse(unittest.TestCase): "-b", "testing"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.config["branch"] == "testing" @@ -55,7 +53,6 @@ class TestCommandLineParse(unittest.TestCase): "-m", "random"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.config["method"] == "random" @@ -68,7 +65,6 @@ class TestCommandLineParse(unittest.TestCase): "-d", "/another/dir/"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.config["mirror_dir"] == "/another/dir/" @@ -81,7 +77,6 @@ class TestCommandLineParse(unittest.TestCase): "-o", "/another/list"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.config["mirror_list"] == "/another/list" @@ -94,7 +89,6 @@ class TestCommandLineParse(unittest.TestCase): "-c", "France,Germany"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.config["only_country"] == ["France", "Germany"] @@ -107,7 +101,6 @@ class TestCommandLineParse(unittest.TestCase): "-c Denmark"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.custom is True @@ -120,7 +113,6 @@ class TestCommandLineParse(unittest.TestCase): "--geoip"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.geoip is True @@ -133,7 +125,6 @@ class TestCommandLineParse(unittest.TestCase): "-f 5"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.fasttrack == 5 @@ -146,7 +137,6 @@ class TestCommandLineParse(unittest.TestCase): "-i"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.interactive is True @@ -159,7 +149,6 @@ class TestCommandLineParse(unittest.TestCase): "-t 5"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.max_wait_time == 5 @@ -172,7 +161,6 @@ class TestCommandLineParse(unittest.TestCase): "-q"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.quiet is True diff --git a/tests/test_default_config.py b/tests/test_default_config.py index 227ce1126d26abbdf1420aae9f2edc5f9d2d278d..ba371eb8ffc103864576b4485984d85409e33afa 100755 --- a/tests/test_default_config.py +++ b/tests/test_default_config.py @@ -29,7 +29,6 @@ class TestDefaultConfig(unittest.TestCase): "-g"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" assert app.config["branch"] == "stable" @patch("os.getuid") @@ -41,7 +40,6 @@ class TestDefaultConfig(unittest.TestCase): "-g"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" assert app.config["method"] == "rank" @patch("os.getuid") @@ -53,7 +51,6 @@ class TestDefaultConfig(unittest.TestCase): "-g"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" assert app.config["mirror_dir"] == "mock/var/" @patch("os.getuid") @@ -65,7 +62,6 @@ class TestDefaultConfig(unittest.TestCase): "-g"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" assert app.config["mirror_file"] == "mock/var/mirrors.json" @patch("os.getuid") @@ -77,7 +73,6 @@ class TestDefaultConfig(unittest.TestCase): "-g"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" assert app.config["mirror_list"] == "mock/etc/mirrorlist" @patch("os.getuid") @@ -89,7 +84,6 @@ class TestDefaultConfig(unittest.TestCase): "-g"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" assert app.config["no_update"] is False # @patch("os.getuid") diff --git a/tests/test_geoip.py b/tests/test_geoip.py index f67d7db4801fd7d6fb0ace602406da6e32eaa325..9898532469928505b9feaefba2b6be414777b823 100755 --- a/tests/test_geoip.py +++ b/tests/test_geoip.py @@ -32,7 +32,6 @@ class TestGeoip(unittest.TestCase): "--geoip"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() app.load_all_mirrors() assert app.selected_countries == "France" @@ -49,7 +48,6 @@ class TestGeoip(unittest.TestCase): "--geoip"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() app.load_all_mirrors() assert app.selected_countries == app.mirrors.countrylist diff --git a/tests/test_pacman_mirrors.py b/tests/test_pacman_mirrors.py index 7ff4a9d5dca2a36ecf00a1bcf978d00119c9eeb0..43f418f2966fbad024ab82d96211ed39fb18a7eb 100755 --- a/tests/test_pacman_mirrors.py +++ b/tests/test_pacman_mirrors.py @@ -31,7 +31,6 @@ class TestPacmanMirrors(unittest.TestCase): "-m", "random"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() app.network = HttpFn.update_mirrors() app.load_all_mirrors()