
    Hoh<                        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 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Z/ddiZ0ee1eee2ge#f      f   e3d<   e0ji                  d  eee2ge#f   e"jj                        e"jl                  e"jn                  fD               	 d dl8Z8dee1   deejv                     defdZ<de jz                  ddfdZ>de jz                  ddfdZ?de1de1de2dedee1gef   f
dZ@d ee1gdf   d!ee1   de2fd"ZA G d# d$      ZBde jz                  ddfd%ZCd&ZDd'ej                  ded!ee1   d(ej                  ddf
d)ZGde jz                  ddfd*ZHd+ZI e j                  d,d-.      ZKeKj                  d/        e j                  d
0      ZMeMj                  d1 e j                  d2d34      d56       eKj                  d78      ZQeQj                  d9d:eMgeId;<      ZSeSj                  d=d> e j                  d?      d@6        G dA dBe j                        ZUeSj                  dCdDdEdFge>dGeUdHI       eSj                  dJdKg dg ddLM       eSj                  dNdK eVe0       eVe0      dOM       eSj                  dPdKe2g dQdRS       eQj                  dTdUeMgeIV      ZWeWj                  eC/       eWj                  dPdKe2g dQdWS       eQj                  dXdYeMgeIV      ZXeXj                  eH/       eXj                  dZd[e2 ej                         xs d\       eKj                         Z[e[j                  re[j                  e[       yeKj                          y# e*$ r Y 9w xY w# e*$ r Y >w xY w# e*$ r.  ejr                  d       Z: ejr                  d d       Z8Y w xY w)]    N)AnyCallableDequeDictIterable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     n/var/www/html/wine-match-dev/backend/winematch-backend/venv/lib/python3.12/site-packages/ua_parser/__main__.py	<genexpr>r)   =   s*       ^^U#s   &(c                     g S r#    )_1_2s     r(   <lambda>r.   J   s    "     )
compare_toc                       y r#   r+   r+   r/   r(   r.   r.   M   s    r/   c                      t         S r#   )snapshotr+   r/   r(   r.   r.   N   s    h r/   )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   )r6   r7   ruless      r(   	get_rulesrA   R   s\    HHFG'"wRZZ NN8$ L Lr/   argsc                 p    t         j                        }t        |      }t        t        |            }t	         j                  j
                   d| d| d||z  dd       t         j                   j                        }t        j                  dt        t        t         j                              z   t        t        t         j                              z   t        t        t        j                   j                              z         } fd j                  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 )N: z lines, z	 unique (z.0%)   c              3      K   | ]O  }t         |   rj                  d gk7  rj                  ndgD ]"  }|dk7  rj                  nd gD ]	  }|||f  $ Q yw)r   r    N)	CACHEABLE
