
    j-/              3          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 ddl	m
Z
mZmZ ddlZddlmZmZmZmZ ddlmZ ddlmZmZ ddlmZ ej4                  j7                  d	      Zd
Z eddg      Zde d ej@                          de dZ! ejD                  ee!       ejF                  d ej                  dde      dd       ejH                  dddd       ejH                  dddd       ejH                  d d!dd"       ejH                  d#d$d%dd&       ejH                  d'd(d)dd*       ejH                  d+d,d-dd.       ejH                  d/d0dd1       ejH                  d2d3d4 ejJ                   ejL                         d5      ejN                  6       ejH                  d7d8d9:       ejH                  d;dd<       ejH                  d=d>ddd?@       ejH                  dAdBddC       ejH                  dDdEddF       ejH                  dGdHddI       ejH                  dJ ej                  ddddeK      dLM      de
e   dNe(de(d!e(d%e(d)e(d-e(dOe(d4e)dPe*dQe(d>e(dRe(dSe(dHe(dTee   dUdf"dV                                                                                                                       Z+dWe
e   dUe(fdXZ,d	edUefdYZ-e.dZk(  r e+        yy)[aS  Python-based command-line client for Magika.

This client serves as a fallback and is maintained for backward compatibility.
It is primarily useful for installations from the pure-Python wheel, especially
on platforms where the Rust-based binary client is not yet available.

For optimal performance, the Rust-based client is recommended.
    N)Path)ListOptionalTuple)MagikaMagikaErrorPredictionModecolors)
get_logger)ContentTypeLabelMagikaResult)OverwriteReasonmagikazmagika-dev@google.comz-hz--help)help_option_namesz
Magika version: "z"
Default model: "z"

Send any feedback to z or via GitHub issues.
)context_settingsepilogfileF)existsreadable	path_type)typerequirednargsz-rz--recursiveTzgWhen passing this option, magika scans every file within directories, instead of outputting "directory")is_flaghelpz--jsonjson_outputzOutput in JSON format.z--jsonljsonl_outputzOutput in JSONL format.z-iz--mime-typemime_outputzCOutput the MIME type instead of a verbose content type description.z-lz--labellabel_outputzOutput a simple label instead of a verbose content type description. Use --list-output-content-types for the list of supported output.z-cz--compatibility-modemagic_compatibility_modezUCompatibility mode: output is as close as possible to `file` and colors are disabled.z-sz--output-scorez>Output the prediction's score in addition to the content type.z-mz--prediction-modeprediction_mode_str)case_sensitive)r   defaultz--batch-size    z'How many files to process in one batch.)r$   r   z--no-dereferencezVThis option causes symlinks not to be followed. By default, symlinks are dereferenced.z--colors/--no-colorswith_colorszEnable/disable use of colors.)r   r$   r   z-vz	--verbosezEnable more verbose output.z-vvz--debugzEnable debug logging.z	--versionoutput_versionzPrint the version and exit.z--model-dir)r   	file_okaydir_okayresolve_pathr   zUse a custom model.)r   r   	recursiveoutput_score
batch_sizeno_dereferenceverbosedebug	model_dirreturnc                    | }|rd}t        |      }|r|j                  t        j                         |r|j                  t        j                         |rd|j                  d       |j                  dt                |j                  dt        j                                 t        j                  d       t        |      dk(  r&|j                  d       t        j                  d       d}|D ]W  }t        |      d	k(  rd
}|j                         r%|j                  dt        |       d       t        j                  d       Y |r\t        |      dkD  r&|j                  d       t        j                  d       |r&|j                  d       t        j                  d       |	dk  s|	dkD  r&|j                  d       t        j                  d       |r(|r&|j                  d       t        j                  d       t        |      t        |      z   t        |      z   dkD  r&|j                  d       t        j                  d       |rg }|D ]  }|j                         r]|j!                         r|j#                  |       5|j%                         sF|j'                  t)        |j+                  d                   pt        |      d	k(  r|j                  dt        |       d       t        j                  d        t-        t/        d |            }|j1                  dt        |       d       |j3                  d|        |?t4        j6                  j9                  d      }||j;                         dk7  rt=        |      }	 t        |t?        |      |
|||      }d}d}tB        jD                  tB        jF                  tB        jH                  tB        jJ                  tB        jJ                  tB        jJ                  tB        jL                  d}g }t        |      |	z  }t        |      |	z  dk7  r|dz  }tO        |      D ]  }|||	z  |dz   |	z   }tQ        |      rtS              g}njU                  |      }|r|j'                  tW        ||             Y|rGtW        ||      D ]7  \  } }!|j                  tY        jZ                  |!j]                                      9 tW        ||      D ]k  \  } }!|!j^                  r|r"|!j`                  jb                  jd                  }"ni|r+t        |!j`                  jb                  jf                        }"n<|!j`                  jb                  jh                   d|!j`                  jb                  jj                   d}"|!j`                  jl                  jf                  tn        jp                  k7  r|!j`                  jl                  jf                  |!j`                  jb                  jf                  k7  r|!j`                  jr                  tt        jv                  k(  r_|"d|!j`                  jl                  jh                   d|!j`                  jl                  jj                   d |!j`                  jx                   d!z  }"|r_|j9                  |!j`                  jb                  jj                  tB        jz                        }tB        j|                  }n|!j~                  }"d}d}|rN|!j^                  rBt        |!j`                  jx                  d"z        }#|j                  | |  d#|" d$|# d%|        R|j                  | |  d#|" |        n  |rH|j                  tY        jZ                  |D $!cg c]  \  }$}!|!j]                          c}!}$d&'             yy# t@        $ r:}|j                  t        |             t        j                  d       Y d}~Jd}~ww xY wc c}!}$w )(z4Magika - Determine type of FILEs with deep-learning.F)
use_colorszMagika python clientzMagika version: zDefault model: r   z<You need to pass at least one path, or - to read from stdin.   -TzFile or directory "z" does not exist.z/If you pass "-", you cannot pass anything else.z2If you pass "-", recursive scan is not meaningful.i   zABatch size needs to be greater than 0 and less or equal than 512.z2You should use either --json or --jsonl, not both.zAYou should use only one of --mime, --label, --compatibility-mode.*c                 $    | j                          S N)is_dir)xs    Y/root/aria/tools/markitdown-venv/lib/python3.12/site-packages/magika/cli/magika_client.py<lambda>zmain.<locals>.<lambda>   s    AHHJ     zConsidering z fileszFiles: NMAGIKA_MODEL_DIR )r1   prediction_moder.   r/   r0   r4   )document
executablearchiveaudioimagevideocodez ()z# [Low-confidence model best-guess: z	), score=]d   z:  %   )indent)@r   setLevelloggingINFODEBUGraw_print_to_stdoutVERSIONr   _get_default_model_namesysexitlenerrorstrr   intis_fileappendr:   extendsortedrgloblistfilterinfor0   osenvirongetstripr   r	   r   r
   LIGHT_PURPLELIGHT_GREEN	LIGHT_REDYELLOW
