σ
ίΘ[c           @` s   d  Z  d d l m Z m Z m Z m Z d d l Z d d l m Z d d g Z	 d d d	     YZ
 d a e e d
  Z e e d  Z d S(   uG   
This file contains routines to verify the correctness of UCD strings.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t   datau	   parse_ucdu	   check_ucdt   UCDWordsc           B` s;   e  Z d  Z d   Z d   Z d   Z d   Z d   Z RS(   u£   
    Manages a list of acceptable UCD words.

    Works by reading in a data file exactly as provided by IVOA.  This
    file resides in data/ucd1p-words.txt.
    c         C` sψ   t    |  _ t    |  _ i  |  _ i  |  _ t j d d d ³ } x© | j   D] } g  | j d  D] } | j	   ^ qe \ } } } | j
   } | d k r± |  j j |  n  | d k rΠ |  j j |  n  | |  j | <| |  j | <qO WWd  QXd  S(   Nu   data/ucd1p-words.txtt   encodingu   asciiu   |u   QPEVu   QSEV(   t   sett   _primaryt
   _secondaryt   _descriptionst   _capitalizationR   t   get_pkg_data_fileobjt	   readlinest   splitt   stript   lowert   add(   t   selft   fdt   linet   xt   typet   namet   descrt
   name_lower(    (    s5   lib/python2.7/site-packages/astropy/io/votable/ucd.pyt   __init__   s    		1c         C` s   | j    |  j k S(   uA   
        Returns True if *name* is a valid primary name.
        (   R   R   (   R   R   (    (    s5   lib/python2.7/site-packages/astropy/io/votable/ucd.pyt
   is_primary,   s    c         C` s   | j    |  j k S(   uC   
        Returns True if *name* is a valid secondary name.
        (   R   R	   (   R   R   (    (    s5   lib/python2.7/site-packages/astropy/io/votable/ucd.pyt   is_secondary2   s    c         C` s   |  j  | j   S(   u[   
        Returns the official English description of the given UCD
        *name*.
        (   R
   R   (   R   R   (    (    s5   lib/python2.7/site-packages/astropy/io/votable/ucd.pyt   get_description8   s    c         C` s   |  j  | j   S(   uM   
        Returns the standard capitalization form of the given name.
        (   R   R   (   R   R   (    (    s5   lib/python2.7/site-packages/astropy/io/votable/ucd.pyt   normalize_capitalization?   s    (   t   __name__t
   __module__t   __doc__R   R   R   R   R   (    (    (    s5   lib/python2.7/site-packages/astropy/io/votable/ucd.pyR      s   				c         C` s  t  d k r t   a  n  | r3 t j d |   } n t j d |   } | d k	 ru t d j | j d  |     n  d } d j | |  } |  j d  } g  } xΪt	 |  D]Μ\ } }	 |	 j
 d  }
 |
 d	 k r'|	 j d d	  \ } }	 t j | |  st d
 j |    n  | j   } n* |
 d	 k rKt d j |	    n d } t j | |	  s{t d j |	    n  | d k r;| r;| d k rκt  j |	  s8t  j |	  rΟt d j |	    qηt d j |	    q8q;t  j |	  s;t  j |	  r t d j |	    q8t d j |	    q;n  y t  j |	  } Wn t k
 rg|	 } n X| j | | f  q― W| S(   u-  
    Parse the UCD into its component parts.

    Parameters
    ----------
    ucd : str
        The UCD string

    check_controlled_vocabulary : bool, optional
        If `True`, then each word in the UCD will be verified against
        the UCD1+ controlled vocabulary, (as required by the VOTable
        specification version 1.2), otherwise not.

    has_colon : bool, optional
        If `True`, the UCD may contain a colon (as defined in earlier
        versions of the standard).

    Returns
    -------
    parts : list
        The result is a list of tuples of the form:

            (*namespace*, *word*)

        If no namespace was explicitly specified, *namespace* will be
        returned as ``'ivoa'`` (i.e., the default namespace).

    Raises
    ------
    ValueError : *ucd* is invalid
    u   [^A-Za-z0-9_.:;\-]u   [^A-Za-z0-9_.;\-]u&   UCD has invalid character '{}' in '{}'i    u   [A-Za-z0-9][A-Za-z0-9\-_]*u	   {}(\.{})*u   ;u   :i   u   Invalid namespace '{}'u   Too many colons in '{}'u   ivoau   Invalid word '{}'u2   Secondary word '{}' is not valid as a primary wordu   Unknown word '{}'u2   Primary word '{}' is not valid as a secondary wordN(   t   _ucd_singletont   NoneR   t   ret   searcht
   ValueErrort   formatt   groupR   t	   enumeratet   countt   matchR   R   R   R   t   KeyErrort   append(   t   ucdt   check_controlled_vocabularyt	   has_colont   mt   word_component_ret   word_ret   partst   wordst   it   wordt   colon_countt   nst   normalized_word(    (    s5   lib/python2.7/site-packages/astropy/io/votable/ucd.pyt	   parse_ucdI   sX    !	
c         C` sC   |  d k r t Sy t |  d | d | Wn t k
 r> t SXt S(   u5  
    Returns False if *ucd* is not a valid `unified content descriptor`_.

    Parameters
    ----------
    ucd : str
        The UCD string

    check_controlled_vocabulary : bool, optional
        If `True`, then each word in the UCD will be verified against
        the UCD1+ controlled vocabulary, (as required by the VOTable
        specification version 1.2), otherwise not.

    has_colon : bool, optional
        If `True`, the UCD may contain a colon (as defined in earlier
        versions of the standard).

    Returns
    -------
    valid : bool
    R/   R0   N(   R#   t   TrueR;   R&   t   False(   R.   R/   R0   (    (    s5   lib/python2.7/site-packages/astropy/io/votable/ucd.pyt	   check_ucd€   s    	(    (   R!   t
   __future__R    R   R   R   R$   t   utilsR   t   __all__R   R#   R"   R=   R;   R>   (    (    (    s5   lib/python2.7/site-packages/astropy/io/votable/ucd.pyt   <module>   s   "5[