Skip to content

Commit 0359289

Browse files
author
Sky NSS
committed
Change username pwd to auth (username, password)
1 parent 8b5f337 commit 0359289

File tree

6 files changed

+65
-91
lines changed

6 files changed

+65
-91
lines changed

pandas/io/common.py

+43-43
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ def _stringify_path(filepath_or_buffer):
184184

185185

186186
def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
187-
compression=None, username=None,
188-
password=None, verify_ssl=None):
187+
compression=None, auth=None,
188+
verify_ssl=None):
189189
"""
190190
If the filepath_or_buffer is a url, translate and return the buffer.
191191
Otherwise passthrough.
@@ -194,11 +194,10 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
194194
----------
195195
filepath_or_buffer : a url, filepath (str, py.path.local or pathlib.Path),
196196
or buffer
197-
support 'https://username:[email protected]:port/aaa.csv'
197+
supports 'https://username:[email protected]:port/aaa.csv'
198198
encoding : the encoding to use to decode py3 bytes, default is 'utf-8'
199199
compression:
200-
username: Authentication username (for https basic auth)
201-
password: Authentication password (for https basic auth)
200+
auth: (str,str), default None. (username, password) for HTTP(s) basic auth
202201
verify_ssl: Default True. If False, allow self signed and invalid SSL
203202
certificates for https
204203
@@ -210,8 +209,7 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
210209

211210
if _is_url(filepath_or_buffer):
212211
ureq, kwargs = get_urlopen_args(filepath_or_buffer,
213-
uname=username,
214-
pwd=password,
212+
auth=auth,
215213
verify_ssl=verify_ssl)
216214
req = _urlopen(ureq, **kwargs)
217215
content_encoding = req.headers.get('Content-Encoding', None)
@@ -262,16 +260,45 @@ def file_path_to_url(path):
262260
}
263261

264262

265-
def split_uname_from_url(url_with_uname):
266-
o = parse_url(url_with_uname)
267-
usrch = '{}:{}@{}'.format(o.username, o.password, o.hostname)
268-
url_no_usrpwd = url_with_uname.replace(usrch, o.hostname)
269-
return o.username, o.password, url_no_usrpwd
270-
271-
272-
def get_urlopen_args(url_with_uname, uname=None, pwd=None, verify_ssl=True):
263+
def get_urlopen_args(url_with_uname, auth=None, verify_ssl=True):
264+
def split_auth_from_url(url_with_uname):
265+
o = parse_url(url_with_uname)
266+
usrch = '{}:{}@{}'.format(o.username, o.password, o.hostname)
267+
url_no_usrpwd = url_with_uname.replace(usrch, o.hostname)
268+
return (o.username, o.password), url_no_usrpwd
269+
270+
def get_urlopen_args_py2(uname, pwd, url_no_usrpwd, verify_ssl=True):
271+
req = Request(url_no_usrpwd)
272+
upstr = '{}:{}'.format(uname, pwd)
273+
base64string = base64.encodestring(upstr).replace('\n', '')
274+
req.add_header("Authorization", "Basic {}".format(base64string))
275+
# I hope pandas can support self signed certs too
276+
kwargs = {}
277+
if verify_ssl not in [None, True]:
278+
kwargs['context'] = ssl._create_unverified_context()
279+
return req, kwargs
280+
281+
def get_urlopen_args_py3(uname, pwd, url_no_usrpwd, verify_ssl=True):
282+
# not using urllib.request Request for PY3 because
283+
# this looks like better code from extensibility purpose
284+
passman = HTTPPasswordMgrWithDefaultRealm()
285+
passman.add_password(None, url_no_usrpwd, uname, pwd)
286+
authhandler = HTTPBasicAuthHandler(passman)
287+
if verify_ssl in [None, True]:
288+
opener = build_opener(authhandler)
289+
else:
290+
context = ssl.create_default_context()
291+
context.check_hostname = False
292+
context.verify_mode = ssl.CERT_NONE
293+
opener = build_opener(authhandler, HTTPSHandler(context=context))
294+
install_opener(opener)
295+
return url_no_usrpwd, {}
296+
297+
uname = pwd = None
298+
if auth and len(auth) == 2:
299+
uname, pwd = auth
273300
if not uname and not pwd:
274-
uname, pwd, url_no_usrpwd = split_uname_from_url(url_with_uname)
301+
(uname, pwd), url_no_usrpwd = split_auth_from_url(url_with_uname)
275302
else:
276303
url_no_usrpwd = url_with_uname
277304
if compat.PY3:
@@ -282,33 +309,6 @@ def get_urlopen_args(url_with_uname, uname=None, pwd=None, verify_ssl=True):
282309
return req, kwargs
283310

284311

285-
def get_urlopen_args_py2(uname, pwd, url_no_usrpwd, verify_ssl=True):
286-
req = Request(url_no_usrpwd)
287-
upstr = '{}:{}'.format(uname, pwd)
288-
base64string = base64.encodestring(upstr).replace('\n', '')
289-
req.add_header("Authorization", "Basic {}".format(base64string))
290-
# I hope pandas can support self signed certs too
291-
kwargs = {}
292-
if verify_ssl not in [None, True]:
293-
kwargs['context'] = ssl._create_unverified_context()
294-
return req, kwargs
295-
296-
297-
def get_urlopen_args_py3(uname, pwd, url_no_usrpwd, verify_ssl=True):
298-
passman = HTTPPasswordMgrWithDefaultRealm()
299-
passman.add_password(None, url_no_usrpwd, uname, pwd)
300-
authhandler = HTTPBasicAuthHandler(passman)
301-
if verify_ssl in [None, True]:
302-
opener = build_opener(authhandler)
303-
else:
304-
context = ssl.create_default_context()
305-
context.check_hostname = False
306-
context.verify_mode = ssl.CERT_NONE
307-
opener = build_opener(authhandler, HTTPSHandler(context=context))
308-
install_opener(opener)
309-
return url_no_usrpwd, {}
310-
311-
312312
def _infer_compression(filepath_or_buffer, compression):
313313
"""
314314
Get the compression method for filepath_or_buffer. If compression='infer',

