Skip to content
This repository was archived by the owner on Aug 28, 2020. It is now read-only.

Commit b55321a

Browse files
committed
update code documentation after refactoring the debugger
1 parent 35fb919 commit b55321a

File tree

3 files changed

+98
-22
lines changed

3 files changed

+98
-22
lines changed

pugdebug/debugger.py

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717

1818
class PugdebugDebugger(QObject):
19+
1920
server = None
2021

2122
connections = deque()
@@ -42,15 +43,20 @@ class PugdebugDebugger(QObject):
4243
def __init__(self):
4344
"""Init the debugger object
4445
45-
Create a PugdebugServer object used to communicate with xdebug client
46-
through TCP.
46+
Create a PugdebugServer objects that listens to new connections
47+
from xdebug.
4748
48-
Connect signals to slots.
49+
Connect server signals to slots.
4950
"""
5051
super(PugdebugDebugger, self).__init__()
5152

5253
self.server = PugdebugServer()
5354

55+
self.connect_server_signals()
56+
57+
def connect_server_signals(self):
58+
"""Connect server signals to slots
59+
"""
5460
self.server.server_connected_signal.connect(
5561
self.handle_server_connected
5662
)
@@ -59,25 +65,38 @@ def __init__(self):
5965
)
6066

6167
def connect_connection_signals(self, connection):
68+
"""Connect signals for a new connection
69+
70+
Connect signals that gets emitted when a connection is stopped
71+
or detached, when a step command is done, when variables are read,
72+
when stacktraces are read, when breakpoints are set or removed,
73+
when breakpoints are read, when expressions are evaluated.
74+
"""
75+
76+
# Stop/detach signals
6277
connection.server_stopped_signal.connect(
6378
self.handle_server_stopped
6479
)
6580
connection.server_detached_signal.connect(
6681
self.handle_server_stopped
6782
)
6883

84+
# Step command signals
6985
connection.server_stepped_signal.connect(
7086
self.handle_server_stepped
7187
)
7288

89+
# Variables signals
7390
connection.server_got_variables_signal.connect(
7491
self.handle_server_got_variables
7592
)
7693

94+
# Stacktraces signals
7795
connection.server_got_stacktraces_signal.connect(
7896
self.handle_server_got_stacktraces
7997
)
8098

99+
# Breakpoints signals
81100
connection.server_set_init_breakpoints_signal.connect(
82101
self.handle_server_set_breakpoint
83102
)
@@ -91,6 +110,7 @@ def connect_connection_signals(self, connection):
91110
self.handle_server_listed_breakpoints
92111
)
93112

113+
# Expressions signals
94114
connection.server_expression_evaluated_signal.connect(
95115
self.handle_server_expression_evaluated
96116
)
@@ -100,6 +120,13 @@ def connect_connection_signals(self, connection):
100120

101121
def cleanup(self):
102122
"""Cleanup debugger when it's done
123+
124+
If there is an active connection, disconnect from it and clear
125+
all the remaining connections.
126+
127+
Stop the server from listening.
128+
129+
Clean up attributes.
103130
"""
104131
if self.is_connected():
105132
self.current_connection.disconnect()
@@ -113,9 +140,13 @@ def cleanup(self):
113140
self.current_line = 0
114141

115142
def is_connected(self):
143+
"""Is there an active connection
144+
"""
116145
return self.current_connection is not None
117146

118147
def has_pending_connections(self):
148+
"""Are there any pending connections?
149+
"""
119150
return len(self.connections) > 0
120151

121152
def start_debug(self):
@@ -126,9 +157,13 @@ def start_debug(self):
126157
self.server.connect()
127158

128159
def handle_server_connected(self, connection):
129-
"""Handle when server gets connected
160+
"""Handle when the server establishes a new connection
161+
162+
Connect the signals for the new connection.
163+
164+
Add it to the queue of connections.
130165
131-
Emit a debugging started signal.
166+
If there is no active connection, start the new connection.
132167
"""
133168
self.connect_connection_signals(connection)
134169

@@ -138,6 +173,12 @@ def handle_server_connected(self, connection):
138173
self.start_new_connection()
139174

140175
def start_new_connection(self):
176+
"""Start a new connection
177+
178+
Get the first (oldest) connection from the queue, set it's init
179+
message as the init message for the session, set it as the current
180+
connection and emit the debugging started signal.
181+
"""
141182
connection = self.connections.popleft()
142183
self.init_message = connection.init_message
143184
self.current_connection = connection
@@ -146,22 +187,27 @@ def start_new_connection(self):
146187

147188
def stop_debug(self):
148189
"""Stop a debugging session
190+
191+
If there is an active connection, stop only that one, otherwise
192+
stop the server from listening to new connections.
149193
"""
150194
if self.is_connected():
151195
self.current_connection.stop()
152196
else:
153197
self.server.stop()
154198

155199
def detach_debug(self):
156-
"""Detach a debugging session
200+
"""Detach the current connection
157201
"""
158202
if self.is_connected():
159203
self.current_connection.detach()
160204

