
[c           @   s  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 Z	 d  d l
 Z
 d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l m Z d d l m Z d  d l m Z d  d l Z y d  d l m Z Wn' e k
 r=d e f d	     YZ n Xd d
 l m Z d d l m Z m  Z  m! Z! m" Z" m# Z# d d l$ m% Z% m& Z& d d l m' Z' d d l( m) Z) d d l* m+ Z+ e j, re- Z- n	 d   Z- e  e j. f Z/ d e f d     YZ0 d   Z1 d d  Z3 d   Z4 d   Z5 d   Z6 d   Z7 d   Z8 d   Z9 e j, r]d   Z: n	 d   Z: e j, r{d   Z; n	 d   Z; d   Z< d    Z= d!   Z> d"   Z? d#   Z@ e j, re
 jA ZA d$   ZB n eC jA ZA d%   ZB d&   ZD d aE d'   ZF d; ZG d= ZH d,   ZI d-   ZJ d.   ZK d/   ZL d0   ZM d1   ZN d2   ZO d3   ZP d4   ZQ d5   ZR d6   ZS e d d7   ZT d8   ZU d9   ZV d S(>   i(   t   divisionN(   t   contextmanageri   (   t   data(   t   LooseVersion(   t   StringIOR   c           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyR      s   (   t   six(   t   string_typest   integer_typest	   text_typet   binary_typet   next(   t   zipt   range(   t   wraps(   t   suppress(   t   AstropyUserWarningc         C   s   |  | k |  | k  S(   N(    (   t   at   b(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   <lambda>.   s    t   NotifierMixinc           B   s8   e  Z d  Z d Z d   Z d   Z d   Z d   Z RS(   s  
    Mixin class that provides services by which objects can register
    listeners to changes on that object.

    All methods provided by this class are underscored, since this is intended
    for internal use to communicate between classes in a generic way, and is
    not machinery that should be exposed to users of the classes involved.

    Use the ``_add_listener`` method to register a listener on an instance of
    the notifier.  This registers the listener with a weak reference, so if
    no other references to the listener exist it is automatically dropped from
    the list and does not need to be manually removed.

    Call the ``_notify`` method on the notifier to update all listeners
    upon changes.  ``_notify('change_type', *args, **kwargs)`` results
    in calling ``listener._update_change_type(*args, **kwargs)`` on all
    listeners subscribed to that notifier.

    If a particular listener does not have the appropriate update method
    it is ignored.

    Examples
    --------

    >>> class Widget(NotifierMixin):
    ...     state = 1
    ...     def __init__(self, name):
    ...         self.name = name
    ...     def update_state(self):
    ...         self.state += 1
    ...         self._notify('widget_state_changed', self)
    ...
    >>> class WidgetListener(object):
    ...     def _update_widget_state_changed(self, widget):
    ...         print('Widget {0} changed state to {1}'.format(
    ...             widget.name, widget.state))
    ...
    >>> widget = Widget('fred')
    >>> listener = WidgetListener()
    >>> widget._add_listener(listener)
    >>> widget.update_state()
    Widget fred changed state to 2
    c         C   s8   |  j  d k r! t j   |  _  n  | |  j  t |  <d S(   s   
        Add an object to the list of listeners to notify of changes to this
        object.  This adds a weakref to the list of listeners that is
        removed from the listeners list when the listener has no other
        references to it.
        N(   t
   _listenerst   Nonet   weakreft   WeakValueDictionaryt   id(   t   selft   listener(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   _add_listenerb   s    c         C   s:   |  j  d k r d St t   |  j  t |  =Wd QXd S(   s   
        Removes the specified listener from the listeners list.  This relies
        on object identity (i.e. the ``is`` operator).
        N(   R   R   R   t   KeyErrorR   (   R   R   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   _remove_listenero   s    c         O   s   |  j  d k r d Sd j |  } xo |  j  j   D]^ } |   } | d k rS q2 n  t | |  r2 t | |  } t |  r | | |   q q2 q2 Wd S(   sA  
        Notify all listeners of some particular state change by calling their
        ``_update_<notification>`` method with the given ``*args`` and
        ``**kwargs``.

        The notification does not by default include the object that actually
        changed (``self``), but it certainly may if required.
        Ns   _update_{0}(   R   R   t   formatt	   valuerefst   hasattrt   getattrt   callable(   R   t   notificationt   argst   kwargst   method_nameR   t   method(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   _notify{   s    
	c         C   sJ   y t  t |   j   } Wn  t k
 r; |  j j   } n Xd | d <| S(   sj   
        Exclude listeners when saving the listener's state, since they may be
        ephemeral.
        R   N(   t   superR   t   __getstate__t   AttributeErrort   __dict__t   copyR   (   R   t   state(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyR,      s    	
N(	   R   R   t   __doc__R   R   R   R   R*   R,   (    (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyR   3   s   +			c         C   s   t  t |    S(   s   
    Returns the first item returned by iterating over an iterable object.

    Example:

    >>> a = [1, 2, 3]
    >>> first(a)
    1
    (   R   t   iter(   t   iterable(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   first   s    c         c   s   t  |  t  s' t d j |     n  | d k r? t   } n  y |  j   } Wn  t k
 rq |  j |   } n Xxc t | d t j	 d  D]F } | | k r | j
 |  | Vx t | |  D] } | Vq Wq q Wd S(   s  
    itersubclasses(cls)

    Generator over all subclasses of a given class, in depth first order.

    >>> class A(object): pass
    >>> class B(A): pass
    >>> class C(A): pass
    >>> class D(B,C): pass
    >>> class E(D): pass
    >>>
    >>> for cls in itersubclasses(A):
    ...     print(cls.__name__)
    B
    D
    E
    C
    >>> # get ALL (new-style) classes currently defined
    >>> [cls.__name__ for cls in itersubclasses(object)]
    [...'tuple', ...'type', ...]

    From http://code.activestate.com/recipes/576949/
    s>   itersubclasses must be called with new-style classes, not {!r}t   keyR   N(   t
   isinstancet   typet	   TypeErrorR    R   t   sett   __subclasses__t   sortedt   operatort
   attrgettert   addt   itersubclasses(   t   clst   _seent   subst   sub(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyR?      s    	"c            s   t       f d    } | S(   s   
    This decorator registers a custom SIGINT handler to catch and ignore SIGINT
    until the wrapped function is completed.
    c             s   t  j   } t  j   d k o- | j   d k } d t f   f d     Y} |   } | rs t j t j |  } n  z   |  |   Wd  | r | d  k	 r t j t j |  n t j t j t j  | j	 r t
  q n  Xd  S(   Ni   t
   MainThreadt   SigintHandlerc              s    e  Z d    Z   f d   Z RS(   c         S   s   t  |  _ d  S(   N(   t   Falset   sigint_received(   R   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   __init__   s    c            s)   t  j d j   j  t  t |  _ d  S(   Ns/   KeyboardInterrupt ignored until {} is complete!(   t   warningst   warnR    R   R   t   TrueRG   (   R   t   signumt   frame(   t   func(    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   __call__   s    	(   R   R   RH   RO   (    (   RN   (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyRE      s   	(   t	   threadingt   currentThreadt   activeCountt   getNamet   objectt   signalt   SIGINTR   t   SIG_DFLRG   t   KeyboardInterrupt(   R&   R'   t   curr_threadt   single_threadRE   t   sigint_handlert   old_handler(   RN   (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   wrapped   s    
		(   R   (   RN   R]   (    (   RN   s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   ignore_sigint   s    %c         C   s4   t  j |   \ } } x | D] } Pq Wt | |  S(   sm   Return the items of an iterable paired with its next item.

    Ex: s -> (s0,s1), (s1,s2), (s2,s3), ....
    (   t	   itertoolst   teeR   (   R3   R   R   t   _(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   pairwise  s    c         C   s   |  S(   s   
    In Python 2 this is a no-op.  Strings are left alone.  In Python 3 this
    will be replaced with a function that actually encodes unicode strings to
    ASCII bytes.
    (    (   t   s(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   encode_ascii  s    c         C   s   |  S(   s   
    In Python 2 this is a no-op.  Strings are left alone.  In Python 3 this
    will be replaced with a function that actually decodes ascii bytes to
    unicode.
    (    (   Rc   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   decode_ascii&  s    c            s   t  j r# t   d  r#   j   St   d  rJ   j rJ t d   n  t   d  s] t St   d  r t   f d   d D  r t St S(   s   
    Returns True if the file-like object can be read from.  This is a common-
    sense approximation of io.IOBase.readable.
    t   readablet   closeds   I/O operation on closed filet   readt   modec         3   s   |  ] } |   j  k Vq d  S(   N(   Ri   (   t   .0t   c(   t   f(    s3   lib/python2.7/site-packages/astropy/io/fits/util.pys	   <genexpr>@  s    s   r+(	   R   t   PY2R"   Rf   Rg   t
   ValueErrorRF   t   anyRK   (   Rl   (    (   Rl   s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt
   isreadable0  s    
,c            s   t  j r# t   d  r#   j   St   d  rJ   j rJ t d   n  t   d  s] t St   d  r t   f d   d D  r t St S(   s   
    Returns True if the file-like object can be written to.  This is a common-
    sense approximation of io.IOBase.writable.
    t   writableRg   s   I/O operation on closed filet   writeRi   c         3   s   |  ] } |   j  k Vq d  S(   N(   Ri   (   Rj   Rk   (   Rl   (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pys	   <genexpr>X  s    s   wa+(	   R   Rm   R"   Rq   Rg   Rn   RF   Ro   RK   (   Rl   (    (   Rl   s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt
   iswritableH  s    
,c         C   s   t  |  t  S(   s  
        Returns True if the given object represents an OS-level file (that is,
        ``isinstance(f, file)``).

        On Python 3 this also returns True if the given object is higher level
        wrapper on top of a FileIO object, such as a TextIOWrapper.
        (   R6   t   file(   Rl   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   isfilea  s    	c         C   sR   t  |  t j  r t St |  d  r2 t |  j  St |  d  rN t |  j  St S(   s  
        Returns True if the given object represents an OS-level file (that is,
        ``isinstance(f, file)``).

        On Python 3 this also returns True if the given object is higher level
        wrapper on top of a FileIO object, such as a TextIOWrapper.
        t   buffert   raw(	   R6   t   iot   FileIORK   R"   Ru   Rv   Rw   RF   (   Rl   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyRu   l  s    	c         C   s   t  |  |  S(   s  
        A wrapper around the `open()` builtin.

        This exists because in Python 3, `open()` returns an
        `io.BufferedReader` by default.  This is bad, because
        `io.BufferedReader` doesn't support random access, which we need in
        some cases.  In the Python 3 case (implemented in the py3compat module)
        we must call open with buffering=0 to get a raw random-access file
        reader.
        (   t   open(   t   filenameRi   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   fileobj_open  s    c         C   s   t  |  | d d S(   s  
        A wrapper around the `open()` builtin.

        This exists because in Python 3, `open()` returns an
        `io.BufferedReader` by default.  This is bad, because
        `io.BufferedReader` doesn't support random access, which we need in
        some cases.  In the Python 3 case (implemented in the py3compat module)
        we must call open with buffering=0 to get a raw random-access file
        reader.
        t	   bufferingi    (   Rz   (   R{   Ri   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyR|     s    c         C   s   t  |  t  r |  St  |  t j  r2 t |  j  St |  d  rH |  j St |  d  r^ |  j St |  d  rz t	 |  j
  St	 t |    Sd S(   s   
    Returns the 'name' of file-like object f, if it has anything that could be
    called its name.  Otherwise f's class or type is returned.  If f is a
    string f itself is returned.
    t   nameR{   t	   __class__N(   R6   R   t   gzipt   GzipFilet   fileobj_namet   fileobjR"   R~   R{   t   strR   R7   (   Rl   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyR     s    c         C   s   t  |  t  r t St |  d  r) |  j St |  d  rT t |  j d  rT |  j j St |  d  r t |  j d  r |  j j St Sd S(   s  
    Returns True if the given file-like object is closed or if f is a string
    (and assumed to be a pathname).

    Returns False for all other types of objects, under the assumption that
    they are file-like objects with no sense of a 'closed' state.
    Rg   R   t   fpN(   R6   R   RK   R"   Rg   R   R   RF   (   Rl   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   fileobj_closed  s    	!
!
c         C   s   t  |  d  r- t  |  j d  r- |  j } n_ t  |  d  rC |  j St  |  d  rp t  |  j d  rp |  j } n t  |  d  r |  } n d St |  S(   sm   
    Returns the 'mode' string of a file-like object if such a thing exists.
    Otherwise returns None.
    R   Ri   t   fileobj_modeR   N(   R"   R   R   R   R   t   _fileobj_normalize_mode(   Rl   R   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyR     s    
!!	c         C   sw   |  j  } t |  t j  rH | t j k r. d S| t j k rA d Sd Sn  d | k rs | j d d  } | d 7} n  | S(   s   Takes care of some corner cases in Python where the mode string
    is either oddly formatted or does not truly represent the file mode.
    t   rbt   wbt   +t    N(   Ri   R6   R   R   t   READt   WRITER   t   replace(   Rl   Ri   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyR     s    	c         C   s\   t  |  d  r |  j St d k	 r8 t |  t j  r8 t St |   } | rT d | k St Sd S(   s   
    Returns True if the give file or file-like object has a file open in binary
    mode.  When in doubt, returns True by default.
    t   binaryR   N(	   R"   R   Rx   R   R6   t
   TextIOBaseRF   R   RK   (   Rl   Ri   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   fileobj_is_binary  s    
c            s   t  |  t  r |  j   |  St  |  t  r~ t   f d   t d  D    x | D] } d   t |  <qW W|  j    Sd S(   s   
        This is a version of string/unicode.translate() that can handle string
        or unicode strings the same way using a translation table made with
        `string.maketrans`.
        c         3   s;   |  ]1 } t    |  | k r | t    |  f Vq d  S(   N(   t   ord(   Rj   t   x(   t   table(    s3   lib/python2.7/site-packages/astropy/io/fits/util.pys	   <genexpr>*  s    i   N(   R6   R   t	   translateR
   t   dictR   R   R   (   Rc   R   t   deletecharsRk   (    (   R   s3   lib/python2.7/site-packages/astropy/io/fits/util.pyR      s    "c         C   sC   | r6 | j    } x! | D] } d  | t |  <q Wn  |  j |  S(   N(   R/   R   R   R   (   Rc   R   R   Rk   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyR   2  s
    c            s>   |  j  d  }    f d    d j  f d   | D  S(   s   
    Like :func:`textwrap.wrap` but preserves existing paragraphs which
    :func:`textwrap.wrap` does not otherwise handle well.  Also handles section
    headers.
    s   

c            s=   t   f d   |  j   D  r& |  St j |      Sd  S(   Nc         3   s!   |  ] } t  |    k  Vq d  S(   N(   t   len(   Rj   t   l(   t   width(    s3   lib/python2.7/site-packages/astropy/io/fits/util.pys	   <genexpr>D  s    (   t   allt
   splitlinest   textwrapt   fill(   t   t(   R'   R   (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt
   maybe_fillC  s    "c         3   s   |  ] }   |  Vq d  S(   N(    (   Rj   t   p(   R   (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pys	   <genexpr>I  s    (   t   splitt   join(   t   textR   R'   t
   paragraphs(    (   R'   R   R   s3   lib/python2.7/site-packages/astropy/io/fits/util.pyR   :  s    c   	      C   s{  t  |   r't d k r[ t j d k rR t t j   d  t d  k  rR t a q[ t a n  t rt	 d	 | j
  } | | k  r t j |  d | d | St j | d | } xS t d | |  D]? } t | | |  } t j |  d | d | | | | | +q W| Sqwt j |  d | d | SnP t j |  j
 | } |  j |  } t j | d | d | } | j   } | Sd S(
   s7   Create a numpy array from a file or a file-like object.t   darwini    s   10.9i   i   t   dtypet   countNi   @(   Ru   t   CHUNKED_FROMFILER   t   syst   platformR   t   mac_verRK   RF   t   intt   itemsizet   npt   fromfilet   emptyR   t   minR   Rh   t
   frombufferR/   (	   t   infileR   R   t
   chunk_sizet   arrayt   begt   endt	   read_sizeRc   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   _array_from_fileT  s*    "		*i   i    i   i   c         C   s   t  |  r+ t | t j  r+ d   } n t } t j d k rv |  j t d k rv |  j d d k rv t |  j	 } n/ t j j
 d  r t |  j	 } n | |  |  Sd } |  j t j  j   }  x5 | |  j k  r | |  | | | !|  | | 7} q Wd S(   s  
    Write a numpy array to a file or a file-like object.

    Parameters
    ----------
    arr : `~numpy.ndarray`
        The Numpy array to write.
    outfile : file-like
        A file-like object such as a Python file object, an `io.BytesIO`, or
        anything else with a ``write`` method.  The file object must support
        the buffer interface in its ``write``.

    If writing directly to an on-disk file this delegates directly to
    `ndarray.tofile`.  Otherwise a slower Python implementation is used.
    c         S   s   |  j  |  S(   N(   t   tofile(   R   Rl   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyR     s    R   i   i   i    t   winN(   Ru   R6   Rx   t   BufferedIOBaset   _array_to_file_likeR   R   t   nbytest   _OSX_WRITE_LIMITR   t
   startswitht   _WIN_WRITE_LIMITt   viewR   t   ndarrayt   flatten(   t   arrt   outfileRr   t	   chunksizet   idx(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   _array_to_file~  s    "c         C   s-  t  |   d k r d S|  j j rQ y | j |  j  Wn t k
 rI qQ Xd Sn  t t d  r x t j |   D] } | j | j	    qp Wn |  j
 j } t j d k r | d k s t j d k r| d k rxT |  j D] } | j | j   j	    q Wn' x$ |  j D] } | j | j	    qWd S(   sp   
    Write a `~numpy.ndarray` to a file-like object (which is not supported by
    `numpy.ndarray.tofile`).
    i    Nt   nditert   littlet   >t   bigt   <(   R   t   flagst
   contiguousRr   R   R8   R"   R   R   t   tostringR   t	   byteorderR   t   flatt   byteswap(   R   R   t   itemR   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyR     s$     c         C   s   t  |   } | r0 t | t  r0 t |  } nS | rV t |  t  rV t |  } n- t |  t  r t | t j  r | j } n  |  j	 |  d S(   s   
    Write a string to a file, encoding to ASCII if the file is open in binary
    mode, or decoding if the file is open in text mode.
    N(
   R   R6   R
   Rd   Re   R   R   R   R   Rr   (   Rl   Rc   t   binmode(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   _write_string  s    !c         C   st   |  j  | k r |  S|  j  j | j k rc t j |  j  t j  oR t j | t j  rc |  j |  S|  j |  Sd S(   s   
    Converts an array to a new dtype--if the itemsize of the new dtype is
    the same as the old dtype and both types are not numeric, a view is
    returned.  Otherwise a new array must be created.
    N(   R   R   R   t
   issubdtypet   numberR   t   astype(   R   R   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   _convert_array  s    c         C   s(   |  j  d k s t  d |  j d d >S(   sg   
    Given a numpy dtype, finds its "zero" point, which is exactly in the
    middle of its range.
    t   ui   i   (   t   kindt   AssertionErrorR   (   R   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   _unsigned_zero  s    c         C   s   |  j  d k o |  j d k S(   NR   i   (   R   R   (   R   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   _is_pseudo_unsigned  s    c         C   s   t  |  t  S(   N(   R6   t   all_integer_types(   t   val(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   _is_int  s    c         C   s4   y t  |   } Wn t k
 r/ t |   } n X| S(   sA   Converts a given string to either an int or a float if necessary.(   R   Rn   t   float(   R   t   num(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   _str_to_num  s
    c         C   sO  g  } |  j  d  } t | t |   | d  } t j |  d j d  d t d f } t j | d k  d } d } d } x t |  D] }	 yL t j | | | k  d d }
 | |
 d d } |
 d k r d } n  Wn t	 k
 r t |   } n X| | k r| | } n  | j
 |  | | ! t |   | k rAPn  | } q W| S(   s   
    Split a long string into parts where each part is no longer
    than ``strlen`` and no word is cut into two pieces.  But if
    there is one single word which is longer than ``strlen``, then
    it will be split in the middle of the word.
    t    i   t   utf8R   i    i(   R   t   maxR   R   R   t   encodeR   t   nonzeroR   t	   Exceptiont   append(   t   inputt   strlent   wordst   nblankst   nmaxR   t	   blank_loct   offsett   xoffsetR   t   loc(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   _words_group!  s,    (!
c         C   sJ   |  d k	 r! t j j |   }  n  t j d |   \ } } t j |  | S(   s   
    Create a temporary file name which should not already exist.  Use the
    directory of the input file as the base name of the mkstemp() output.
    t   dirN(   R   t   ost   patht   dirnamet   tempfilet   mkstempt   close(   R   Rl   t   fn(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt	   _tmp_nameH  s
    c         C   sj   t  |  t j  r |  S|  } xG t | d  re | j d k	 re t  | j t j  rY | j S| j } q Wd S(   su   
    If the array has an mmap.mmap at base of its base chain, return the mmap
    object; otherwise return None.
    t   baseN(   R6   t   mmapR"   R  R   (   R   R  (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   _get_array_mmapU  s    !c         c   s   y	 d  VWn t  k
 r } d } t |  t  s< |  g }  n  | d  k rc t j j |  j j  } n  t j j	 |  r t
 j |  } t d   |  D  } | | k  r d j | |  } q n  x |  D] } | j   q Wt  | t |    n Xd  S(   NR   c         s   s   |  ] } | j  Vq d  S(   N(   t   size(   Rj   t   hdu(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pys	   <genexpr>q  s    s6   Not enough space on disk: requested {}, available {}. (   t   OSErrorR6   t   listR   R   R   R   t   _fileR~   t   isdirR   t   get_free_space_in_dirt   sumR    t   _closeR   (   t   hdulistR   t   exct   error_messaget
   free_spacet   hdulist_sizeR  (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   _free_space_checke  s"    	c         C   s3   y t  t |    SWn t t f k
 r. | SXd S(   s   
    Attempts to extract an integer number from the given value. If the
    extraction fails, the value of the 'default' argument is returned.
    N(   R   R   R8   Rn   (   t   valuet   default(    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   _extract_number|  s    c         C   s   t  j d j |   d  S(   s8  
    Return a string representing the path to the file requested from the
    io.fits test data set.

    .. versionadded:: 2.0.3

    Parameters
    ----------
    filename : str
        The filename of the test data file.

    Returns
    -------
    filepath : str
        The path to the requested file.
    s   io/fits/tests/data/{}t   astropy(   R   t   get_pkg_data_filenameR    (   R{   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   get_testdata_filepath  s    I       I    I       i(W   t
   __future__R    R   R_   Rx   R  R<   R   R   RU   t   stringR   R   R   RP   RI   R   t
   contextlibR   t   utilsR   t   distutils.versionR   t   numpyR   R   t   ImportErrorRT   t   externR   t
   extern.sixR   R	   R
   R   R   t   extern.six.movesR   R   R   t   utils.compatR   t   utils.exceptionsR   Rm   t   cmpt   integerR   R   R4   R   R?   R^   Rb   Rd   Re   Rp   Rs   Ru   R|   R   R   R   R   R   t	   maketransR   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R  R  R  R  R  (    (    (    s3   lib/python2.7/site-packages/astropy/io/fits/util.pyt   <module>   s   (			u	*	.		
	
									!								&	3	+			
				'			