From bd1ae14e53dad5e54bee50cc442f41ec1125bbce Mon Sep 17 00:00:00 2001 From: Olof Johansson Date: Sun, 21 Apr 2013 13:42:33 +0200 Subject: [PATCH] Isolate py3/2.7 conditional imports to own modules --- lib/svtplay_dl/fetcher/hls.py | 6 +---- lib/svtplay_dl/fetcher/http.py | 6 +---- lib/svtplay_dl/service/aftonbladet.py | 6 +---- lib/svtplay_dl/service/expressen.py | 5 +--- lib/svtplay_dl/service/hbo.py | 5 +--- lib/svtplay_dl/service/justin.py | 5 +--- lib/svtplay_dl/service/kanal5.py | 6 +---- lib/svtplay_dl/service/radioplay.py | 5 +--- lib/svtplay_dl/service/sr.py | 7 +----- lib/svtplay_dl/service/tv4play.py | 6 +---- lib/svtplay_dl/service/viaplay.py | 5 +--- .../{utils.py => utils/__init__.py} | 12 +++------- lib/svtplay_dl/utils/io.py | 14 +++++++++++ lib/svtplay_dl/utils/urllib.py | 24 +++++++++++++++++++ 14 files changed, 52 insertions(+), 60 deletions(-) rename lib/svtplay_dl/{utils.py => utils/__init__.py} (94%) create mode 100644 lib/svtplay_dl/utils/io.py create mode 100644 lib/svtplay_dl/utils/urllib.py diff --git a/lib/svtplay_dl/fetcher/hls.py b/lib/svtplay_dl/fetcher/hls.py index 7f23b36..6b826e1 100644 --- a/lib/svtplay_dl/fetcher/hls.py +++ b/lib/svtplay_dl/fetcher/hls.py @@ -6,14 +6,10 @@ import os import re from svtplay_dl.utils import get_http_data, select_quality +from svtplay_dl.utils.io import StringIO from svtplay_dl.output import progressbar, progress_stream, ETA from svtplay_dl.log import log -if sys.version_info > (3, 0): - from io import BytesIO as StringIO -else: - from StringIO import StringIO - def download_hls(options, url, baseurl=None): data = get_http_data(url) globaldata, files = parsem3u(data) diff --git a/lib/svtplay_dl/fetcher/http.py b/lib/svtplay_dl/fetcher/http.py index b654e91..e76d04f 100644 --- a/lib/svtplay_dl/fetcher/http.py +++ b/lib/svtplay_dl/fetcher/http.py @@ -7,11 +7,7 @@ import re from svtplay_dl.output import progress # FIXME use progressbar() instead from svtplay_dl.log import log - -if sys.version_info > (3, 0): - from urllib.request import urlopen, Request, HTTPError -else: - from urllib2 import urlopen, Request, HTTPError +from svtplay_dl.utils.urllib import urlopen, Request, HTTPError def download_http(options, url): """ Get the stream from HTTP """ diff --git a/lib/svtplay_dl/service/aftonbladet.py b/lib/svtplay_dl/service/aftonbladet.py index 2b75586..6130360 100644 --- a/lib/svtplay_dl/service/aftonbladet.py +++ b/lib/svtplay_dl/service/aftonbladet.py @@ -5,17 +5,13 @@ import sys import re import xml.etree.ElementTree as ET +from svtplay_dl.utils.urllib import urlparse, parse_qs from svtplay_dl.service import Service from svtplay_dl.utils import get_http_data from svtplay_dl.log import log from svtplay_dl.fetcher.rtmp import download_rtmp from svtplay_dl.fetcher.http import download_http -if sys.version_info > (3, 0): - from urllib.parse import urlparse, parse_qs -else: - from urlparse import urlparse, parse_qs - class Aftonbladet(Service): def handle(self, url): return "aftonbladet.se" in url diff --git a/lib/svtplay_dl/service/expressen.py b/lib/svtplay_dl/service/expressen.py index feb75ae..50155c2 100644 --- a/lib/svtplay_dl/service/expressen.py +++ b/lib/svtplay_dl/service/expressen.py @@ -10,10 +10,7 @@ from svtplay_dl.utils import get_http_data, select_quality from svtplay_dl.log import log from svtplay_dl.fetcher.rtmp import download_rtmp -if sys.version_info > (3, 0): - from urllib.parse import quote_plus -else: - from urllib import quote_plus +from svtplay_dl.utils.urllib import quote_plus class Expressen(Service): def handle(self, url): diff --git a/lib/svtplay_dl/service/hbo.py b/lib/svtplay_dl/service/hbo.py index 859c908..b83487e 100644 --- a/lib/svtplay_dl/service/hbo.py +++ b/lib/svtplay_dl/service/hbo.py @@ -3,12 +3,9 @@ from __future__ import absolute_import import sys import re -if sys.version_info > (3, 0): - from urllib.parse import urlparse -else: - from urlparse import urlparse import xml.etree.ElementTree as ET +from svtplay_dl.utils.urllib import urlparse from svtplay_dl.service import Service from svtplay_dl.utils import get_http_data, select_quality from svtplay_dl.log import log diff --git a/lib/svtplay_dl/service/justin.py b/lib/svtplay_dl/service/justin.py index bbc1d47..f13382e 100644 --- a/lib/svtplay_dl/service/justin.py +++ b/lib/svtplay_dl/service/justin.py @@ -3,12 +3,9 @@ from __future__ import absolute_import import sys import re -if sys.version_info > (3, 0): - from urllib.parse import urlparse -else: - from urlparse import urlparse import xml.etree.ElementTree as ET +from svtplay_dl.utils.urllib import urlparse from svtplay_dl.service import Service from svtplay_dl.utils import get_http_data, select_quality, check_redirect from svtplay_dl.log import log diff --git a/lib/svtplay_dl/service/kanal5.py b/lib/svtplay_dl/service/kanal5.py index 4b1ba8c..7bcc654 100644 --- a/lib/svtplay_dl/service/kanal5.py +++ b/lib/svtplay_dl/service/kanal5.py @@ -5,11 +5,7 @@ import sys import re import json -if sys.version_info > (3, 0): - from http.cookiejar import CookieJar, Cookie -else: - from cookielib import CookieJar, Cookie - +from svtplay_dl.utils.urllib import CookieJar, Cookie from svtplay_dl.service import Service from svtplay_dl.utils import get_http_data, select_quality, subtitle_json from svtplay_dl.log import log diff --git a/lib/svtplay_dl/service/radioplay.py b/lib/svtplay_dl/service/radioplay.py index d51d226..2e378c2 100644 --- a/lib/svtplay_dl/service/radioplay.py +++ b/lib/svtplay_dl/service/radioplay.py @@ -2,11 +2,8 @@ from __future__ import absolute_import import sys import re import json -if sys.version_info > (3, 0): - from urllib.parse import urlparse -else: - from urlparse import urlparse +from svtplay_dl.utils.urllib import urlparse from svtplay_dl.service import Service from svtplay_dl.utils import get_http_data diff --git a/lib/svtplay_dl/service/sr.py b/lib/svtplay_dl/service/sr.py index 28d3eb9..78edff9 100644 --- a/lib/svtplay_dl/service/sr.py +++ b/lib/svtplay_dl/service/sr.py @@ -5,17 +5,12 @@ import sys import re import xml.etree.ElementTree as ET +from svtplay_dl.utils.urllib import urlparse, parse_qs, unquote_plus from svtplay_dl.service import Service from svtplay_dl.utils import get_http_data from svtplay_dl.log import log from svtplay_dl.fetcher.http import download_http -if sys.version_info > (3, 0): - from urllib.parse import urlparse, parse_qs, unquote_plus -else: - from urlparse import urlparse, parse_qs - from urllib import unquote_plus - class Sr(Service): def handle(self, url): return "sverigesradio.se" in url diff --git a/lib/svtplay_dl/service/tv4play.py b/lib/svtplay_dl/service/tv4play.py index c819cb4..555f3e1 100644 --- a/lib/svtplay_dl/service/tv4play.py +++ b/lib/svtplay_dl/service/tv4play.py @@ -5,17 +5,13 @@ import sys import re import xml.etree.ElementTree as ET +from svtplay_dl.utils.urllib import urlparse, parse_qs from svtplay_dl.service import Service from svtplay_dl.utils import get_http_data, select_quality, subtitle_smi from svtplay_dl.log import log from svtplay_dl.fetcher.rtmp import download_rtmp from svtplay_dl.fetcher.hds import download_hds -if sys.version_info > (3, 0): - from urllib.parse import urlparse, parse_qs -else: - from urlparse import urlparse, parse_qs - class Tv4play(Service): def handle(self, url): return ("tv4play.se" in url) or ("tv4.se" in url) diff --git a/lib/svtplay_dl/service/viaplay.py b/lib/svtplay_dl/service/viaplay.py index d3a610f..312377b 100644 --- a/lib/svtplay_dl/service/viaplay.py +++ b/lib/svtplay_dl/service/viaplay.py @@ -3,12 +3,9 @@ from __future__ import absolute_import import sys import re -if sys.version_info > (3, 0): - from urllib.parse import urlparse -else: - from urlparse import urlparse import xml.etree.ElementTree as ET +from svtplay_dl.utils.urllib import urlparse from svtplay_dl.service import Service from svtplay_dl.utils import get_http_data, subtitle_sami from svtplay_dl.log import log diff --git a/lib/svtplay_dl/utils.py b/lib/svtplay_dl/utils/__init__.py similarity index 94% rename from lib/svtplay_dl/utils.py rename to lib/svtplay_dl/utils/__init__.py index 16f091e..85fe655 100644 --- a/lib/svtplay_dl/utils.py +++ b/lib/svtplay_dl/utils/__init__.py @@ -8,15 +8,9 @@ import re import xml.etree.ElementTree as ET import json -if sys.version_info > (3, 0): - from urllib.request import build_opener, HTTPCookieProcessor, HTTPRedirectHandler - from urllib.error import HTTPError, URLError - from urllib.response import addinfourl - from http.cookiejar import CookieJar -else: - from urllib2 import HTTPError, URLError, build_opener, HTTPCookieProcessor, HTTPRedirectHandler - from urllib import addinfourl - from cookielib import CookieJar +from svtplay_dl.utils.urllib import build_opener, HTTPCookieProcessor, \ + HTTPRedirectHandler, HTTPError, URLError, \ + addinfourl, CookieJar log = logging.getLogger('svtplay_dl') progress_stream = sys.stderr diff --git a/lib/svtplay_dl/utils/io.py b/lib/svtplay_dl/utils/io.py new file mode 100644 index 0000000..2909cf6 --- /dev/null +++ b/lib/svtplay_dl/utils/io.py @@ -0,0 +1,14 @@ +# ex:ts=4:sw=4:sts=4:et +# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- + +# Pylint does not seem to handle conditional imports +# pylint: disable=F0401 +# pylint: disable=W0611 + +from __future__ import absolute_import +import sys + +if sys.version_info > (3, 0): + from io import BytesIO as StringIO +else: + from StringIO import StringIO diff --git a/lib/svtplay_dl/utils/urllib.py b/lib/svtplay_dl/utils/urllib.py new file mode 100644 index 0000000..12cdfe3 --- /dev/null +++ b/lib/svtplay_dl/utils/urllib.py @@ -0,0 +1,24 @@ +# ex:ts=4:sw=4:sts=4:et +# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- + +# Pylint does not seem to handle conditional imports +# pylint: disable=F0401 +# pylint: disable=W0611 + +from __future__ import absolute_import +import sys + +if sys.version_info > (3, 0): + # pylint: disable=E0611 + from urllib.parse import unquote_plus, quote_plus, urlparse, parse_qs + from urllib.request import urlopen, Request, build_opener, \ + HTTPCookieProcessor, HTTPRedirectHandler + from urllib.error import HTTPError, URLError + from urllib.response import addinfourl + from http.cookiejar import CookieJar, Cookie +else: + from urllib import addinfourl, unquote_plus, quote_plus + from urlparse import urlparse, parse_qs + from urllib2 import urlopen, Request, HTTPError, URLError, build_opener, \ + HTTPCookieProcessor, HTTPRedirectHandler + from cookielib import CookieJar, Cookie