
    @};d=                     j   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mZmZm	Z	m
Z
mZmZ d dlmZ d dlZ ej                  d ej                                 dZ	 d dlZdZ	 d dlZdZ G d d	e
j,                  ej.                        Z G d
 dej2                        Z G d d      Zy#  dZY MxY w#  dZY OxY w)    N)GLibGtkGdkGioGObjectPangoPeas)RB	rhythmboxi.  TFc                       e Zd Zd Z ej
                  ej                        Zd Zd Z	d Z
d Zd Zd Zd Zej                   j"                  fd	Zd
 Zd Zy)PythonConsolePlugin)typec                 P    t         j                  j                  |        d | _        y N)r   Object__init__windowselfs    K/usr/lib/x86_64-linux-gnu/rhythmbox/plugins/python-console/pythonconsole.pyr   zPythonConsolePlugin.__init__C   s    	..$$+    c           	         | j                   }|j                  j                  }t        j                  j                  dd       }|j                  d| j                  |       |j                  |       |j                  ddt        j                  j                  t        d      d             t        j                  j                  dd       | _        | j                  j                  d| j                  |       |j                  | j                         |j                  ddt        j                  j                  t        d      d	             t        j                  j                  d
d       | _        | j                  j                  d| j                  |       |j                  | j                         |j                  dd
t        j                  j                  t        d      d             y )Npython-consoleactivatetoolszPython Consolezapp.python-console)labeldetailed_actionpython-debugger-winpdbzPython Debugger (winpdb)zapp.python-debugger-winpdbpython-debugger-debugpyzPython Debugger (debugpy)zapp.python-debugger-debugpy)objectpropsapplicationr   SimpleActionnewconnectshow_console
add_actionadd_plugin_menu_itemMenuItem_rpdb2_actionattach_winpdbdebugpy_actionenable_debugpy)r   shellappactions       r   do_activatezPythonConsolePlugin.do_activateG   s   
++%# 0$7&..T..6..7	llQ/0.  01
 &&**+CTJ$J(:(:EB..""#7	llQ9:5  78
 ((,,-FM$j$*=*=uE..$$%7	llQ:;6  89r   c                    | j                   }|j                  j                  }|j                  dd       |j	                  d       |j                  dd       |j	                  d       d | _        |j                  dd       |j	                  d       d | _        | j                  | j                  j                          y y )Nr   r   r   r   )	r    r!   r"   remove_plugin_menu_itemremove_actionr+   r-   r   destroy)r   r/   r0   s      r   do_deactivatez!PythonConsolePlugin.do_deactivatef   s    
++%#g'78$%g'?@,-$g'@A-.$	[[;; r   c                 T   | j                   st        t        |d}t        || j                        }|j                  dd       |j                  dt        d      z   dz   d       t        j                         | _         | j                   j                  d	       | j                   j                  |       | j                   j                  d
| j                         | j                   j                          n| j                   j                          | j                   j                          y )N)__builtins__r
   r/   )	namespace
destroy_cbiX  i  zprint("z=You can access the main window through the 'shell' variable :z\n%s" % shell)FzRhythmbox Python Consoler6   )r   r9   r
   PythonConsoledestroy_consoleset_size_requestevalr*   r   Window	set_titleaddr%   show_all
grab_focus)r   r1   	parameterr/   nsconsoles         r   r&   z PythonConsolePlugin.show_consoley   s    	&	2 r(,(<(<>7
C%
<<	 3 44 "" $)*
 4;;;34;;??7;;y$"6"67;;;;++r   c                    t         du r.| j                  dd       | j                  j                  d       yt        j
                  j                  t        j                         d      }t        d      |z  }t        j                  d dt        j                  j                  t        j                  j                  |      }|j!                         t        j"                  j$                  k(  rsd}t        j
                  j'                  |      r9t)        |      }|j+                         j-                         }|j/                          d }	t1        j2                  |	|       |j5                          y)	NFrpdb2winpdbrpdb2_passwordzAfter you press OK, Rhythmbox will wait until you connect to it with winpdb or rpdb2. If you have not set a debugger password in the file %s, it will use the default password ('rhythmbox').r   r   c                 .    t        j                  |        y)NF)rI   start_embedded_debugger)passwords    r   start_debuggerz9PythonConsolePlugin.attach_winpdb.<locals>.start_debugger   s    	!!(+r   )
have_rpdb2missing_python_moduler+   set_enabledospathjoinr
   user_data_dirr*   r   MessageDialogMessageTypeINFOButtonsType	OK_CANCELrunResponseTypeOKexistsopenreadrstripcloser   idle_addr6   )
r   r1   rE   r/   pwd_pathmsgdialogrN   pwd_filerO   s
             r   r,   z!PythonConsolePlugin.attach_winpdb   s   5gx0  '
WW\\"**,.>?(	  J  	K  NV  	V#T1coo&:&:COO<U<UWZ[&ZZ\S%%(((8ggnnXH~H}}%%'HNN ==*..	r   c                    t         du r.| j                  dd       | j                  j                  d       y	 t	        j
                  t              \  }}t        j                  j                  }t        d      |z  }| j                  j                  d       | j                  ||       y# t        $ r;}t        j                  j                  }t        d      t        |      z  }Y d }~Rd }~ww xY w)NFdebugpyzRhythmbox is now listening for Debug Adapter Protocol connections on port %d. You can now attach to it using a compatible debugger such as vimspector, nvim-dap or Visual Studio Code.z3Unable to start Debug Adapter Protocol listener: %s)have_debugpyrQ   r-   rR   rj   listen
DEBUG_PORTr   rX   rY   r*   	ExceptionERRORstrmessage_dialog)	r   r1   rE   r/   hostportmsgtyperf   es	            r   r.   z"PythonConsolePlugin.enable_debugpy   s    Ui3""5)
L
+JD$oo""G
  E  F  IM  MC##E*
 c7#	  Loo##G
A
BSV
KCLs   AB* *	C.31C))C.c                 x    t        d      ||fz  }| j                  |t        j                  j                         y )NzqThe %s Python module is not available. Install the module and then restart Rhythmbox to enable debugging with %s.)r*   rq   r   rX   ro   )r   moduledebuggerrf   s       r   rQ   z)PythonConsolePlugin.missing_python_module   sB    	
}~  CI  KS  BT  	T#c3??001r   c                     t        j                  d d|t         j                  j                  |      }|j	                  d| j
                         |j                          y )Nr   response)r   rW   rZ   r^   r%   message_dialog_responseshow)r   rf   rt   rg   s       r   rq   z"PythonConsolePlugin.message_dialog   sA    T1gs/A/A3G&..T99:++-r   c                 $    |j                          y r   )r6   )r   rg   rsps      r   r{   z+PythonConsolePlugin.message_dialog_response   s    ..r   c                 F    | j                   j                          d | _         y r   )r   r6   )r   argss     r   r=   z#PythonConsolePlugin.destroy_console   s    ++$+r   N)__name__
__module____qualname____gtype_name__r   propertyr   r    r   r2   r7   r&   r,   r.   rQ   r   rX   rY   rq   r{   r=    r   r   r   r   >   s^    '
'

7>>
29>&,0$2 (+';'; r   r   c                   p    e Zd Zd Zd Zi dfdZd Zd Zd Zd Z	d	 Z
d
 Zd Zd ZddZddZd Zd Zy)r<   c                 R    | j                   j                         j                         S r   )view
