
    \jP                         d dl Z d dlZd dlZdZ	 d dlZdZdZ	 d dlZd dlZd dl	ZdZd dl
mZ d dlmZ d dlmZ d dlmZmZ d Z G d	 d
e      Zy# e$ r Y Aw xY w# e$ r Y ;w xY w)    NFT)RepeatingTask)log)UpdateProcessor)FEATURESSEGMENTSc                     d| vrd| d<   y y )Nversion    )items    [/root/env/lib/python3.12/site-packages/ldclient/impl/integrations/files/file_data_source.py_sanitize_json_itemr      s    Y     c                   p    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Z G d d      Z G d d      Zy)_FileDataSourcec                     || _         || _        d| _        || _        t	        | j                  t
              r| j                  g| _        || _        d | _        || _        || _	        y )NF)
_store_ready_inited_paths
isinstancestr_auto_update_auto_updater_poll_interval_force_polling)selfstorereadypathsauto_updatepoll_intervalforce_pollings          r   __init__z_FileDataSource.__init__   sY    dkk3' KK/DK'!++r   c                     | j                          | j                  r| j                         | _        | j                  j                          y N)	_load_allr   _start_auto_updaterr   r   setr   s    r   startz_FileDataSource.start+   s6    !%!9!9!;D 	r   c                 R    | j                   r| j                   j                          y y r&   )r   stopr*   s    r   r-   z_FileDataSource.stop5   s"    ##% r   c                     | j                   S r&   )r   r*   s    r   initializedz_FileDataSource.initialized9   s    ||r   c           
         t         i t        i i}| j                  D ]  }	 | j                  ||        	 | j                  j                  |       d| _        y # t        $ rC}t        j                  d|dt        |             t        j                          Y d }~ y d }~ww xY w# t        $ r?}t        j                  dt        |      z         t        j                          Y d }~y d }~ww xY w)NzUnable to load flag data from "z": TzUnable to store data: %s)r   r   r   
_load_file	Exceptionr   errorrepr	traceback	print_excr   initr   )r   all_datapathes       r   r'   z_FileDataSource._load_all<   s    r8R1KK 	Dh/		"KKX&DL  		dDQRGTU##%  	"II047:;!!	"s.   A"B& 	B# 8BB#&	C./5C))C.c           	      R   d }t        |d      5 }|j                         }d d d        | j                  |      }|j                  di       j	                         D ]'  \  }}t        |       | j                  |t        |       ) |j                  di       j	                         D ],  \  }}| j                  |t        | j                  ||             . |j                  di       j	                         D ]'  \  }}	t        |	       | j                  |t        |	       ) y # 1 sw Y   xY w)Nrflags
flagValuessegments)
openread_parse_contentgetitemsr   	_add_itemr   _make_flag_with_valuer   )
r   r9   r8   contentfparsedkeyflagvaluesegments
             r   r1   z_FileDataSource._load_fileL   s   $_ 	ffhG	$$W-GR0668 	5IC%NN8Xt4	5 !**\26<<> 	WJCNN8Xt/I/I#u/UV	W"JJz26<<> 	8LC(NN8Xw7	8	 	s   DD&c                 b    t         rt        j                  |      S t        j                  |      S r&   )	have_yamlyaml	safe_loadjsonloads)r   rG   s     r   rB   z_FileDataSource._parse_contentZ   s#    >>'**zz'""r   c                     ||   }|j                  d      }|j                  |      |||<   y t        d|j                  d|d      )NrJ   zIn z, key "z" was used more than once)rC   r2   	namespace)r   r8   kindr   rD   rJ   s         r   rE   z_FileDataSource._add_item_   sD    hhuo99S>!E#JY\]^^r   c                     |ddddi|gdS )Nr
   T	variationr   )rJ   r	   onfallthrough
variationsr   )r   rJ   rL   s      r   rF   z%_FileDataSource._make_flag_with_valueg   s&    Q ")
 	