161205
def handle_server_stopped(self):
162-
"""Handle when server gets disconnected
206+
"""Handle when a server stopped signal is received
207+
208+
If there are pending connections, start a new one.
163209
164-
Emit a debugging stopped signal.
210+
Otherwise clean up and emit a server stopped signal.
165211
"""
166212
if self.has_pending_connections():
167213
self.start_new_connection()

pugdebug/pugdebug.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,42 +136,53 @@ def connect_toolbar_action_signals(self):
136136
def connect_debugger_signals(self):
137137
"""Connect debugger signals
138138
139-
Connect signal that gets emitted when the debugging is started.
139+
Connecting signals that get emitted when a session starts or stops,
140+
when a step command is completed, when variables are read, when
141+
stacktraces are read, when a breakpoint gets removed, when breakpoints
142+
are read, when one or more expressions are evaluated.
140143
141-
Connect signal that gets emmitted when the debugging is stopped.
142-
143-
Connect signal that gets emitted when a step command is execute.
144-
145-
Connect signal that gets emitted when all variables from xdebug are
146-
read.
144+
Connect signal that gets emitted when an error happens during the
145+
debugging session.
147146
"""
148147

148+
# Start/stop signals
149149
self.debugger.debugging_started_signal.connect(
150150
self.handle_debugging_started
151151
)
152152
self.debugger.debugging_stopped_signal.connect(
153153
self.handle_debugging_stopped
154154
)
155+
156+
# Step command signals
155157
self.debugger.step_command_signal.connect(self.handle_step_command)
158+
159+
# Variables signals
156160
self.debugger.got_all_variables_signal.connect(
157161
self.handle_got_all_variables
158162
)
163+
164+
# Stacktraces signals
159165
self.debugger.got_stacktraces_signal.connect(
160166
self.handle_got_stacktraces
161167
)
168+
169+
# Breakpoints signals
162170
self.debugger.breakpoint_removed_signal.connect(
163171
self.handle_breakpoint_removed
164172
)
165173
self.debugger.breakpoints_listed_signal.connect(
166174
self.handle_breakpoints_listed
167175
)
176+
177+
# Expression signals
168178
self.debugger.expression_evaluated_signal.connect(
169179
self.handle_expression_evaluated
170180
)
171181
self.debugger.expressions_evaluated_signal.connect(
172182
self.handle_expressions_evaluated
173183
)
174184

185+
# Error signals
175186
self.debugger.error_signal.connect(
176187
self.handle_error
177188
)
@@ -339,9 +350,12 @@ def start_debug(self):
339350
def handle_debugging_started(self):
340351
"""Handle when debugging starts
341352
342-
This handler should be called when the connection to
343-
xdebug is established and the initial message from xdebug
344-
is read.
353+
This handle should be called when a new connection is
354+
established with xdebug.
355+
356+
When debugging multiple requests, for example ajax requests,
357+
this should be called for every request, as every request
358+
has its own connection.
345359
346360
Sets initial breakpoints, breakpoints that were set before the
347361
debugging session started.
@@ -359,7 +373,11 @@ def handle_debugging_started(self):
359373
def stop_debug(self):
360374
"""Stop a debugging session
361375
362-
Stop the currently active debugging session (if any).
376+
If the debugging session has a currently active connection,
377+
it will stop that connection.
378+
379+
If there is no active connections, the debugging session will
380+
tell the server to stop listening to new connections.
363381
"""
364382
self.debugger.stop_debug()
365383

pugdebug/server.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ def stop(self):
4848
self.wait_for_accept = False
4949

5050
def __connect(self):
51+
"""Connect to the server and listen to new incomming connections
52+
53+
For every accepted connection, see if it is valid and emit a signal
54+
with that new connection.
55+
56+
Otherwise silently disregard that connection.
57+
"""
5158
socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
5259
socket_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
5360
socket_server.settimeout(1)
@@ -122,9 +129,14 @@ def __init__(self, socket):
122129
self.parser = PugdebugMessageParser()
123130

124131
def init_connection(self):
125-
"""
126-
No need to do this in a new thread, it is already inside
127-
a thread separate from the main thread.
132+
"""Init a new connection
133+
134+
Read in the init message from xdebug and decide based on the
135+
idekey should this connection be accepted or not.
136+
137+
Do note that it is not needed to call it from a new thread, as
138+
it is already called from a thread separate from the main application
139+
thread and thus should not block the main thread.
128140
"""
129141
idekey = get_setting('debugger/idekey')
130142

0 commit comments

Comments
 (0)