
    AHjK                        d Z ddl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
 ddlmZ  ej                  e      Zej!                  d	e
        ee	
      deez  dz  dedededef
d       Zdedz  dededededz  f
dZ ed
      dedefd       ZdedefdZdddedej0                  e   dej0                  e   dedededee   fdZdedededefd Z ee	
      d!ej6                  e   dej0                  e   d"eded#ed$ededz  fd%       Zd&ed'ededefd(Zdedededz  fd)Zd*eez  dz  d+edefd,Zd#eez  dz  defd-Z d$eez  dz  defd.Z!y)/z/
Filters for date parsing and date validators.
    N)Counter)datetime)	lru_cache   )
CACHE_SIZEMIN_DATE)	Extractorzminimum date setting: %s)maxsize
date_inputoutputformatearliestlatestreturnc           	         | yt        | t              r| }nN	 |dk(  r2t        t        | dd       t        | dd       t        | dd             }nt        j                  | |      }|j
                  |j
                  cxk  r|j
                  k  r:n n7|j                         |j                         cxk  r|j                         k  ry	 t        j                  d
|        y# t        $ r Y yw xY w)zEValidate a string w.r.t. the chosen outputformat and basic heuristicsNF%Y-%m-%d            
   Tzdate not valid: %s)	
isinstancer   intstrptime
ValueErroryear	timestampLOGGERdebug)r   r   r   r   
dateobjects        K/root/tools/cai/cai_env/lib/python3.12/site-packages/htmldate/validators.pyis_valid_dater!      s      *h'
		z)%
2A'Z!_)=s:aPRCS?T

 &..z<H
 	7FKK7 J$8$8$:Pf>N>N>PP Q LL%z2  		s   AC 	C+*C+c                     | 6t        | |||      r(	 t        j                  d|        | j                  |      S y# t        $ r!}t        j                  d| |       Y d}~yd}~ww xY w)z5Robust validation and conversion for plausible dates.Nzcustom parse result: %sz$value error during conversion: %s %s)r!   r   r   strftimer   error)r   r   r   r   errs        r    validate_and_convertr&   :   sp     -L(F#	RLL2J?&&|44   	RLL?SQQ	Rs   &: 	A$AA$   c                     t        ddddd      }	 |j                  |        d| vrt        j                  d	|        yy
# t        t        f$ r!}t        j                  d| |       Y d}~yd}~ww xY w)z*Validate the output format in the settingsi  	   r   r   z"wrong output format or type: %s %sNF%zmalformed output format: %sT)r   r#   	TypeErrorr   r   r$   )r   r   r%   s      r    is_valid_formatr,   L   ss     $1a+JL)
 ,2LA z" 9<Ms   > A.A))A.r   c                 (    | dk  r| | dk\  rdndz  } | S )z!Adapt year from YY to YYYY formatd   Z   il  i   )r   s    r    correct_yearr1   ]   s     cz
,K    F)
incomplete
htmlstringpatternyearpatr3   c                   t        |j                  |             }|j                  |j                  }}t        |      D ]}  }	|j	                  |	      }
|
t
        j                  d|	       ||	= 0|
d   }|st        |      }nt        t        |            }||cxk  r|k  rcn t
        j                  d|	       ||	=  |S )z5Filter the date patterns to find plausible years onlyznot a year pattern: %sr   zno potential year: %s)	r   findallr   listsearchr   r   r   r1   )r4   r5   r6   r   r   r3   occurrencesmin_yearmax_yearitem
year_match
lastdigitspotential_years                r    plausible_year_filterrB   d   s     '//*56K!hH[! "^^D)
LL148D!]
 _N)#j/:N>5X5LL0$7D!"" r2   	referenceattemptoptionsc                 ,   	 t        t        j                  ||j                        j	                               }|j                  r| rt        | |      } | S |} | S t        | |      } | S # t
        $ r#}t        j                  d||       | cY d}~S d}~ww xY w)z*Compare the date expression to a referencez-datetime.strptime exception: %s for string %sN)r   r   r   formatr   	Exceptionr   r   originalminmax)rC   rD   rE   r   r%   s        r    compare_valuesrL      s    ))'7>>BLLNO	 1:C	9-	  AJ	  	9-	  Dc7Ss   7A' '	B0BBB	bestmatchcopyearmin_datemax_datec                     | fdj                  | d   | d   | d   g      }t        |d||      r:|dk(  st        | d         |k\  r$t        j	                  d	||       t        |d|      S y)
z-Filter free text candidates in the YMD formatN-r         r   r   r   r   zdate found for pattern "%s": %s)joinr!   r   r   r   convert_date)rM   r5   rN   r   rO   rP   pagedates          r    filter_ymd_candidaterY      ss     88Yq\9Q<1FG:RqLC	!-8LL:GXN*lCCr2   
datestringinputformatc                     ||k(  r| S t        | t              r| j                  |      S t        j                  | |      }|j                  |      S )z.Parse date and return string in desired format)r   r   r#   r   )rZ   r[   r   r   s       r    rW   rW      sP     l"*h'""<00"":{;J|,,r2   c                     | dkD  r_t        j                  |       }|j                  |j                        }t	        ||j                  |j
                  |j                        r|S y)z4Test if the extracted reference date can be returnedr   rU   N)r   fromtimestampr#   rG   r!   rJ   rK   )rC   rE   r   	converteds       r    check_extracted_referencer`      sU    1}++I6
''7	w~~GKK
 r2   date_objectdefaultc                     t        | t              r| S t        | t              r	 t        j                  |       S |S # t        $ r t
        j                  d|        Y |S w xY w)zTCheck if the input is a usable datetime or ISO date string, return default otherwisezinvalid datetime string: %s)r   r   strfromisoformatr   r   warning)ra   rb   s     r    check_date_inputrg      sa    +x(+s#	G))+66 N  	GNN8+FN	Gs   ; AAc                 "    t        | t              S )zEValidates the minimum date and/or defaults to earliest plausible date)rg   r   )rO   s    r    get_min_dateri      s    Hh//r2   c                 h    t        j                         j                  dddd      }t        | |      S )a4  Validates the maximum date and/or defaults to the end of the current day.
    A day-granular default stays stable across calls (unlike datetime.now()),
    which lets the date-validation caches be reused from one document to the
    next in batch processing, and accepts dates published earlier the same day.   ;   i?B )hourminutesecondmicrosecond)r   nowreplacerg   )rP   end_of_todays     r    get_max_datert      s7    
 <<>))26 * L Hl33r2   )"__doc__loggingrecollectionsr   r   	functoolsr   settingsr   r   utilsr	   	getLogger__name__r   r   rd   boolr!   r&   r,   r   r1   PatternrB   rL   MatchrY   rW   r`   rg   ri   rt   r0   r2   r    <module>r      s    	    * 			8	$ ' 2 :"3%"" " 	"
 
" "J4  	
 	4Z$ 2# $   s s   ZZ_ ZZ_	
    S\Bc C )   :xx}ZZ_  	
   	4Z &
-S 
-s 
-# 
-# 
-	 	y 	S4Z 		(S.4"7 	( 	x 	08c>D0 0X 0
48c>D0 4X 4r2   