Commit eccb8166 authored by Frede H's avatar Frede H Committed by Ramon Buldó

refactoring and adding misc features pacman_mirrors.py (#17)

* added verbose

* small fixes

* replaced single quotes with double quotes

* separate long functions into smaller ones

* spelling errors

* bump version to 3.0
parent 1000aef3
......@@ -13,4 +13,4 @@ Development Lead
Contributors
------------
None yet. Why not be the first?
* Frede Hundewadt
......@@ -3,6 +3,13 @@
History
-------
2.0.1 (2017-01)
---------------------
* Refactoring
* Code improvements
* --verbose option
2.0 (2016-03-01)
---------------------
......
# -*- coding: utf-8 -*-
__author__ = "Roland Singer, Esclapion, philm and Ramon Buldó"
__version__ = '20150808'
__version__ = "20150808"
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of pacman-mirrors.
#
......@@ -26,28 +25,29 @@ class CustomHelpFormatter(argparse.HelpFormatter):
"""
def _split_lines(self, text, width):
"""Split lines"""
return text.splitlines()
def _get_help_string(self, action):
"""Place default value in help string."""
h = action.help
if '%(default)' not in action.help:
if action.default != '' and action.default != [] and action.default is not None and action.default is not False:
if "%(default)" not in action.help:
if action.default != "" and action.default != [] and action.default is not None and action.default is not False:
if action.default is not argparse.SUPPRESS:
defaulting_nargs = [argparse.OPTIONAL, argparse.ZERO_OR_MORE]
if action.option_strings or action.nargs in defaulting_nargs:
if '\n' in h:
if "\n" in h:
lines = h.splitlines()
lines[0] += ' (default: %(default)s)'
h = '\n'.join(lines)
lines[0] += " (default: %(default)s)"
h = "\n".join(lines)
else:
h += ' (default: %(default)s)'
h += " (default: %(default)s)"
return h
def _fill_text(self, text, width, indent):
"""Permit multiple line descriptions."""
return ''.join([indent + line for line in text.splitlines(True)])
return "".join([indent + line for line in text.splitlines(True)])
def _format_action_invocation(self, action):
"""Removes duplicate ALLCAPS with positional arguments."""
......@@ -72,12 +72,14 @@ class CustomHelpFormatter(argparse.HelpFormatter):
for option_string in action.option_strings:
parts.append(option_string)
return '%s %s' % (', '.join(parts), args_string)
return "%s %s" % (", ".join(parts), args_string)
return ', '.join(parts)
return ", ".join(parts)
def _get_default_metavar_for_optional(self, action):
"""Get default metavar for optional"""
return action.dest.upper()
def _get_default_metavar_for_positional(self, action):
"""get default metavar for positional"""
return action.dest
#!/usr/bin/env python
#
# This file is part of pacman-mirrors.
#
# pacman-mirrors is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pacman-mirrors is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pacman-mirrors. If not, see <http://www.gnu.org/licenses/>.
#
# from https://wiki.maemo.org/Internationalize_a_Python_application
import os
......@@ -10,13 +27,13 @@ import gettext
APP_NAME = "pacman_mirrors"
APP_DIR = os.path.join(sys.prefix,
'share')
LOCALE_DIR = os.path.join(APP_DIR, 'locale')
"share")
LOCALE_DIR = os.path.join(APP_DIR, "locale")
# Now we need to choose the language. We will provide a list, and gettext
# will use the first translation available in the list
DEFAULT_LANGUAGES = os.environ.get('LANG', '').split(':')
DEFAULT_LANGUAGES += ['en_US']
DEFAULT_LANGUAGES = os.environ.get("LANG", "").split(":")
DEFAULT_LANGUAGES += ["en_US"]
# Try to get the languages from the default locale
languages = []
......
This diff is collapsed.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of pacman-mirrors.
#
......@@ -16,9 +15,10 @@
# You should have received a copy of the GNU General Public License
# along with pacman-mirrors. If not, see <http://www.gnu.org/licenses/>.
#
# Author(s): Esclapion
# Hugo Posnic
# Authors: Esclapion
# Hugo Posnic <huluti@manjaro.org>
""" Module PacmanMirrors GUI"""
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
......@@ -28,6 +28,7 @@ _ = i18n.language.gettext
class PacmanMirrors(Gtk.Window):
""" Class PacmanMirrors """
def __init__(self, server_list):
Gtk.Window.__init__(self, title=_("List of mirrors sorted by response time"))
self.set_size_request(700, 350)
......@@ -118,7 +119,7 @@ class PacmanMirrors(Gtk.Window):
# Go back to selection
dialog.destroy()
def launch(server_list):
def run(server_list):
window = PacmanMirrors(server_list)
window.connect("delete-event", Gtk.main_quit)
window.show_all()
......
......@@ -54,4 +54,3 @@ try:
except KeyboardInterrupt:
print("\n" + _("Error: interrupted by the user."))
exit(1)
......@@ -22,7 +22,7 @@ mirror_files = glob.glob('data/mirrors/*')
setup(
name='pacman-mirrors',
version='2.0',
version='2.0.1',
description="Package that provides all mirrors for Manjaro Linux.",
long_description=readme + '\n\n' + history,
author="Roland Singer, Esclapion, philm, Ramon Buldó",
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_pacman-mirrors
......@@ -22,48 +21,48 @@ class TestPacmanMirrors(unittest.TestCase):
@patch("os.getuid")
def test_run(self, mock_os_getuid):
mock_os_getuid.return_value = 0
with unittest.mock.patch('sys.argv', ["pacman-mirrors", "-g", "-m", "random", "-d" "data/mirrors"]):
with unittest.mock.patch("sys.argv", ["pacman-mirrors", "-g", "-m", "random", "-d" "data/mirrors"]):
pm = pacman_mirrors.PacmanMirrors()
pm.parse_cmd()
pm.generate_servers_lists()
pm.command_line_parse()
pm.load_server_lists()
@patch("os.getuid")
def test_run_country(self, mock_os_getuid):
mock_os_getuid.return_value = 0
with unittest.mock.patch('sys.argv', ["pacman-mirrors", "-g", "-c", "Germany", "-m", "random", "-d" "data/mirrors"]):
with unittest.mock.patch("sys.argv", ["pacman-mirrors", "-g", "-c", "Germany", "-m", "random", "-d" "data/mirrors"]):
pm = pacman_mirrors.PacmanMirrors()
pm.parse_cmd()
pm.generate_servers_lists()
assert pm.only_country == ["Germany"]
pm.command_line_parse()
pm.load_server_lists()
assert pm.config["only_country"] == ["Germany"]
@patch("os.getuid")
@patch.object(pacman_mirrors.PacmanMirrors, 'get_geoip_country')
@patch.object(pacman_mirrors.PacmanMirrors, "get_geoip_country")
def test_geoip_country_available(self, mock_geoip, mock_os_getuid):
mock_os_getuid.return_value = 0
mock_geoip.return_value = "France"
with unittest.mock.patch('sys.argv',
with unittest.mock.patch("sys.argv",
["pacman-mirrors", "-g", "--geoip", "-m", "random", "-d" "data/mirrors"]):
pm = pacman_mirrors.PacmanMirrors()
pm.parse_cmd()
pm.generate_servers_lists()
assert pm.only_country == ["France"]
pm.command_line_parse()
pm.load_server_lists()
assert pm.config["only_country"] == ["France"]
@patch("os.getuid")
@patch.object(pacman_mirrors.PacmanMirrors, 'get_geoip_country')
@patch.object(pacman_mirrors.PacmanMirrors, "get_geoip_country")
def test_geoip_country_not_available(self, mock_geoip, mock_os_getuid):
mock_os_getuid.return_value = 0
mock_geoip.return_value = "testetestes"
with unittest.mock.patch('sys.argv',
mock_geoip.return_value = "Antarctica"
with unittest.mock.patch("sys.argv",
["pacman-mirrors", "-g", "--geoip", "-m", "random", "-d" "data/mirrors"]):
pm = pacman_mirrors.PacmanMirrors()
pm.only_country = []
pm.parse_cmd()
pm.generate_servers_lists()
assert pm.only_country == pm.available_countries
pm.config["only_country"] = []
pm.command_line_parse()
pm.load_server_lists()
assert pm.config["only_country"] == pm.available_countries
def tearDown(self):
pass
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment