
    W|h                         S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKr\" \ S\	5      r
\" \ S\5      r\R                  S:  a  \r S SKrSr S SKrS SKrSr \R                  S:  a	  S SKJrJr  OS SKJrJr  SrSr\(       a  \R6                  R9                  S5      r\R<                  " \5      r\R@                  r!\RD                  r#\RH                  " 5       S    S	:X  a  \R@                  r# \RJ                  r&\RD                  \&l'        \RD                  \!\!/\&l(        \(       a  S
 r+\+rCCSr,Sr-Sr.Sr/S r00 r1Sr2\Rf                  " \25      r4Sr5\Rf                  " \55      r6Sr7\Rf                  " \75      r8Sr9\Rf                  " \95      r:Sr;\Rf                  " \;5      r<Sr=\Rf                  " \=5      r>\?" SSSSSSSSS.5      r@\?" \@R                  5        V Vs/ s H  u  pX/PM
     snn 5      rB\@R                  5       rDS =qE=qFqGSSSSSS S!.rHS" rIS# rJ " S$ S%\K5      rL " S& S'\L5      rM " S( S)\L5      rN " S* S+\L5      rO " S, S-\L5      rP " S. S/\L5      rQ " S0 S1\L5      rRS2 rSS3 rTS4 rUS5 rVSJS6 jrWSKS7 jrXS8 rYS9 rZ  SLS: jr[SJS; jr\S< r]SKS= jr^SJS> jr_SJS? jr`SJS@ jraSA rbSB rcSMSC jrdSKSD jreSE rfSMSF jrgSMSG jrhSNSH jriSNSI jrjg! \ a    Sr GNf = f! \ a    Sr GNf = f! \ a    Sr GNvf = f! \) aF     \RT                  r&\RD                  \&l'        \RD                  \#\#/\&l(         GN! \) a    Sr  GNf = ff = fs  snn f )O    Nizipifilter)   r   TF)posix_fadvisePOSIX_FADV_RANDOMcFreeBSDc                 \    [        U R                  5       X5      nUS:w  a  [        US5      eg )Nr   	fallocate)
_fallocatefilenoIOError)fdoffsetlen_ress       A/home/james-whalen/.local/lib/python3.13/site-packages/whisper.py_py_fallocater   Z   s-    ryy{F1c	c;'' 
    z!Lz!fz!dz!Ldz!2LfLz!3Laveragesumlastmaxminavg_zeroabsmaxabsmin)      r                  c                      g N )aks     r   <lambda>r*      s    r   r   <   i  iQ i:	 i3secondsminuteshoursdaysweeksyearsc                 d    S H  nUR                  U 5      (       d  M  Us  $    [        SU -  5      e)Nr,   zInvalid unit '%s')
startswith
ValueError)svalues     r   getUnitStringr8      s6    Hel I 	&*++r   c                     U R                  5       R                  SS5      u  pUR                  5       (       a  [	        U5      [
        [        S5         -  nOy[        R                  " S5      nUR                  U5      nU(       a=  [	        UR                  S5      5      [
        [        UR                  S5      5         -  nO[        SU-  5      eUR                  5       (       a  [	        U5      nX4$ [        R                  " S5      nUR                  U5      nU(       aB  [	        UR                  S5      5      [
        [        UR                  S5      5         -  U-  nX4$ [        SU-  5      e! [         a    [        SU -  5      ef = f)	N:r   z!Invalid retention definition '%s'r6   z^(\d+)([a-z]+)$r   z$Invalid precision specification '%s'z$Invalid retention specification '%s')stripsplitr5   isdigitintUnitMultipliersr8   recompilematchgroup)retentionDef	precisionpointsprecision_rerB   	points_res         r   parseRetentionDefrI      s`   I&,,.44S!<Y Is1C!DDI::01Ly)Eekk!n%ekkRSn8U(VVi=	IJJ^^[F 	 

-.IOOF#E5;;q>"_]5;;q>5R%SSW``f 	 =FGG+ 
 I
8<G
HHIs   "E2 2Fc                       \ rS rSrSrSrg)WhisperException   z"Base class for whisper exceptions.r'   N__name__
__module____qualname____firstlineno____doc____static_attributes__r'   r   r   rK   rK      s    *r   rK   c                       \ rS rSrSrSrg)InvalidConfiguration   zInvalid configuration.r'   NrM   r'   r   r   rU   rU          r   rU   c                       \ rS rSrSrSrg)InvalidAggregationMethod   zInvalid aggregation method.r'   NrM   r'   r   r   rY   rY      s    #r   rY   c                       \ rS rSrSrSrg)InvalidTimeInterval   zInvalid time interval.r'   NrM   r'   r   r   r\   r\      rW   r   r\   c                       \ rS rSrSrSrg)InvalidXFilesFactor   zInvalid xFilesFactor.r'   NrM   r'   r   r   r_   r_      s    r   r_   c                       \ rS rSrSrSrg)TimestampNotCovered   7Timestamp not covered by any archives in this database.r'   NrM   r'   r   r   rb   rb      s    ?r   rb   c                   &    \ rS rSrS rS rS rSrg)CorruptWhisperFile   c                 F    [         R                  X5        Xl        X l        g r&   )	Exception__init__errorpath)selfrk   rl   s      r   rj   CorruptWhisperFile.__init__   s    t#JIr   c                 @    SU R                   < SU R                  < S3$ )Nz<CorruptWhisperFile[z] >)rl   rk   rm   s    r   __repr__CorruptWhisperFile.__repr__   s    ,0IItzzBBr   c                 >    U R                   < SU R                  < S3$ )Nz ()rk   rl   rq   s    r   __str__CorruptWhisperFile.__str__   s    

DII..r   rv   N)rN   rO   rP   rQ   rj   rr   rw   rS   r'   r   r   rf   rf      s    
C/r   rf   c                  2     [         qg! [         a     gf = f)z(Disable writing IO statistics to stdout N)_openopen	NameErrorr'   r   r   disableDebugr}      s    	D	 		s   	 
c                  R   ^  [         q " S S[        5      q S q0 m U 4S jqU 4S jqg)z'Enable writing IO statistics to stdout c                   8    \ rS rSrS rS rS rS rS rS r	Sr
g	)
r{      c                 @    [        U0 UD6U l        SU l        SU l        g Nr   )rz   f
writeCount	readCount)rm   argskwargss      r   rj   open.__init__   s!    d%f%dfdodnr   c                     U $ r&   r'   rq   s    r   	__enter__open.__enter__   s    kr   c                 8    U R                   R                  5         g r&   )r   close)rm   r   s     r   __exit__open.__exit__   s    
ffllnr   c                     U =R                   S-  sl         [        S[        U5      U R                   4-  5        U R                  R	                  U5      $ )Nr   zWRITE %d bytes #%d)r   debuglenr   write)rm   datas     r   r   
open.write   s>    
ooo CIt#??@VV\\$r   c                     U =R                   S-  sl         [        SXR                   4-  5        U R                  R                  U5      $ )Nr   zREAD %d bytes #%d)r   r   r   read)rm   sizes     r   r   	open.read   s8    
nnn4"889VV[[r   c                 .    [        U R                  U5      $ r&   )getattrr   )rm   attrs     r   __getattr__open.__getattr__  s    TVVT""r   )r   r   r   N)rN   rO   rP   rQ   rj   r   r   r   r   r   rS   r'   r   r   r{   r{      s     
 

#r   r{   c                      [        SU -  5        g )NzDEBUG :: %s)print)messages    r   r   r     s    	-'
!"r   c                 6   > [         R                   " 5       TU '   g r&   )timename__timingBlockss    r   
startBlockr     s    99;N4r   c                 p   > [        SU [        R                  " 5       TR                  U 5      -
  4-  5        g )Nz%s took %.5f seconds)r   r   popr   s    r   endBlockr     s,    	
 D$))+8J8J48P*P#Q
QRr   N)r{   rz   objectr   r   r   )r   s   @r   enableDebugr      s-     %#V #2# .'Sr   c                    [         (       a(  [        R                  U R                  5      nU(       a  U$ U R	                  5       nU R                  S5        U R                  [        5      n [        R                  " [        U5      u  pEpg [        U     SUs=::  a  S::  d  O  [        SU R                  5      e/ n[#        U5       HY  n	U R                  [$        5      n
 [        R                  " [&        U
5      u  pnUUUX-  U[(        -  S.nUR+                  U5        M[     U R                  U5        [        R                  US5      UUUS.n[         (       a  U[        U R                  '   U$ ! [        R                  [        [        4 a    [        SU R                  5      ef = f! [          a    [        SU R                  5      ef = f! [        R                  [        [        4 a    [        SU	-  U R                  5      ef = f)Nr   zUnable to read headerr   z!Unable to read archive%d metadata)r   secondsPerPointrF   	retentionr   r   )aggregationMethodmaxRetentionxFilesFactorarchives)CACHE_HEADERS__headerCachegetr   tellseekr   metadataSizestructunpackmetadataFormatrk   r5   	TypeErrorrf   aggregationTypeToMethodKeyErrorxrangearchiveInfoSizearchiveInfoFormat	pointSizeappend)fhinfooriginalOffsetpackedMetadataaggregationTyper   xffarchiveCountr   ipackedArchiveInfor   r   rF   archiveInfos                  r   __readHeaderr     s   ]RWW%Dk779.''!*77<(.?
--
7 7_C
?O, 
cQ
4bgg
>>(,a0Q*0--8IK\*]'v
 ("+y K OOK     ''.044_iP 	
$ ]!M"''	+O ,,
I	. ?
4bgg
>>?
 
 ?
4bgg
>>? LL*i0 QBQFPPQs$   +E. 
	F' G.6F$'!G9Hc                     [        XS9u  p#U$ )zSets the xFilesFactor for file in path

path is a string pointing to a whisper file
xFilesFactor is a float between 0 and 1

returns the old xFilesFactor
)r   __setAggregation)rl   r   _old_xffs       r   setXFilesFactorr   K  s     "$B,1	.r   c                 "    [        XU5      u  p4U$ )a  Sets the aggregationMethod for file in path

path is a string pointing to the whisper file
aggregationMethod specifies the method to use when propagating data (see
``whisper.aggregationMethods``)
xFilesFactor specifies the fraction of data points in a propagation interval
that must have known values for a propagation to occur. If None, the
existing xFilesFactor in path will not be changed

returns the old aggregationMethod
r   )rl   r   r   old_agmr   s        r   setAggregationMethodr   Y  s     "$<H,7	.r   c                 6   [        U S[        5       n[        (       a3  [        R                  " UR                  5       [        R                  5        [        U5      nUc  US   nUc  US   n[        X1US   U[        US   5      5        [        (       a4  UR                  5         [        R                  " UR                  5       5        [        (       a%  UR                  [         ;   a  [         UR                  	 SSS5        WS   US   4$ ! , (       d  f       N= f)z:Set aggregationMethod and or xFilesFactor for file in pathr+bNr   r   r   r   )r{   	BUFFERINGLOCKfcntlflockr   LOCK_EXr   __writeHeaderMetadatar   	AUTOFLUSHflushosfsyncr   r   r   )rl   r   r   r   r   s        r   r   r   k  s     D%#rtkk"))+u}}-D.)l 23"n1E&D,<(=? yhhjhhryy{}M1

 ) $, "
#T.%9	::- $#s   C&D


Dc                     [         U   n [        U5      nUS:  d  US:  a  [        SU-  5      e[        R                  " [        U5      n[        R                  " [        U5      n[        R                  " [        U5      n[        R                  " [        U5      nXR-   U-   U-   nU R                  S5        U R                  U5        g! [         a    [        SU-  5      ef = f! [         a    [        SU-  5      ef = f)zWrites header metadata to fh z#Unrecognized aggregation method: %sz$Invalid xFilesFactor %s, not a floatr   r   z,Invalid xFilesFactor %s, not between 0 and 1N)aggregationMethodToTyper   rY   floatr5   r_   r   pack
longFormatfloatFormatr   r   )r   r   r   r   r   r   r   s          r   r   r     s   6-.?@O
,&L
 A)
L*+ , , KK
O</Z6,[,7,Z6,"1L@<O.''!*((>- 
 6
"#H#4$5 6 66 
 ,
D*+ , ,,s   	C
 C& 
C#&C?c           	         U (       d  [        S5      eU R                  S S9  [        U 5       H  u  pU[        U 5      S-
  :X  a    g
XS-      nUS   US   :  d  [        SXUS-   U4-  5      eUS   US   -  S:w  a  [        SXS   US-   US   4-  5      eUS   US   -  nUS   US   -  nXT:  d  [        SXUS-   U4-  5      eUS   nUS   US   -  nXg:  a  M  [        S	US-   XqU4-  5      e   g
)a  Validates an archiveList.
An ArchiveList must:
1. Have at least one archive config. Example: (60, 86400)
2. No archive may be a duplicate of another.
3. Higher precision archives' precision must evenly divide all lower
   precision archives' precision.
4. Lower precision archives must cover larger time intervals than higher
   precision archives.
5. Each archive must have at least enough points to consolidate to the next
   archive

Returns True or False
z4You must specify at least one archive configuration!c                     U S   $ r   r'   )r(   s    r   r*   %validateArchiveList.<locals>.<lambda>  s    1r   keyr   r   zsA Whisper database may not be configured having two archives with the same precision (archive%d: %s, archive%d: %s)z~Higher precision archives' precision must evenly divide all lower precision archives' precision (archive%d: %s, archive%d: %s)zLower precision archives must cover larger time intervals than higher precision archives (archive%d: %s seconds, archive%d: %s seconds)zEach archive must have at least enough points to consolidate to the next archive (archive%d consolidates %d of archive%d's points but it has only %d total points)N)rU   sort	enumerater   )archiveListr   archivenextArchiver   nextRetentionarchivePointspointsPerConsolidations           r   validateArchiveListr     sq    

U
VV~&k*jaCq  !e$K1:A& 	N	
QUK(	)* *
 1~
"a' 	) 
AJA{1~.	/0 0 
WQZ'IN[^3M$ 	9 
q1um,	-. . AJM(^wqz92 	> 
Q&=9	:; ;? +r   c           
      (   Uc  SnUc  Sn[        U5        [        R                  R                  U 5      (       a  [	        SU -  5      e[        U S[        5       n [        (       a3  [        R                  " UR                  5       [        R                  5        [        (       a+  [        (       a   [        UR                  5       SS[        5        [!        U VVs/ s H	  u  pxXx-  PM     snn5      n	[#        XcX[%        U5      5        [&        [(        [%        U5      -  -   n
U
nU H=  u  px[*        R,                  " [.        XU5      nUR1                  U5        X[2        -  -  nM?     [4        (       a  U(       a  X-
  n[7        XjU5        OmU(       a&  UR9                  US-
  5        UR1                  S5        O@X-
  nS	nSU-  nX:  a  UR1                  U5        X-  nX:  a  M  UR1                  USU 5        [:        (       a4  UR=                  5         [        R>                  " UR                  5       5        URA                  5          SSS5        gs  snnf ! [B         a"    [        RD                  " URF                  5        e f = f! , (       d  f       g= f)
a  create(path,archiveList,xFilesFactor=0.5,aggregationMethod='average')

path               is a string
archiveList        is a list of archives, each of which is of the form
                   (secondsPerPoint, numberOfPoints)
xFilesFactor       specifies the fraction of data points in a propagation interval
                   that must have known values for a propagation to occur
aggregationMethod  specifies the function to use when propagating data (see
                   ``whisper.aggregationMethods``)
Ng      ?r   zFile %s already exists!wbr   r       i @  )$r   r   rl   existsrU   r{   r   r   r   r   r   r   CAN_FADVISEFADVISE_RANDOMr   r   r   r   r   r   r   r   r   r   r   r   CAN_FALLOCATEr   r   r   r   r   r   r   unlinkr   )rl   r   r   r   sparseuseFallocater   r   rF   oldest
headerSizearchiveOffsetPointerr   	remaining	chunksizezeroess                   r   creater	    s    L! k" WW^^D
84?
@@D$	"b-	BIIK/	biik1a):;KXK1HO,KXYfB6,.  ?S5E#EFj'%0
!/kk"35I\bc
)!34 &1 
<(5	"),
$q()
(5		9$#
((6


 ) # 	
#$	


hhjS #" YF   	ii	U #"s9   JA>II)C6I!A#II,J  J
Jc                    U S:X  a)  [        [        U5      5      [        [        U5      5      -  $ U S:X  a  [        [        U5      5      $ U S:X  a  US   $ U S:X  a  [        U5      $ U S:X  a  [	        U5      $ U S:X  aU  U(       d  [        S5      eU Vs/ s H  o3=(       d    S	PM     nn[        [        U5      5      [        [        U5      5      -  $ U S
:X  a  [        U[        S9$ U S:X  a  [	        U[        S9$ [        SU -  5      es  snf )Nr   r   r   r   r   r   z%Using avg_zero without neighborValuesr   r   r   r   z"Unrecognized aggregation method %s)r   r   r   r   r   rY   abs)r   knownValuesneighborValuesxvaluess        r   	aggregater  )  s   )#[!"U3{+;%<<<E![!""F"r?E!{E!{J&$%LMM,-nf1fnF-Vc&k 222H${$$H${$$
"*->>@ @ .s   Dc                    US   nUS   nX"US   -  -
  nU R                  US   5        U R                  [        5      n [        R                  " [
        U5      u  pU	S:X  a  US   nO!Xy-
  nXS   -  nU[        -  nUS   XS   -  -   nUS   US   -  nU[        -  nXS   -
  nUU-   US   -  nUUS   -   nU R                  U5        UU:  a  U R                  UU-
  5      nOMUS   US   -   nU R                  UU-
  5      nU R                  US   5        UU R                  UUS   -
  5      -  n[
        S   [
        SS  nn[        U5      [        -  nUUU-  -   n [        R                  " UU5      nS /U-  nUnUS   n[        S[        U5      S
5       H!  nUU   nUU:X  a  UUS-      UUS
-  '   UU-  nM#     U V s/ s H
  n U c  M  U PM     n!n U!(       d  g[        [        U!5      5      [        [        U5      5      -  n"U"U:  a  [        UU!U5      n#[        R                  " [
        UU#5      n$U R                  US   5        U R                  [        5      n [        R                  " [
        U5      u  n%n&U%S:X  a&  U R                  US   5        U R                  U$5        gUU%-
  nXS   -  nU[        -  nUS   XS   -  -   n'U R                  U'5        U R                  U$5        gg! [        R                   a    [        SU R                  5      ef = f! [        R                   a    [        S	U R                  5      ef = fs  sn f ! [        R                   a    [        SU R                  5      ef = f)Nr   r   r   r   Unable to read base datapointr   r   r   Unable to read datapointsr   FT)r   r   r   r   r   pointFormatrk   rf   r   r   r   r   r  r   r   )(r   header	timestamphigherlowerr   r   lowerIntervalStartpackedPointhigherBaseIntervalhigherBaseValuehigherFirstOffsettimeDistancepointDistancebyteDistancehigherPoints
higherSizerelativeFirstOffsetrelativeLastOffsethigherLastOffsetseriesString	higherEnd	byteOrder
pointTypesrF   seriesFormatunpackedSeriesr  currentIntervalstepr   	pointTimevr  knownPercentaggregateValuemyPackedPointlowerBaseIntervallowerBaseValuelowerOffsets(                                           r   __propagater7  B  s   01~# 6G0H$HI''&
	"+G,2MM+{,S) 1x(%:L +<$==M 9,Lx(L&>,IJ()V4E-FF,i'*)8,<<+j8F6NJ'&*::''
))77+.??@Lx 6&>1I779'889LGGF8BGG,vh/??@@L &a.+ab/Z)|	)&j612,C]]<>N
 6F?.&/	!	"$!S(!,aq!IO#-a!e4nQ!VtO	 - +<NqaN+<	s;'(5^1D+EE,S0+~NNKK-?PMGGE(O'')$KI,2MM+{,S). AggeHohh}  (*;;l",=&>>m"Y.l(O|Fm'CDkggkhh} Y 
 G
<bgg
FFG@ 
 C
8"''
BBC = << I>HHIs/   K- L  M	M	M -+L+M+M9c                     [        U5      n[        U S[        5       n[        (       a+  [        (       a   [        UR                  5       SS[        5        [        XAX#5      sSSS5        $ ! , (       d  f       g= f)zl
update(path, value, timestamp=None)

path is a string
value is a float
timestamp is either an int or float
r   r   N)	r   r{   r   r   r   r   r   r   file_update)rl   r7   r  nowr   s        r   updater;    sO     ,%D%#r{~~BIIKA'89r)1 $##s   AA))
A7c                 h   [         (       a3  [        R                  " U R                  5       [        R                  5        [        U 5      nUc  [        [        R                  " 5       5      nUc  Un[        U5      nX2-
  nXTS   :  a  US:  d  [        S5      e[        US   5       H  u  pgUS   U:  a  M  US   US-   S  n  O   X"WS   -  -
  n	[        R                  " [        X5      n
U R                  US   5        U R                  [        5      n [        R                   " [        U5      u  pUS:X  a(  U R                  US   5        U R)                  U
5        U	nODX-
  nXS   -  nU[        -  nUS   UUS
   -  -   nU R                  U5        U R)                  U
5        UnW H  n[+        XU	UU5      (       d    OUnM     [,        (       a5  U R/                  5         [0        R2                  " U R                  5       5        g g ! [        R"                   a    [%        S	U R&                  5      ef = f)Nr   r   rd   r   r   r   r   r   r  r   )r   r   r   r   r   r   r>   r   rb   r   r   r   r  r   r   r   r   rk   rf   r   r   r7  r   r   r   r   )r   r7   r  r:  r  diffr   r   lowerArchives
myIntervalr3  r  baseInterval	baseValuer  r   r!  myOffsetr  r  s                       r   r9  r9    s   	T	KK		U]]+&[
diik
CI)n)	$((dai
?A A fZ01ja{d":&q1uv.M	 2 0A(BBC*++k:=-'''(
	"+G &k; G\ QGGGHHH]L,L ,=$>>M 9,Lx L76?$BCHGGHHH] &er:vu==F 
 YHHJHHRYY[ - 
 G
<bgg
FFGs   H +H1c                 p   U(       d  gU VVs/ s H  u  p4[        U5      [        U5      4PM     nnnUR                  S SS9  [        U S[        5       n[
        (       a+  [        (       a   [        UR                  5       SS[        5        [        XQU5      sSSS5        $ s  snnf ! , (       d  f       g= f)zXupdate_many(path,points)

path is a string
points is a list of (timestamp,value) points
Nc                     U S   $ r   r'   )ps    r   r*   update_many.<locals>.<lambda>  s    AaDr   T)r   reverser   r   )r>   r   r   r{   r   r   r   r   r   r   file_update_many)rl   rF   r:  tr0  r   s         r   update_manyrJ    s     

-34V6ASVU1XV&4++.$+/D%#r{~~BIIKA'89B, $# 5##s   "B!AB''
B5c                    [         (       a3  [        R                  " U R                  5       [        R                  5        [        U 5      nUc  [        [        R                  " 5       5      n[        US   5      n[        U5      n/ nU Hi  nX'S   -
  nUS   U:  a<  U(       a  UR                  5         [        XXV5        / n [        U5      nUS   U:  a  M<  U(       d    OUR                  U5        Mk     U(       a#  U(       a  UR                  5         [        XXV5        [        (       a5  U R                  5         [         R"                  " U R                  5       5        g g ! [         a    S n M  f = f)Nr   r   r   )r   r   r   r   r   r   r>   r   iternextrG  __archive_update_manyStopIterationr   r   r   r   r   )	r   rF   r:  r  r   currentArchivecurrentPointspointages	            r   rH  rH    s)   	T	KK		U]]+&[
diik
C&$%(>.-e
a.C

%
+	b.Hh 
%
+ # ( "nDYHHJHHRYY[   s   E  E0/E0c           	      F  ^" US   nU VVs/ s H  u  pVXUU-  -
  U4PM     nnn/ nS n	Sn
[        U5      n[        SU5       H  nUS-   U:  a  X|   S   X|S-      S   :X  a  M!  X|   u  pU	(       a  XU-   :X  a"  U
[        R                  " [        X5      -  n
Un	MX  [        U
5      [
        -  nXUS-
  -  -
  nUR                  X45        [        R                  " [        X5      n
Un	M     U
(       a.  [        U
5      [
        -  nXUS-
  -  -
  nUR                  X45        U R                  US   5        U R                  [
        5      n [        R                  " [        U5      u  nnUS:X  a  US   S   nU H  u  nnUU-
  nUU-  nU[
        -  nUS   UUS   -  -   nU R                  U5        US   US   -   nU[        U5      -   U-
  nUS:  ay  U R                  US U*  5        U R                  5       U:X  d%   SUU R                  5       U[        U5      4-  5       eU R                  US   5        U R                  UU* S  5        M  U R                  U5        M     UnUS	    Vs/ s H  nUS   US   :  d  M  UPM     nnU H\  m"U"4S
 jnU Vs/ s H  nU" US   5      PM     nn[        U5      n Sn!U  H  n[!        XUUT"5      (       d  M  Sn!M     U!(       d    g T"nM^     g s  snnf ! [        R                   a    [        SU R                  5      ef = fs  snf s  snf )Nr   r   r   r   r   r  r   z<archiveEnd=%d fh.tell=%d bytesBeyond=%d len(packedString)=%dr   c                    > X TS   -  -
  $ )Nr   r'   )r   r  s    r   fit"__archive_update_many.<locals>.fit`  s    e-..//r   FT)r   r   r   r   r  r   r   r   r   r   rk   rf   r   r   r   setr7  )#r   r  r   rF   r.  r  r7   alignedPointspackedStringspreviousIntervalcurrentStringlenAlignedPointsr   intervalnumberOfPointsstartIntervalpackedBasePointr@  rA  packedStringr  r   r!  rB  
archiveEndbytesBeyondr  arcr>  rV  rE  lowerIntervalsuniqueLowerIntervalspropagateFurtherr  s#                                     @r   rN  rN    s   	"	#$-35-3)	 T!12E:-3  5 --'!%&a1uM$4Q$7=Q;OPQ;R$R%(X(.E"Ev{{;@@m!=)Y6n&.12D*EFmM9:kk+x?m! ' '94N$0B(CDM-78 '''(
GGI&/G &k? K\9 Q #A&L #0xl*L D(M 9,Lx L76?$BCHGGH"WV_4Jc,//:=KQhh|Mk\*+WWY*$ F	RWWYS->?	@$ 	gggh hh|[LM*+hh|' #0, &"("4 K"43+,w7H/II "4- K e0)67Ac!A$iN7~.(	R65	9	9 ) F C5> 
 G
<bgg
FFG>K 8s#   K%K+ &L:LL++Lc                      [        U S5       n[        U5      sSSS5        $ ! , (       d  f       g= f! [        [        4 a     gf = f)z
info(path)

path is a string
rbN)r{   r   r   OSError)rl   r   s     r   r   r   n  sG    		dD	R" 
		 
 7	 			s#   4 #	4 
14 4 AAc                 j    [        U S5       n[        XQX#U5      sSSS5        $ ! , (       d  f       g= f)ar  fetch(path,fromTime,untilTime=None,archiveToSelect=None)

path is a string
fromTime is an epoch time
untilTime is also an epoch time, but defaults to now.
archiveToSelect is the requested granularity, but defaults to None.

Returns a tuple of (timeInfo, valueList)
where timeInfo is itself a tuple of (fromTime, untilTime, step)

Returns None if no data can be returned
rj  N)r{   
file_fetch)rl   fromTime	untilTimer:  archiveToSelectr   s         r   fetchrq  |  s(     D$2bIOD s   $
2c                    [        U 5      nUc  [        [        R                  " 5       5      nUc  Un[        U5      n[        U5      nX:  a  [        SU< SU< S35      eX5S   -
  nX:  a  g X&:  a  g X:  a  UnX#:  a  UnX1-
  nU(       a  [	        U5      S-   n[        U5      S   nUS    H$  n	U(       a  U	S   U:X  a    OS n	M  U	S	   U:  d  M$    O   U(       a  W	(       d  [        S
U-  5      e[        U W	X5      $ )Nz"Invalid time interval: from time 'z' is after until time ''r   z:1r   r   r   r   zInvalid granularity: %s)r   r>   r   r\   strrI   r5   __archive_fetch)
r   rn  ro  r:  rp  r  
oldestTimer=  retentionStrr   s
             r   rm  rm    s   &[
diik
CI]()n)
 
	9	  N++*^H_I	$ '$.L'5a8O
#g	"	#	6g			% $ W
./B
CC	Wh	::r   c                 n   US   n[        X"U-  -
  5      U-   n[        X3U-  -
  5      U-   nXV:X  a  Xd-  nU R                  US   5        U R                  [        5      n [        R
                  " [        U5      u  pUS:X  a  Xe-
  U-  n
XVU4nS/U
-  nX4$ XX-
  nX-  nU[        -  nUS   XS   -  -   nXh-
  nX-  nU[        -  nUS   XS   -  -   nU R                  U5        UU:  a  U R                  UU-
  5      nOMUS   US   -   nU R                  UU-
  5      nU R                  US   5        UU R                  UUS   -
  5      -  n[        S   [        SS nn[        U5      [        -  n
UUU
-  -   n [        R
                  " UU5      nS/U
-  nUn[        S[        U5      S	5       H#  nUU   nUU:X  a  UUS-      nUUUS	-  '   UU-  nM%     XVU4nX4$ ! [        R                   a    [        SU R                  5      ef = f! [        R                   a    [        SU R                  5      ef = f)
z
Fetch data from a single archive. Note that checks for validity of the time
period requested happen above this level so it's possible to wrap around the
archive on a read and request data older than the archive's retention
r   r   r  r   Nr   r   r  r   )r>   r   r   r   r   r   r  rk   rf   r   r   r   )r   r   rn  ro  r.  fromIntervaluntilIntervalr  r@  rA  rF   timeInfo	valueListr  r   r!  
fromOffsetuntilOffsetr'  rc  r)  r*  r+  r,  r-  r   r/  
pointValues                               r   ru  ru    s    
"	#$XD12T9,it#345<-"M'''(
	"+G &k; G\ Q*t3FT2HI   ,,&-*,x L6?$BC* -,&-*,!\FO%CD+ ''*+77;34L"WV_4J77:
23LGGGHBGGK'(*;;<<L &a.+ab/Z)|	)&j612,C]]<>N
 fvo) /!S(!,aq!IO#!!a%(j$iQtO - 40(
	i 
 G
<bgg
FFGJ 
 C
8"''
BBCs   G 8H	 +H	+H4c           	          [        U S5       n[        US5       n[        XVX#U5      sSSS5        sSSS5        $ ! , (       d  f       O= f SSS5        g! , (       d  f       g= f)zMerges the data from one whisper file into another. Each file must have
the same archive configuration. time_from and time_to can optionally be
specified for the merge.
rj  zrb+N)r{   
file_merge)	path_frompath_to	time_fromtime_tor:  fh_fromfh_tos          r   merger    sN     It	gu		C@ 
	 			    A:	A
A	A
A#c                 z   [        U 5      n[        U5      nUS   US   :w  a'  [        U R                  < SUR                  < S35      eUc  [        [        R                  " 5       5      nUb  UnOUnUb  UnOSnXx:  a  [        S5      eUS   n	U	R                  [        R                  " S5      S9  U	 H  n
UnUnXU
S   -
  :  a  XJS   -
  nXU
S   -
  :  a  M&  [        X
X5      u  pUu  nnn[        [        S [        [        UUU5      U5      5      5      n[        U5      S:X  a  Mv  [        XU
U5        M     g )	Nr    and zD archive configurations are unalike. Resize the input before mergingr   ztime_to must be >= time_fromr   r   c                     U S   S L$ )Nr   r'   )rF   s    r   r*   file_merge.<locals>.<lambda>B  s    VAYd*r   )r   NotImplementedErrorr   r>   r   r5   r   operator
itemgetterru  listr   r   r   r   rN  )r  r  r  r  r:  
headerFromheaderToro  rn  r   r   archiveFrom	archiveTor{  r  startendarchive_steppointsToWrites                      r   r  r    s\   G$*% (
x
33
+2<<EF F 	[
diik
CIIHH 
3
44
#(
--H''4-5gKI7;///+..k---(;RX!)UC*
6%l+V46 7M =Q%7MB! r   c           	          [        U S5       n[        US5       n[        XVX#U5      sSSS5        sSSS5        $ ! , (       d  f       O= f SSS5        g! , (       d  f       g= f)zRCompare two whisper databases. Each file must have the same archive configuration rj  N)r{   	file_diff)r  r  ignore_empty
until_timer:  r  r  s          r   r=  r=  J  sL    It	gt	w|E 
	 			 r  c           	        ^^^^ [        U 5      n[        U5      nUS   US   :w  a'  [        U R                  < SUR                  < S35      eUS   nUR                  [        R
                  " S5      S9  / nUc  [        [        R                  " 5       5      nU(       a  Un	OUn	[        U5       GH  u  p/ nXKS   -
  n[        XX5      u  nm[        XX5      u  nm[        US   US   5      [        US   US   5      [        US   US   5       smnm[        UUUU4S	 j[        SUT-
  T-  5      5      nU(       a#  U Vs/ s H  nUS   c  M  US   c  M  UPM     nnO U Vs/ s H  nUS   c  US   c  M  UPM     nnU Vs/ s H  nUS   US   :w  d  M  UPM     nnUR                  XUR                  5       45        [        X5      n	GM!     U$ s  snf s  snf s  snf )
Nr   r  zD archive configurations are unalike. Resize the input before diffingr   r   r   r   r   c                 &   > U T-  T-   TU    TU    4$ r&   r'   )r6   r  
fromValuesr  toValuess    r   r*   file_diff.<locals>.<lambda>r  s    A,u4jmXa[Qr   )r   r  r   r   r  r  r>   r   r   ru  r   r   mapr   r   __len__)r  r  r  r  r:  r  r  r   archive_diffsro  archive_numberr   diffs	startTimefromTimeInfo
toTimeInfor  rF   rE  r  r  r  r  s                      @@@@r   r  r  Q  s   G$*% (
x
33
-4\\5::	GH H 
#(
--H''4-5-[
diik
CII!*8!4nEk**I)? \:,UYRZ	\!_jm	,	\!_jm	,	\!_jm	,	. UC
 QC%KL89;F!K6aQqT!A$6fKf!J6aQqT%516fJ/1!A$!A$,QE/.1ABCI)I+ "5, 
 LJ/s*   
GGG&G:GGGr&   )NN)NNFF)NNN)FNN)k	itertoolsr  r   platformr@   r   sysr   r   zipr   filterr   version_inforanger   r   CAN_LOCKImportErrorctypesctypes.utilr   r   r   fadviser   r   utilfind_library	libc_nameCDLLlibcc_int64	c_off64_tc_intc_off_tunameposix_fallocate64r   restypeargtypesAttributeErrorposix_fallocater   r   r   r   r   r   r   r   calcsizelongSizer   	floatSizevalueFormat	valueSizer  r   r   r   r   r   dictr   itemsr   r  aggregationMethodsr   r   r   r?   r8   rI   ri   rK   rU   rY   r\   r_   rb   rf   r}   r   r   r   r   r   r   r   r	  r  r7  r;  r9  rJ  rH  rN  r   rq  rm  ru  r  r  r=  r  )r)   r0  s   00r   <module>r     s  8   	  	  
 y&#&
)Y
/v&(-338+ 	kk&&s+)	Y	$nn)LL'^^aI%nnG
''JJ!<<I>J ( I
	 	
??:&OOK(	OOK(	OOK(	~. //"34


	  	  3J3P3P3RS3R413RST ,335  4 4 4
X 
,:+y +
+ 
$/ $
* 
* 
@* @
/) /	'ST4n$;8<7;t DH&+GT@2Vr27t- %POdE"2;jHVA,C^F,G!  (  -  +& 
 ''j!<<j#\\7G<j mb  Tsf   K
 
K #K( 01K7 M

KKK%$K%(K43K47M>1L22L?:M>L??M