
    ZThy                         d dl Z d dlmZmZm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  G d d      Z G d	 d
e	      Zy)    N)AnyDictOptional)	CsvConfig)CsvUploadService)DataImportService)TimeFormatTypec                   N    e Zd ZU dZeeef   ed<   dZe	ed<   d
dZ
d Zdefd	Zy)BaseCh10Filea  
    Base class for uploading IRIG Chapter 10/Chapter 11 files.

    Implement a concrete version of this class that parses a ch10 stream and returns
    a csv row of data on each iteration.

    Set `gzip` to `True` if sending a compressed stream.

    Example:
    ```python

    class Ch10(BaseCh10File):

        def __init__(self, path):
            self.file = open(path, "rb")
            self.initialize_csv_data_columns = None

        def initialize_csv_data_columns(self):
            self.csv_config_data_columns = self.process_ch10_computer_f1_packet()

        def process_ch10_computer_f1_packet(self) -> Dict[int, dict]:
            # Processes the first Computer F1 packet
            # and returns the measurements as a dict.
            ...

        def process_ch10_pcm_packet(self) -> str:
            # Processed the data packets and returns
            # a CSV row.
            ...

        def __next__(self) -> str:
            # On all iterations, return data for the CSV file.
            if end_of_file:
                raise StopIteration()
            else:
                return self.process_ch10_data_packet()
    ```
    csv_config_data_columnsFgzipreturnNc                     t         )z
        Must populate the `csv_config_data_columns` attribute
        that is the data_columns entry in the CsvConfig.

        See the Sift data_import module or API docs for the schema.
        NotImplementedErrorselfs    ]/home/www/backend.miabetepe.com/venv/lib/python3.12/site-packages/sift_py/data_import/ch10.pyinitialize_csv_data_columnsz(BaseCh10File.initialize_csv_data_columns7   s
     "!    c                     | S N r   s    r   __iter__zBaseCh10File.__iter__@   s    r   c                     t         r   r   r   s    r   __next__zBaseCh10File.__next__C   s    !!r   )r   N)__name__
__module____qualname____doc__r   intdict__annotations__r   boolr   r   strr   r   r   r   r   r      s8    %N "#t)_,D$""# "r   r   c                   X    e Zd ZdZej
                  ddfdedededee   dee   de	fd	Z
y)
Ch10UploadServicezService to upload ch10 files.N	ch10_file
asset_nametime_formatrun_namerun_idr   c           	         |j                          t        |d      sJ d       |d|dd|j                  d}|r||d<   |r||d<   t        |      }t	        j
                  | j                  d	| j                   d
dt        j                  d|j                         i            }|j                  dk7  r%t        d|j                   d|j                         	 |j                         }		 |	d   }|	d   }dd	| j                   i}|j"                  rd|d<   t	        j
                  |||      }|j                  dk7  r%t        d|j                   d|j                         t%        | j&                  |      S # t        j                  j                  t         f$ r }
t        d|j                   d|
       d}
~
ww xY w# t         $ r}
t        d|
 d      d}
~
ww xY w)ay  
        Uploads the ch10 file to the specified asset.

        Override `time_format` to specify the time data format. Default is `TimeFormatType.ABSOLUTE_UNIX_NANOSECONDS`.
        Override `run_name` to specify the name of the run to create for this data. Default is None.
        Override `run_id` to specify the id of the run to add this data to. Default is None.
        r   zF`csv_config_data_columns` was not set correctly on the first iteration      )formatcolumn_number)r)   first_data_rowtime_columndata_columnsr+   r,   zBearer zapplication/octet-stream)AuthorizationContent-Encoding
csv_config)urlheadersdata   z3Config file upload request failed with status code z. zInvalid response: z.
N	uploadUrldataImportIdz Response missing required keys: zI. This is unexpected. Please reach out to the Sift team about this error.r5   r   r6   z1Data file upload request failed with status code )r   getattrr   r   requestspost_upload_uri_apikeyjsondumpsto_dictstatus_code	ExceptiontextdecoderJSONDecodeErrorKeyErrorr   r   
_rest_conf)r   r(   r)   r*   r+   r,   config_infor7   responseupload_infoe
upload_urldata_import_idr9   s                 r   uploadzCh10UploadService.uploadJ   s%    	--/y";< 	
T	
<
 %%!" &=='
 &.K
#$,K!{+
==  #*4<<.!9$> \:+=+=+?@A
 3&EhFZFZE[[]^f^k^k]lm 	H"--/K	)+6J"-n"=N wt||n5
 >>*0G&'==
 3&CHDXDXCYY[\d\i\i[jk  !.AA; ,,h7 	H0s1#FGG	H  	21#5~ 	s0   E: ,
G  :#F=F88F= 	G	GG)r   r   r   r    r	   ABSOLUTE_UNIX_NANOSECONDSr   r%   r   r   rS   r   r   r   r'   r'   G   sk    ' '5&N&N"& $SBSB SB $	SB
 3-SB SB 
SBr   r'   )rC   typingr   r   r   r?   sift_py.data_import.configr   sift_py.data_import.csvr   sift_py.data_import.statusr   sift_py.data_import.time_formatr	   r   r'   r   r   r   <module>rZ      s9     & &  0 4 8 :8" 8"vVB( VBr   