
    G@d@4                         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	 ddl
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  G d d	e          Zd
 Zd Zedk    r e             dS dS )z0Dialog window for recovering files from autosave    )pathN)getsavefilename)Qt)
QApplicationQDialogQDialogButtonBoxQHBoxLayoutQLabelQMessageBoxQPushButtonQTableWidgetQVBoxLayoutQWidget)_running_under_pytestc                        e Zd ZdZddZ fdZ fdZd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z fdZ xZS )RecoveryDialogz<Dialog window to allow users to recover from autosave files.Nc                    t          j        | |           t          |           | _        |                     | j                   | j                            | j                                        dz             g | _        |                     |           | 	                                 | 
                                 |                                  |                     t          d                     |                     dd           |                     t           j        t           j        z  t           j        z             |r7t)          |d          r'|j        | _        | j                                         dS d| _        dS )a  
        Constructor

        Parameters
        ----------
        autosave_mapping : List[Tuple[str]]
            List of tuples, containing the name of the original file and the
            name of the corresponding autosave file. The first entry of the
            tuple may be `None` to indicate that the original file is unknown.
        parent : QWidget, optional
            Parent of the dialog window. The default is None.
           zRecover from autosavei  i  splashN)r   __init__r   layout	setLayout
setSpacingspacingfiles_to_opengather_data	add_label	add_tableadd_cancel_buttonsetWindowTitler   setFixedSizesetWindowFlagsr   DialogMSWindowsFixedSizeDialogHintWindowStaysOnTopHinthasattrr   hide)selfautosave_mappingparents      Elib/python3.11/site-packages/spyder/plugins/editor/widgets/recover.pyr   zRecoveryDialog.__init__   s^    	v&&&!$''t{###t{2244q8999)***   A566777#s###I77#$	% 	% 	%  	gfh// 	 -DKKDKKK    c                     | j         | j                                          t          t          |                                            dS zReimplement Qt method.N)r   showsuperr   acceptr)   	__class__s    r,   r2   zRecoveryDialog.accept?   A    ;"Knd##**,,,,,r-   c                     | j         | j                                          t          t          |                                            dS r/   )r   r0   r1   r   rejectr3   s    r,   r7   zRecoveryDialog.rejectE   r5   r-   c                     d|i}	 t          j        |          |d<   t          j        |          |d<   n# t          $ r Y nw xY w|S )z
        Gather data about a given file.

        Returns a dict with fields 'name', 'mtime' and 'size', containing the
        relevant data for the file. If the file does not exists, then the dict
        contains only the field `name`.
        namemtimesize)ospgetmtimegetsizeOSError)r)   r9   ress      r,   gather_file_datazRecoveryDialog.gather_file_dataK   s_     tn	<--CL+d++CKK 	 	 	D	
s   .5 
AAc                 6   g | _         |D ]U\  }}|r|                     |          }nd}|                     |          }d|vr9| j                             ||f           V| j                             | j                   t          | j                   | _        dS )aR  
        Gather data about files which may be recovered.

        The data is stored in self.data as a list of tuples with the data
        pertaining to the original file and the autosave file. Each element of
        the tuple is a dict as returned by gather_file_data(). Autosave files
        which do not exist, are ignored.
        Nr:   )key)datarA   appendsortrecovery_data_key_functionlennum_enabled)r)   r*   origautosave	orig_dictautosave_dicts         r,   r   zRecoveryDialog.gather_data[   s     	. 	9 	9ND( ! 11$77		 	 11(;;Mm++Ii78888	4:;;;ty>>r-   c                 H    |\  }}|rd|d         |d         fS dd|d         fS )a-  
        Convert item in `RecoveryDialog.data` to tuple so that it can be sorted.

        Sorting the tuples returned by this function will sort first by name of
        the original file, then by name of the autosave file. All items without an
        original file name will be at the end.
        r   r9       )r)   itemrL   rM   s       r,   rG   z)RecoveryDialog.recovery_data_key_functionq   s?     $( 	= 	1y(-*?@@q-/00r-   c                     t          d          }t          ||           }|                    d           | j                            |           dS )z3Add label with explanation at top of dialog window.zAutosave files found. What would you like to do?

