forked from JanCaha/debug_vs_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
159 lines (131 loc) · 5.47 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# -*- coding: utf-8 -*-
"""
/***************************************************************************
DebugVS
A QGIS plugin
Plugin to connect Visual Studio Remote Debugger
-------------------
begin : 2018-10-16
copyright : (C) 2018 by Luiz Motta
email : [email protected]
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
This script initializes the plugin, making it known to QGIS.
"""
__author__ = 'Luiz Motta'
__date__ = '2018-10-16'
__copyright__ = '(C) 2018, Luiz Motta'
__revision__ = '$Format:%H$'
import os, sys
from qgis.PyQt.QtCore import QObject, pyqtSlot
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QToolButton, QMenu, QAction, QFileDialog
from qgis.core import QgsApplication
def classFactory(iface):
return DebugVSPlugin( iface )
class DebugVSPlugin( QObject ):
def __init__(self, iface):
super().__init__()
self.iface = iface
self.debugpy = None
try:
import debugpy
self.debugpy = debugpy
except:
pass
self.port = 5678
self.host = 'localhost'
self.actionsScript = []
self.toolButton = QToolButton()
self.toolButton.setMenu( QMenu() )
self.toolButton.setPopupMode( QToolButton.MenuButtonPopup )
self.toolBtnAction = self.iface.addToolBarWidget( self.toolButton )
self.msgBar = iface.messageBar()
self.pluginName = 'DebugVS'
self.nameActionEnable = 'Enable Debug for Visual Studio'
self.action = None
# Check exist sys.argv - /debugpy/.../pydevd_process_net_command
if not hasattr(sys, 'argv'):
sys.argv = []
def initGui(self):
# Action Run
icon = QIcon( os.path.join( os.path.dirname(__file__), 'code.svg' ) )
self.actionEnable = QAction( icon, self.nameActionEnable, self.iface.mainWindow() )
self.actionEnable.setToolTip( self.nameActionEnable )
self.actionEnable.triggered.connect( self.enable )
self.iface.addPluginToMenu( f"&{self.nameActionEnable}" , self.actionEnable )
# Action Load Script
title = 'Load script'
icon = QgsApplication.getThemeIcon('mActionScriptOpen.svg')
self.actionLoad = QAction( icon, title, self.iface.mainWindow() )
self.actionLoad.setToolTip( title )
self.actionLoad.triggered.connect( self.load )
self.iface.addPluginToMenu( f"&{self.nameActionEnable}" , self.actionLoad )
#
m = self.toolButton.menu()
m.addAction( self.actionEnable )
m.addAction( self.actionLoad )
self.toolButton.setDefaultAction( self.actionEnable )
def unload(self):
for action in [ self.actionEnable, self.actionLoad ] + self.actionsScript:
self.iface.removePluginMenu( f"&{self.nameActionEnable}", action )
self.iface.removeToolBarIcon( action )
self.iface.unregisterMainWindowAction(action)
self.iface.removeToolBarIcon( self.toolBtnAction )
def _addActionScript(self, filename):
icon = QgsApplication.getThemeIcon('processingScript.svg')
title = os.path.split( filename )[-1]
action = QAction( icon, title, self.iface.mainWindow() )
action.setToolTip( filename )
action.triggered.connect( self.run )
m = self.toolButton.menu()
m.addAction( action )
self.actionsScript.append( action )
def _existsActionScript(self, filename):
filenames = [ a.toolTip() for a in self.actionsScript ]
return filename in filenames
def _checkEnable(self):
if not self.debugpy.is_attached():
self.msgBar.popWidget()
msg = f"{self.nameActionEnable} AND attach in Visual Studio Code"
self.msgBar.pushWarning( self.pluginName, msg )
return False
return True
@pyqtSlot(bool)
def enable(self, checked):
self.msgBar.popWidget()
if self.debugpy is None:
self.msgBar.pushCritical( self.pluginName, "Need install debugpy: pip3 install debugpy")
return
msgPort = f'"request": "attach", "Port": {self.port}, "host": "{self.host}"'
if self.debugpy.is_client_connected():
self.msgBar.pushWarning( self.pluginName, f"Remote Debug for Visual Studio is active({msgPort})")
return
t_, self.port = self.debugpy.listen( ( self.host, self.port ) )
msgPort = f'"request": "enable_attach", "Port": {self.port}, "host": "{self.host}"'
self.msgBar.pushInfo( self.pluginName, f"Remote Debug for Visual Studio is running({msgPort})")
@pyqtSlot(bool)
def load(self, checked):
if not self._checkEnable():
return
filename, _ = QFileDialog.getOpenFileName(None, 'Debug script', '','Python Files (*.py)' )
if not filename:
return
self.debugpy.wait_for_client()
exec(open(filename).read())
if not self._existsActionScript( filename ):
self._addActionScript( filename )
@pyqtSlot(bool)
def run(self, checked):
if not self._checkEnable():
return
action = self.sender()
filename = action.toolTip()
self.debugpy.wait_for_client()
exec(open(filename).read())