
    	]j-"                        d Z ddlZddlZddlZddlZddlmZ ddl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mZmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z"  ejF                  e$      Z% eejL                        Z' G d de'ejP                        Z) eee)      d        Z* G d de+      Z, G d dejP                        Z-y)zThis file and its contents are licensed under the Apache License 2.0. Please see the included NOTICE for copyright information and LICENSE for a copy of the license.
    N)deepcopy)datetime)version)flag_set)	load_func)get_all_files_from_dirget_temp_dirpath_to_open_binary_file)settings)models)	post_save)receiver)gettext_lazy)	Converter)
Annotationc            	          e Zd Z G d dej                        Z ej                   ed      ddd      Z ej                   ed      dd	
      Z
 ej                  ej                  d      Z ej                   ed      dd      Z ej                   ed      ddd      Z ej                   ed      dej"                  ej$                        Z ej(                   ed      e      Z ej.                  ddej0                        Z ej.                  ej4                  dej6                  d ed            Zy)Exportc                   \    e Zd Zd ed      fZd ed      fZd ed      fZd ed      fZy	)
Export.StatuscreatedCreatedin_progressIn progressfailedFailed	completed	CompletedN__name__
__module____qualname___CREATEDIN_PROGRESSFAILED	COMPLETED     I/root/env/lib/python3.12/site-packages/label_studio/data_export/models.pyStatusr      ;    Qy\)#Q}%551X;&;/	r(   r*   titleT i   )blankdefault
max_length
created atCreation time)auto_now_add	help_text	upload_tonullzmd5 of file   )r0   r/   finished atComplete or fail timeNr4   r7   r/   zExport status@   r0   choicesr/   zExporting meta data)r/   projects.Projectexports)related_name	on_delete+
created byrA   rB   r7   verbose_name)r   r    r!   r   TextChoicesr*   	CharFieldr"   r,   DateTimeField
created_at	FileFieldr   DELAYED_EXPORT_DIRfilemd5finished_atr>   r#   status	JSONFielddictcounters
ForeignKeyCASCADEprojectAUTH_USER_MODELSET_NULL
created_byr'   r(   r)   r   r      s_   0## 0 F	'
	E &%%	,!J
 6--D &

	-C
 '&&&	-)	K V	/	F  v	
 H  f..G
 #""  //|_Jr(   r   )senderc                 n    |r3|j                   s&|j                         |_         |j                          y y y N)r,   get_default_titlesave)rZ   instancer   kwargss       r)   set_export_default_namera   W   s+    x~~!335 &wr(   c                   >    e Zd Zed        Zed        Zedd       Zy)