cachesizescaches)r&   pcnrB   s       r(   r)   zrun_stdout.<locals>.<genexpr>s   sr      !*1$//aS2H$++vh %&&[$//qc	  
Aq	s   AA-r     Tendflushg    eA  z>5.2fzs (>4.0fzus/line))listr:   lensetr=   namerA   basesr7   mathceilmaxmaprJ   log10rI   joinstrfilter
get_parserrun)rB   linescountuniquesr@   wrK   rL   rM   rX   tsecstpls   `            r(   
run_stdoutrk   d   s   OEJE#e*oG	TYY^^BugXgYi%PS?TTU
VWdjj$,,/E 				
c#tzz"
#	$
c#t{{#
$	% c$**doo.
/	0	A 51a xxCq!v+2C!Q.G!HIJqcT
T2q!Q&5M3w$hU#eCE{(345r/   c           
          t         j                        }t        |      dz  }t         j                   j
                        } j                  D cg c]=  }t        |   r j                  ndgD ]   }|dk7  r j                  ndgD ]  }|||f	 " ? }}}}|st        j                  d       ddi}|j                   fd j                  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| <   -  j                  dgk(  s  j                  dgk(  s j                  dgk(  rd|d<   |	j!                  |       y |
D ]S  \  }}t/        ||      }|D ]+  \  }}}t+        ||||      }t-        ||      |z  || d| <   - |	j!                  |       U y c c}}}w )NrS   r    r   zNo parser selectedsizerO   c              3      K   | ]6  }t         |   rj                  nd gD ]  }| d| |d k(  r|n| d| f  8 yw)r    rN   N)rH   rJ   )r&   rK   rL   rB   s      r(   r)   zrun_csv.<locals>.<genexpr>   s]      !*1$++F8  3as!v+QaS!:66s   <?unix)dialectquotingc                     | d   S N   r+   rh   s    r(   r.   zrun_csv.<locals>.<lambda>   s
    qt r/   )keyc                     | d   S rs   r+   ru   s    r(   r.   zrun_csv.<locals>.<lambda>   s
    qt r/   rt   rN   r   )rm   )rU   r:   rV   rA   rY   r7   rH   rJ   rI   r;   r<   updatecsv
DictWriterstdoutQUOTE_MINIMALwriterowsort	itertoolsgroupbynextrb   rc   dict)rB   rd   LENr@   rK   rL   rM   r6   columnsrg   groupedzeroes_psparserr'   	cachesizerows   `                 r(   run_csvr      s^   OE
e*t
Cdjj$,,/E  !*1$++F8 %&&[$//qc	  
Aq	G  %&rlGNN  
 	

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

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


3[s   AH?r   r'   r   r@   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   r@   rrL   s         r(   rb   rb      s     	7	% 	5	7	% ?6*-.F+E956

5Ayay/!Qy\23999r/   r   rd   c                 v    t        j                         }|D ]
  } | |        t        j                         |z
  S r#   )timeperf_counter_ns)r   rd   rh   lines       r(   rc   rc      s<     	A d!A%%r/   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	r/   rv   r8   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   )r   popr'   r   r   popleftbisectinsort)r   rv   rL   dss       r(   __getitem__zBelady.__getitem__   s    s!

s##1# ::%%'*c111^^C((r(djj2b63-8
  JJsOr/   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 )Nr   r   )r   rV   r'   r   r   r   r   r   )r   rv   r   r   next_distancer   ks          r(   __setitem__zBelady.__setitem__   s    ^^C 2tzz?dll* tzz"~a00::>>#DAq

1

3djj=#"67r/   )r$   
__module____qualname__intr   r`   r   r	   r   r   r   r+   r/   r(   r   r      sK     49 s x'>  8s 8= 8T 8r/   r   c           
         t        t        j                  dd d d        G fdd      }t        | 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(        j+                                     | j                        D ]'  \  }} |       }t-        j.                          t        j0                         }	t3        t5        | ||                  }
D ]  }|
j7                  |        t-        j.                          t        j0                         }||k(  rdj9                  d      }n7t;        d |j=                  |	d      D              }dj9                  |||z        }t        |j>                  jA                         dd|| dd||jB                  z
  |z  dz  dd|        ~~
* y )NrO   )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>.Counterr8   Nc                     d| _         y )Nr   re   )r   s    r(   r   z&run_hitrates.<locals>.Counter.__init__  s	    DJr/   uar   c                2    | xj                   dz  c_         S )Nr   r   )r   r   r   r   s      r(   __call__z&run_hitrates.<locals>.Counter.__call__  s    JJ!OJHr/   )r8   N)r$   r   r   r   r`   r   r   r   )r   s   r(   Counterr     s"    		s 	V 	= 	r/   r   rd   rf   r   r   r8   c                     t        |       S r#   )r   )r   rd   s    r(   beladyzrun_hitrates.<locals>.belady*  s    gu%%r/   z{0:>14} {0:>12}rN   c              3   4   K   | ]  }|j                     y wr#   )	size_diff)r&   ss     r(   r)   zrun_hitrates.<locals>.<genexpr>=  s     U11;;Us   filenamez{:8} bytes ({:3.0f}b/entry)8(z): d   z2.0fz% hit rate )"r   r   ALLrU   r:   rV   rW   r=   r   rZ   r^   r\   rI   r   tracemallocr4   r   productchainra   r!   r   gccollectr5   r   r   r   formatsumr0   r$   r%   re   )rB   r   totalrf   rg   r   r'   
cache_sizemissesbeforer   r   afterdiffoverheadrd   r   s                  @@r(   run_hitratesr     s   

	A  OE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/r/   rS   r4   rQ   c                 t    | j                          |D ]  }|j                  |        |j                          y r#   )waitr   )r4   r   rd   rQ   r   s        r(   workerr   K  s3     
