
    H&hT                         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	m
Z
 d dlmZ  eeef      dd       Z ee      d        Z G d	 d
e      Zy)    N)dispatch)Refactor)Idscommon_names)Enumc           	      2   t        |       }|j                  |       |j                  }|j                         D cg c]&  }|D ]  }t	        |d   |      j                         ! ( }}}t        | t              r| g} t        t        | |            S c c}}w )aU  
    Get common names from scientific names or ids

    :param x: (str|list(str)|Ids) One or more scientific names or partial names,
        or an `Ids` object
    :param db: (str) Data source, default: "ncbi". NCBI only supported right
        now, other sources to come.
    :param \*\*kwargs: Curl options passed on to `requests.get`

    :return: dict, keys are supplied scientific names, and values
        are common names

    :note: Remember to set your Entrez API key as `ENTREZ_KEY`

    Usage::
      
      from pytaxize import scicomm
      
      # from names (str or list of str's)
      scicomm.sci2comm('Helianthus annuus')
      scicomm.sci2comm('Puma concolor')
      scicomm.sci2comm(['Helianthus annuus', 'Poa annua'])
      scicomm.sci2comm('Gadus morhua')
      scicomm.sci2comm('Pomatomus saltatrix')
      scicomm.sci2comm('Loxodonta africana')

      scicomm.sci2comm('Lycaon pictus', db="itis")
    
      ## no results
      ### not a real name
      scicomm.sci2comm('foo bar')
      ### good name, many id results, but no common names
      scicomm.sci2comm("Echinacea")

      # from an Ids object
      from pytaxize import Ids
      x = Ids('Helianthus annuus')
      x.ncbi()
      scicomm.sci2comm(x)
      x.itis()
      scicomm.sci2comm(x)
      x = Ids('Lycaon pictus')
      x.itis()
      x.ids
      scicomm.sci2comm(x)
    )dbid)
r   r
   idsvaluesCommonNamescall
isinstancestrdictzip)xr
   zoutwress         _/mounts/lovelace/software/anaconda3/envs/py312/lib/python3.12/site-packages/pytaxize/scicomm.pysci2commr   
   s    ` 	AADDBDK
%%C47JJL
Lq!
LQ;qw#((*
L*
LC
L!SCAs Ms   +Bc           	          | j                   }|j                         D cg c]0  }|D ])  }t        |d   | j                        j	                         + 2 }}}t        t        | j                  |            S c c}}w )Nr   )r   r   r   db_idsr   r   r   name)r   r   r   r   r   s        r   r   r   B   sf    
%%C:=**,
RQPQ
R1;qw)..0
R0
RC
RAFFC !! Ss   5A7c                   4     e Zd Zd Z fdZd Zd Zd Z xZS )r   c                 F    t         t        |           || _        || _        y )N)superr   __init__r   r
   )selfr   r
   	__class__s      r   r!   zCommonNames.__init__J   s    	+t%'DGDG    c                     | j                   dk(  r| j                  | j                        S | j                   dk(  r| j                  | j                        S y )Nncbiitis)r
   r&   r   r'   )r"   s    r   r   zCommonNames.callO   sE    ww&YYtwwww&YYtww r$   c                    |g S t         j                  j                  d      }|t        d      d||d}d} t	        ||d      j
                  di |}|j                  d      }|D cg c]  }|j                   c}S c c}w )	N
ENTREZ_KEYzENTREZ_KEY is not definedtaxonomy)r
   IDapi_keyz9https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgigetz,//TaxaSet/Taxon/OtherNames/GenbankCommonName )osenvironr-   	Exceptionr   xmlxpathtext)	r"   r   kwargskeyqueryurlr   r   r   s	            r   r&   zCommonNames.ncbiU   s    	
	JJNN<(c	56
6qS9eGc+HS%'++5f5c
))B
Ca aff   s   +Bc                 X    t        |      }|d   |D cg c]  }|d   	 c}S g S c c}w )N)tsnr   
commonNamer   )r"   r   r5   r   r   s        r   r'   zCommonNames.itisb   s2    q!c/21v/As+!ao+IrI+s   ')	__name__
__module____qualname____doc__r!   r   r&   r'   __classcell__)r#   s   @r   r   r   H   s    
 !Jr$   r   )r&   )r/   requestsjsonmultipledispatchr   pytaxize.refactorr   pytaxize.idsr   pytaxize.itisr   enumr   r   listr   objectr   r.   r$   r   <module>rJ      s_    	   % &  & 	3+5 5n 
#" "
J& Jr$   