B
    îq\î+  ã               @   s`  d 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 yddlZW n ek
r|   edƒ‚Y nX eed	ƒ Zd
ddddgZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Z d)d*„ Z!d+d,„ Z"d-d.„ Z#G d/d
„ d
ej$ƒZ%G d0d„ dej&ƒZ'e' (e	j)e¡ e' (e	j*e¡ e' +e	j,e¡ e' (e-e'j.¡ e' (ej/e¡ e' (ee¡ e' (ee¡ e' (ej0e ¡ e' (ee¡ e' (ej1ej2j3j4¡ xLej5ej6ej7ej8ej9ej:ej;ej<ej=ej>ej?gD ]Z@e' (e@ej2j3jA¡ qúW x4ejBejCejDejEejFgD ]Z@e' (e@ej2j3jG¡ q0W x(ejHeIejJejKgD ]Z@e' (e@e"¡ q`W e% Ld1e#¡ e% Ld2e%jM¡ e% Ld3e%jN¡ e% Ld4e¡ e% Ld5e¡ e% Ld6e¡ e% Ld7e¡ e% Ld8e!¡ e% Ld9e¡ xZe	jOd:fejPd;fejQd<fejRd=fejSd>ffD ]*\ZTZUe' +eTeeUƒ¡ e% LeUeeTƒ¡ qW d?d„ ZVd@d„ ZWdBdAd„ZXdS )Caæ  
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.

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>
é    N)ÚTimeÚ	TimeDelta)Úunits)Úcoordinates)Ú
minversion)ÚSerializedColumnz9`import yaml` failed, PyYAML package is required for YAMLz3.12ÚAstropyLoaderÚAstropyDumperÚloadÚload_allÚdumpc             C   s   dt | ¡ ƒi}|  d|¡S )NÚunitz!astropy.units.Unit)ÚstrZ	to_stringÚrepresent_mapping)ÚdumperÚobjÚout© r   ú3lib/python3.7/site-packages/astropy/io/misc/yaml.pyÚ_unit_representer\   s    r   c             C   s   |   |¡}t |d ¡S )Nr   )Úconstruct_mappingÚuÚUnit)ÚloaderÚnodeÚmapr   r   r   Ú_unit_constructora   s    
r   c             C   s   |   d|¡}|S )Nz!astropy.table.SerializedColumn)r   )r   r   r   r   r   r   Ú_serialized_column_representerf   s    r   c             C   s   |   |¡}t|ƒS )N)r   r   )r   r   r   r   r   r   Ú_serialized_column_constructork   s    
r   c             C   s   |j  ¡ }|  d|¡S )Nz!astropy.time.Time)ÚinfoÚ_represent_as_dictr   )r   r   r   r   r   r   Ú_time_representerp   s    
r!   c             C   s   |   |¡}tj |¡}|S )N)r   r   r   Ú_construct_from_dict)r   r   r   r   r   r   r   Ú_time_constructoru   s    
r#   c             C   s   |j  ¡ }|  d|¡S )Nz!astropy.time.TimeDelta)r   r    r   )r   r   r   r   r   r   Ú_timedelta_representer{   s    
r$   c             C   s   |   |¡}tj |¡}|S )N)r   r   r   r"   )r   r   r   r   r   r   r   Ú_timedelta_constructor€   s    
r%   c             C   sj   |j d s|j d st |¡}t |¡r4|j}d}nd}t | ¡ ¡}t|t	|j
ƒ|j|d}|  d|¡S )NZC_CONTIGUOUSZF_CONTIGUOUSÚFÚC)ÚbufferÚdtypeÚshapeÚorderz!numpy.ndarray)ÚflagsÚnpZascontiguousarrayZ	isfortranÚTÚbase64Z	b64encodeZtostringÚdictr   r)   r*   r   )r   r   r+   Zdata_b64r   r   r   r   Ú_ndarray_representer†   s    

