ó
šßÈ[c           @`  sŸ  d  Z  d d l m Z m Z d d l Z d d l Z d d l m Z m	 Z	 d d l
 m Z d d l
 m Z d d l m Z d d	 l m Z d d
 l m Z y d d l Z Wn e k
 rÉ e d ƒ ‚ n Xe e d ƒ Z d d d d d g Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d „  Z& d  „  Z' d! „  Z( d e j) f d" „  ƒ  YZ* d e j+ f d# „  ƒ  YZ, e, j- e j. e ƒ e, j- e j/ e ƒ e, j0 e j1 e ƒ e, j- e2 e, j3 ƒ e, j- e j4 e! ƒ e, j- e e ƒ e, j- e	 e ƒ e, j- e j5 e% ƒ e, j- e e ƒ e, j- e j6 e j7 j8 j9 ƒ xo e j: e j; e j< e j= e j> e j? e j@ e jA e jB e jC e jD e jE g D] ZF e, j- eF e j7 j8 jG ƒ q¾WxK e jH e jI e jJ e jK e jL e jM g D] ZF e, j- eF e j7 j8 jN ƒ qWx6 e jO e jP e jQ e jR g D] ZF e, j- eF e' ƒ qNWe* jS d$ e( ƒ e* jS d% e* jT ƒ e* jS d& e* jU ƒ e* jS d' e ƒ e* jS d( e" ƒ e* jS d) e ƒ e* jS d* e  ƒ e* jS d+ e& ƒ e* jS d, e ƒ x| e jV d- f e jW d. f e jX d/ f e jY d0 f e jZ d1 f f D]8 \ Z[ Z\ e, j0 e[ e# e\ ƒ ƒ e* jS e\ e$ e[ ƒ ƒ qAWd2 „  Z] d3 „  Z^ d d4 „ Z` d S(5   s#  
This module contains functions for serializing core astropy objects via the
YAML protocol.

It provides functions `~astropy.io.misc.yaml.dump`,
`~astropy.io.misc.yaml.load`, and `~astropy.io.misc.yaml.load_all` which
call the corresponding functions in `PyYaml <http://pyyaml.org>`_ but use the
`~astropy.io.misc.yaml.AstropyDumper` and `~astropy.io.misc.yaml.AstropyLoader`
classes to define custom YAML tags for the following astropy classes:

- `astropy.units.Unit`
- `astropy.units.Quantity`
- `astropy.time.Time`
- `astropy.time.TimeDelta`
- `astropy.coordinates.SkyCoord`
- `astropy.coordinates.Angle`
- `astropy.coordinates.Latitude`
- `astropy.coordinates.Longitude`
- `astropy.coordinates.EarthLocation`
- `astropy.table.SerializedColumn`

.. Note ::

   This module requires PyYaml version 3.12 or later, which in turn requires
   Python 2.7 or Python 3.4 or later.

Example
=======
::

  >>> from astropy.io.misc import yaml
  >>> import astropy.units as u
  >>> from astropy.time import Time
  >>> from astropy.coordinates import EarthLocation

  >>> t = Time(2457389.0, format='mjd',
  ...          location=EarthLocation(1000, 2000, 3000, unit=u.km))
  >>> td = yaml.dump(t)

  >>> print(td)
  !astropy.time.Time
  format: mjd
  in_subfmt: '*'
  jd1: 4857390.0
  jd2: -0.5
  location: !astropy.coordinates.earth.EarthLocation
    ellipsoid: WGS84
    x: !astropy.units.Quantity
      unit: &id001 !astropy.units.Unit {unit: km}
      value: 1000.0
    y: !astropy.units.Quantity
      unit: *id001
      value: 2000.0
    z: !astropy.units.Quantity
      unit: *id001
      value: 3000.0
  out_subfmt: '*'
  precision: 3
  scale: utc

  >>> ty = yaml.load(td)
  >>> ty
  <Time object: scale='utc' format='mjd' value=2457389.0>

  >>> ty.location  # doctest: +FLOAT_CMP
  <EarthLocation (1000., 2000., 3000.) km>
i    (   t   absolute_importt   divisionNi   (   t   Timet	   TimeDelta(   t   units(   t   coordinates(   t
   minversion(   t   six(   t   SerializedColumns9   `import yaml` failed, PyYAML package is required for YAMLs   3.12t   AstropyLoadert   AstropyDumpert   loadt   load_allt   dumpc         C`  s)   i t  | j ƒ  ƒ d 6} |  j d | ƒ S(   Nt   units   !astropy.units.Unit(   t   strt	   to_stringt   represent_mapping(   t   dumpert   objt   out(    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _unit_representer_   s    c         C`  s    |  j  | ƒ } t j | d ƒ S(   NR   (   t   construct_mappingt   ut   Unit(   t   loadert   nodet   map(    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _unit_constructord   s    c         C`  s   |  j  d | ƒ } | S(   Ns   !astropy.table.SerializedColumn(   R   (   R   R   R   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _serialized_column_representeri   s    c         C`  s   |  j  | ƒ } t | ƒ S(   N(   R   R   (   R   R   R   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _serialized_column_constructorn   s    c         C`  s   | j  j ƒ  } |  j d | ƒ S(   Ns   !astropy.time.Time(   t   infot   _represent_as_dictR   (   R   R   R   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _time_representers   s    c         C`  s%   |  j  | ƒ } t j j | ƒ } | S(   N(   R   R   R   t   _construct_from_dict(   R   R   R   R   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _time_constructorx   s    c         C`  s   | j  j ƒ  } |  j d | ƒ S(   Ns   !astropy.time.TimeDelta(   R   R    R   (   R   R   R   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _timedelta_representer~   s    c         C`  s%   |  j  | ƒ } t j j | ƒ } | S(   N(   R   R   R   R"   (   R   R   R   R   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _timedelta_constructorƒ   s    c      	   C`  s¥   | j  d p | j  d s, t j | ƒ } n  t j | ƒ rM | j } d } n d } t j | j ƒ  ƒ } t d | d t	 | j
 ƒ d | j d | ƒ } |  j d	 | ƒ S(
   Nt   C_CONTIGUOUSt   F_CONTIGUOUSt   Ft   Ct   buffert   dtypet   shapet   orders   !numpy.ndarray(   t   flagst   npt   ascontiguousarrayt	   isfortrant   Tt   base64t	   b64encodet   tostringt   dictR   R+   R,   R   (   R   R   R-   t   data_b64R   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _ndarray_representer‰   s    				c         C`  s3   |  j  | ƒ } t j | d ƒ | d <t j |   S(   NR*   (   R   R3   t	   b64decodeR/   t   ndarray(   R   R   R   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _ndarray_constructor   s    c         `  s   ‡  f d †  } | S(   Nc         `  s   | j  j ƒ  } |  j ˆ  | ƒ S(   N(   R   R    R   (   R   R   R   (   t   tag(    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   representer¤   s    (    (   R<   R=   (    (   R<   s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _quantity_representer£   s    c         `  s   ‡  f d †  } | S(   Nc         `  s   |  j  | ƒ } ˆ  j j | ƒ S(   N(   R   R   R"   (   R   R   R   (   t   cls(    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   constructor«   s    (    (   R?   R@   (    (   R?   s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _quantity_constructorª   s    c         C`  s%   | j  j ƒ  } |  j d | ƒ } | S(   Ns,   !astropy.coordinates.sky_coordinate.SkyCoord(   R   R    R   (   R   R   R   R   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _skycoord_representer±   s    		c         C`  s(   |  j  | ƒ } t j j j | ƒ } | S(   N(   R   t   coordst   SkyCoordR   R"   (   R   R   R   R   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _skycoord_constructor¸   s    c         C`  sŒ   | j  d k r d | j } n] | j d k r> d | j  } n> | j  d k rf d | j | j  f } n d | j | j  f } |  j d | ƒ S(   Ng        u   %ru   %rji    u   %r+%rju   %r%rju    tag:yaml.org,2002:python/complex(   t   imagt   realt   represent_scalar(   t   selft   data(    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _complex_representer¿   s    c         C`  s   |  j  | ƒ } t | ƒ S(   N(   t   construct_scalart   complex(   R   R   R   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _complex_constructorË   s    c           B`  s    e  Z d  Z d „  Z d „  Z RS(   sž  
    Custom SafeLoader that constructs astropy core objects as well
    as Python tuple and unicode objects.

    This class is not directly instantiated by user code, but instead is
    used to maintain the available constructor functions that are
    called when parsing a YAML stream.  See the `PyYaml documentation
    <http://pyyaml.org/wiki/PyYAMLDocumentation>`_ for details of the
    class signature.
    c         C`  s   t  |  j | ƒ ƒ S(   N(   t   tuplet   construct_sequence(   RI   R   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _construct_python_tupleÜ   s    c         C`  s   |  j  | ƒ S(   N(   RL   (   RI   R   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _construct_python_unicodeß   s    (   t   __name__t
   __module__t   __doc__RQ   RR   (    (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyR	   Ð   s   
	c           B`  s)   e  Z d  Z d „  Z e r' d „  Z n  RS(   s°  
    Custom SafeDumper that represents astropy core objects as well
    as Python tuple and unicode objects.

    This class is not directly instantiated by user code, but instead is
    used to maintain the available representer functions that are
    called when generating a YAML stream from an object.  See the
    `PyYaml documentation <http://pyyaml.org/wiki/PyYAMLDocumentation>`_
    for details of the class signature.
    c         C`  s   |  j  d | ƒ S(   Ns   tag:yaml.org,2002:python/tuple(   t   represent_sequence(   RI   RJ   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   _represent_tupleï   s    c         C`  sV   | d  k r t St | t ƒ r/ | d k r/ t St | t j t t t f ƒ rR t Sd  S(   N(    (	   t   Nonet   Truet
   isinstanceRO   R   t   string_typest   boolt   intt   float(   RI   RJ   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   ignore_aliasesõ   s    (   RS   RT   RU   RW   t   YAML_LT_3_12R_   (    (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyR
   ã   s   
	u    tag:yaml.org,2002:python/complexs   tag:yaml.org,2002:python/tuples    tag:yaml.org,2002:python/unicodes   !astropy.units.Units   !numpy.ndarrays   !astropy.time.Times   !astropy.time.TimeDeltas,   !astropy.coordinates.sky_coordinate.SkyCoords   !astropy.table.SerializedColumns   !astropy.units.Quantitys   !astropy.coordinates.Angles   !astropy.coordinates.Latitudes   !astropy.coordinates.Longitudes(   !astropy.coordinates.earth.EarthLocationc         C`  s   t  j |  d t ƒS(   s&  Parse the first YAML document in a stream using the AstropyLoader and
    produce the corresponding Python object.

    Parameters
    ----------
    stream : str or file-like object
        YAML input

    Returns
    -------
    obj : object
        Object corresponding to YAML document
    t   Loader(   t   yamlR   R	   (   t   stream(    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyR   /  s    c         C`  s   t  j |  d t ƒS(   s,  Parse the all YAML documents in a stream using the AstropyLoader class and
    produce the corresponding Python object.

    Parameters
    ----------
    stream : str or file-like object
        YAML input

    Returns
    -------
    obj : object
        Object corresponding to YAML document

    Ra   (   Rb   R   R	   (   Rc   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyR   @  s    c         K`  s    t  | d <t j |  d | | S(   s  Serialize a Python object into a YAML stream using the AstropyDumper class.
    If stream is None, return the produced string instead.

    Parameters
    ----------
    data: object
        Object to serialize to YAML
    stream : file-like object, optional
        YAML output (if not supplied a string is returned)
    **kwargs
        Other keyword arguments that get passed to yaml.dump()

    Returns
    -------
    out : str or None
        If no ``stream`` is supplied then YAML output is returned as str

    t   DumperRc   (   R
   Rb   R   (   RJ   Rc   t   kwargs(    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyR   R  s    
(a   RU   t
   __future__R    R   R3   t   numpyR/   t   timeR   R   t    R   R   R   RC   t   utilsR   t   externR   t   tableR   Rb   t   ImportErrorR`   t   __all__R   R   R   R   R!   R#   R$   R%   R8   R;   R>   RA   RB   RE   RK   RN   t
   SafeLoaderR	   t
   SafeDumperR
   t   add_representert   IrreducibleUnitt   CompositeUnitt   add_multi_representerR   RO   RW   R:   RD   t   bool_R=   t   SafeRepresentert   represent_boolt   int_R]   t   intct   intpt   int8t   int16t   int32t   int64t   uint8t   uint16t   uint32t   uint64t   np_typet   represent_intt   float_R^   t   float16t   float32t   float64t
   longdoublet   represent_floatt   complex_RM   t	   complex64t
   complex128t   add_constructorRQ   RR   t   Quantityt   Anglet   Latitudet	   Longitudet   EarthLocationR?   R<   R   R   RX   R   (    (    (    s3   lib/python2.7/site-packages/astropy/io/misc/yaml.pyt   <module>D   sž   																-(	!	%			
	
				