pandas/io/excel.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ def read_excel(io, sheet_name=0, header=0, skiprows=None, skip_footer=0,
212212
if not isinstance(io, ExcelFile):
213213
io = ExcelFile(io,
214214
engine=engine,
215-
username=kwds.get('username', None),
216-
password=kwds.get('password', None),
215+
auth=kwds.get('auth', None),
217216
verify_ssl=kwds.get('verify_ssl', None))
218217

219218
return io._parse_excel(
@@ -264,8 +263,7 @@ def __init__(self, io, **kwds):
264263
if _is_url(self._io):
265264
verify_ssl = kwds.get('verify_ssl', None)
266265
ureq, kwargs = get_urlopen_args(self._io,
267-
uname=kwds.get('username', None),
268-
pwd=kwds.get('password', None),
266+
auth=kwds.get('auth', None),
269267
verify_ssl=verify_ssl)
270268
io = _urlopen(ureq, **kwargs)
271269
elif not isinstance(self.io, (ExcelFile, xlrd.Book)):

pandas/io/html.py

+12-20
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,20 @@ def _get_skiprows(skiprows):
117117
type(skiprows).__name__)
118118

119119

120-
def _read(obj, username=None, password=None, verify_ssl=None):
120+
def _read(obj, auth=None, verify_ssl=None):
121121
"""Try to read from a url, file or string.
122122
123123
Parameters
124124
----------
125125
obj : str, unicode, or file-like
126-
username: username for http basic auth
127-
password: password for http basic auth
126+
auth: None or (username, password) for http basic auth
128127
verify_ssl: Default True. Set to False to disable cert verification
129128
Returns
130129
-------
131130
raw_text : str
132131
"""
133132
if _is_url(obj):
134-
ureq, kwargs = get_urlopen_args(obj, username, password, verify_ssl)
133+
ureq, kwargs = get_urlopen_args(obj, auth, verify_ssl)
135134
with urlopen(ureq, **kwargs) as url:
136135
text = url.read()
137136
elif hasattr(obj, 'read'):
@@ -191,14 +190,13 @@ class _HtmlFrameParser(object):
191190
functionality.
192191
"""
193192

194-
def __init__(self, io, match, attrs, encoding, username=None,
195-
password=None, verify_ssl=None):
193+
def __init__(self, io, match, attrs, encoding, auth=None,
194+
verify_ssl=None):
196195
self.io = io
197196
self.match = match
198197
self.attrs = attrs
199198
self.encoding = encoding
200-
self.username = username
201-
self.password = password
199+
self.auth = auth
202200
self.verify_ssl = verify_ssl
203201

204202
def parse_tables(self):
@@ -452,8 +450,7 @@ def _parse_tables(self, doc, match, attrs):
452450
return result
453451

454452
def _setup_build_doc(self):
455-
raw_text = _read(self.io, self.username,
456-
self.password, self.verify_ssl)
453+
raw_text = _read(self.io, self.auth, self.verify_ssl)
457454
if not raw_text:
458455
raise ValueError('No text parsed from document: %s' % self.io)
459456
return raw_text
@@ -743,8 +740,7 @@ def _parse(flavor, io, match, attrs, encoding, **kwargs):
743740
p = parser(io, compiled_match,
744741
attrs,
745742
encoding,
746-
username=kwargs.get('username', None),
747-
password=kwargs.get('password', None),
743+
auth=kwargs.get('auth', None),
748744
verify_ssl=kwargs.get('verify_ssl', None))
749745
try:
750746
tables = p.parse_tables()
@@ -768,7 +764,7 @@ def read_html(io, match='.+', flavor=None, header=None, index_col=None,
768764
skiprows=None, attrs=None, parse_dates=False,
769765
tupleize_cols=False, thousands=',', encoding=None,
770766
decimal='.', converters=None, na_values=None,
771-
keep_default_na=True, username=None, password=None,
767+
keep_default_na=True, auth=None,
772768
verify_ssl=False):
773769
r"""Read HTML tables into a ``list`` of ``DataFrame`` objects.
774770
@@ -870,11 +866,7 @@ def read_html(io, match='.+', flavor=None, header=None, index_col=None,
870866
871867
.. versionadded:: 0.19.0
872868
873-
username : str, default None
874-
username for HTTP(s) basic auth
875-
876-
password : str, default None
877-
password for HTTP(s) basic auth
869+
auth: (str,str), default None. (username, password) for HTTP(s) basic auth
878870
879871
verify_ssl : bool, default True
880872
If False, ssl certificate is not verified (allow self signed SSL certs)
@@ -926,5 +918,5 @@ def read_html(io, match='.+', flavor=None, header=None, index_col=None,
926918
parse_dates=parse_dates, tupleize_cols=tupleize_cols,
927919
thousands=thousands, attrs=attrs, encoding=encoding,
928920
decimal=decimal, converters=converters, na_values=na_values,
929-
keep_default_na=keep_default_na, username=username,
930-
password=password, verify_ssl=verify_ssl)
921+
keep_default_na=keep_default_na, auth=auth,
922+
verify_ssl=verify_ssl)

pandas/io/json/json.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def write(self):
174174
def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True,
175175
convert_axes=True, convert_dates=True, keep_default_dates=True,
176176
numpy=False, precise_float=False, date_unit=None, encoding=None,
177-
lines=False, username=None, password=None, verify_ssl=None):
177+
lines=False, auth=None, verify_ssl=None):
178178
"""
179179
Convert a JSON string to pandas object
180180
@@ -263,8 +263,7 @@ def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True,
263263
264264
.. versionadded:: 0.19.0
265265
266-
username: str, default None. Authentication username for HTTP(s) basic auth
267-
passowrd: str, default None. Authentication password for HTTP(s) basic auth
266+
auth: (str,str), default None. (username, password) for HTTP(s) basic auth
268267
verify_ssl: boolean, default None (True).
269268
If false, allow self siged SSL certificates
270269
@@ -327,8 +326,7 @@ def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True,
327326

328327
filepath_or_buffer, _, _ = get_filepath_or_buffer(path_or_buf,
329328
encoding=encoding,
330-
username=username,
331-
password=password,
329+
auth=auth,
332330
verify_ssl=verify_ssl)
333331
if isinstance(filepath_or_buffer, compat.string_types):
334332
try:

pandas/io/parsers.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,11 @@ def _read(filepath_or_buffer, kwds):
391391
kwds['encoding'] = encoding
392392

393393
compression = kwds.get('compression')
394-
username = kwds.get('username', None)
395-
password = kwds.get('password', None)
394+
auth = kwds.get('auth', None)
396395
verify_ssl = kwds.get('verify_ssl', None)
397396
compression = _infer_compression(filepath_or_buffer, compression)
398397
filepath_or_buffer, _, compression = get_filepath_or_buffer(
399-
filepath_or_buffer, encoding, compression, username, password,
398+
filepath_or_buffer, encoding, compression, auth,
400399
verify_ssl)
401400
kwds['compression'] = compression
402401

@@ -580,9 +579,8 @@ def parser_f(filepath_or_buffer,
580579
memory_map=False,
581580
float_precision=None,
582581

583-
# Basic auth (http/https)
584-
username=None,
585-
password=None,
582+
# Basic auth (http/https) (username, password)
583+
auth=None,
586584

587585
# skip verify self signed SSL certificates
588586
verify_ssl=None):
@@ -667,8 +665,7 @@ def parser_f(filepath_or_buffer,
667665
infer_datetime_format=infer_datetime_format,
668666
skip_blank_lines=skip_blank_lines,
669667

670-
username=username,
671-
password=password,
668+
auth=auth,
672669
verify_ssl=verify_ssl
673670
)
674671

pandas/tests/io/test_common.py

-11
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,6 @@ def test_write_fspath_hdf5(self):
190190

191191
tm.assert_frame_equal(result, expected)
192192

193-
def test_split_url_extract_uname_pwd(self):
194-
for url, uname, pwd, nurl in [('https://aaa:[email protected]:1010/aaa.txt',
195-
'aaa',
196-
'bbb',
197-
'https://ccc.com:1010/aaa.txt'
198-
)]:
199-
un, p, u = common.split_uname_from_url(url)
200-
assert u == nurl
201-
assert un == uname
202-
assert p == pwd
203-
204193

205194
class TestMMapWrapper(object):
206195

0 commit comments

Comments
 (0)