
    Zi                    r    S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKJ	r	   " S S5      r
 " S S\
5      rg)	zmThis module includes a bunch of convenient base classes that are
reused in many of the other parser modules.
    )annotationsN)
exceptionsc                  <    \ rS rSrSrS	S jrS rS
S jrSS jrSr	g)
BaseParser   zThe :class:`.BaseParser` abstracts out some common functionality
that is used across all document Parsers. In particular, it has
the responsibility of handling all unicode and byte-encoding.
c                    [        S5      e)zThis method must be overwritten by child classes to extract raw
text from a filename. This method can return either a
byte-encoded string or unicode.
z$must be overwritten by child classes)NotImplementedError)selffilenamekwargss      p/var/www/eduai.edurigo.com/question_generate/ques_gen_env/lib/python3.13/site-packages/textract/parsers/utils.pyextractBaseParser.extract   s    
 ""HII    c                &    UR                  US5      $ )zsEncode the ``text`` in ``encoding`` byte-encoding. This ignores
code points that can't be encoded in byte-strings.
ignore)encode)r
   textencodings      r   r   BaseParser.encode   s     {{8X..r   c                l    U R                   " U40 UD6nU R                  XR5      nU R                  Xc5      $ )zProcess ``filename`` and encode byte-string with ``encoding``. This
method is called by :func:`textract.parsers.process` and wraps
the :meth:`.BaseParser.extract` method in `a delicious unicode
sandwich <http://nedbatchelder.com/text/unipain.html>`_.

)r   decoder   )r
   r   input_encodingoutput_encodingr   byte_stringunicode_strings          r   processBaseParser.process$   s4     ll86v6[A{{>;;r   Nc                    [        U[        5      (       a  U$ U(       d  gU(       a  UR                  U5      $ [        R                  " U5      nUS   S:  a  US   OSnUR                  USS9$ )zSDecode ``text`` using the `chardet
<https://github.com/chardet/chardet>`_ package.
 
confidenceg?r   utf8replace)errors)
isinstancestrr   chardetdetect)r
   r   r   resultr   s        r   r   BaseParser.decode3   sm     dC  K  ;;~.. %)/)=)D6*%&{{8I{66r    )returnzbytes | str)r"   )N)
__name__
__module____qualname____firstlineno____doc__r   r   r   r   __static_attributes__r+   r   r   r   r      s    
J/<7r   r   c                  $    \ rS rSrSrS rS rSrg)ShellParserJ   zThe :class:`.ShellParser` extends the :class:`.BaseParser` to make
it easy to run external programs from the command line with
`Fabric <http://www.fabfile.org/>`_-like behavior.
c                    [         R                  " U[         R                  [         R                  S9nUR                  5       u  pEUR                  S:w  a2  [        R                  " SR                  U5      UR                  UU5      eXE4$ ! [         aL  nUR                  [        R
                  :X  a(  [        R                  " SR                  U5      SSS5      ee SnAff = f)zRun ``command`` and return the subsequent ``stdout`` and ``stderr``
as a tuple. If the command is not successful, this raises a
:exc:`textract.exceptions.ShellError`.
)stdoutstderr    r   Nr   )
subprocessPopenPIPEOSErrorerrnoENOENTr   
ShellErrorjoincommunicate
returncode)r
   argspipeer7   r8   s         r   runShellParser.runP   s    	##!!D& ))+ ??a''	  ~3  
	ww%,,& !++HHTN	  
	s   2B 
C!ACC!c                `    [         R                  " 5       u  p[        R                  " U5        U$ )zReturn a unique tempfile name.)tempfilemkstemposclose)r
   handler   s      r   temp_filenameShellParser.temp_filenamex   s&     $++-
r   r+   N)r-   r.   r/   r0   r1   rH   rP   r2   r+   r   r   r4   r4   J   s    
&Pr   r4   )r1   
__future__r   r?   rM   r;   rK   r'   textractr   r   r4   r+   r   r   <module>rT      s8    #  	    67 67r6* 6r   