def _get_RedLaws(): global RedLaws extdir = os.path.join(rootdir, EXTDIR) # get all the fits files in EXTDIR globstr = os.path.join(extdir, '*.fits') if extdir.lower().startswith('ftp:'): from astropy.extern.six.moves.urllib import request response = request.urlopen(extdir).read().decode('utf-8').splitlines() files = list( set([x.split()[-1] for x in response if x.endswith('.fits')])) # Rid symlink # noqa files = [os.path.join(extdir, f) for f in files] else: files = glob.glob(globstr) if not files: warnings.warn('Extinction files not found in %s' % (extdir, )) return # replace ###.fits at the end of file names with *.fits # and get a unique set patterns = set(f[:-8] + '*.fits' for f in files) # use _refTable to get the most recent version of each extinction file # and add that to the RedLaws dict for pattern in patterns: lawf = sorted(fnmatch.filter(files, pattern))[-1] key = pyfits.getval(lawf, 'shortnm') RedLaws[key.lower()] = lawf
def get_latest_file(template, raise_error=False, err_msg=''): """Find the filename that appears last in sorted order based on given template. Parameters ---------- template : str Search template in the form of ``path/pattern`` where pattern is acceptable by :py:mod:`fnmatch`. raise_error : bool, optional Raise an error when no files found. Otherwise, will issue warning only. err_msg : str Alternate message for when no files found. If not given, generic message is used. Returns ------- filename : str Latest filename. Raises ------ IOError No files found. """ path, pattern = os.path.split(template) # Remote FTP directory if path.lower().startswith('ftp:'): from astropy.extern.six.moves.urllib.request import urlopen response = urlopen(path).read().decode('utf-8').splitlines() allfiles = list(set([x.split()[-1] for x in response])) # Rid symlink # Local directory else: allfiles = os.listdir(path) matched_files = sorted(fnmatch.filter(allfiles, pattern)) # Last file in sorted listing if matched_files: filename = os.path.join(path, matched_files[-1]) # No files found else: if not err_msg: err_msg = 'No files found for {0}'.format(template) if raise_error: raise IOError(err_msg) else: log.warn(err_msg) filename = '' return filename
def _fetch_redirects(self): from astropy.extern.six.moves.urllib.request import urlopen import json f = urlopen(self._remote_root + "redirects.json") reader = codecs.getreader("utf-8") self._redirects = json.load(reader(f)) f.close()
def test_localconnect_succeeds(localhost): """ Ensure that connections to localhost are allowed, since these are genuinely not remotedata. """ # port "0" means find open port # see http://stackoverflow.com/questions/1365265/on-localhost-how-to-pick-a-free-port-number httpd = StoppableHTTPServer(('localhost', 0), SimpleHTTPServer.SimpleHTTPRequestHandler) port = httpd.socket.getsockname()[1] server = Thread(target=httpd.serve_forever) server.setDaemon(True) server.start() time.sleep(0.1) urlopen('http://{localhost:s}:{port:d}'.format(localhost=localhost,port=port)).close()
def test_outgoing_fails(): with pytest.raises(IOError): with no_internet(): urlopen('http://www.astropy.org')
def _download_file(remote_url, target): """ Accepts a URL, downloads the file to a given open file object. This is a modified version of astropy.utils.data.download_file that downloads to an open file object instead of a cache directory. """ from contextlib import closing from astropy.extern.six.moves.urllib.request import urlopen, Request from astropy.extern.six.moves.urllib.error import URLError, HTTPError from astropy.utils.console import ProgressBarOrSpinner from . import conf timeout = conf.remote_timeout download_block_size = 32768 try: # Pretend to be a web browser (IE 6.0). Some servers that we download # from forbid access from programs. headers = {'User-Agent': 'Mozilla/5.0', 'Accept': ('text/html,application/xhtml+xml,' 'application/xml;q=0.9,*/*;q=0.8')} req = Request(remote_url, headers=headers) with closing(urlopen(req, timeout=timeout)) as remote: # get size of remote if available (for use in progress bar) info = remote.info() size = None if 'Content-Length' in info: try: size = int(info['Content-Length']) except ValueError: pass dlmsg = "Downloading {0}".format(remote_url) with ProgressBarOrSpinner(size, dlmsg) as p: bytes_read = 0 block = remote.read(download_block_size) while block: target.write(block) bytes_read += len(block) p.update(bytes_read) block = remote.read(download_block_size) # Append a more informative error message to HTTPErrors, URLErrors. except HTTPError as e: e.msg = "{}. requested URL: {!r}".format(e.msg, remote_url) raise except URLError as e: append_msg = (hasattr(e, 'reason') and hasattr(e.reason, 'errno') and e.reason.errno == 8) if append_msg: msg = "{0}. requested URL: {1}".format(e.reason.strerror, remote_url) e.reason.strerror = msg e.reason.args = (e.reason.errno, msg) raise e # This isn't supposed to happen, but occasionally a socket.timeout gets # through. It's supposed to be caught in `urrlib2` and raised in this # way, but for some reason in mysterious circumstances it doesn't. So # we'll just re-raise it here instead. except socket.timeout as e: # add the requested URL to the message (normally just 'timed out') e.args = ('requested URL {!r} timed out'.format(remote_url),) raise URLError(e)
def get_read_fd(): return generic_io.get_file( urllib_request.urlopen(httpserver.url + "test.asdf"))
def get_read_fd(): return generic_io.get_file( urllib_request.urlopen( httpserver.url + "test.asdf"))
def get_latest_file(template, raise_error=False, err_msg=''): """Find the filename that appears last in sorted order based on given template. Parameters ---------- template : str Search template in the form of ``path/pattern`` where pattern is acceptable by :py:mod:`fnmatch`. raise_error : bool, optional Raise an error when no files found. Otherwise, will issue warning only. err_msg : str Alternate message for when no files found. If not given, generic message is used. Returns ------- filename : str Latest filename. Raises ------ IOError No files found. """ path, pattern = os.path.split(irafconvert(template)) # Remote FTP directory if path.lower().startswith('ftp:'): from astropy.extern.six.moves.urllib import request response = request.urlopen(path).read().decode('utf-8').splitlines() allfiles = list(set([x.split()[-1] for x in response])) # Rid symlink # Local directory elif os.path.isdir(path): allfiles = os.listdir(path) # Bogus directory else: allfiles = [] matched_files = sorted(fnmatch.filter(allfiles, pattern)) # Last file in sorted listing if matched_files: filename = os.path.join(path, matched_files[-1]) # No files found else: if not err_msg: err_msg = 'No files found for {0}'.format(template) if raise_error: raise IOError(err_msg) else: warnings.warn(err_msg, AstropyUserWarning) filename = '' return filename
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Mon Jun 4 09:48:36 2018 @author: Dartoon """ import numpy as np import matplotlib.pyplot as plt #If you have trouble accessing the image you can download it straight away using Python: from astropy.extern.six.moves.urllib import request url = "http://python4astronomers.github.com/_downloads/image2.fits" open("image2.fits", "wb").write(request.urlopen(url).read()) #ls from sherpa.astro.ui import * image_data()
def get_affiliated_packages(): source = urlopen(DEFAULT_AFFILIATED_REGISTRY) packages = json.loads(source.read()) packages = packages['packages'] return packages