get_bufferget_end_iterr   s    r   r   zPythonConsole.get_end_iter   s    					,	,	..r   c                 T    | j                   j                         j                  |      S r   )r   r   get_iter_at_mark)r   marks     r   r   zPythonConsole.get_iter_at_mark   s     					0	0	66r   Nc                    t         j                  j                  |        | j                  t         j                  j
                  t         j                  j                         | j                  t         j                  j                         t        j                         | _        | j                  j                  t        j                  d             | j                  j                  d       | j                  j!                  t         j"                  j$                         | j'                  | j                         | j                  j)                          | j                  j+                         }|j-                  d      | _        |j-                  d      | _        | j0                  j3                  dd       |j-                  d      | _        | j4                  j3                  dd       t7        j8                  d	      | _        || _        || _        d
| _         |jC                  d| jE                         d       |jG                  | jE                         d       |jC                  d| jE                         d       dg| _$        d| _%        d| _&        | jH                  | j<                  d<   tO        | tP        jR                  jU                         | j.                        | _)        tO        | tP        jV                  jU                         | j0                        | _+        | j                  jY                  d| jZ                         |jY                  d| j\                         y )N	MonospaceTnormalerror
foregroundredcommandbluez^\s+F
input-line>>> input r   __history__zkey-press-eventzmark-set)/r   ScrolledWindowr   
set_policy
PolicyTypeNEVER	AUTOMATICset_shadow_type
ShadowTypeNONETextViewr   modify_fontr   font_description_from_stringset_editableset_wrap_modeWrapModeCHARrB   r|   r   
create_tagr   r   set_propertyr   recompile_PythonConsole__spaces_patternr:   r;   block_commandcreate_markr   inserthistoryhistory_poscurrent_command
gtkoutfilesysstdoutfilenostderrr%   "_PythonConsole__key_press_event_cb_PythonConsole__mark_set_cb)r   r:   r;   buffers       r   r   zPythonConsole.__init__   sN   d#//#..&&(@(@As~~**+lln$)))::;GH))))#,,++,((499))..99!&!!(+$+!!'*$***,.""9-$,,,L&1**W-$$.$/$ 	\4#4#4#6=--!!#V,Wd//148 $,$$"&,,$.. 4!2!2!4dkkB$+4!2!2!4djjA$+ ))%t'@'@A..T//0r   c                    |j                   t        j                  k(  r8|j                  t        j                  j
                  k(  r| j                          y |j                   t        j                  k(  r}|j                  t        j                  j
                  k(  rU|j                         }|j                  d      }| j                  |      }| j                         }|j                  ||d      }| j                  |z   dz   | _        | j                  |       | j                         }|j                  |d       | j                         }|j!                  ||       t#        j$                  | j&                  |      }|A|j                  |||j)                         |j+                                 | j                         }|j-                  |       t/        j0                  | j2                         y|j                   t        j                  k(  r|j                         }|j                  d      }	|j                  d      }| j                  |      }| j                         }|j                  ||d      }| j                  |z   dz   | _        | j                  |       | j                  |	      }
