
    Dj                        d Z ddlmZ ddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ ddlmZmZmZ dZ G d	 d
ed          Zd*dZd+dZd,dZed-d            Zd.dZd/dZd0d Zd1d"Zd2d)ZdS )3z0Local account discovery index for the Typer CLI.    )annotationsN)contextmanager)datetimetimezone)Path)CallableIterator	TypedDictzaccounts.jsonc                  F    e Zd ZU dZded<   ded<   ded<   ded<   ded<   d	S )
AccountIndexEntryz!Persisted local account metadata.strusernamelast_used_atsession_pathcookiejar_pathboolchina_mainlandN)__name__
__module____qualname____doc____annotations__     U/home/thesage/.hermes/venv/lib/python3.11/site-packages/pyicloud/cli/account_index.pyr   r      sQ         ++MMMr   r   F)totalsession_root
str | Pathreturnr   c                0    t          |           t          z  S )z6Return the JSON file path for the local account index.)r   ACCOUNT_INDEX_FILENAMEr   s    r   account_index_pathr#      s      666r   
index_pathdict[str, AccountIndexEntry]c                D   	 t          j        |                     d                    }n+# t          $ r i cY S t          t           j        f$ r i cY S w xY wt          |t                    r|                    d          nd}t          |t                    si S i }|	                                D ]\  }}t          |t                    rt          |t                    s0|                    d          }|                    d          }|                    d          }t          |t                    rt          |t                    s|t          |t                    r|nd||d	||<   |                    d
          }	t          |	t                    r|	||         d
<   |S )z+Load indexed accounts from a specific path.utf-8encodingaccountsNr   r   r    r   r   r   r   r   )jsonloads	read_textFileNotFoundErrorOSErrorJSONDecodeError
isinstancedictgetitemsr   r   )
r$   rawr*   
normalizedr   entryr   r   r   r   s
             r   _load_accounts_from_pathr:       s   j--w-??@@   			T)*   			 '1d&;&;Eswwz"""Hh%% 	/1J#>>++ D D%(C(( 	
5$0G0G 	yy00#344yy00,,, 	J~s4S4S 	 ,6|S,I,IQLLr(,	 
  

8 #344nd++ 	D5CJx !12s   (+ AAAc                :    t          t          |                     S )z Load indexed accounts from disk.)r:   r#   r"   s    r   load_accountsr<   C   s     $$6|$D$DEEEr   Iterator[Path]c              #    K   t          |           }|j                            dd           |                    |j         d          }|                    dd          5 }	 ddl}n # t          $ r |V  Y ddd           dS w xY w|                    |	                                |j
                   	 |V  |                    |	                                |j                   n2# |                    |	                                |j                   w xY w	 |                                 n# t          $ r Y nt          $ r Y nw xY wddd           dS # 1 swxY w Y   dS )	zHSerialize account index updates when the platform supports file locking.Tparentsexist_okz.lockza+r'   r(   r   N)r#   parentmkdirwith_suffixsuffixopenfcntlImportErrorflockfilenoLOCK_EXLOCK_UNunlinkr0   r1   )r   r$   	lock_path	lock_filerG   s        r   _locked_indexrP   I   s      $L11JD4888&&**;'B'B'BCCI	w	/	/ 9	LLLL 	 	 	       	 	I$$&&666	;KK	((**EM::::EKK	((**EM::::	  	 	 	D 	 	 	D	!                 s~   "E$A)(E)B7EB0E7C);.E)/DED10E1
E	;E=	E	EE		EE Er*   Nonec                @    t          t          |           |           dS )z!Persist indexed accounts to disk.N)_save_accounts_to_pathr#   )r   r*   s     r   _save_accountsrT   d   s#    
 -l;;XFFFFFr   c                   s(	 |                                   n# t          $ r Y nw xY wdS | j                            dd           dfdt	                    D             i}d}	 t          j        dd| j        d| j         dd	d
          5 }|                    t          j
        |dd          dz              |                                 t          j        |                                           t          |j                  }ddd           n# 1 swxY w Y   t          j        ||            dS # t"          $ r* |&	 |                                  n# t          $ r Y nw xY w w xY w)z,Persist indexed accounts to a specific path.NTr?   r*   c                "    i | ]}||         S r   r   ).0r   r*   s     r   
