compat: Make urllib/urlparse usage py2+3 compatible
This commit is contained in:
parent
790b64de13
commit
7b029bfcc4
@ -10,6 +10,25 @@ if PY2:
|
||||
import Queue as queue # noqa
|
||||
import thread # noqa
|
||||
|
||||
def fake_python3_urllib_module():
|
||||
import types
|
||||
import urllib as py2_urllib
|
||||
import urlparse as py2_urlparse
|
||||
|
||||
urllib = types.ModuleType(b'urllib') # noqa
|
||||
urllib.parse = types.ModuleType(b'urlib.parse')
|
||||
|
||||
urllib.parse.quote = py2_urllib.quote
|
||||
urllib.parse.unquote = py2_urllib.unquote
|
||||
|
||||
urllib.parse.urlparse = py2_urlparse.urlparse
|
||||
urllib.parse.urlsplit = py2_urlparse.urlsplit
|
||||
urllib.parse.urlunsplit = py2_urlparse.urlunsplit
|
||||
|
||||
return urllib
|
||||
|
||||
urllib = fake_python3_urllib_module()
|
||||
|
||||
string_types = basestring
|
||||
text_type = unicode
|
||||
|
||||
@ -22,6 +41,7 @@ else:
|
||||
import configparser # noqa
|
||||
import queue # noqa
|
||||
import _thread as thread # noqa
|
||||
import urllib # noqa
|
||||
|
||||
string_types = (str,)
|
||||
text_type = str
|
||||
|
||||
@ -5,11 +5,9 @@ import os
|
||||
import stat
|
||||
import string
|
||||
import threading
|
||||
import urllib
|
||||
import urlparse
|
||||
|
||||
from mopidy import compat, exceptions
|
||||
from mopidy.compat import queue
|
||||
from mopidy.compat import queue, urllib
|
||||
from mopidy.internal import encoding, xdg
|
||||
|
||||
|
||||
@ -61,8 +59,8 @@ def path_to_uri(path):
|
||||
"""
|
||||
if isinstance(path, compat.text_type):
|
||||
path = path.encode('utf-8')
|
||||
path = urllib.quote(path)
|
||||
return urlparse.urlunsplit((b'file', b'', path, b'', b''))
|
||||
path = urllib.parse.quote(path)
|
||||
return urllib.parse.urlunsplit((b'file', b'', path, b'', b''))
|
||||
|
||||
|
||||
def uri_to_path(uri):
|
||||
@ -78,7 +76,7 @@ def uri_to_path(uri):
|
||||
"""
|
||||
if isinstance(uri, compat.text_type):
|
||||
uri = uri.encode('utf-8')
|
||||
return urllib.unquote(urlparse.urlsplit(uri).path)
|
||||
return urllib.parse.unquote(urllib.parse.urlsplit(uri).path)
|
||||
|
||||
|
||||
def split_path(path):
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import collections
|
||||
import urlparse
|
||||
|
||||
from mopidy import compat, exceptions
|
||||
from mopidy.compat import urllib
|
||||
|
||||
PLAYBACK_STATES = {'paused', 'stopped', 'playing'}
|
||||
|
||||
@ -96,7 +96,7 @@ def _check_query_value(key, arg, msg):
|
||||
def check_uri(arg, msg='Expected a valid URI, not {arg!r}'):
|
||||
if not isinstance(arg, compat.string_types):
|
||||
raise exceptions.ValidationError(msg.format(arg=arg))
|
||||
elif urlparse.urlparse(arg).scheme == '':
|
||||
elif urllib.parse.urlparse(arg).scheme == '':
|
||||
raise exceptions.ValidationError(msg.format(arg=arg))
|
||||
|
||||
|
||||
|
||||
@ -4,10 +4,9 @@ import codecs
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import urllib
|
||||
import urlparse
|
||||
|
||||
from mopidy import compat
|
||||
from mopidy.compat import urllib
|
||||
from mopidy.internal import encoding, path
|
||||
from mopidy.models import Track
|
||||
|
||||
@ -28,7 +27,7 @@ def path_to_playlist_uri(relpath):
|
||||
"""Convert path relative to playlists_dir to M3U URI."""
|
||||
if isinstance(relpath, compat.text_type):
|
||||
relpath = relpath.encode('utf-8')
|
||||
return b'm3u:%s' % urllib.quote(relpath)
|
||||
return b'm3u:%s' % urllib.parse.quote(relpath)
|
||||
|
||||
|
||||
def m3u_extinf_to_track(line):
|
||||
@ -101,7 +100,7 @@ def parse_m3u(file_path, media_dir=None):
|
||||
track = m3u_extinf_to_track(line)
|
||||
continue
|
||||
|
||||
if urlparse.urlsplit(line).scheme:
|
||||
if urllib.parse.urlsplit(line).scheme:
|
||||
tracks.append(track.replace(uri=line))
|
||||
elif os.path.normpath(line) == os.path.abspath(line):
|
||||
uri = path.path_to_uri(line)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user