|j5                  | j6                  |
|       |j                  |d       | j                  j9                         }|j;                  d      s| j                  dd  dk7  r| j<                  r
d| _        d	}n?|j;                  d
      rd	}n+| j?                  | j                         d| _        d| _        d}| j                         }|j!                  |	|       |j                  ||       | j                         }|j!                  ||       |j-                  |       t/        j0                  | j2                         y|j                   t        j@                  k(  s|j                   t        jB                  k(  rA|jE                  d       | jG                          t/        j0                  | j2                         y|j                   t        jH                  k(  s|j                   t        jJ                  k(  rA|jE                  d       | jM                          t/        j0                  | j2                         y|j                   t        jN                  k(  s:|j                   t        jP                  k(  s|j                   t        jR                  k(  rc|j                         }| j                  |j                  d            }| j                  |jU                               }|jW                  |      dk(  S |j                   t        jX                  k(  r||j                         }| j                  |j                  d            }|j                  t        j                  jZ                  k(  r|j]                  d|       y|j-                  |       yy )Nr   T
z
... r   :z

z... \r   Fr   key_press_eventr   r   )/keyvalr   KEY_DstateModifierTypeCONTROL_MASKr6   
KEY_Returnr   get_markr   r   get_textr   history_addr   	move_markr   matchr   startendplace_cursorr   rd   scroll_to_end	apply_tagr   rb   endswithr   _PythonConsole__runKEY_KP_DownKEY_Downemit_stop_by_namehistory_down	KEY_KP_UpKEY_Up
history_upKEY_KP_LeftKEY_LeftKEY_BackSpace
get_insertcompareKEY_Home
SHIFT_MASKmove_mark_by_name)r   r   eventr   inp_markinpcurlinespaceslin_marklin	cur_stripcom_marks                r   __key_press_event_cbz"PythonConsole.__key_press_event_cb   s   
\\SYY
[[C$$111<<>||s~~%{{c&&333OO6oog&8			x	(3				3
//#sD
)4..5<4D 
			3	==h				3	Hc" HHT**D16
MM#tFLLNVZZ\:;



C	s==##$
||s~~%OO6ool+8oog&8 
		x	(3				3
//#sD
)4..5<4D 
		x	(3	DLL#s+	==d##**,9BC F*t/A/ADH4 H 	JJt##$DDH 
			3	Hc"	==h				3	Hc"	s==##$
||s&||s||#+,==##$
||s}}$||szz!+,??==##$
||s&||s||#||s(((OO6			vw7	83			v002	33
++c
a
||s||# OO6			vw7	83kkS%%000
Xs+  
 $r   c                     | j                  |j                  d            }| j                  |j                               }| j                  j	                  |j                  |      dk7         y )Nr   )r   r   r   r   r   r   )r   r   iternamer   poss         r   __mark_set_cbzPythonConsole.__mark_set_cbo  sT    


 8
9%


 1 1 3
4#))U+r12r   c                     | j                   j                         }| j                  |j                  d            }| j	                         }|j                  ||d      S )Nr   T)r   r   r   r   r   r   )r   r   r   r   s       r   get_command_linezPythonConsole.get_command_linet  sN    99!&foog67##	c4	((r   c                 v   | j                   j                         }|j                  d      }| j                  |      }| j	                         }|j                  ||       |j                  ||       |j                  | j                  |      | j	                                | j                   j                          y )Nr   )	r   r   r   r   r   deleter   select_rangerD   )r   r   r   r   r   r   s         r   set_command_linezPythonConsole.set_command_linez  s    99!&		!$d###--S--Wd++D1'')+))r   c                     |j                         dk7  rRt        | j                        | _        || j                  | j                  dz
  <   | j                  j	                  d       y y )Nr      )striplenr   r   append)r   r   s     r   r   zPythonConsole.history_add  sQ    	ZZ\R$,,'4(,4<<  1$%<<r r   c                     | j                   dkD  rd| j                         | j                  | j                   <   | j                   dz
  | _         | j                  | j                  | j                             y y )Nr   r   )r   r   r   r   r   s    r   r   zPythonConsole.history_up  sb    	$($9$9$;4<<  !&&*4d&6&678 r   c                    | j                   t        | j                        dz
  k  rd| j                         | j                  | j                   <   | j                   dz   | _         | j	                  | j                  | j                             y y )Nr   )r   r   r   r   r   r   s    r   r   zPythonConsole.history_down  sp    	DLL)A--$($9$9$;4<<  !&&*4d&6&678 .r   c                 b    | j                         }| j                  j                  |dddd       y)N        F      ?)r   r   scroll_to_iter)r   r   s     r   r   zPythonConsole.scroll_to_end  s-    				$))4eS#6	r   c                    | j                   j                         }|!|j                  | j                         |       nx| j                         }|j	                         }|j                  ||       t        j                         }|j                  |      }|j                  ||| j                                t        j                  | j                         y r   )r   r   r   r   