<dictcomp>z*_save_accounts_to_path.<locals>.<dictcomp>z   s     SSShXx1SSSr   wr'   .z.tmpF)r)   dirprefixrE   delete   )indent	sort_keys
)rM   r0   rB   rC   sortedtempfileNamedTemporaryFilenamewriter-   dumpsflushosfsyncrJ   r   replace	Exception)r$   r*   payload	temp_path	temp_files    `   r   rS   rS   l   s   
  	  	 	 	D	D4888SSSS&BRBRSSSG "I(!)z)))
 
 
 	- OODJwqDIIIDPQQQOOHY%%''(((Y^,,I	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	
9j)))))      """"$   sa    
''((D: A<DD: DD: D D: :E.EE.
E)&E.(E))E.r9   keyring_hasCallable[[str], bool]r   c                    t          | d                                                   p7t          | d                                                   p || d                   S )Nr   r   r   )r   exists)r9   rp   s     r   _is_discoverablert      s^     	U>"##**,, 	*&'((//11	*;uZ())r   list[AccountIndexEntry]c                   t          |           5 }t          |          }fd|                                D             |k    rt          |           ddd           n# 1 swxY w Y   fdt	                    D             S )z4Drop stale entries and return discoverable accounts.c                <    i | ]\  }}t          |          ||S r   rt   )rW   r   r9   rp   s      r   rX   z"prune_accounts.<locals>.<dictcomp>   s?     
 
 
%{33
e
 
 
r   Nc                     g | ]
}|         S r   r   )rW   r   retaineds     r   
<listcomp>z"prune_accounts.<locals>.<listcomp>   s    @@@8HX@@@r   )rP   r:   r6   rS   rb   )r   rp   r$   r*   rz   s    `  @r   prune_accountsr|      s    
 
|	$	$ 9
+J77
 
 
 
#+>>#3#3
 
 

 x":x8889 9 9 9 9 9 9 9 9 9 9 9 9 9 9 A@@@vh/?/?@@@@s   AA$$A(+A(r   r   r   r   r   bool | Nonec                  t          |           5 }fdt          |                                          D             }|                    |          }|t	          j        t          j                                                  ||d}	|||	d<   n|d|v r|d         |	d<   |	||<   t          ||           |	cddd           S # 1 swxY w Y   dS )z7Upsert one account entry and prune any stale neighbors.c                <    i | ]\  }}t          |          ||S r   rx   )rW   	username_r9   rp   s      r   rX   z$remember_account.<locals>.<dictcomp>   s?     
 
 
 	5{33
u
 
 
r   )tzr,   Nr   )
rP   r:   r6   r5   r   nowr   utc	isoformatrS   )
r   r   r   r   r   rp   r$   r*   previousr9   s
        `    r   remember_accountr      sK    
|	$	$ 

 
 
 
$<Z$H$H$N$N$P$P
 
 

 <<)) $LHL999CCEE(,	$
 $
 %&4E"##!&6(&B&B&./?&@E"#"z8444'                 s   B(CC
C
)r   r   r   r   )r$   r   r   r%   )r   r   r   r%   )r   r   r   r=   )r   r   r*   r%   r   rQ   )r$   r   r*   r%   r   rQ   )r9   r   rp   rq   r   r   )r   r   rp   rq   r   ru   )r   r   r   r   r   r   r   r   r   r}   rp   rq   r   r   )r   
__future__r   r-   ri   rc   
contextlibr   r   r   pathlibr   typingr   r	   r
   r!   r   r#   r:   r<   rP   rT   rS   rt   r|   r   r   r   r   <module>r      s   6 6 " " " " " "  				  % % % % % % ' ' ' ' ' ' ' '       0 0 0 0 0 0 0 0 0 0(     	    7 7 7 7       FF F F F    4G G G G% % % %P   A A A A"     r   