-
Notifications
You must be signed in to change notification settings - Fork 0
/
attach.py
113 lines (83 loc) · 3.3 KB
/
attach.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
"""
This script adds the the containing package as a valid debug adapter in the Debugger's settings
"""
from os.path import join, abspath, dirname
from threading import Timer
import sublime
import time
import sys
adapter_type = "mayapy" # NOTE: type name must be unique to each adapter
package_path = dirname(abspath(__file__))
adapter_path = join(package_path, "adapter")
# The version is only used to display in the GUI
version = "1.0"
# You can have several configurations here depending on your adapter's offered functionalities,
# but they all need a "label", "description", and "body"
config_snippets = [
{
"label": "Maya: Python Debugging",
"description": "Run and Debug Python code in Maya",
"body": {
"name": "Maya: Python Debugging",
"type": adapter_type,
"program": "\${file\}",
"request": "attach", # can only be attach or launch
"interpreter": sys.executable,
"maya": # The host/port over which maya commands will be sent
{
"host": "localhost",
"port": 7001
},
"debugpy": # The host/port used to communicate with debugpy in maya
{
"host": "localhost",
"port": 7002
},
}
},
]
# The settings used by the Debugger to run the adapter.
settings = {
"type": adapter_type,
"command": [sys.executable, adapter_path]
}
# Instantiate variables needed for checking thread
running = False
check_speed = 1 # number of seconds to wait between checks for adapter presence in debugger instances
def check_for_adapter():
"""
Gets run in a thread to inject configuration snippets and version information
into adapter objects of type adapter_type in each debugger instance found
"""
from Debugger.modules.debugger.debugger import Debugger
while running:
time.sleep(check_speed)
for instance in Debugger.instances.values():
adapter = getattr(instance, "adapters", {}).get(adapter_type, None)
if adapter and not adapter.version:
adapter.version = version
adapter.snippets = config_snippets
def plugin_loaded():
""" Add adapter to debugger settings for it to be recognized """
# Add entry to debugger settings
debugger_settings = sublime.load_settings('debugger.sublime-settings')
adapters_custom = debugger_settings.get('adapters_custom', {})
adapters_custom[adapter_type] = settings
debugger_settings.set('adapters_custom', adapters_custom)
sublime.save_settings('debugger.sublime-settings')
# Start checking thread
global running, timer
running = True
Timer(1, check_for_adapter).start()
def plugin_unloaded():
""" This is done every unload just in case this adapter is being uninstalled """
# Wait for checking thread to finish
global running
running = False
time.sleep(check_speed + .1)
# Remove entry from debugger settings
debugger_settings = sublime.load_settings('debugger.sublime-settings')
adapters_custom = debugger_settings.get('adapters_custom', {})
adapters_custom.pop(adapter_type, "")
debugger_settings.set('adapters_custom', adapters_custom)
sublime.save_settings('debugger.sublime-settings')