get_offsetr   TextIterget_iter_at_offsetr   r   rd   r   )r   texttagr   r   offset
start_iters          r   writezPythonConsole.write  s    99!&[	==""$d+



4OO6	==t:))&1:	CT%6%6%89--""#r   c                    | j                   j                         }|j                  d      }|j                  | j	                  |      | j                                t        |t              st        |t              r=|D ]7  }|r"| j                  d|z   dz   | j                         | j                  |       9 n5|r"| j                  dz   dz   | j                         | j                  |       | j                         }|j                  d|       |j                  |d       | j                         }|j                  d|       | j                   j                  | j                         dddd       y )Nr   r   r   r   r  Fr  )r   r   r   r   r   r   
isinstancelisttupler  r   r   r   r   r  )r   r   display_commandr   r   cr   s          r   r?   zPythonConsole.eval  s-   99!&%#--%%c*				 *We"< q	ZZ
T!4<<0JJqM
 JJvzD $,,/::g#<---V#7C())4,,.UCEr   c                 t   | j                   t        j                   ct        _         | _         | j                  t        j                  ct        _        | _        	 	 t        || j                  | j                        }|t        t        |             | j                   t        j                   ct        _         | _         | j                  t        j                  ct        _        | _        y # t        $ r t        || j                         Y xw xY w#  t        t        d      r(t        j                  t        k(  r| j                          nt        j                          Y xY w# | j                   t        j                   ct        _         | _         | j                  t        j                  ct        _        | _        w xY w)N	last_type)r   r   r   r?   r:   printreprSyntaxErrorexechasattrr  
SystemExitr6   	traceback	print_exc)r   r   rs      r   __runzPythonConsole.__run  s    KK#*dk KK#*dk5"Wdnndnn5A}
47^ "[[#**3:t{![[#**3:t{  "$..!"c;CMMZ$?LLN![[#**3:t{![[#**3:t{s2   7C' 'D	D D		D AEE AF7c                 >    | j                   | j                          y y r   )r;   r   s    r   r6   zPythonConsole.destroy  s    	__ ?? !r   r   )F)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r?   r   r6   r   r   r   r<   r<      sX    /7 !# +1\m^3
)	99
$F.5*r   r<   c                   \    e 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eZy)r   zA fake output file object.  It sends output to a TK test widget,
	and if asked for a file number, returns one set on instance creationc                 .    || _         || _        || _        y r   )fnrG   r  )r   rG   r%  r  s       r   r   zgtkoutfile.__init__  s    $'$,$(r   c                      y r   r   r   s    r   rc   zgtkoutfile.close      r   c                      y r   r   r   s    r   flushzgtkoutfile.flush  r'  r   c                     | j                   S r   )r%  r   s    r   r   zgtkoutfile.fileno  s
    .r   c                      y)Nr   r   r   s    r   isattyzgtkoutfile.isatty  s    r   c                      yNr   r   r   as     r   ra   zgtkoutfile.read      r   c                      yr.  r   r   s    r   readlinezgtkoutfile.readline  r1  r   c                     g S r   r   r   s    r   	readlineszgtkoutfile.readlines  s    )r   c                 P    | j                   j                  || j                         y r   rG   r  r  )r   ss     r   r  zgtkoutfile.write      $,,,,Q9r   c                 P    | j                   j                  || j                         y r   r7  )r   ls     r   
writelineszgtkoutfile.writelines  r9  r   c                     t        d      N)   zIllegal seekIOErrorr/  s     r   seekzgtkoutfile.seek      (< ==r   c                     t        d      r>  r@  r   s    r   tellzgtkoutfile.tell  rC  r   N)r   r   r   __doc__r   rc   r)  r   r,  ra   r3  r5  r  r<  rB  rE  truncater   r   r   r   r     s5    I ("###99==r   r   )stringr   r   rS   r  gi.repositoryr   r   r   r   r   r   r	   r
   gettextinstall
locale_dirrm   rI   rP   rj   rk   r   Activatabler   r   r<   r   r   r   r   <module>rN     s   F  
 	 	  C C C   ]R]]_ -

J'..$*:*: JXNC&& N` M
s   B# B, #B),B2