
    \j;                        U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl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mZmZmZmZmZmZmZmZmZmZ ddlmZmZmZmZm Z m!Z!m"Z"m#Z# ddl#m$Z$m%Z% ddl&m'Z'm(Z( 	 ddl)m"Z* 	 ddl,m"Z- ddl.m/Z/ d	d	d	d
dZ0ddiZ1ee2eee3ge$f      f   e4d<   e1jk                  d  eee3ge$f   e#jl                        e#jn                  e#jp                  fD               	 d dl9Z9dee2   deejx                     defdZ=de2de>e2   dz  de>e2   fdZ?de j                  deeAe2e2e3f      fdZBde j                  ddfdZCde j                  ddfdZDde2d e2d!e3d"edee2gef   f
d#ZEd$ee2gdf   d%ee2   de3fd&ZF G d' d(      ZGde j                  ddfd)ZHd*ZId+ej                  de d%ee2   d,ej                  ddf
d-ZLde j                  ddfd.ZMd/ZN e j                  d0d12      ZPePj                  d3        e j                  d
4      ZReRj                  d5 e j                  d6d78      d9:       ePj                  d;<      ZVeVj                  d=d>eRgeNd?@      ZXeXj                  dAdB e j                  dC      dD:        G dE dFe j                        ZZeXj                  dGdHdIdJgeCdKeZdLM       eXj                  dNdOdPgdQdRj                  e1       dST       eVj                  dUdVeRgeNW      Z\e\j                  eH3       e\j                  dXdYe3g dZd[\       eVj                  d]d^eRgeNW      Z]e]j                  eM3       e]j                  d_d`e3 ej                         xs da       ePj                         Z`e`j                  re`j                  e`       yePj                          y# e+$ r Y <w xY w# e+$ r Y Aw xY w# e+$ r.  ejt                  d       Z; ejt                  d d       Z9Y w xY w)b    N)AnyCallableDequeDictIterableIteratorListOptionalSequenceTupleUnioncast   )BasicResolverCachingResolverDomainMatchersParserPartialResultResolvercaching)CacheLocal)load_builtins	load_yaml)r   )ParseTFbasicre2regexlegacynoneCACHESc              #   T   K   | ]   }|j                   j                         |f " y wN)__name__lower).0caches     </root/env/lib/python3.12/site-packages/ua_parser/__main__.py	<genexpr>r+   >   s*       ^^U#s   &(c                     g S r%    )_1_2s     r*   <lambda>r0   K   s    "     )
compare_toc                       y r%   r-   r-   r1   r*   r0   r0   N   s    r1   c                      t         S r%   )snapshotr-   r1   r*   r0   r0   O   s    h r1   )starttake_snapshotparsersregexesreturnc                     |rXt         st        j                  d       t        |      }d| v r,t        dt        j                         | j                  d       |S t               }|S )Nz/yaml loading unavailable, please install pyyamlr!   z@The legacy parser is incompatible with custom regexes, ignoring.)file)r   sysexitprintstderrremover   )r8   r9   ruless      r*   	get_rulesrC   S   s\    HHFG'"wRZZ NN8$ L Lr1   itemallc                     | dk(  r|sJ |S | j                  d      r'| j                  d      sJ | dd j                  d      S | gS )N*{}r   ,)
startswithendswithsplit)rD   rE   s     r*   
parse_itemrO   e   sQ    s{
s
		}}S!!!Abz$$vr1   argsc              #      K   t               }| j                  D ]N  }|j                  d      \  }fdt        |g d      D        D ]  }||vs|j	                  |       |  P y w)N:c           	   3      K   | ]a  }t         |   rt        t        t                    nd gD ]7  }|d k7  rt	        t
        t        d            ndgD ]  }||dk(  rd n||f  9 c yw)r"   Nr   )	CACHEABLErO   listr#   mapint)r(   ppccsscss       r*   r+   z#rules_to_parsers.<locals>.<genexpr>t   s      
6?mz!T&\2&
 8:fs3
1d 341#	
  27B/
/
/
s   A'A*r   )setselectorrN   rO   add)rP   seenr^   ptripletr[   r\   s        @@r*   rules_to_parsersrc   p   sq     5DMM 
..%1a
 $GH
 	G d"!	
s   AA-A-c                    t        t        t        j                  | j                              }t        |      }t        t        |            }t        | j                  j                   d| d| d||z  dd       t        t        |             }t        g |D ch c]  \  }}}|
 c}}| j                        }t        d |D              }|D ]  \  }}	}
dj                  t        t        t        d ||	dk7  xr |	|
f                  }t        || d	dd
       t!        ||	|
|      }t#        ||      }|dz  }|dz  t        |      z  }t        |dd|dd        y c c}}w )N: z lines, z	 unique (z.0%)c              3      K   | ]O  \  }}}t        j                  d t        |      z   t        |      z   |xr t        j                  |      z          Q yw)   N)mathceillenlog10)r(   ra   r[   r\   s       r*   r+   zrun_stdout.<locals>.<genexpr>   sG      CJ1a		!c!f*s1v%)<tzz!}=>s   AA-r"    Tendflushg    eA  z>5.2fzs (>4.0fzus/line))rU   rV   r=   internr<   rk   r]   r?   namerc   rC   r9   maxjoinstrfilter
get_parserrun)rP   linescountuniquesr8   ra   _rB   wr[   nru   parsertsecstpls                   r*   
run_stdoutr      s`   SZZ+,EJE#e*oG	TYY^^BugXgYi%PS?TTU
VW#D)*G3'2wq!Q23T\\BE NU 	A  
51axxCq!v+2C!Q.G!HIJqcT
T2Aq!U+3w$hU#eCE{(34
5 3s   E#c           
      @   t        t        t        j                  | j                              }t        |      dz  }t        t        |             }|st        j                  d       t        g |D ch c]  \  }}}|
 c}}| j                        }ddi}|j                  d |D               t        j                  t        j                  t        |      dt        j                        }|j                  |       |j!                  d 	       t#        j$                  |d
 	      }	i }
