o
    joi                     @   sH   d Z ddlZddlmZ ddlZddlZeeejf ZG dd dZ	dS )z4Helper classes for generating FAR files with Pynini.    N)Unionc                	   @   sT   e Zd ZdZ		ddededejddfd	d
Zdedej	ddfddZ
dddZdS )Exportera  Helper class to collect and export FSTs into an FAR archive file.

  For example, the following code collects 2 FSTs and writes them upon
  destruction of the involved exporter instance.

    exporter = grm.Exporter(filename)
    exporter.export("FST1", fst1)
    exporter.export("FST2", fst2)

  Typically, instead of explicitly creating an Exporter, a client will use the
  exporter provided as the argument to the generator_main function.
  standarddefaultfilenamearc_typefar_typereturnNc                 C   s4   t d| i | _t|| _|| _|| _d| _dS )a:  Creates an exporter that writes a FAR archive file upon destruction.

    Args:
      filename: A string with the filename.
      arc_type: A string with the arc type; one of: "standard", "log", "log64".
      far_type: A string with the file type; one of: "default", "sstable",
          "sttable", "stlist".
    zSetting up exporter for %rTN)	logginginfo_fstsosfspath	_filename	_arc_type	_far_type_is_open)selfr   r   r    r   H/home/ubuntu/.local/lib/python3.10/site-packages/pynini/export/export.py__init__(   s   
zExporter.__init__namefstc                 C   s(   | j sJ td|| j || j|< dS )zRegister an FST under a given name to be saved into the FST archive.

    Args:
      name: A string with the name of the fst.
      fst: An FST to be stored.
    zAdding FST %r to archive %rN)r   r
   r   r   r   )r   r   r   r   r   r   __setitem__;   s   
zExporter.__setitem__c                 C   s   | j sJ td| j tj| jd| j| jd}t| j	D ]}td|| j | j	| ||< qW d   n1 s:w   Y  td| j d| _ dS )z=Writes the registered FSTs into the given file and closes it.zWriting FSTs into %rw)r   r   zWriting FST %r to %rNzWriting FSTs to %r completeF)
r   r
   r   r   pyniniFarr   r   sortedr   )r   sinkr   r   r   r   closeF   s   



zExporter.close)r   r   )r	   N)__name__
__module____qualname____doc__	_Filenamestrr   FarTyper   Fstr   r   r   r   r   r   r      s    
r   )
r#   r   typingr   r
   r   r%   PathLiker$   r   r   r   r   r   <module>   s   