
    %
i\                     Z   d Z ddlZddlZddlZddlZddlZddl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 dZdZdZdZd	                    d
          ZdZdZdZ G d dej                  Zd Zd Zd Zd Zd Zd Z d!dZ!da"da#ea$d"dZ%d Z&d Z'd Z(d!dZ)d#dZ*d$dZ+d Z,d!d Z-dS )%z2
Various utilities for running/importing a script
    N)dialog)TreeView   )	ParseArgs      zaNo debugging
Step-through in the debugger
Run in the debugger
Post-Mortem of unhandled exceptions
      
s   
c                   $    e Zd ZdZd Zd Zd ZdS )DlgRunScriptz#A class for the 'run script' dialogc                 t   t           j                            | t          j                   |                     t          j        d           |                     t          j        d           |                     t          j        dd           | 	                    | j
        t          j                   || _        d S )NscriptargsdebuggingTypei)r   Dialog__init__win32uiIDD_RUN_SCRIPTAddDDX	IDC_EDIT1	IDC_EDIT2
IDC_COMBO1HookCommandOnBrowseIDC_BUTTON2bHaveDebugger)selfr   s     C:\Users\Dell Inspiron 16\Desktop\tws\AgrotaPowerBi\back-agrota-powerbi\mcp-client-agrota\venv\Lib\site-packages\pythonwin/pywin/framework/scriptutils.pyr   zDlgRunScript.__init__)   s    tW%;<<<G%x000G%v...G&===(;<<<*    c                 2   t           j                            |           }|                     t          j                  }t          D ]}|                    |           |                    | d                    | j	        s|
                    d           d S d S )Nr   r   )r   r   OnInitDialog
GetDlgItemr   r   debugging_options	AddString	SetCurSelr   EnableWindow)r   rccboos       r    r#   zDlgRunScript.OnInitDialog1   s    ]''--oog011" 	 	AMM!d?+,,,! 	 Q	  	 r!   c                 D   |dk    rdS t           j        t           j        z  }t          j        dd d |d|           }|                    d           |                                t           j        k    rdS |                                | d<   | 	                    d           dS )Nr   r   zPython Scripts (*.py)|*.py||z
Run Scriptr   )
win32conOFN_OVERWRITEPROMPTOFN_FILEMUSTEXISTr   CreateFileDialogSetOFNTitleDoModalIDOKGetPathName
UpdateData)r   idcode	openFlagsdlgs        r    r   zDlgRunScript.OnBrowse:   s    1991083MM	&tT9&Dd
 
 	%%%;;==HM))1**Xqr!   N)__name__
__module____qualname____doc__r   r#   r    r!   r    r   r   &   sG        ))+ + +         r!   r   c                  <    	 ddl } | j        S # t          $ r Y dS w xY w)a  Get the default Python debugger.  Returns the debugger, or None.

    It is assumed the debugger has a standard "pdb" defined interface.
    Currently always returns the 'pywin.debugger' debugger, or None
    (pdb is _not_ returned as it is not effective in this GUI environment)
    r   N)pywin.debuggerdebuggerImportError)pywins    r    GetDebuggerrD   I   s=    ~   tts   
 
c                     t           j        D ]Q}	 |rt          j        |          | k    r dS !# t          j        $ r}t          d| d|            Y d}~Jd}~ww xY wdS )zUGiven a path only, see if it is on the Pythonpath.  Assumes path is a full path spec.r   zWarning: The sys.path entry 'z' is invalid
Nr   )syspathr   FullPatherrorprint)rG   syspathdetailss      r    IsOnPythonPathrM   X   s     8 T T	T 7+G44<<qq} 	T 	T 	TR'RRRRSSSSSSSS	T1s   .AAAc                    t           j                            |           \  }}t          j        |          x}}t           j                            |          d         }g }d}t          |          sit          |          dk    rSt           j                            |          \  }}|                    |           t          |          r|t          j
        v rt           j                            t           j                            ||d                    s|t           j                            t           j                            ||d                    s>t           j                            t           j                            ||d                    r1|                                 d                    |          dz   |z   |fS t          |          dk    S|}||fS )zGiven a filename, return (module name, new path).
    eg - given "c:\a\b\c\my.py", return ("b.c.my",None) if "c:\a" is on sys.path.
    If no package found, will return ("my", "c:\a\b\c")
    r   Nr   z__init__.pyz__init__.pycz__init__.pyo.)osrG   splitr   rH   splitextrM   lenappendrF   modulesexistsjoinreverse)fileNamerG   fnameorigPathmodBitsnewPathReturnmodBits          r    GetPackageModuleNamer_   e   s   
 '--))KD%&t,,,D8GU##A&EGM$ %$ii!mm7==..LD&NN6""" t$$