r1   c             C   s(   |   |¡}t |d ¡|d< tjf |ŽS )Nr(   )r   r/   Z	b64decoder-   Úndarray)r   r   r   r   r   r   Ú_ndarray_constructorš   s    
r3   c                s   ‡ fdd„}|S )Nc                s   |j  ¡ }|  ˆ |¡S )N)r   r    r   )r   r   r   )Útagr   r   Úrepresenter¡   s    
z*_quantity_representer.<locals>.representerr   )r4   r5   r   )r4   r   Ú_quantity_representer    s    r6   c                s   ‡ fdd„}|S )Nc                s   |   |¡}ˆ j |¡S )N)r   r   r"   )r   r   r   )Úclsr   r   Úconstructor¨   s    
z*_quantity_constructor.<locals>.constructorr   )r7   r8   r   )r7   r   Ú_quantity_constructor§   s    r9   c             C   s   |j  ¡ }|  d|¡}|S )Nz,!astropy.coordinates.sky_coordinate.SkyCoord)r   r    r   )r   r   r   r   r   r   r   Ú_skycoord_representer®   s    
r:   c             C   s   |   |¡}tjj |¡}|S )N)r   ÚcoordsÚSkyCoordr   r"   )r   r   r   r   r   r   r   Ú_skycoord_constructorµ   s    
r=   c             C   sd   |j dkrd|j }nB|jdkr,d|j  }n,|j dkrHd|j|j f }nd|j|j f }|  d|¡S )Ng        z%rz%rjr   z%r+%rjz%r%rjz tag:yaml.org,2002:python/complex)ÚimagÚrealZrepresent_scalar)ÚselfÚdatar   r   r   Ú_complex_representer¼   s    


rB   c             C   s   |   |¡}t|ƒS )N)Úconstruct_scalarÚcomplex)r   r   r   r   r   r   Ú_complex_constructorÈ   s    
rE   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r   až  
    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 |  |¡ƒS )N)ÚtupleZconstruct_sequence)r@   r   r   r   r   Ú_construct_python_tupleÙ   s    z%AstropyLoader._construct_python_tuplec             C   s
   |   |¡S )N)rC   )r@   r   r   r   r   Ú_construct_python_unicodeÜ   s    z'AstropyLoader._construct_python_unicodeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rG   rH   r   r   r   r   r   Í   s   
c               @   s$   e Zd ZdZdd„ Zer dd„ ZdS )r	   a°  
    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   |   d|¡S )Nztag:yaml.org,2002:python/tuple)Zrepresent_sequence)r@   rA   r   r   r   Ú_represent_tupleì   s    zAstropyDumper._represent_tuplec             C   s<   |d krdS t |tƒr"|dkr"dS t |ttttfƒr8dS d S )NTr   )Ú
isinstancerF   r   ÚboolÚintÚfloat)r@   rA   r   r   r   Úignore_aliasesò   s    zAstropyDumper.ignore_aliasesN)rI   rJ   rK   rL   rM   ÚYAML_LT_3_12rR   r   r   r   r   r	   à   s   
z tag:yaml.org,2002:python/complexztag:yaml.org,2002:python/tuplez tag:yaml.org,2002:python/unicodez!astropy.units.Unitz!numpy.ndarrayz!astropy.time.Timez!astropy.time.TimeDeltaz,!astropy.coordinates.sky_coordinate.SkyCoordz!astropy.table.SerializedColumnz!astropy.units.Quantityz!astropy.coordinates.Anglez!astropy.coordinates.Latitudez!astropy.coordinates.Longitudez(!astropy.coordinates.earth.EarthLocationc             C   s   t j| tdS )a&  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
    )ÚLoader)Úyamlr
   r   )Ústreamr   r   r   r
   ,  s    c             C   s   t j| tdS )a,  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

    )rT   )rU   r   r   )rV   r   r   r   r   =  s    c             K   s   t |d< tj| fd|i|—ŽS )a  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

    ZDumperrV   )r	   rU   r   )rA   rV   Úkwargsr   r   r   r   O  s    )N)YrL   r/   Znumpyr-   Zastropy.timer   r   Zastropyr   r   r   r;   Zastropy.utilsr   Zastropy.tabler   rU   ÚImportErrorrS   Ú__all__r   r   r   r   r!   r#   r$   r%   r1   r3   r6   r9   r:   r=   rB   rE   Z
SafeLoaderr   Z
SafeDumperr	   Zadd_representerZIrreducibleUnitZCompositeUnitZadd_multi_representerr   rF   rM   r2   r<   Zbool_r5   ZSafeRepresenterZrepresent_boolZint_ZintcZintpZint8Zint16Zint32Zint64Zuint8Zuint16Zuint32Zuint64Znp_typeZrepresent_intZfloat_Zfloat16Zfloat32Zfloat64Z
longdoubleZrepresent_floatZcomplex_rD   Z	complex64Z
complex128Zadd_constructorrG   rH   ZQuantityZAngleZLatitudeZ	LongitudeZEarthLocationr7   r4   r
   r   r   r   r   r   r   Ú<module>C   sš   