DataExportc                 v   t         j                  j                  t        j                  |dz         }t         j                  j                  t        j                  |dz         }t
        j                  j                  |       j                         }	 t        j                         }	| j                  | j                  | j                  j!                  d      | j"                  j$                  | j&                  j                         |dd|	it)        |      |j!                  d      ||d	d
}
t+        |dd      5 }|j-                  |       ddd       t+        |dd      5 }t/        j0                  |
|d       ddd       |S #  d}	t        j                  d       Y xY w# 1 sw Y   XxY w# 1 sw Y   |S xY w)zPGenerate two files: meta info and result file and store them locally for loggingz.jsonz
-info.json)rV   nonez.Version is not detected in save_export_files()z%Y-%m-%dT%H:%M:%SZ)r,   idrJ   rY   task_numberannotation_numberr   )GETtimeresult_filenamerN   )rV   platformdownloadwutf-8)encodingNFensure_ascii)ospathjoinr   
EXPORT_DIRr   objectsfiltercountr   get_git_versionloggererrorr,   rf   rJ   strftimerY   emailtasksrR   openwritejsondump)rV   nowget_argsdatarN   namefilename_resultsfilename_inforh   platform_versioninfofs               r)   save_export_fileszDataExport.save_export_files`   s}    77<<(;(;TG^LX%8%8$:MN&..55g5FLLN	K&668 !jj%0099:NO%0066&}}224%6 #$45H~%9:#3	
$ "C': 	aGGDM	-w7 	31IIdAE2	33	K%LLIJ&	 		3s$   F 9F"!F.F"F+.F8c                 J   t        | j                         d       }g }t        |j                        }|j	                         j                         D ]C  \  }}t        |      }|j                  |d<   |j                  |vrd|d<   |j                  |       E t        |d       S )N)configproject_dirr   Tdisabledc                 &    | j                  dd      S )Nr   F)get)r   s    r)   <lambda>z/DataExport.get_export_formats.<locals>.<lambda>   s    QUU:u-E r(   )key)
r   get_parsed_configsetsupported_formatsall_formatsitemsr   r   appendsorted)rV   	converterformatsr   formatformat_infos         r)   get_export_formatszDataExport.get_export_formats   s    W%>%>%@dS		 ; ;<#,#8#8#:#@#@#B 	(FK";/K"(++K{{"33*.J'NN;'	( g#EFFr(   Nc                    t        j                         }t        j                  |d      }t	        j
                  t        j                  |      j                  d            j                         }dt        | j                        z   dz   |j                  d      z   d|dd	  z   }	t        j                  | |||||	      }
t        | j                         d
t        j                   j#                  t$        j&                  t$        j(                        || j*                  j,                  j.                  j0                  |      }t3               5 }|j5                  |
||d       t7        |      }t9        t        j:                  |            dk(  rj|d   }t        j                   j=                  |      d   }d| }t?        |      }|	t        j                   j=                  |      d   z   }|||fcd
d
d
       S tA        jB                  |d|       t?        t        j                   jE                  |dz               }d}|	dz   }|||fcd
d
d
       S # 1 sw Y   y
xY w)zGenerate export file and return it as an open file object.

        Be sure to close the file after using it, to avoid wasting disk space.
        Frq   ro   zproject-z-at-z%Y-%m-%d-%H-%M-r      N)r   r   
upload_dirdownload_resourcesaccess_tokenhostname)is_dir   zapplication/zipz.zipzapplication/zip)#r   r   r   dumpshashlibrN   encode	hexdigeststrrf   r}   rc   r   r   r   rs   rt   ru   r   
MEDIA_ROOT
UPLOAD_DIRorganizationrY   
auth_tokenr   r	   convertr   lenlistdirsplitextr
   shutilmake_archiveabspath)rV   r   output_formatr   r   r   r   r   rN   r   
input_jsonr   tmp_dirfilesoutput_fileextcontent_typeoutfilenames                      r)   generate_export_filezDataExport.generate_export_file   s    llnzz%e4kk$**T*11':;EEGC

O+f4s||DT7UUZ[\_`abc\d[eXff11'3$PSUYZ
,,.ww||H$7$79L9LM1 --88CCGG
	 ^ 	/wj'=O*73E2::g&'1,#Ahgg&&{3B7!-cU3.{;"''"2"2;"?"CCL(2	/ 	/ 8*277??7V;K+LMC,Lf}Hh.#	/ 	/ 	/s   B"I8AIIr\   )r   r    r!   staticmethodr   r   r   r'   r(   r)   rc   rc   ^   s?         D 
G 
G '/ '/r(   rc   c            	           e Zd Z G d dej                        Z ej                  dddej                        Z ej                  ddej                  d      Z	 ej                  e
d	ej                  d
      Z ej                  ej                  d      Z ej                   dej"                  ej$                        Z ej(                  ddd      Z ej                   d      Z ej.                   ed      ddd      Z ej.                   ed      ddd      Z ej.                   ed      ddd      Z ej                  ej8                  dej:                  d ed            Z fdZ xZ S )ConvertedFormatc                   \    e Zd Zd ed      fZd ed      fZd ed      fZd ed      fZy	)
ConvertedFormat.Statusr   r   r   r   r   r   r   r   Nr   r'   r(   r)   r*   r      r+   r(   r*   r?   Texport_conversions)r7   rA   rB   zorganizations.Organization)r7   rB   rA   converted_formatsz'Export snapshot for this converted file)rA   rB   r4   r5   r<   r=   z"Traceback report in case of errors)r7   r.   r4   )r0   r1   r2   )r7   r3   r4   z
updated atzUpdated timer9   r:   Nr;   rC   rD   rE   c                     t        d      r&| j                  r| j                  j                          t        |   |i | y )NDff_back_dev_4664_remove_storage_file_on_export_delete_29032023_short)r   rM   deletesuper)selfargsr`   	__class__s      r)   r   zConvertedFormat.delete   s4    Z[yy		  "''r(   )!r   r    r!   r   rG   r*   rT   rU   rV   r   r   exportrK   r   rL   rM   rH   r>   r#   rP   	TextField	tracebackexport_typerI   r"   rJ   
updated_atrO   rW   rX   rY   r   __classcell__)r   s   @r)   r   r      s   0## 0  f)..	G %6$$$..)	L V(..;	F 6--D VF
 !  d$BfgI"&""b1K%%%	,!	J &%%	, 	J '&&&	-)	K #""  //|_J( (r(   r   ).__doc__r   loggingrs   r   copyr   r   ujsonr   corer   core.feature_flagsr   core.utils.commonr   core.utils.ior   r	   r
   django.confr   	django.dbr   django.db.models.signalsr   django.dispatchr   django.utils.translationr   r"   label_studio_sdk.converterr   tasks.modelsr   	getLoggerr   r{   EXPORT_MIXINExportMixinModelr   ra   objectrc   r   r'   r(   r)   <module>r      s      	      ' ' X X    . $ 6 0 #			8	$ --.7[&,, 7t 
)F# $Z/ Z/zB(fll B(r(   