From a72791507f12b4404ba18be0272958230a2ac7a2 Mon Sep 17 00:00:00 2001 From: agnewee Date: Mon, 4 Dec 2017 23:55:25 +0800 Subject: [PATCH 1/6] move ulib and urlparse to compat --- pydu/compat.py | 6 +++++- pydu/request.py | 9 +-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/pydu/compat.py b/pydu/compat.py index 0444588..9700eae 100644 --- a/pydu/compat.py +++ b/pydu/compat.py @@ -4,10 +4,14 @@ import sys PY2 = sys.version_info[0] == 2 -# urljoin +# urllib, urlparse and urljoin if PY2: + import urllib as ulib + import urlparse from urlparse import urljoin else: + import urllib.request as ulib + import urllib.parse as urlparse from urllib.parse import urljoin # Dictionary iteration diff --git a/pydu/request.py b/pydu/request.py index b74660a..ee7ff37 100644 --- a/pydu/request.py +++ b/pydu/request.py @@ -1,16 +1,9 @@ import os import shutil import tempfile -from pydu.compat import PY2, string_types +from pydu.compat import string_types, ulib, urlparse from pydu.string import safeunicode -if PY2: - import urllib as ulib - import urlparse -else: - import urllib.request as ulib - import urllib.parse as urlparse - class FileName(object): @staticmethod From 0c81bbd6930e2c42f03c4610ff15afd01662cfab Mon Sep 17 00:00:00 2001 From: agnewee Date: Mon, 4 Dec 2017 23:55:44 +0800 Subject: [PATCH 2/6] add compat test case --- tests/test_compat.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tests/test_compat.py b/tests/test_compat.py index 7203f12..54f46cc 100644 --- a/tests/test_compat.py +++ b/tests/test_compat.py @@ -1,6 +1,7 @@ from pydu.compat import (PY2, iterkeys, itervalues, iteritems, text_type, string_types, numeric_types, - is_iterable, has_next_attr) + is_iterable, has_next_attr, urljoin, + imap) def test_itersth(): @@ -47,8 +48,21 @@ def test_types(): def test_urljoin(): - from pydu.compat import urljoin + assert 'http://uyun.cn/test' == urljoin('http://uyun.cn', 'test') def test_imap(): - from pydu.compat import imap + assert list(imap(pow, (2, 3, 10), (5, 2, 3))) == [32, 9, 1000] + assert list(imap(max, (1, 4, 7), (2, 3, 8))) == [2, 4, 8] + + +# TODO: add test case +def test_ulib(): + from pydu.compat import ulib + pass + + +# TODO: add test case +def test_urlparse(): + from pydu.compat import urlparse + pass \ No newline at end of file From bb0cf800d8c56fbb49b59fe4589cf5b6da69a523 Mon Sep 17 00:00:00 2001 From: agnewee Date: Tue, 5 Dec 2017 00:48:44 +0800 Subject: [PATCH 3/6] import PY2 --- pydu/request.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydu/request.py b/pydu/request.py index ee7ff37..e9f2912 100644 --- a/pydu/request.py +++ b/pydu/request.py @@ -1,7 +1,7 @@ import os import shutil import tempfile -from pydu.compat import string_types, ulib, urlparse +from pydu.compat import PY2, string_types, ulib, urlparse from pydu.string import safeunicode From d470afa84eb3f007056a9950dc1758b21f5b44dc Mon Sep 17 00:00:00 2001 From: agnewee Date: Tue, 5 Dec 2017 00:49:15 +0800 Subject: [PATCH 4/6] add request test case --- tests/test_request.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_request.py b/tests/test_request.py index 3e29a62..967e7e6 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -1,4 +1,8 @@ -from pydu.request import FileName +import os +from tempfile import mkdtemp + +from pydu.request import FileName, download +from pydu.file import remove def test_filename_from_url(): @@ -36,3 +40,11 @@ def test_filename_from_headers(): headers = 'Content-Disposition: attachment; filename=' assert FileName.from_headers(headers) is None + + +def test_download(): + file_url = 'https://www.cnblogs.com/images/logo_small.gif' + file_dir = mkdtemp() + file_path = download(file_url, file_dir) + assert os.path.exists(file_path) + remove(file_dir) From d1b051711492371541bb7515dee53170b59a008b Mon Sep 17 00:00:00 2001 From: agnewee Date: Tue, 5 Dec 2017 00:49:36 +0800 Subject: [PATCH 5/6] add string test case --- tests/test_string.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_string.py b/tests/test_string.py index 44e738f..4fda139 100644 --- a/tests/test_string.py +++ b/tests/test_string.py @@ -14,6 +14,9 @@ def test_safeunicode(): assert safeunicode(1) == u'1' assert safeunicode('中文') == u'中文' + assert safeunicode(u'hell0') == u'hello' + assert safeunicode(u'中文') == u'中文' + def test_lstrips(): assert lstrips('foobbar', '') == 'foobbar' From 79827ac7efb9ddff8226109a1b35680d4063e089 Mon Sep 17 00:00:00 2001 From: agnewee Date: Tue, 5 Dec 2017 00:57:23 +0800 Subject: [PATCH 6/6] repaire test case error --- tests/test_string.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_string.py b/tests/test_string.py index 4fda139..6715995 100644 --- a/tests/test_string.py +++ b/tests/test_string.py @@ -14,7 +14,7 @@ def test_safeunicode(): assert safeunicode(1) == u'1' assert safeunicode('中文') == u'中文' - assert safeunicode(u'hell0') == u'hello' + assert safeunicode(u'hello') == u'hello' assert safeunicode(u'中文') == u'中文'