Fck))GNN27<<fm#L#LMM * w~~bgll4&P&PQQ * w~~bgll4&P&PQQ	 * !!!xx((3.6EE $ii!mm" %M-r!   c                      	 t          j                                                    \  } }|                                 S # t           j        $ r Y dS w xY w)z<Gets the edit control (eg, EditView) with the focus, or NoneN)r   GetMainFrameMDIGetActiveGetActiveViewrI   )
childFramebIsMaximiseds     r    rc   rc      s\    #*#7#9#9#F#F#H#H 
L'')))=   tts   ;> AAc                      t                      } | d S t          | d          r| S 	 |                                 S # t          $ r Y nw xY w	 |                                 S # t          $ r Y d S w xY w)N
SCIAddText)rc   hasattrGetRichEditCtrlAttributeErrorGetEditCtrl)views    r    GetActiveEditControlrm      s    ??D|tt\"" ##%%%   !!!   s!   : 
AAA 
A-,A-c                      t                      } | t          | t                    rdS |                                 }t	          |d          r|| fS dS )zzReturns the active editor document and view, or (None,None) if no
    active document or it's not an editor document.
    N)NN	MarkerAdd)rc   
isinstancer   GetDocumentrh   )rl   docs     r    GetActiveEditorDocumentrs      sW     ??D|z$11||




CsK   Dy<r!   c                 D   d}t                      }|dS 	 |                                }|                                }| rt          |          dk    s@|                                dd         dk    s |                                dd         dk    rl|                                rX	 |                    |           |                                }t          j                     n# t          j
        $ r t          w xY wn# t          j
        t          f$ r Y nw xY w|sdS |S )zGets the file name for the active frame, saving it if necessary.

    Returns None if it can't be found, or raises KeyboardInterrupt.
    Nr      Untitled   Script)rc   rq   r4   rS   GetTitle
IsModifiedOnSaveDocument	linecache
clearcacher   rI   KeyboardInterruptrj   )	bAutoSavepathNameactiverr   s       r    GetActiveFileNamer      s?   
 H__F~t  ""??$$ 	,MMA||~~bqb!Z//||~~bqb!X--~~ 	,,&&x000"00H (****} , , ,++, M>*    tOs*   BD  (<C% $D  %C<<D   DD c                    d}t                      }| !	 t                      }n# t          $ r Y d S w xY w| }|st          }|d}|t          k    rt          }n|}|t
          }|r|r*t          |d u          }||d<   ||d<   ||d<   |                                t          j	        k    rd S |d         }	|d         }|d         }|	sd S |t          k    r|	 |                                }
n# t          $ r d }
Y nw xY w|
t          |
j                  dk    rtd}t          j        |t          j        t          j                  t          j        t          j        z            }|t          j        k    rd S |t          j        k    rt.          }|a|	a|an|}	t          t0          j                            |	          d                   dk    r|	dz  }	t0          j                            |	          \  }}t          |          dk    rU	 t1          j        |           |}	n# t:          $ r0 t=          |	          }|t          j        d	|	z             Y d S |}	Y nFw xY wt          j        |          }tA          |          stB          j        "                    |           	 tG          |	d
          }n;# t:          $ r.}t          j        d|j$        |j%        fz             Y d }~d S d }~ww xY w|&                                '                    tP          tR                    '                    tT          tR                    tR          z   }tB          j+        }tY          |          tB          _+        tB          j+        -                    d|	           tB          j        d         }t0          j                            |	          d         }|s|tB          j        d<   d}n"tB          j        -                    d|           d}d}t          j.        d           t0          j                            |	          d         }t          j/                     t          j0        d|z  d           d}ddl1m2} |&|tf          k    rt          j        d           tf          }	 ti          ||	d          }n#  tk          d|	           Y d S xY w|	tl          _7        	 |t.          k    r#|8                    |tl          j9        d           nH|t          k    r#|8                    |tl          j9        d           ntu          |tl          j9                   d}nZ# tv          j<        $ r t{          d           d}d}Y n5t|          $ r}|}d}Y d }~n"d }~wt          $ r{ |j?        r*|j?        j@        r|j?        j@        A                                 t          jC                     |j?        r+|j?        j@        r|j?        j@        D                    g            d}Y n |j?        r*|j?        j@        r|j?        j@        A                                 t          jC                     |j?        r+|j?        j@        r|j?        j@        D                    g            |t          k    r|F                                 Y nxY wtl          `7|tB          _+        |rtB          j        d= n|tB          j        d<   |G                                 |rt          j0        d|	 d|            nt          j0        d|z             	 tB          jH        I                                 n# t          $ r Y nw xY wt          j.        d           d S )Nr   r   r   r   r   r   zThere are no active break-points.

