a
    ߙfb                      @   sJ   d Z ddlZddlmZ ddgZG dd dZdadd	dZdd
dZdS )zG
This file contains routines to verify the correctness of UCD strings.
    N)data	parse_ucd	check_ucdc                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )UCDWordsz
    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|}| D ]b}dd |dD \}}}|	 }|dv rn| j
| |dv r| j
| || j|< || j|< q4W d    n1 s0    Y  d S )	Nzdata/ucd1p-words.txtascii)encodingc                 S   s   g | ]}|  qS  )strip).0xr   r   5lib/python3.9/site-packages/astropy/io/votable/ucd.py
<listcomp>!   s   z%UCDWords.__init__.<locals>.<listcomp>|ZQPEVCZQSEVC)set_primary
_secondary_descriptions_capitalizationr   Zget_pkg_data_fileobj	readlinessplitloweradd)selffdlinetypenameZdescrZ
name_lowerr   r   r   __init__   s&    
zUCDWords.__init__c                 C   s   |  | jv S )zA
        Returns True if *name* is a valid primary name.
        )r   r   r   r   r   r   r   
is_primary+   s    zUCDWords.is_primaryc                 C   s   |  | jv S )zC
        Returns True if *name* is a valid secondary name.
        )r   r   r   r   r   r   is_secondary1   s    zUCDWords.is_secondaryc                 C   s   | j |  S )z[
        Returns the official English description of the given UCD
        *name*.
        )r   r   r   r   r   r   get_description7   s    zUCDWords.get_descriptionc                 C   s   | j |  S )zM
        Returns the standard capitalization form of the given name.
        )r   r   r   r   r   r   normalize_capitalization>   s    z!UCDWords.normalize_capitalizationN)	__name__
__module____qualname____doc__r   r   r    r!   r"   r   r   r   r   r      s   r   Fc              	   C   s  t du rt a |r td| }ntd| }|durPtd|d d|  dd}| d	| d
}| d}g }t|D ]R\}}	|	d}
|
dkr|	dd\}}	t	||std| d|
 }n|
dkrtd|	 dnd}t	||	std|	 d|dkr|r|dkr^t |	st |	rLtd|	 dntd|	 dn:t |	st |	rtd|	 dntd|	 dzt |	}W n ty   |	}Y n0 |||f qz|S )a6  
    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
        if *ucd* is invalid
    Nz[^A-Za-z0-9_.:;\-]z[^A-Za-z0-9_.;\-]zUCD has invalid character 'r   z' in ''z[A-Za-z0-9][A-Za-z0-9\-_]*z(\.z)*;:   zInvalid namespace 'zToo many colons in 'ZivoazInvalid word 'zSecondary word 'z ' is not valid as a primary wordzUnknown word 'zPrimary word 'z"' is not valid as a secondary word)_ucd_singletonr   research
ValueErrorgroupr   	enumeratecountmatchr   r   r    r"   KeyErrorappend)ucdcheck_controlled_vocabulary	has_colonmZword_component_reZword_repartsZwordsiZwordZcolon_countnsZnormalized_wordr   r   r   r   H   sV    "






c                 C   s8   | du rdS zt | ||d W n ty2   Y dS 0 dS )a5  
    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
    NT)r6   r7   F)r   r.   )r5   r6   r7   r   r   r   r      s    
)FF)FF)	r&   r,   Zastropy.utilsr   __all__r   r+   r   r   r   r   r   r   <module>   s   5
Y