Browse Source

Use dictionary instead of list of tuples in url access checker

Refactor url_access_checker/api.py to use idiomatic python
instead of list of tuple

Change-Id: Ia2a6a5f301b51b88eda8a4086f7a769230a6c055
Related-Bug: #1488946
tivaliy 3 years ago
parent
commit
04760b3557
1 changed files with 25 additions and 24 deletions
  1. 25
    24
      url_access_checker/api.py

+ 25
- 24
url_access_checker/api.py View File

@@ -19,9 +19,10 @@ import os
19 19
 import socket
20 20
 
21 21
 import requests
22
+import six
22 23
 from six.moves import urllib
23 24
 
24
-import url_access_checker.errors as errors
25
+from url_access_checker import errors
25 26
 
26 27
 
27 28
 def check_urls(urls, proxies=None, timeout=60):
@@ -38,47 +39,47 @@ def check_urls(urls, proxies=None, timeout=60):
38 39
     proxies -- proxy servers to use for the request
39 40
     timeout -- the max time to wait for a response, default 60 seconds
40 41
     """
41
-    responses = map(lambda u: _get_response_tuple(
42
-        u, proxies=proxies, timeout=timeout), urls)
43
-    failed_responses = filter(lambda x: x[0], responses)
42
+    responses = {u: _unavailable_url(u, proxies=proxies, timeout=timeout)
43
+                 for u in urls}
44
+
45
+    failed_responses = {k: v for k, v in six.iteritems(responses) if v}
44 46
 
45 47
     if failed_responses:
46 48
         raise errors.UrlNotAvailable(json.dumps(
47
-            {'failed_urls': map(lambda r: r[1], failed_responses)}))
49
+            {'failed_urls': sorted(failed_responses)}))
48 50
     else:
49 51
         return True
50 52
 
51 53
 
52
-def _get_response_tuple(url, proxies=None, timeout=60):
53
-    """Return a tuple which contains a result of url test
54
+def _unavailable_url(url, proxies=None, timeout=60):
55
+    """Return the result of url test
54 56
 
55 57
     Arguments:
56 58
     url -- a string containing url for testing, can be local file
57 59
     proxies -- proxy servers to use for the request
58 60
     timeout -- the max time to wait for a response, default 60 seconds
59 61
 
60
-    Result tuple content:
61
-        result[0] -- boolean value, True if the url is deemed failed
62
-        result[1] -- unchange url argument
62
+    Result content:
63
+        result -- boolean value, True if the url is deemed failed
63 64
     """
64 65
 
65 66
     parsed = urllib.parse.urlparse(url)
66 67
     if parsed.scheme == 'file':
67
-        return _get_file_existence_tuple(url)
68
+        return _get_file_existence(url)
68 69
     elif parsed.scheme in ['http', 'https']:
69
-        return _get_http_response_tuple(url, proxies, timeout)
70
+        return _get_http_response(url, proxies, timeout)
70 71
     elif parsed.scheme == 'ftp':
71
-        return _get_ftp_response_tuple(url, timeout)
72
+        return _get_ftp_response(url, timeout)
72 73
     else:
73 74
         raise errors.InvalidProtocol(url)
74 75
 
75 76
 
76
-def _get_file_existence_tuple(url):
77
+def _get_file_existence(url):
77 78
     path = url[len('file://'):]
78
-    return (not os.path.exists(path), url)
79
+    return not os.path.exists(path)
79 80
 
80 81
 
81
-def _get_http_response_tuple(url, proxies=None, timeout=60):
82
+def _get_http_response(url, proxies=None, timeout=60):
82 83
     try:
83 84
         # requests seems to correctly handle various corner cases:
84 85
         # proxies=None or proxies={} mean 'use the default' rather than
@@ -89,26 +90,26 @@ def _get_http_response_tuple(url, proxies=None, timeout=60):
89 90
         # default timeout is None which can lead to bad things when processes
90 91
         # never exit. LP#1478138
91 92
         response = requests.get(url, proxies=proxies, timeout=timeout)
92
-        return (response.status_code != 200, url)
93
+        return response.status_code != 200
93 94
     except (requests.exceptions.ConnectionError,
94 95
             requests.exceptions.Timeout,
95 96
             requests.exceptions.HTTPError,
96 97
             requests.exceptions.ProxyError,
97 98
             ValueError,
98 99
             socket.timeout):
99
-        return (True, url)
100
+        return True
100 101
 
101 102
 
102
-def _get_ftp_response_tuple(url, timeout=60):
103
-    """Return a tuple which contains a result of ftp url test
103
+def _get_ftp_response(url, timeout=60):
104
+    """Return the result of ftp url test
104 105
 
105
-    It will try to open ftp url as anonymous user and return (True, url) if
106
-    any errors occur, or return (False, url) otherwise.
106
+    It will try to open ftp url as anonymous user and return True if
107
+    any errors occur, or return False otherwise.
107 108
     """
108 109
     try:
109 110
         # NOTE(mkwiek): requests don't have tested ftp adapter yet, so
110 111
         # lower level urllib2 is used here
111 112
         urllib.request.urlopen(url, timeout=timeout)
112
-        return (False, url)
113
+        return False
113 114
     except urllib.error.URLError:
114
-        return (True, url)
115
+        return True

Loading…
Cancel
Save