JJL R HHJr/   c                    t        | 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(                  t*        ||	|j-                  |t/        |            |fd	      j1                          K t3        j4                         }|j7                          |j9                          t/        |      | j"                  z  }t3        j4                         |z
  dz  }t        ||z  ddd	       L y )Nzlocking-lruz	local-lruc                  4    t        j                  t              S r#   )r   Lru	CACHESIZEr+   r/   r(   r.   zrun_threaded.<locals>.<lambda>^  s    7;;y;Q r/   r   r   11rD   rO   TrP   *   r   )targetrB   daemonrS   rT   zus/line)rR   )rU   r:   r   r   r   r   r   r   r   r   r   r=   randomRandom	threadingEventBarrierthreadsr   rangeThreadr   samplerV   r4   r   r   rW   r   )rB   rd   r   	resolversrX   resolverr   r4   rQ   r   r   sttotlinesrh   s                 r(   run_threadedr   Y  s   OE-/*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:r/   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_stringr8   c                     |dk(  rt        || j                  t               y |dk(  rt        || j                  t               y t	        d|      )Nr{   ry   zinvalid output )setattrdestrk   r   
ValueError)r   r   r  r   r  s        r(   r   zToFunc.__call__  sE     XItyy*5u_Ityy'2vj9::r/   r#   )r$   r   r   argparseArgumentParser	Namespacer   r`   r
   r	   r   r+   r/   r(   r
  r
    s\     (,;''; %%; c8C=$./	;
  }; 
;r/   r
  z-Oz--outputr{   ry   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  z--bases+a  Base resolvers to benchmark. `basic` is a linear search
    through the regexes file, `re2` is a prefiltered regex set
    implemented in C++, `regex` is a prefiltered regex set implemented
    in Rust, `legacy` is the legacy API (essentially a basic resolver
    with a clearing cache of fixed 200 entries, but less layered so
    usually slightly faster than an equivalent basic-based resolver).)nargsr  r  r  z--cacheszCache implementations to test. `clearing` completely
    clears the cache when full, `lru` uses a least-recently-eviction
    policy. `lru` is not thread-safe, so `lru-threadsafe` adds a mutex
    and measures *uncontended* locking overhead.z--cachesizes)	
      2   r      i  rS   i  i  a  Caches are a classic way to trade memory for performances.
    Different base resolvers and traffic patterns have different
    benefits from caches, this option allows testing the benefits of
    various cache sizes (and thus amounts of memory used) on the cache
    strategies. )r  r  r  r  hitratesz=measure hitrates of cache configurations against sample files)r  r  r   zHList of cache sizes to test hitrates for, for each cache
    algorithm. r   zPestimate impact of concurrency and contention on different parser configurationsz-nz	--threads)r  r  )^r  r   r   ry   r   ior   rZ   r   r   r;   r   r   typestypingr   r   r   r   r   r   r	   r
   r   r   r   rO   r   r   r   r   r   r   r   r   r   r   loadersr   r   r   r   ImportErrorr   r   user_agent_parserr   rH   r!   r`   r   __annotations__rx   r   S3FifoSiever   SimpleNamespacer3   IOBaserA   r  rk   r   rb   rc   r   r   r   r   r   r   r   EPILOGr  r   set_defaultsfpadd_argumentFileTypeadd_subparserssub
add_parserr  Actionr
  rU   r  threaded	cpu_count
parse_argsrB   r   
print_helpr+   r/   r(   <module>r6     s%      
 	 	   	  
      	 	 	 " -	,	0 % 		 8>tnS(8SE5L1223 D   	XseUl#W[[1 	tCy 8BII+> 8 $5X'' 5D 5B7($$ 7 7t:::(+: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	
%      
//
I     
LL
4  	   
	9
  
 >>	HD	   	  <  (   
	9
   >>	[D	   	  <  (   	BLLNa	   99IIdO
G  		  		0  $u$$$H (%''&K	s6   9N0  N< 1O 0N98N9<OO/O;:O;