Selecting this debug option without any
break-points is unlikely to have the desired effect
as the debugger is unlikely to be invoked..

Would you like to step-through in the debugger instead?.pyz The file '%s' can not be locatedrbz&The file could not be opened - %s (%d)zRunning script %s...)interactz?No debugger is installed.  Debugging options have been ignored!execz
run script)start_steppingzDebugging session cancelled.zScript 'z' returned exit code z)Exception raised while running script  %s)JrD   r   r~   
lastScriptlastArgslastDebuggingTyper   r2   r-   r3   RS_DEBUGGER_GO_GetCurrentDebuggerrj   rS   breaksr   
MessageBox
LoadStringIDR_DEBUGGERMB_YESNOCANCELMB_ICONINFORMATIONIDCANCELIDYESRS_DEBUGGER_STEPrP   rG   rR   rQ   statOSErrorLocatePythonFilerH   rM   rF   rT   openstrerrorerrnoreadreplace	byte_crlfbyte_lfbyte_crargvr   insertDoWaitCursorPumpWaitingMessagesSetStatusTextpywin.frameworkr   RS_DEBUGGER_NONEcompile_HandlePythonFailure__main____file__run__dict__r   bdbBdbQuitrJ   
SystemExiteditcurrentViewEnsureNoPrompt	traceback	print_excAppendToPromptRS_DEBUGGER_PMpmclosestdoutflush)defNamedefArgsbShowDialogr   _debugger_stop_frame_rA   r   r   r9   r   rdmsgr)   rG   	fnameonly
fullScriptfexcr7   oldArgvoldPath0newPath0insertedPath0bWorkedbaseexitCoder   
codeObjects                               r    	RunScriptr      s    }}H	(**HH  	 	 	FF	  z!!D) &{ &84/00 HF,O;;==HM))FX6{O, 	FN**x/C
1133!   ~#bi..A"5"5 N'&w';<<+h.II 
 ***F''$4M)
 	BGV$$Q'((A--%gmmF++OD)
4yyA~~	 GIFF 	  	  	 )&11J!"#E#NOOOFFF	  %%d## 	"HOOD!!!   4ci7PP	
 	
 	
 		 6688Iw//77IIGSD hGCHHOOAvx{Hw}}V$$Q'H 8$$$G7==  #D!!!047;;;H(((((( ].>>>M	
 	
 	
 )T6622

\6222H!,,,LLX%6qLIIIIn,,LLX%6qLIIII X.///;   ,---       = 	7X]6 	7M%44666= 	9X]6 	9M%44R888= 	7X]6 	7M%44666= 	9X]6 	9M%44R888N**KKMMMCH HQKKGGIII RPPPhPPQQQQIDPQQQ
    s   # 
