From 9c65c3e05f8681147912ef7790476667d1f321b8 Mon Sep 17 00:00:00 2001 From: Tobias Gruetzmacher Date: Mon, 3 Feb 2020 01:03:31 +0100 Subject: [PATCH] Drop Python 2 support: six & other imports --- dosagelib/cmd.py | 7 +++---- dosagelib/director.py | 6 +++--- dosagelib/events.py | 2 +- dosagelib/http.py | 2 +- dosagelib/output.py | 24 ++++++++++-------------- dosagelib/plugins/r.py | 2 +- dosagelib/scraper.py | 2 +- dosagelib/util.py | 35 +++++++++++------------------------ requirements.txt | 1 - scripts/comicfury.py | 2 +- scripts/keenspot.py | 2 +- scripts/scriptutil.py | 13 ++++--------- scripts/webtoons.py | 2 +- setup.cfg | 1 - tests/httpmocks.py | 5 +---- tests/modules/check_comics.py | 2 +- 16 files changed, 40 insertions(+), 68 deletions(-) diff --git a/dosagelib/cmd.py b/dosagelib/cmd.py index 0822f0863..b75e767f6 100644 --- a/dosagelib/cmd.py +++ b/dosagelib/cmd.py @@ -5,9 +5,8 @@ from __future__ import absolute_import, division, print_function -import os import argparse -import six +import os from . import events, configuration, singleton, director, scraper from . import AppName, __version__ @@ -168,8 +167,8 @@ def display_comic_help(scraperobj): orig_context = out.context out.context = scraperobj.name try: - out.info(u"URL: " + six.text_type(scraperobj.url)) - out.info(u"Language: " + scraperobj.language()) + out.info('URL: {}'.format(scraperobj.url)) + out.info('Language: {}'.format(scraperobj.language())) if scraperobj.adult: out.info(u"Adult comic, use option --adult to fetch.") disabled = scraperobj.getDisabledReasons() diff --git a/dosagelib/director.py b/dosagelib/director.py index be30098d3..a94105995 100644 --- a/dosagelib/director.py +++ b/dosagelib/director.py @@ -8,9 +8,9 @@ from __future__ import absolute_import, division, print_function import os import threading -from six.moves import _thread -from six.moves.queue import Queue, Empty -from six.moves.urllib.parse import urlparse +import _thread +from queue import Queue, Empty +from urllib.parse import urlparse from .output import out from . import events, scraper diff --git a/dosagelib/events.py b/dosagelib/events.py index b2fe97c7a..94c1f54c7 100644 --- a/dosagelib/events.py +++ b/dosagelib/events.py @@ -7,7 +7,7 @@ from __future__ import absolute_import, division, print_function import os import time -from six.moves.urllib.parse import quote as url_quote +from urllib.parse import quote as url_quote import codecs import json diff --git a/dosagelib/http.py b/dosagelib/http.py index 98259f1db..9fe10ba2f 100644 --- a/dosagelib/http.py +++ b/dosagelib/http.py @@ -10,7 +10,7 @@ from time import time, sleep import requests from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.retry import Retry -from six.moves.urllib.parse import urlparse +from urllib.parse import urlparse from .configuration import UserAgent diff --git a/dosagelib/output.py b/dosagelib/output.py index acdaf1164..6c01c2845 100644 --- a/dosagelib/output.py +++ b/dosagelib/output.py @@ -1,25 +1,21 @@ # -*- coding: utf-8 -*- # Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs # Copyright (C) 2012-2014 Bastian Kleineidam -# Copyright (C) 2015-2016 Tobias Gruetzmacher +# Copyright (C) 2015-2020 Tobias Gruetzmacher from __future__ import absolute_import, division, print_function -import time -import sys -import os -import threading -import traceback import codecs import contextlib -import pydoc import io -import six +import os +import pydoc +import sys +import threading +import time +import traceback -try: - from shutil import get_terminal_size -except ImportError: - from backports.shutil_get_terminal_size import get_terminal_size +from shutil import get_terminal_size import colorama from colorama import Fore, Style @@ -111,8 +107,8 @@ class Output(object): self.stream.write(u'%s%s> ' % (timestamp, get_threadname())) if color and self.is_tty: s = u'%s%s%s' % (color, s, Style.RESET_ALL) - self.stream.write(six.text_type(s)) - self.stream.write(six.text_type(os.linesep)) + self.stream.write(str(s)) + self.stream.write(str(os.linesep)) self.stream.flush() def writelines(self, lines, level=0): diff --git a/dosagelib/plugins/r.py b/dosagelib/plugins/r.py index 580f91f47..bd274ce24 100644 --- a/dosagelib/plugins/r.py +++ b/dosagelib/plugins/r.py @@ -7,7 +7,7 @@ from __future__ import absolute_import, division, print_function from re import compile -from six.moves.urllib.parse import urljoin +from urllib.parse import urljoin from ..helpers import bounceStarter, xpath_class from ..scraper import _BasicScraper, _ParserScraper diff --git a/dosagelib/scraper.py b/dosagelib/scraper.py index aa14de897..c2a9e7528 100644 --- a/dosagelib/scraper.py +++ b/dosagelib/scraper.py @@ -7,7 +7,7 @@ from __future__ import absolute_import, division, print_function import os import re -from six.moves.urllib.parse import urljoin +from urllib.parse import urljoin from lxml import html, etree from lxml.html.defs import link_attrs as html_link_attrs diff --git a/dosagelib/util.py b/dosagelib/util.py index 59ea2998e..525e71389 100644 --- a/dosagelib/util.py +++ b/dosagelib/util.py @@ -5,30 +5,20 @@ from __future__ import absolute_import, division, print_function -from six.moves.urllib.parse import (parse_qs, - quote as url_quote, unquote as url_unquote, urlparse, urlunparse, urlsplit) -from six.moves.urllib_robotparser import RobotFileParser -import requests -import sys +import html import os import re -import traceback -import time import subprocess +import sys +import time +import traceback -try: - import html -except ImportError: - # Python 2.7 - from HTMLParser import HTMLParser - html = HTMLParser() -from six.moves import range -import six +from functools import lru_cache +from urllib.parse import (parse_qs, quote as url_quote, unquote as url_unquote, + urlparse, urlunparse, urlsplit) +from urllib.robotparser import RobotFileParser -try: - from functools import lru_cache -except ImportError: - from backports.functools_lru_cache import lru_cache +import requests from .output import out from .configuration import UserAgent, App, SupportUrl @@ -65,10 +55,7 @@ def get_nt_system_uid(): r"""Get the MachineGuid from HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\MachineGuid """ - try: - import _winreg as winreg - except ImportError: - import winreg + import winreg lm = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) try: key = winreg.OpenKey(lm, r"Software\Microsoft\Cryptography") @@ -106,7 +93,7 @@ def backtick(cmd, encoding='utf-8'): def unicode_safe(text, encoding=UrlEncoding, errors='ignore'): """Decode text to Unicode if not already done.""" - if isinstance(text, six.text_type): + if isinstance(text, str): return text return text.decode(encoding, errors) diff --git a/requirements.txt b/requirements.txt index 504681be8..1797b97d2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,5 @@ colorama imagesize lxml requests>=2.0 -six cached_property; python_version<'3.8' importlib_metadata; python_version<'3.8' diff --git a/scripts/comicfury.py b/scripts/comicfury.py index fd506ad81..c436e55fa 100755 --- a/scripts/comicfury.py +++ b/scripts/comicfury.py @@ -10,7 +10,7 @@ processing. from __future__ import absolute_import, division, print_function import sys -from six.moves.urllib.parse import urlsplit +from urllib.parse import urlsplit from scriptutil import ComicListUpdater diff --git a/scripts/keenspot.py b/scripts/keenspot.py index 5e98c33ac..e4826b13e 100755 --- a/scripts/keenspot.py +++ b/scripts/keenspot.py @@ -10,7 +10,7 @@ JSON file for further processing. """ from __future__ import absolute_import, division, print_function -from six.moves.urllib.parse import urlsplit +from urllib.parse import urlsplit from scriptutil import ComicListUpdater from dosagelib.util import check_robotstxt diff --git a/scripts/scriptutil.py b/scripts/scriptutil.py index 33d4e5163..8cf07748f 100644 --- a/scripts/scriptutil.py +++ b/scripts/scriptutil.py @@ -1,21 +1,16 @@ # -*- coding: utf-8 -*- # Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs # Copyright (C) 2012-2014 Bastian Kleineidam -# Copyright (C) 2015-2019 Tobias Gruetzmacher +# Copyright (C) 2015-2020 Tobias Gruetzmacher from __future__ import absolute_import, division, print_function +import codecs +import json import os import re import sys import time -import json -import codecs - -try: - from os import replace as rename -except ImportError: - from os import rename from lxml import html @@ -99,7 +94,7 @@ class ComicListUpdater(object): for name, entry in sorted(data.items(), key=first_lower): self.write_entry(newf, name, entry, min_comics, indent) self.copy_after_end(oldf, newf) - rename(filename + '.new', filename) + os.replace(filename + '.new', filename) def copy_until_start(self, src, dest): for line in src: diff --git a/scripts/webtoons.py b/scripts/webtoons.py index 0e7f0119b..9bff6f0d4 100644 --- a/scripts/webtoons.py +++ b/scripts/webtoons.py @@ -8,7 +8,7 @@ JSON file for further processing. """ from __future__ import absolute_import, division, print_function -from six.moves.urllib.parse import urlsplit, parse_qs +from urllib.parse import urlsplit, parse_qs from scriptutil import ComicListUpdater from dosagelib.util import check_robotstxt diff --git a/setup.cfg b/setup.cfg index 29aa9b2d1..abc7a628f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -36,7 +36,6 @@ install_requires = imagesize lxml requests>=2.0 - six cached_property; python_version<'3.8' importlib_metadata; python_version<'3.8' diff --git a/tests/httpmocks.py b/tests/httpmocks.py index b506f8c61..3e8d0bcd0 100644 --- a/tests/httpmocks.py +++ b/tests/httpmocks.py @@ -7,10 +7,7 @@ import gzip import os.path import re -try: - from functools import lru_cache -except ImportError: - from backports.functools_lru_cache import lru_cache +from functools import lru_cache from responses import add, GET diff --git a/tests/modules/check_comics.py b/tests/modules/check_comics.py index b4eab6596..e0bb9da6b 100644 --- a/tests/modules/check_comics.py +++ b/tests/modules/check_comics.py @@ -7,7 +7,7 @@ from __future__ import absolute_import, division, print_function import re import multiprocessing -from six.moves.urllib.parse import urlsplit +from urllib.parse import urlsplit # Dictionary with per-host locks.