o
    joi                     @   s   d Z ddlmZmZ ddlmZ ddlmZ ddlZddlm	Z	 e
dddZejZeee	jf Zd	ed
eeef fddZdeegdf d
dfddZdS )a  Interface for generating multiple FAR files from a Pynini file.

Given a number of output FAR files (FST archive) via --output in the form
designator1=file1,designator2=file2,... a Pynini file produces the FAR files
file1, file2,...  by exporting a number of FSTs into these FAR files. To this
end, a Pynini file creates a generator_main file with the signature:

  def generator_main(exporter_map: multi_grm.ExporterMapping):

which can contain lines of the form

  my_exporter = exporter_map[designator]
  my_exporter[fst_name] = fst

to export a given FST under a corresponding name into the file identified by the
given designator. The generator main function should be have a:

  if __name__ == '__main__':
    multi_grm.run(generator_main)

For an example, see multi_grm_example.py.
    )CallableMapping)app)flagsN)exportoutputszHThe output FAR files in the form designator1=file1,designator2=file2,...target_file_pairsreturnc                 C   s^   i }|  dD ]%}| d}t|dkrtd|d ||d < td|d |d  q|S )aa  Generates a map from a list designator1=file1,designator2=file2,...

  Args:
    target_file_pairs: A string of the form
                       designator1=file1,designator2=file2,...
  Returns:
    A map from designators to file names.
  Raises:
    app.UsageError: If there is a parsing error. Does not check whether the
      files can be written.
  ,=   zA--outputs must be of form designator1=file1,designator2=file2,...   r   z1Setting up target designator [%s] with file [%s].)splitlenr   
UsageErrorlogginginfo)r   resulttarget_file_pairtarget_file_list r   K/home/ubuntu/.local/lib/python3.10/site-packages/pynini/export/multi_grm.py_get_target_file_map6   s   

r   generator_mainc                    s*    fdd}t d dt_t| dS )a%  Executes the multi_grm FAR export program to export a number of FARs.

  Args:
    generator_main: The FAR generator_main function to execute. It takes a
      single argument "exporter_map", which is a map from string designators to
      export.Exporter objects with a __setitem__ method.
  c                    s|   z6|  d | rtd|  ttj}|stddd | D } | | D ]}|  q-W d S    dt	_
 )Nr   z#Unexpected command line arguments: z3--outputs must specify at least one name=file pair.c                 S   s   i | ]
\}}|t |qS r   )r   Exporter).0
designatorfilenamer   r   r   
<dictcomp>a   s    
z%run.<locals>.main.<locals>.<dictcomp>fatal)popr   r   r   _OUTPUTSvalueitemsvaluescloseFLAGSstderrthreshold)unused_argvr   exporter_mapexr   r   r   mainW   s*   


zrun.<locals>.mainr   warningN)r   mark_flag_as_requiredr&   r'   r   run)r   r,   r   r+   r   r/   N   s   	
r/   )__doc__typingr   r   abslr   r   r   pynini.exportr   DEFINE_stringr!   r&   strr   ExporterMappingr   r/   r   r   r   r   <module>   s    