|d   d   dk(  r>t'        |	      \  }}|D ]+  \  }}}t)        ||d|      }t+        ||      |z  |
| d| <   - t-        d |D              s$t-        d |D              st-        d |D              rd|
d<   |j                  |
       y |	D ]S  \  }}t/        |
|      }|D ]+  \  }}}t)        ||||      }t+        ||      |z  || d| <   - |j                  |       U y c c}}w )Nrr   zNo parser selectedsizern   c              3   N   K   | ]  \  }}}| d | |dk(  r|n| d | f  yw)rm   r"   Nr-   )r(   ra   r[   r   s       r*   r+   zrun_csv.<locals>.<genexpr>   s<      ;B1aA3as!v+QaS!:6s   #%unix)dialectquotingc                     | d   S N   r-   r   s    r*   r0   zrun_csv.<locals>.<lambda>   s
    qt r1   )keyc                     | d   S r   r-   r   s    r*   r0   zrun_csv.<locals>.<lambda>   s
    qt r1   r   r   rm   c              3   .   K   | ]  \  }}}|d k(    yw)r!   Nr-   )r(   ra   r   s      r*   r+   zrun_csv.<locals>.<genexpr>   s     1gaAAM1   c              3   .   K   | ]  \  }}}|d k(    yw)r"   Nr-   )r(   r   r[   s      r*   r+   zrun_csv.<locals>.<genexpr>   s     2wq!QqF{2r   c              3   .   K   | ]  \  }}}|d k(    yw)r   Nr-   )r(   r   r\   s      r*   r+   zrun_csv.<locals>.<genexpr>   s     -'!QqAv-r   )r   )rU   rV   r=   rt   r<   rk   rc   r>   rC   r9   updatecsv