This dialog will be shown again on next startup if any autosave files are not restored, moved or deleted.TN)r   r
   setWordWrapr   	addWidget)r)   txtlabels      r,   r   zRecoveryDialog.add_label   sZ     E F F sD!!$e$$$$$r-   c                     t          |          }|                    d           |                    t          j        t          j        z             | j                            |||           dS )z'Add a label to specified cell in table.   N)r
   	setMarginsetAlignmentr   	AlignLeftAlignVCentertablesetCellWidget)r)   rowcolrU   rV   s        r,   add_label_to_tablez!RecoveryDialog.add_label_to_table   s\    s2<"/9:::
  c511111r-   c                 6    t          t           j                  d           }| _        t	          d          t	          d          t	          d          g}|                    |           |                                                                 |                    t          j
                   |                    t          j
                   |                    t           j                   |                    d           |                    d           t!           j                  D ]'\  }\  }}                     |d                     |                                          |d                     |                     t'                      }t)                      }t	          d	          }t+          t	          d
                    }	|	                    |           |	j        t0                                       |f fd	           |                    |	           t	          d          }t+          t	          d                    }	|	                    |           |	j        t0                                       |f fd	           |                    |	           t	          d          }t+          t	          d                    }	|	                    |           |	j        t0                                       |f fd	           |                    |	           |                    |            j                            |d|           )|                                 |                                  j                            |           dS )z0Add table with info about files to be recovered.r   zOriginal filezAutosave fileActionsFz(::item { border-bottom: 1px solid gray }r   rO   zXRecover the autosave file to its original location, replacing the original if it exists.Restorec                 .                         |          S N)restorecheckedmy_idxr)   s     r,   <lambda>z*RecoveryDialog.add_table.<locals>.<lambda>       V0D0D r-   zDelete the autosave file.Discardc                 .                         |          S rf   )discardrh   s     r,   rk   z*RecoveryDialog.add_table.<locals>.<lambda>   rl   r-   z{Display the autosave file (and the original, if it exists) in Spyder's Editor. You will have to move or delete it manually.Openc                 .                         |          S rf   )
open_filesrh   s     r,   rk   z*RecoveryDialog.add_table.<locals>.<lambda>   s    0G0G r-      N) r   rH   rD   r]   r   setHorizontalHeaderLabelsverticalHeaderr(   setHorizontalScrollBarPolicyr   ScrollBarAsNeededsetVerticalScrollBarPolicysetSelectionModeNoSelectionsetShowGridsetStyleSheet	enumeratera   file_data_to_strr   r	   r   
setToolTipclickedboolconnectrT   r   r^   resizeRowsToContentsresizeColumnsToContentsr   )
r)   r]   labelsidxoriginalrK   widgetr   tooltipbuttons
   `         r,   r   zRecoveryDialog.add_table   s`   S^^Q55