11C) )C87C8H 2IIIJ+ +
K#5#KK#R R.>A8T8 8"Z	Z&U00BZ7BZ\, ,
\98\9c            	      ~   	 t                      } n# t          $ r d} Y nw xY w| ;t          j                            |           d                                         dvrd} | ~t          j        t          j        z  }t          j
        ddd|d          }|                    d           |                                t          j        k    rdS |                                } t          j                            |           \  }}t          j                            |          \  }}d}t           j                                        D ]u\  }}t'          |dd          r_|j        }	t          j                            |	          \  }
}|                                dv rd	}|
|z   }	t          j        |	|           r|} n4vt-          |           \  }}|rt           j                            |           |t           j        v rd}d
}nd}d}t          j        |                                dz   d           t          j        d           	 t7          d|z   dd          }n+# t8          $ r t          j        d|z   dz              Y dS w xY w	 t;          |t<          j                   t           j                             |          }|r!ddl!m"}  |t           j        |                   }t          j        d|z   dz   |z   dt'          |dd          z  z              n#  tG          |           Y nxY wt          j        d           dS )zThis code looks for the current window, and determines if it can be imported.  If not,
    it will prompt for a file name, and allow it to be imported.Nr   )r   z.pywz.pyxz.Python Scripts (*.py;*.pyw)|*.py;*.pyw;*.pyx||zImport Scriptr   r   )z.pyoz.pycr   reloadimporting module...zimport z<auto import>r   zInvalid filename for import: "")r   zSuccessfully zed module 'z': %sz<unkown file>)$r   r~   rP   rG   rR   lowerr-   r.   r/   r   r0   r1   r2   r3   r4   rQ   rF   rU   itemsgetattrr   ComparePathr_   rT   r   
capitalizer   r   SyntaxErrorr   r   r   get	importlibr   r   )r   r8   r9   rG   modNamemodExtnewPathkeymodrZ   r   extbNeedReloadwhatcodeObjr   s                   r    
ImportFiler     sv   $&&    7H%%a(..008OOOH083MM	&tT9&V
 
 	(((;;==HM))1??$$ GMM(++MD'g&&w//OGVG K%%'' % %S3
D)) 	LE((//ID#yy{{...3JE"5(33 /99 	%HOOG$$$#+$//++o=qAAA )g-GG   >H3NOOO#Wh'(((koog&& 	/((((((&W-..C  ZAAA	B	
 	
 	
 	
#T"""""s+      I $J ?J BL L&c                  2   	 t                      } n# t          $ r Y dS w xY wd}t          j        |                                dz   d           t          j        d           	 t          |           }n-# t          $ r }t          d|  d|            Y d}~dS d}~ww xY w	 |	                                dz   }|
                                 n# |
                                 w xY w	 t          || d          }t          |           r7t          j        d	t          j                            |           z   d
z              nD# t           $ r t#          ||            Y n( t%          j                     t#          |           Y nxY wt          j        d           dS )zThis code looks for the current window, and gets Python to check it
    without actually executing any code (ie, by compiling only)
    Ncheckr   r   zCan't open file 'z' - r	   r   z7Python and the TabNanny successfully checked the file ''r   )r   r~   r   r   r   r   r   r   rJ   r   r   r   RunTabNannyrP   rG   basenamer   r   r   r   )r   r   r   rL   r7   r   s         r    	CheckFiler     s   $&&    D$//++o=qAAANN   9(9999:::vvxx$									#$&11x   	!I'""8,,-  
  - - -T8,,,,,#T"""""sC    
#A3 3
B=BB!C C#'AD? ?F #F c                    dd l }t                      }|t          j        d           d S |                                }t
          j        t
          j        f}|xt
          _        t
          _        	 |                    |            |\  t
          _        t
          _        n# |\  t
          _        t
          _        w xY w|	                                }|r	 |
                                d         }t          |          }t          | |           	 t                                          d           n#  Y nxY wt          j        d|z             n;# t           t"          t$          f$ r! t'          d           t'          |           Y nw xY wdS dS )Nr   z9The TabNanny is not around, so the children can run amok!r   z%The TabNanny found trouble at line %dz0The tab nanny complained, but I can't see where!)ioFindTabNannyr   r   StringIOrF   stderrr   r   getvaluerQ   int_JumpToPositionrm   SCISetViewWSr   
IndexError	TypeError
ValueErrorrJ   )filenamer   tabnannynewoutold_outdatalinenos          r    r   r     s}   III~~HVWWW [[]]Fj#*$G$$CJ)x    ")
CJJ
CJ((((??D 	ZZ\\!_F[[FHf---$&&33A6666!"IF"RSSSSIz2 	 	 	DEEE$KKKKK	 q1s6   0B B:9E !D0 /E 0D42E 5FFc                 (    t          | ||           d S N)JumpToDocument)rY   r   cols      r    r   r   5  s    8VS)))))r!   c                 v   t          j                                        |           }|d S |                                                                }	 |                                }|                                |k    r|                    |           |                                 n$# t          $ r |                                }Y nw xY w|dk    r|
                    |dz
            }||z   dz
  }	|                                }
	 |                    |           n# t          $ r t          d           Y nw xY w|                    t          |	|
          t          |	|z   |
                     |r2|                                }|dz
  |z
  }|                    |d           |                                 |S )Nr   r   z&Doesn't appear to be one of our views?)r   GetAppOpenDocumentFileGetFirstViewGetParentFrameGetEditorViewrc   SetActiveViewAutoRestorerj   	LineIndexGetTextLengthEnsureCharsVisiblerJ   SetSelminGetFirstVisibleLine
