Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_host():
with mock.patch('socket.gethostname', mock.Mock(return_value='test')),\
mock.patch('socket.gethostbyname', mock.Mock(return_value='127.0.0.1')):
assert utils.get_host() == 'test'
with mock.patch('socket.gethostbyname', mock.Mock(return_value='127.0.0.1')):
assert utils.get_host('test') == 'test'
with mock.patch('socket.gethostname', mock.Mock(return_value='http://test')),\
mock.patch('socket.gethostbyname', mock.Mock(side_effect=Exception)):
assert utils.get_host() == 'localhost'
with mock.patch('socket.gethostbyname', mock.Mock(side_effect=Exception)):
with pytest.raises(Exception) as error:
utils.get_host('test')
assert str(error.value).startswith('Hostname (test) is not recognized')
compress = Compress()
compress.init_app(app)
_, version = retrieve_meta_info_and_version('dtale')
template = dict(
info={
'title': 'D-Tale',
'version': version,
'description': 'Web Client for Visualizing Pandas Objects',
'contact': {
'name': 'Man Alpha Technology',
'email': 'ManAlphaTech@man.com',
'url': 'https://github.com/man-group/dtale'
},
},
host=get_host(host),
schemes=['http'],
)
try:
from flasgger import Swagger # flake8: NOQA
Swagger(app, template=template)
except ImportError:
logger.debug('flasgger dependency not found, please install to enable feature')
@app.route('/')
@app.route('/dtale')
@swag_from('swagger/dtale/root.yml')
def root():
"""
:class:`flask:flask.Flask` routes which redirect to dtale/main
:return: 302 - flask.redirect('/dtale/main')
:class:`flask:flask.Flask` process
:param host: hostname to use otherwise it will default to the output of :func:`python:socket.gethostname`
:type host: str, optional
:param port: port to use otherwise default to the output of :func:dtale.app.find_free_port
:type port: str, optional
:param force: boolean flag to determine whether to ignore the :func:dtale.app.find_free_port function
:type force: bool
:return:
"""
global ACTIVE_HOST, ACTIVE_PORT
if force:
active_host = get_host(ACTIVE_HOST)
curr_base = build_url(ACTIVE_PORT, active_host)
final_host = get_host(host)
new_base = build_url(port, final_host)
if curr_base != new_base:
if is_up(new_base):
try:
kill(new_base) # kill the original process
except BaseException:
raise IOError((
'Could not kill process at {}, possibly something else is running at port {}. Please try '
'another port.'
).format(new_base, port))
while is_up(new_base):
time.sleep(0.01)
ACTIVE_HOST = final_host
ACTIVE_PORT = port
return
if is_up(new_base):
try:
kill(new_base) # kill the original process
except BaseException:
raise IOError((
'Could not kill process at {}, possibly something else is running at port {}. Please try '
'another port.'
).format(new_base, port))
while is_up(new_base):
time.sleep(0.01)
ACTIVE_HOST = final_host
ACTIVE_PORT = port
return
if ACTIVE_HOST is None:
ACTIVE_HOST = get_host(host)
if ACTIVE_PORT is None:
ACTIVE_PORT = int(port or find_free_port())
"""
Helper function to initalize global state corresponding to the host & port being used for your
:class:`flask:flask.Flask` process
:param host: hostname to use otherwise it will default to the output of :func:`python:socket.gethostname`
:type host: str, optional
:param port: port to use otherwise default to the output of :func:dtale.app.find_free_port
:type port: str, optional
:param force: boolean flag to determine whether to ignore the :func:dtale.app.find_free_port function
:type force: bool
:return:
"""
global ACTIVE_HOST, ACTIVE_PORT
if force:
active_host = get_host(ACTIVE_HOST)
curr_base = build_url(ACTIVE_PORT, active_host)
final_host = get_host(host)
new_base = build_url(port, final_host)
if curr_base != new_base:
if is_up(new_base):
try:
kill(new_base) # kill the original process
except BaseException:
raise IOError((
'Could not kill process at {}, possibly something else is running at port {}. Please try '
'another port.'
).format(new_base, port))
while is_up(new_base):
time.sleep(0.01)
ACTIVE_HOST = final_host
ACTIVE_PORT = port