O$$a&8&8!I,,G''///##%%%**2+?@@@(()=>>>|7888 	%   FGGG)249)=)=  	5  	5%C%(H##CD,A,A(,K,KLLL##CD,A,A(,K,KLLLYYF ]]F ? @ @G 9..Fg&&&N4 ((+.DDDDDF F FV$$$344G 9..Fg&&&N4 ((+.DDDDDF F FV$$$ 1 2 2G !6++Fg&&&N4 ((+.GGGGGI I IV$$$V$$$J$$S!V4444""$$$%%'''e$$$$$r-   c                    |st          d          S |d         }	 t          j        dt          j        |d                             }|d                    t          d          |          z  }|d                    t          d          |d	         t          d
                    z  }n%# t
          $ r |dt          d          z   z  }Y nw xY w|S )z
        Convert file data to a string for display.

        This function takes the file data produced by gather_file_data().
        z<i>File name not recorded</i>r9   z%Y-%m-%d %H:%M:%Sr:   z<br><i>{}</i>: {}zLast modifiedz<br><i>{}</i>: {} {}Sizer;   bytesz<br>z<i>File no longer exists</i>)r   timestrftime	localtimeformatKeyError)r)   rD   r@   mtime_as_strs       r,   r~   zRecoveryDialog.file_data_to_str   s      	645556l	>=)<(,tG}(E(EG GL&--a.@.@,OOOC*11fIItF|QwZZ9 9 9CC 	> 	> 	>6A<====CCC	>
s   BB) )C
Cc                     t          t           j        |           }|j                            | j                   | j                            |           dS )z7Add a cancel button at the bottom of the dialog window.N)r   Cancelrejectedr   r7   r   rT   )r)   
button_boxs     r,   r    z RecoveryDialog.add_cancel_button   sJ    %&6&=tDD
##DK000j)))))r-   c                    t          j                                        d          }t          |                                                                |                                 dz  z
            }t          |                                                                |                                 dz  z
            }| 	                    ||           dS )zCenter the dialog.r   rs   N)
r   desktopscreenGeometryintcenterxwidthyheightmove)r)   screenr   r   s       r,   r   zRecoveryDialog.center   s    %''66q99!!##djjllQ&6677!!##dkkmma&7788		!Qr-   c                 b   | j         |         \  }}|r	|d         }n=t          | t          d          t          j        |d                             \  }}|sd S 	 	 t          j        |d         |           nL# t          t          f$ r8 t          j
        |d         |           t          j        |d                    Y nw xY w|                     |           d S # t          $ rJ}t          d                              ||d                   }|                     ||           Y d }~d S d }~ww xY w)Nr9   zRestore autosave file to ...zUnable to restore {} using {})rD   r   r   r<   basenameosreplaceAttributeErrorr?   shutilcopy2remove