DictWriterstdoutQUOTE_MINIMALwriterowsort	itertoolsgroupbynextrz   r{   rE   dict)rP   r|   LENr8   ra   r   rB   columnsr   groupedzeroespsr   r)   	cachesizerows                   r*   run_csvr      s   SZZ+,E
e*t
C#D)*G%&3'2wq!Q23T\\BErlGNN FM  	

W!!		A JJwLL^L$^<G F qz!}w-B " 	?FE165!U3A*-a-3*>FfXQug&'	? 	1112'22-W--v	

6  	26	* " 	<FE165)U;A'*1e}';C6(!E7#$	< 	


3O 3s   2Hr   r)   r   rB   c                    | dk(  rt         S | dk(  rt        |      }n:| dk(  rt        |      }n)| dk(  rt        |      }nt	        j
                  d|        |t        vrt	        j
                  d|       t        j                  |      }|t              j                  S t        t         ||                  j                  S )Nr!   r   r   r    zunknown parser zunknown cache algorithm )r   r   Re2ResolverRegexResolverr=   r>   r#   getr   parser   )r   r)   r   rB   rr[   s         r*   rz   rz      s     	7	% 	5	7	% ?6*-.F+E956

5Ayay/!Qy\23999r1   r   r|   c                 v    t        j                         }|D ]
  } | |        t        j                         |z
  S r%   )timeperf_counter_ns)r   r|   r   lines       r*   r{   r{      s<     	A d!A%%r1   c                   L    e Zd Zdedee   fdZdedee   fdZ	dededdfd	Z
y)
Beladymaxsizedatac                 :   || _         i | _        t        j                         | _        i | _        t        |      D ]0  \  }}| j
                  j                  |g       j                  |       2 | j
                  j                         D ]  }|j                           y r%   )r   r)   collectionsdequequeue	distances	enumerate
setdefaultappendvaluesreverse)selfr   r   iefreqss         r*   __init__zBelady.__init__   s    /1
-8->->-@
/1dO 	7DAqNN%%a,33A6	7^^**, 	EMMO	r1   r   r:   c                 H   | j                   |   j                          | j                  j                  |      x}rg| j                  j                         d   |k(  sJ | j                   |   x}r't        j                  | j                  |d   |f       |S | j                  |= |S )Nr   rJ   )r   popr)   r   r   popleftbisectinsort)r   r   r[   dss       r*   __getitem__zBelady.__getitem__   s    s!

s##1# ::%%'*c111^^C((r(djj2b63-8
  JJsOr1   entryNc                 X   | j                   |   }|sy |d   }t        | j                        | j                  k\  r@|| j                  d   d   kD  ry | j                  j                         \  }}| j                  |= || j                  |<   t        j                  | j                  ||f       y )NrJ   r   )r   rk   r)   r   r   r   r   r   )r   r   r   r   next_distancer   ks          r*   __setitem__zBelady.__setitem__  s    ^^C 2tzz?dll* tzz"~a00::>>#DAq

1

3djj=#"67r1   )r&   
__module____qualname__rW   r	   rx   r   r
   r   r   r   r-   r1   r*   r   r      sK     49 s x'>  8s 8= 8T 8r1   r   c           
         t        t        j                  dd d d        G fdd      }t        t	        t
        j                  | j                              t              }t        t                    }t        |d|d       t                t        t        j                  t        | j                              dz         }dt        d	t         ffd
}t#        j$                          t'        j(                  t'        j*                  |gt-        d t.        j1                                     | j                        D ]'  \  }} |       }t3        j4                          t#        j6                         }	t9        t;        | ||                  }
D ]  }|
j=                  |        t3        j4                          t#        j6                         }||k(  rdj?                  d      }n7tA        d |jC                  |	d      D              }dj?                  |||z        }t        |jD                  jG                         dd|| dd||jH                  z
  |z  dz  dd|        ~~