LineScrollSetFocus)rY   r   r  nCharsbScrollToToprr   framerl   charNostartsizecurTopnScrolls                r    r  r  9  s    .


+
+H
5
5C
{t--//E"""$$  D((%%% " " "!!"zz
++q !!##	<##F++++ 	< 	< 	<:;;;;;	<Ct$$c%&.$&?&?@@@ $))++A:'###MMOOOKs%   AB( (C	C	D D98D9c                    t          j                    \  }}}t          |t                    rj	 |\  }\  }}}}	|r|dk    r|r|}t	          |||           n&# t
          t          f$ r t          |          }Y nw xY wt          j	        d|  d|            n-t          j                     t          j	        d|  d|            d }d S )Nz<string>z
Failed to z - syntax error - z - )rF   exc_inforp   r   r   r   r   strr   r   r   r   )
r   syntaxErrorPathNametyprL   tbr   rY   liner  texts
             r    r   r   ]  s    |~~C"';'' ?	/6,C,(D#t /J 6 6<O 6.HdC0000:& 	 	 	g,,CCC	H4HH3HHIIII=4==G==>>>	BBBs   'A  A98A9c                     	 t          d          S # t          $ r Y nw xY wd} 	 t          j        t          j        dt          j        z            }n7# t          j        $ r% t          d           t          d| z             Y d S w xY wt          j                            |d| z            }	 t          j        |           n'# t          $ r t          d|  d| d	           Y d S w xY wt          j                            |          \  }}t          j                            |          d
         }t          j                            d
|           	 t          |          t          j        d
= S # t          j        d
= w xY w)Nr   ztabnanny.pyz)SOFTWARE\Python\PythonCore\%s\InstallPathzDWARNING - The Python registry does not have an 'InstallPath' settingz*          The file '%s' can not be locatedzTools\Scripts\%szWARNING - The file 'z' can not be located in path 'r   r   )
__import__rB   win32apiRegQueryValuer-   HKEY_LOCAL_MACHINErF   winverrI   rJ   rP   rG   rW   r   r   rQ   rR   r   )r   rG   rZ   tabnannyhometabnannybases        r    r   r   o  s   *%%%    H%';szJ
 
 >   TUUU:hGHHHtt GLL3h>??E
   TXTTTTTTUUUtt "$u!5!5L,7##L11!4LHOOA|$$$,'' HQKKCHQKs;    
,A 0BB,C  C%$C%E, ,E;c                 n   t           j                            |           s| }t          j        D ]}t           j                            t           j                            ||                    } t           j                            |           rm|rjt          j        ddddd          }|	                    |            |
                                }|t          j        k    r|                                }  n- dS | dz  } t           j                            |           r ndS t          j        |           S )z>Given a file name, return a fully qualified file name, or Noner   z*.pyNr   z&Python Files (*.py)|*.py|All files|*.*r   )rP   rG   isfilerF   abspathrW   isdirr   r0   SetOFNInitialDirr2   r-   r3   r4   rH   )rY   bBrowseIfDirbaseNamerG   dr)   s         r    r   r     s!    7>>(## H 	 	Dwrw||D('C'CDDHw}}X&&  
$064,T A &&x000BX]**#$==??#tt
$ E!7>>(++ E 4H%%%r!   )r   )NNr   N)r   r   r   r   r  ).r=   r   r|   rP   rF   r   r   r&  r-   r   	pywin.mfcr   pywin.mfc.docviewr   cmdliner   r   r   r   r   rQ   r%   r   r   r   r   r   rD   rM   r_   rc   rm   rs   r   r   r   r   r   r   r   r   r   r  r   r   r   r>   r!   r    <module>r7     sE    


     				 



               & & & & & &         ' (-uT{{ 
 
	         6=      F  
 
 
     D     
 
 
! ! ! !H 
$ B B B BJO O Od" " "J     F* * * *! ! ! !H   $  D& & & & & &r!   