deactivateEnvironmentErrorr   report_error)r)   r   rJ   rK   	orig_nameignorederrortexts           r,   rg   zRecoveryDialog.restore   se   3h 	VII!0a677Xf-.."0 "0Iw  	+,
8F+Y7777"G, , , , Xf-y999	(6*+++++, OOC      	+ 	+ 	+566VIx'788 dE*********	+s7   A7 6C 7AC =C ?C  C 
D.$?D))D.c                 2   | j         |         \  }}	 t          j        |d                    |                     |           d S # t          $ rI}t          d                              |d                   }|                     ||           Y d }~d S d }~ww xY w)Nr9   zUnable to discard {})rD   r   r   r   r   r   r   r   )r)   r   r   rK   r   r   s         r,   ro   zRecoveryDialog.discard   s     IcN	+Ihv&'''OOC      	+ 	+ 	++,,33HV4DEEDdE*********	+s   /A 
B>BBc                     | j         |         \  }}|r | j                            |d                    | j                            |d                    |                     |           d S )Nr9   )rD   r   rE   r   )r)   r   rJ   rK   s       r,   rr   zRecoveryDialog.open_files	  sh    3h 	4%%d6l333!!(6"2333r-   c           	          t          d          }t          t          j        t          d          t          d                              |||          |           }|                                 d S )NzError message:rd   z<b>{}</b><br><br>{}<br>{})r+   )r   r   Criticalr   exec_)r)   r   r   headingmsgboxs        r,   r   zRecoveryDialog.report_error  sf    $%% !I,,)**11$GG   	r-   c                    t          | j                                                  D ]0}| j                            ||                              d           1| xj        dz  c_        | j        dk    r|                                  d S d S )NFrO   r   )ranger]   columnCount
cellWidget
setEnabledrI   r2   )r)   r   r`   s      r,   r   zRecoveryDialog.deactivate  s    //1122 	> 	>CJ!!#s++66u====Aq  KKMMMMM ! r-   c                 x    | j         r(|                                  |                                 S t          j        S )z/Execute dialog window if there is data to show.)rD   r   r   r   Accepted)r)   s    r,   exec_if_nonemptyzRecoveryDialog.exec_if_nonempty  s0    9 	$KKMMM::<<##r-   c                     t                      rt          j        S t          t          |                                           S )zExecute dialog window.)r   r   r   r1   r   r   r3   s    r,   r   zRecoveryDialog.exec_'  s6    !! 	$##^T**00222r-   rf   )__name__
__module____qualname____doc__r   r2   r7   rA   r   rG   r   ra   r   r~   r    r   rg   ro   rr   r   r   r   r   __classcell__)r4   s   @r,   r   r      sg       FF" " " "H- - - - -- - - - -   * * *,1 1 1% % %2 2 26% 6% 6%p  &* * *  + + +2+ + +      $ $ $3 3 3 3 3 3 3 3 3r-   r   c                 v   t          j        | d          }t          j        |           t          j        | d          }t          j        |           i }t          j        |d          }t	          |d          5 }|                    d           ddd           n# 1 swxY w Y   t          j        |d          }t	          |d          5 }|                    d           ddd           n# 1 swxY w Y   ||fg}t          j        |d          }t          j        |d          }t	          |d          5 }|                    d	           ddd           n# 1 swxY w Y   |||fgz  }t          j        |d
          }t	          |d          5 }|                    d           ddd           n# 1 swxY w Y   t          j        |d
          }|||fgz  }t          j        |d          }t	          |d          5 }|                    d           ddd           n# 1 swxY w Y   |d|fgz  }|||fS )aO  
    Make temporary files to simulate a recovery use case.

    Create a directory under tempdir containing some original files and another
    directory with autosave files. Return a tuple with the name of the
    directory with the original files, the name of the directory with the
    autosave files, and the autosave mapping.
    rJ   rK   zham.pywzham = "original"
Nzham = "autosave"
zspam.pyzspam = "autosave"
zeggs.pyzeggs = "original"
z	cheese.pyzcheese = "autosave"
)r<   joinr   mkdiropenwrite)tempdirorig_dirautosave_dirr*   	orig_filefautosave_files          r,   make_temporary_filesr   .  si    x((HHX8GZ00LH\ 8,,I	i		 &	$%%%& & & & & & & & & & & & & & &H\844M	mS	!	! &Q	$%%%& & & & & & & & & & & & & & &"M23 9--IH\955M	mS	!	! 'Q	%&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ')]344 9--I	i		 '	%&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' 'H\955M)]344 H\;77M	mS	!	! )Q	'((() ) ) ) ) ) ) ) ) ) ) ) ) ) )$.//\#333sZ   :BB #B C..C25C28EE!EF44F8;F8H##H'*H'c                     ddl } ddl}ddlm}  |            }|                                }t          |          \  }}}t          |          }|                                 t          d|j	                    | j
        |           dS )z+Display recovery dialog for manual testing.r   N)qapplicationzfiles_to_open =)r   tempfilespyder.utils.qthelpersr   mkdtempr   r   r   printr   rmtree)r   r   r   appr   unusedr*   dialogs           r,   testr   ]  s    MMMOOO333333
,..C  G';G'D'D$FF$,--F
LLNNN	
V1222FM'r-   __main__)r   r   r   r<   r   r   qtpy.compatr   qtpy.QtCorer   qtpy.QtWidgetsr   r   r   r	   r
   r   r   r   r   r   spyder.config.baser   r   r   r   r   r   rP   r-   r,   <module>r      s   7 6       				   ( ' ' ' ' '      @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ 7 6 6 6 6 6 6 6R3 R3 R3 R3 R3W R3 R3 R3j,4 ,4 ,4^   zDFFFFF r-   