
    $j                         d Z ddlZddlZddlZddlZddlZddlmZmZ  ej	        e
          ZdZdZdZ G d d	e          Zd
ej        dededdfdZdS )z=Iris parser implementation using the Vectorize Iris HTTP API.    N   )
FileParserUnsupportedFileTypeErrorzhttps://api.vectorize.io/v1g       @g     r@c            	       P    e Zd ZdZeefdedededefdZde	ded	efd
Z
d	efdZdS )
IrisParseru  
    Iris file parser using the Vectorize Iris cloud extraction service.

    Uploads files to the Vectorize Iris API, starts an extraction job,
    and polls until the text is ready. The API determines which file types
    are supported — UnsupportedFileTypeError is raised if the file is rejected.

    Authentication:
        Requires HINDSIGHT_API_FILE_PARSER_IRIS_TOKEN and
        HINDSIGHT_API_FILE_PARSER_IRIS_ORG_ID environment variables,
        or pass them explicitly via the constructor.
    tokenorg_idpoll_intervaltimeoutc                 V    || _         || _        || _        || _        dd| i| _        dS )a%  
        Initialize iris parser.

        Args:
            token: Vectorize API token
            org_id: Vectorize organization ID
            poll_interval: Seconds between status poll requests (default: 2)
            timeout: Maximum seconds to wait for extraction (default: 300)
        AuthorizationzBearer N)_token_org_id_poll_interval_timeout_auth_headers)selfr   r	   r
   r   s        i/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/hindsight_api/engine/parsers/iris.py__init__zIrisParser.__init__!   s=      +-/@/@/@A    	file_datafilenamereturnc                   K   t          j        |          d         pd}t          j        t          j        dd                    4 d{V }|                    t           d| j         d	| j        ||d
           d{V }t          ||d           |
                                }|d         }|d         }|                    |t          |          d|i           d{V }	t          |	|d           |                    t           d| j         d| j        d|i           d{V }
t          |
|d           |

                                d         }t          j                    | j        z   }	 |                    t           d| j         d| | j                   d{V }t          ||d           |
                                }|                    d          r|                    di           }|                    d          s+|                    dd          }t#          d| d|           |                    d           }|st#          d!| d"          |cddd          d{V  S t          j                    |k    rt#          d#| j         d$| d"          t%          j        | j                   d{V  U# 1 d{V swxY w Y   dS )%z
        Parse file to text using the Vectorize Iris API.

        Raises:
            UnsupportedFileTypeError: If the Iris API rejects the file type (4xx)
            RuntimeError: If extraction fails for another reason
        r   zapplication/octet-streamg      >@g      ^@)read)r   Nz/org/z/files)namecontentType)headersjsonzfile upload initfileId	uploadUrlzContent-Type)contentr   zfile uploadz/extractionzstart extractionextractionIdTz/extraction/)r   zpoll extraction statusreadydatasuccesserrorzunknown errorzIris extraction failed for '': textzNo content extracted from ''z Iris extraction timed out after zs for ')	mimetypes
guess_typehttpxAsyncClientTimeoutpost_IRIS_BASE_URLr   r   _raise_for_statusr   putbytestime	monotonicr   getRuntimeErrorasynciosleepr   )r   r   r   content_typeclient	init_resp	init_datafile_id
upload_urlupload_respextract_respextraction_iddeadlinestatus_respstatus_datar%   r'   r)   s                     r   convertzIrisParser.convert7   s%      !+H55a8V<V$U]4e-L-L-LMMM 5	9 5	9 5	9 5	9 5	9 5	9 5	9QW$kk!<<<<<*&|DD *        I
 i3EFFF!((I$X.G'4J !'

i(('6 !+ ! !      K
 k8]CCC "(!AAAAA*( "- " "      L
 lH6HIII!-!2!2!4!4^!DM ~''$-7H9$*JJ%UUDLUUmUU . %/ % %       "+x9QRRR)..00??7++  &??6266D88I.. ` $/ B B*+^(+^+^W\+^+^___88F++D V*+T+T+T+TUUUa5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9d >##x//&'k$-'k'k`h'k'k'klllmD$7888888888+9A5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9 5	9s   HK	6AK		
KKc                     dS )zGet parser name.iris )r   s    r   r   zIrisParser.namex   s    vr   N)__name__
__module____qualname____doc___DEFAULT_POLL_INTERVAL_DEFAULT_TIMEOUTstrfloatr   r4   rG   r   rJ   r   r   r   r      s         "  6)B BB B 	B
 B B B B,?9u ?9 ?9 ?9 ?9 ?9 ?9Bc      r   r   responser   stepr   c           
          | j         sdS | j        pd}d| d| d| j         d| j         d| 
}| j        rt          |          t          |          )z
    Raise an appropriate error including the response body on HTTP errors.

    Raises UnsupportedFileTypeError for 4xx responses (file rejected by the API),
    RuntimeError for other HTTP errors.
    Nz<empty>zIris API error during z for 'r(    u    — )is_errorr)   status_codereason_phraseis_client_errorr   r8   )rS   r   rT   bodymsgs        r   r2   r2   }   s|      =%ID
v4
v
vx
v
vH<P
v
vS[Si
v
vpt
v
vC ,&s+++
s

r   )rN   r9   loggingr+   r5   r-   baser   r   	getLoggerrK   loggerr1   rO   rP   r   ResponserQ   r2   rJ   r   r   <module>rb      s    C C         6 6 6 6 6 6 6 6		8	$	$.  g g g g g g g gT # S T      r   