* y )Nrn   )domainsstring
user_agentosdevicec                   .    e Zd ZddZdededef fdZy)run_hitrates.<locals>.Counterr:   Nc                     d| _         y )Nr   r}   )r   s    r*   r   z&run_hitrates.<locals>.Counter.__init__-  s	    DJr1   uar   c                2    | xj                   dz  c_         S )Nr   r   )r   r   r   r   s      r*   __call__z&run_hitrates.<locals>.Counter.__call__0  s    JJ!OJHr1   )r:   N)r&   r   r   r   rx   r   r   r   )r   s   r*   Counterr   ,  s"    		s 	V 	= 	r1   r   r|   r~   r   r   r:   c                     t        |       S r%   )r   )r   r|   s    r*   beladyzrun_hitrates.<locals>.belady;  s    gu%%r1   z{0:>14} {0:>12}rm   c              3   4   K   | ]  }|j                     y wr%   )	size_diff)r(   r\   s     r*   r+   zrun_hitrates.<locals>.<genexpr>N  s     U11;;Us   filenamez{:8} bytes ({:3.0f}b/entry)8(z): d   z2.0fz% hit rate )%r   r   ALLrU   rV   r=   rt   r<   rk   r]   r?   rW   ri   rl   rv   
cachesizesr   tracemallocr6   r   productchainry   r#   r   gccollectr7   r   r   r   formatsumr2   r&   r'   r}   )rP   r   totalr~   r   r   r)   
cache_sizemissesbeforer   r   afterdiffoverheadr|   r   s                  @@r*   run_hitratesr   #  s
   

	A  SZZ+,EJE#e*oG	%'9-	GDJJs4??+,q01A& & & &..&v}}"?@ z 


**,j0ABC 	DLL	


))+F?$++C0DU0@0@0TUUH077:%D 	~~##%a(*aS D)9efll>RV[=[^a=abf<ggrswrxy	
 F/r1   rr   r6   rp   c                 t    | j                          |D ]  }|j                  |        |j                          y r%   )waitr   )r6   r   r|   rp   r   s        r*   workerr   \  s3     
JJL R HHJr1   c                    t        t        t        j                  | j                              }t        t                     }dt        |t        j                  t                    fdt        |t        d             fdt        t                     fdt        t                     fg}|D ]I  \  }}t        |dddd	
       t        j                   d      }t#        j$                         }t#        j&                  | j(                  dz         }t+        |      }	t-        | j(                        D ]I  }
