def _read_from_url(url): """Reads data from *url* with an HTTP *GET*. This function supports fetching from resources which use basic HTTP auth as laid out by RFC1738 § 3.1. See § 5 for grammar definitions for URLs. .. seealso: https://www.ietf.org/rfc/rfc1738.txt :param url: URL of an HTTP resource :type url: ``str`` :return: data read from resource described by *url* :rtype: ``file``-like object """ url, username, password = _strip_basic_auth(url) handler = request.BaseHandler() if username is not None and password is not None: # case: url contains basic auth creds password_mgr = request.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, url, username, password) handler = request.HTTPBasicAuthHandler(password_mgr) opener = request.build_opener(handler) return opener.open(url)
def download_scripts(proxies=None, install_dir=None): import visdom print("Downloading scripts. It might take a while.") # location in which to download stuff: if install_dir is None: install_dir = os.path.dirname(visdom.__file__) # all files that need to be downloaded: b = 'https://unpkg.com/' bb = '%[email protected]/dist/' % b ext_files = { # - js '%[email protected]/dist/jquery.min.js' % b: 'jquery.min.js', '%[email protected]/dist/js/bootstrap.min.js' % b: 'bootstrap.min.js', '%[email protected]/umd/react.production.min.js' % b: 'react-react.min.js', '%[email protected]/umd/react-dom.production.min.js' % b: 'react-dom.min.js', # noqa '%[email protected]/dist/react-modal.min.js' % b: 'react-modal.min.js', # noqa 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_SVG': # noqa 'mathjax-MathJax.js', # here is another url in case the cdn breaks down again. # https://raw.githubusercontent.com/plotly/plotly.js/master/dist/plotly.min.js 'https://cdn.plot.ly/plotly-latest.min.js': 'plotly-plotly.min.js', # Stanford Javascript Crypto Library for Password Hashing '%[email protected]/sjcl.js' % b: 'sjcl.js', # - css '%[email protected]/css/styles.css' % b: 'react-resizable-styles.css', # noqa '%[email protected]/css/styles.css' % b: 'react-grid-layout-styles.css', # noqa '%scss/bootstrap.min.css' % bb: 'bootstrap.min.css', # - fonts '%[email protected]' % b: 'classnames', '%[email protected]' % b: 'layout_bin_packer', '%sfonts/glyphicons-halflings-regular.eot' % bb: 'glyphicons-halflings-regular.eot', '%sfonts/glyphicons-halflings-regular.woff2' % bb: 'glyphicons-halflings-regular.woff2', '%sfonts/glyphicons-halflings-regular.woff' % bb: 'glyphicons-halflings-regular.woff', '%sfonts/glyphicons-halflings-regular.ttf' % bb: 'glyphicons-halflings-regular.ttf', '%sfonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular' % bb: # noqa 'glyphicons-halflings-regular.svg#glyphicons_halflingsregular', } # make sure all relevant folders exist: dir_list = [ '%s' % install_dir, '%s/static' % install_dir, '%s/static/js' % install_dir, '%s/static/css' % install_dir, '%s/static/fonts' % install_dir, ] for directory in dir_list: if not os.path.exists(directory): os.makedirs(directory) # set up proxy handler: from six.moves.urllib import request from six.moves.urllib.error import HTTPError, URLError handler = request.ProxyHandler(proxies) if proxies is not None \ else request.BaseHandler() opener = request.build_opener(handler) request.install_opener(opener) built_path = os.path.join(here, 'static/version.built') is_built = False if os.path.exists(built_path): with open(built_path, 'r') as build_file: build_version = build_file.read().strip() if build_version == visdom.__version__: is_built = True else: os.remove(built_path) # download files one-by-one: for (key, val) in ext_files.items(): # set subdirectory: sub_dir = 'fonts' if '.js' in key: sub_dir = 'js' if '.css' in key: sub_dir = 'css' # download file: filename = '%s/static/%s/%s' % (install_dir, sub_dir, val) if not os.path.exists(filename) or not is_built: req = request.Request(key, headers={'User-Agent': 'Chrome/30.0.0.0'}) try: data = opener.open(req).read() with open(filename, 'wb') as fwrite: fwrite.write(data) except HTTPError as exc: logging.error('Error {} while downloading {}'.format( exc.code, key)) except URLError as exc: logging.error('Error {} while downloading {}'.format( exc.reason, key)) if not is_built: with open(built_path, 'w+') as build_file: build_file.write(visdom.__version__)
def download_scripts(proxies=None, install_dir=None): # location in which to download stuff: if install_dir is None: import visdom install_dir = os.path.dirname(visdom.__file__) # all files that need to be downloaded: b = 'https://unpkg.com/' bb = '%[email protected]/dist/' % b ext_files = { '%[email protected]/dist/jquery.min.js' % b: 'jquery.min.js', '%[email protected]/dist/js/bootstrap.min.js' % b: 'bootstrap.min.js', '%[email protected]/css/styles.css' % b: 'react-resizable-styles.css', '%[email protected]/css/styles.css' % b: 'react-grid-layout-styles.css', '%[email protected]/dist/react.min.js' % b: 'react-react.min.js', '%[email protected]/dist/react-dom.min.js' % b: 'react-dom.min.js', '%[email protected]' % b: 'classnames', '%[email protected]' % b: 'layout_bin_packer', 'https://cdn.rawgit.com/STRML/react-grid-layout/0.14.0/dist/' + 'react-grid-layout.min.js': 'react-grid-layout.min.js', 'https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_SVG': 'mathjax-MathJax.js', 'https://cdn.rawgit.com/plotly/plotly.js/master/dist/plotly.min.js': 'plotly-plotly.min.js', '%scss/bootstrap.min.css' % bb: 'bootstrap.min.css', '%sfonts/glyphicons-halflings-regular.eot' % bb: 'glyphicons-halflings-regular.eot', '%sfonts/glyphicons-halflings-regular.eot?#iefix' % bb: 'glyphicons-halflings-regular.eot?#iefix', '%sfonts/glyphicons-halflings-regular.woff2' % bb: 'glyphicons-halflings-regular.woff2', '%sfonts/glyphicons-halflings-regular.woff' % bb: 'glyphicons-halflings-regular.woff', '%sfonts/glyphicons-halflings-regular.ttf' % bb: 'glyphicons-halflings-regular.ttf', '%sfonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular' % bb: 'glyphicons-halflings-regular.svg#glyphicons_halflingsregular', } # make sure all relevant folders exist: dir_list = [ '%s' % install_dir, '%s/static' % install_dir, '%s/static/js' % install_dir, '%s/static/css' % install_dir, '%s/static/fonts' % install_dir, ] for directory in dir_list: if not os.path.exists(directory): os.makedirs(directory) # set up proxy handler: from six.moves.urllib import request from six.moves.urllib.error import HTTPError, URLError handler = request.ProxyHandler(proxies) if proxies is not None \ else request.BaseHandler() opener = request.build_opener(handler) request.install_opener(opener) # download files one-by-one: for (key, val) in ext_files.items(): # set subdirectory: sub_dir = 'fonts' if '.js' in key: sub_dir = 'js' if '.css' in key: sub_dir = 'css' # download file: filename = '%s/static/%s/%s' % (install_dir, sub_dir, val) if not os.path.exists(filename): req = request.Request(key, headers={'User-Agent': 'Chrome/30.0.0.0'}) try: data = opener.open(req).read() with open(filename, 'wb') as fwrite: fwrite.write(data) except (HTTPError, URLError) as exc: logging.error('Error {} while downloading {}'.format( exc.code, key))