LIGHT_BLUErange_should_read_from_stdin_get_magika_result_from_stdinidentify_pathszipjsondumpsasdictok
predictionoutput	mime_typelabeldescriptiongroupdlr   	UNDEFINEDoverwrite_reasonr   LOW_CONFIDENCEscoreWHITERESETstatus)%r   r+   r   r   r   r    r!   r,   r"   r-   r.   r&   r/   r0   r'   r1   files_paths_lread_from_stdinpexpanded_pathsmodel_dir_strr   mrstart_color	end_colorcolor_by_groupall_predictionsbatches_num	batch_idxbatch_files_pathsbatch_predictions	file_pathresultrx   r   _s%                                        r<   mainr   6   s   J K	{	+B
GLL!
GMM"
56
!1';<
1O1O1Q0RST
;1
OPO q6S="OHH*3q6(2CDEHHQK {aHHFGHHQKHHIJHHQKQ*s*
TU|
EF
;#l++c2J.KKaO
TU 	Axxz99;"))!,XXZ"))&*>?Q3.s1vh6GHI	 #'v.F'W"XGGl3{+,F34HHw{m$%
 

'9:$)<)<)>")D]+I*+>?)"
 KI ''((##!!N 8:Ok"j0K
;*$)q;' >	'
"i!mz%A
 #;/!>v!F G & 5 56G H""3'8:K#LM%():<M%N D!	6&&tzz&--/'BCD &)):<M%N -!	699" "(!2!2!9!9!C!C%!$V%6%6%=%=%C%C!D$*$5$5$<$<$H$H#IFL]L]LdLdLjLjKkkl!m #--0066:J:T:TT & 1 1 4 4 : :%0077==!> & 1 1 B B.==!> # E#)#4#4#7#7#C#C"DBvGXGXG[G[GaGaFb c))/):):)@)@(A!DF #&4&8&8"--44::FLL' %+LL	#]]F"$K "IFII 1 1 7 7# =>E**&-	{"VHAeWAi[Q **&-	{"VHYKHW-#>@ 
JJ4CD[aD	
 o  
Rt Es   ^; `;	_>/_99_>r   c                 D    t        |       dk(  xr t        | d         dk(  S )Nr5   r   r6   )rY   r[   )r   s    r<   ro   ro   S  s%    {q ?SQ%8C%??r>   c                 x    t         j                  j                  j                         }| j	                  |      }|S r9   )rW   stdinbufferreadidentify_bytes)r   contentr   s      r<   rp   rp   W  s/    ii##%G""7+FMr>   __main__)/__doc__importlib.metadata	importlibrs   rQ   re   rW   pathlibr   typingr   r   r   clickr   r   r   r	   r
   magika.loggerr   magika.typesr   r   magika.types.overwrite_reasonr   metadataversionrU   CONTACT_EMAILdictCONTEXT_SETTINGSrV   HELP_EPILOGcommandargumentoptionChoiceget_valid_prediction_modesHIGH_CONFIDENCEboolr[   r\   r   ro   rp   __name__ r>   r<   <module>r      s       	 
  ( (  > > $ 7 9



$
$X
.'4*:;  ///12 3#_ %	 % 
	55D	A
	 	r	 ht:RSi<UV	N  
R 	` 	I	 	?n??ARV	W** B%N 	a
 	( dK4QReY3JK!46S 	ut$RV
 
K

t*K
K
 K
 	K

 K
 K
 #K
 K
 K
 K
 K
 K
 K
 K
 K
  ~!K
" 
#K
 L S W T	^K
\@d @ @& \  zF r>   