r   c                 ~   g }| j                   D ]1  }	 |j                  t        j                  j	                  |             3 t        r,| j                  s t        j                  || j                        S t        j                  || j                  | j                        S #  t        j                  d|z         Y xY w)NzHCannot watch for changes to data file "%s" because it is an invalid path)r   appendosr9   realpathr   warninghave_watchdogr   r   WatchdogAutoUpdaterr'   PollingAutoUpdaterr   )r   resolved_pathsr9   s      r   r(   z#_FileDataSource._start_auto_updaterr   s    KK 	oDo%%bgg&6&6t&<=	o
 !4!4"66~t~~VV"55ndnnVZViVijjofimmns   .B  B<c                       e Zd Zd Zd Zy)#_FileDataSource.WatchdogAutoUpdaterc                    t        |       G fddt        j                  j                        }t               }|D ]0  }|j	                  t
        j                  j                  |             2 t        j                  j                         | _
         |       }|D ]  }| j                  j                  ||         | j                  j                          y )Nc                       e Zd Z fdZy)G_FileDataSource.WatchdogAutoUpdater.__init__.<locals>.LDWatchdogHandlerc                 2    |j                   v r         y y r&   )src_path)r   eventreloaderwatched_filess     r   on_any_eventzT_FileDataSource.WatchdogAutoUpdater.__init__.<locals>.LDWatchdogHandler.on_any_event   s    ~~6 
 7r   N)__name__
__module____qualname__ro   )rm   rn   s   r   LDWatchdogHandlerri      s    #r   rs   )r)   watchdogeventsFileSystemEventHandleraddr^   r9   dirname	observersObserver	_observerscheduler+   )r   rd   rm   rs   	dir_pathsr9   handlerrn   s     `    @r   r$   z,_FileDataSource.WatchdogAutoUpdater.__init__   s    /M#HOO$J$J #
 I& 5bggood345 &//88:DN')G! 7''67NN  "r   c                 l    | j                   j                          | j                   j                          y r&   )r{   r-   joinr*   s    r   r-   z(_FileDataSource.WatchdogAutoUpdater.stop   s"    NN!NN!r   N)rp   rq   rr   r$   r-   r   r   r   rb   rf      s    	#$	"r   rb   c                   $    e Zd Zd Zd Zd Zd Zy)"_FileDataSource.PollingAutoUpdaterc                     || _         || _        | j                         | _        t	        ||| j
                        | _        | j                  j                          y r&   )r   	_reloader_check_file_times_file_timesr   _poll_timerr+   )r   rd   rm   intervals       r   r$   z+_FileDataSource.PollingAutoUpdater.__init__   sE    (DK%DN#557D'(DJJGDKKKr   c                 8    | j                   j                          y r&   )r   r-   r*   s    r   r-   z'_FileDataSource.PollingAutoUpdater.stop   s    KKr   c                     | j                         }d}| j                  j                         D ]/  \  }}|j                  |      |j                  |      |k7  s-d} n || _        |r| j	                          y y )NFT)r   r   rD   rC   r   )r   	new_timeschanged	file_path	file_times        r   r   z(_FileDataSource.PollingAutoUpdater._poll   s|    ..0IG(,(8(8(>(>(@ $	9==+7IMM)<TXa<a"G  )D  r   c                     i }| j                   D ]%  }	 t        j                  j                  |      ||<   ' |S #  d ||<   Y 3xY wr&   )r   r^   r9   getmtime)r   retr9   s      r   r   z4_FileDataSource.PollingAutoUpdater._check_file_times   sN    C %% " 0 0 6CI%
 J% $CIs	   "9AN)rp   rq   rr   r$   r-   r   r   r   r   r   rc   r      s    	 			!	r   rc   N)rp   rq   rr   r$   r+   r-   r/   r'   r1   rB   rE   rF   r(   rb   rc   r   r   r   r   r      sJ    
,&" 8#
_	

k" "2 r   r   )rR   r^   r5   rO   rP   ImportErrorra   rt   watchdog.eventswatchdog.observersldclient.impl.repeating_taskr   ldclient.impl.utilr   ldclient.interfacesr   ldclient.versioned_data_kindr   r   r   r   r   r   r   <module>r      s     	 		I 	M 7 " / ;Xo X+  		  		s"   A A AAA#"A#