t#        j.                  t0        ||	|j3                  |t5        |            |fd	      j7                          K t9        j:                         }|j=                          |j?                          t5        |      | j(                  z  }t9        j:                         |z
  dz  }t        ||z  ddd	       L y )Nzlocking-lruz	local-lruc                  4    t        j                  t              S r%   )r   Lru	CACHESIZEr-   r1   r*   r0   zrun_threaded.<locals>.<lambda>o  s    7;;y;Q r1   r   r    11re   rn   Tro   *   r   )targetrP   daemonrr   rs   zus/line)rq   ) rU   rV   r=   rt   r<   r   r   r   r   r  r  r   r   r   r?   randomRandom	threadingEventBarrierthreadsr   rangeThreadr   samplerk   r6   r   r   r]   r   )rP   r|   r   	resolversru   resolverr   r6   rp   r   r   sttotlinesr   s                 r*   run_threadedr  j  s   SZZ+,E-/*E	w{{9/EFG	oeU3Q-RST	MO,-	-01	-I $ :hb	n"D1
 MM"!q 01!t||$ 	AVQXXeSZ%@#F eg	 !!#		
 u:,!!#b(D0Xe$G,D97:r1   zFor good results the sample `file` should be an actual
non-sorted non-deduplicated sample of user agent strings from traffic
on a comparable (or the actual) site or application targeted for
classification.	ua_parserepi)progepilog)func)add_helpr<   r   zutf-8)encodingzeSample user agent file, the file must contain a single user agent string per line, use `-` for stdin.)typehelpcommands)titlebenchz7benchmark various parser configurations on sample filesa]  Different sites and applications can have different
    traffic pattenrs, and thus want different setups and tradeoffs.
    This subcommand allows testing ua-parser's different base
    resolvers, caches, anc cache sizes in order to customise the
    parser to the application's requirements. It's also useful to
    bench the library itself though.)r  parentsr  descriptionz-Rz	--regexesrbai  Custom regexes.yaml file, if ommitted the benchmark will
    use the embedded regexes file rom uap-core. Custom regexes files
    can allow evaluating the performance impact of new rules or
    cut-down reference files (if legacy rules are nor relevant to your
    needs). Because YAML is (mostly) a superset of JSON, JSON regexes
    files will also work fine.c                   l    e Zd Z	 ddej                  dej
                  deeee   df   de	e   ddf
dZ
y)	ToFuncNr   	namespacer   option_stringr:   c                     |dk(  rt        || j                  t               y |dk(  rt        || j                  t               y t	        d|      )Nr   r   zinvalid output )setattrdestr   r   
ValueError)r   r   r&  r   r'  s        r*   r   zToFunc.__call__  sE     XItyy*5u_Ityy'2vj9::r1   r%   )r&   r   r   argparseArgumentParser	Namespacer   rx   r   r
   r   r-   r1   r*   r%  r%    s\     (,;''; %%; c8C=$./	;
  }; 
;r1   r%  z-Oz--outputr   r   r  a  By default (`stdout`) the result of each configuration /
    combination is printed to stdout with the combination name
    followed by the total parse time for the file and the per-entry
    average. `csv` will instead output a valid CSV table to stdout,
    with a parser combination per column and a cache size per row.
    Combinations without cache will have the same value on every row.
    If no combination uses a cache, the output will have a single row
    with a first cell of value 0.)choicesdefaultr*  actionr  r^   rG   z)*:*:{10,20,50,100,200,500,1000,2000,5000}zjA generative selector expression, composed of 3 parts: 1.
the parser (base), 2. the cache implementation (z, z)
and 3. the cache size. For parser and cache `*` is an alias for stands
in for "every value", a bracketed expression for an enumeration, and
the selector can be repeated to explicitly list each configuration )nargsr0  r  hitratesz=measure hitrates of cache configurations against sample files)r  r!  r  z--cachesizes+)	
      2   r      i  rr   i  i  zHList of cache sizes to test hitrates for, for each cache
    algorithm. )r2  r  r0  r  r	  zPestimate impact of concurrency and contention on different parser configurationsz-nz	--threads)r  r0  )cr,  r   r   r   r   ior   ri   r   r  r=   r	  r   typestypingr   r   r   r   r   r   r	   r
   r   r   r   r   rn   r   r   r   r   r   r   r   r   r   r   loadersr   r   r   r   ImportErrorr    r   user_agent_parserr   rT   r#   rx   rW   __annotations__r   r  S3FifoSiever   SimpleNamespacer5   IOBaserC   rU   rO   r.  tuplerc   r   r   rz   r{   r   r   r  r
  r  r   r  EPILOGr-  r   set_defaultsfpadd_argumentFileTypeadd_subparserssub
add_parserr   Actionr%  rw   r3  threaded	cpu_count
parse_argsrP   r  
print_helpr-   r1   r*   <module>rR     s;      
 	 	   	  
      	 	 	 " -	,	0 % 		 8>tnS(8SE5L1223 D   	XseUl#W[[1 	tCy 8BII+> 8 $S tCy4/ DI 8-- (5c3;O2P 5X'' 5D 544($$ 4 4n:::(+:4<:seSj:2&SE4K &C=& 	&-8 -8`3x)) 3d 3l 	?? C= 
			
 
$:x)) $:d $:N

 
!	 	 k%	@     Xe, 
			3	1
*   *-	BD( 	 	   			4	 
"	  
;X__ ;    u	
%      
8911561B0C DD
G	  	 >>	HD	   	  <  (   
	9
   >>	[D	   	  <  (   	BLLNa	   99IIdO
y  		  		0  $u$$$H (%''&K	s6   ;N5 O 3O 5N>=N>O
	O
/P ?P 