Skip to content

Commit c7ce28c

Browse files
committed
Cleanup and adjusted comments
1 parent a5a4d43 commit c7ce28c

4 files changed

Lines changed: 104 additions & 96 deletions

File tree

pyghthouse/_thread.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ class PHThread(Thread):
1313
- Main routine: Loop for sending frames to the webserver.
1414
- Ending Phase: Closes the connection and cleanup threads.
1515
16-
In the main routine, this thread will build and send frames from
17-
**canvas**. The time between each frame is indicated by
18-
**send_interval**.
16+
In the main routine, this thread will build and send frames from **canvas**. The time between each frame is
17+
indicated by **send_interval**.
1918
2019
Attributes
2120
----------
@@ -36,13 +35,14 @@ class PHThread(Thread):
3635
3736
ready : Event
3837
Event flag is set to *True* in the send process of a frame.
39-
This flag will always be *True* when connected is *True*, unless the even is unset Can be used for waiting operations.
38+
This flag will always be *True* when connected is *True*, unless the event is unset.
39+
Used for waiting operations (see method `Pyghthouse.wait()`).
4040
4141
stop_event : Event
4242
Indicates when the Pyghthouse routine should be stopped.
4343
4444
error : Event
45-
Event flag is set to *True* when an error accured inside the pyghthouse routine.
45+
Event flag is set to *True* when an error occured inside the pyghthouse routine.
4646
"""
4747

4848
def __init__(self, send_interval, image_callback, canvas:PyghthouseCanvas,
@@ -138,7 +138,7 @@ def _send_image(self):
138138
"""
139139
Build and send current frame.
140140
141-
If error accures process, the connection will be closed.
141+
Also sets the **ready** flag after successful frame creation.
142142
"""
143143
if self.callback is not None:
144144
self._set_callback_image()
@@ -153,6 +153,8 @@ def _send_image(self):
153153
def _set_callback_image(self):
154154
"""
155155
Set image from callback function.
156+
157+
When an error occures, the connection will be closed and the exeption will be stored for further handeling in `ph.py`
156158
"""
157159
try:
158160
image_from_callback = self.callback()

pyghthouse/connection/wsconnector.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class WSConnector:
1616
Event flag is set to *True* when websocket thread is connected to the webserver and **send** can be used.
1717
1818
error : Event
19-
Event flag is set to *True* when error accured and connection is closed.
19+
Event flag is set to *True* when error occured and connection is closed.
2020
2121
The flag **error** has a higher priotiy than the **connected** flag. Meaning when **error** is set to *True*, the
2222
connection is closed even when **connected** can be set to *True*.
2323
24-
For further invastigation, check developer notes in **on_error**
24+
For further invastigation, check developer notes in `_on_error()`.
2525
"""
2626

2727
def __init__(self, username:str, token:str, address:str,
@@ -73,10 +73,7 @@ def __init__(self, username:str, token:str, address:str,
7373
if ignore_ssl_cert:
7474
kwargs = None
7575

76-
self.timeout = 3
77-
if timeout > 0:
78-
self.timeout = timeout
79-
setdefaulttimeout(self.timeout)
76+
self.set_timeout(timeout)
8077

8178
self.thread = Thread(target=self.ws.run_forever, kwargs=kwargs)
8279

@@ -90,8 +87,8 @@ def open(self):
9087
After opening the websocket, the websocket thread sets the **connected** event flag to *True* and is ready to
9188
send data.
9289
93-
When an error accured upon opening, the **error** flag will be set to *True* and **on_error()** will be called.
94-
In this case, the connection will be closed again and the **connected** flag will cleared to *False* again.
90+
When an error occured upon opening, the **error** flag will be set to *True* and `_on_error()` will be called.
91+
In this case, the connection will be closed again and the **connected** flag will be cleared to *False* again.
9592
"""
9693
if self.verbosity == VerbosityLevel.ALL:
9794
print("Opening websocket connection.")
@@ -111,7 +108,7 @@ def open(self):
111108

112109
def send(self, data):
113110
"""
114-
Send data via websocket connection.
111+
Sends data via websocket connection.
115112
116113
Raises a *WebSocketConnectionClosedException* when no connection is present.
117114
"""
@@ -139,9 +136,9 @@ def send(self, data):
139136

140137
def close(self):
141138
"""
142-
Close websocket connection.
139+
Closes websocket connection.
143140
144-
This function can still be used when no connection is present.
141+
This function can still be used when no connection is present. In this case, nothing will happen.
145142
"""
146143
if self.verbosity == VerbosityLevel.ALL:
147144
print("Closing connection.")
@@ -163,13 +160,13 @@ def construct_package(self, payload_data):
163160

164161

165162
def set_timeout(self, timeout=10):
166-
self.timeout = 3
163+
self.timeout = 2.5
167164
if timeout > 0:
168165
self.timeout = timeout
169166
setdefaulttimeout(self.timeout)
170167

171168

172-
# Functions used by the websocket thread:
169+
## Functions used by the websocket thread ##
173170

174171
def _on_open(self, ws: WebSocketApp):
175172
"""
@@ -197,12 +194,12 @@ def _on_close(self, ws: WebSocketApp, close_status_code, close_msg):
197194
----------------
198195
Developer notes:
199196
200-
When the websocket thread has been started, **on_close** will always be called, even when an error accured upon
201-
opening the connection. This is a result of the **teardown()** function in WebSocketApp.
197+
When the websocket thread has been started, `_on_close()` will always be called, even when an error occured upon
198+
opening the connection. This is a result of the `teardown()` function in WebSocketApp.
202199
203-
So its possible that **on_close()** will be called even when **on_open()** hasn't been called yet.
200+
So its possible that `_on_close()` will be called even when `_on_open()` hasn't been called yet.
204201
205-
For further invastigation, check the developer notes in **on_error()**.
202+
For further invastigation, check the developer notes in `_on_error()`.
206203
"""
207204
if self.verbosity == VerbosityLevel.ALL:
208205
print("Connection closed.")
@@ -216,23 +213,23 @@ def _on_error(self, ws: WebSocketApp, err: Exception):
216213
217214
This function will save the **exception** and set the flag **error** to *True*.
218215
219-
This function also sets the flag **connected** to *True* to avoid blocking of **open** when an error accured
216+
This function also sets the flag **connected** to *True* to avoid blocking of **open** when an error occured
220217
upon opening the connection.
221218
222219
----------------
223220
Developer notes:
224221
225-
This method is used by WebSocketApp as callback function. The intend is to signal that an error accured in
222+
This method is used by WebSocketApp as callback function. The intend is to signal that an error occured in
226223
WebSocketApp and to allow further error handeling outside of WebSocketApp.
227224
228-
Because we use **run_forever()** without a parameter for **reconnect()**, we use the standard of *0* from the
225+
Because we use `run_forever()` without a parameter for `reconnect()`, we use the standard of *0* from the
229226
websocket-client library. This will always force a teardown of the connection without an attempt to reconnect
230227
upon error.
231228
232-
A teardown of the connection will also call the **_on_close()** callback function, even when **_on_error()**
233-
has been the called. **_on_close()** will even be called when an error accured on the attempt to open the
229+
A teardown of the connection will also call the `_on_close()` callback function, even when `_on_error()`
230+
has been the called. `_on_close()` will even be called when an error occured on the attempt to open the
234231
websocket.
235-
Meaning, **_on_close()** can be called even when **_on_open()** hasn't been called yet.
232+
Meaning, `_on_close()` can be called even when `_on_open()` hasn't been called yet.
236233
237234
Do not raise an exception here!
238235
--------------------------------

pyghthouse/data/canvas.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ def __init__(self, initial_image=None):
1515

1616

1717
def set_image(self, new_image: list) -> True:
18-
18+
"""
19+
Set the canvas to the **new_image**.
20+
21+
Throws exeptions when an invalid image object has been given.
22+
23+
This function is thread-safe.
24+
"""
25+
# Catch invalid image objects
1926
self._check_size(new_image)
2027
self._check_cells(new_image)
2128
self._check_values(new_image)
2229

30+
# Setting the image
2331
with self.lock:
2432

2533
for y in range(self.IMAGE_SHAPE[0]):
@@ -32,7 +40,11 @@ def set_image(self, new_image: list) -> True:
3240

3341

3442
def get_bytes_image(self):
35-
43+
"""
44+
Returns the currently saved image in a bytearray.
45+
46+
This function is thread-safe.
47+
"""
3648
image_bytes = b''
3749

3850
with self.lock:
@@ -46,7 +58,11 @@ def get_bytes_image(self):
4658

4759

4860
def copy_image(self):
61+
"""
62+
Returns a copy of the currently saved image.
4963
64+
This function is thread-safe.
65+
"""
5066
image_copy = [[[0 for k in range(self.IMAGE_SHAPE[2])] for j in range(self.IMAGE_SHAPE[1])] for i in range(self.IMAGE_SHAPE[0])]
5167

5268
with self.lock:
@@ -60,8 +76,12 @@ def copy_image(self):
6076
return image_copy
6177

6278

63-
def _check_size(self, other: list):
79+
## Internal error checking functions ##
6480

81+
def _check_size(self, other: list):
82+
"""
83+
Check if we received a 3-dimensional list with the correct sizes.
84+
"""
6585
try:
6686
other_size = (len(other), len(other[0]), len(other[0][0]))
6787

@@ -76,8 +96,12 @@ def _check_size(self, other: list):
7696

7797

7898
def _check_cells(self, other: list):
79-
80-
# Catch objects like [[[0,1,2],[0,1,2,3,4,5],[1]],1]
99+
"""
100+
Check for the existence of all expected cells in our image object. Also catches if we have too many cells.
101+
102+
We need this additional check because `_check_size()` only ensures that we received a 3-dimensional list.
103+
"""
104+
# Catch objects like [[[0,1,2],[0,1,2,3,4,5],[1]],1,...]
81105
for y in range(self.IMAGE_SHAPE[0]):
82106
try:
83107

@@ -99,7 +123,9 @@ def _check_cells(self, other: list):
99123

100124

101125
def _check_values(self, other: list):
102-
126+
"""
127+
Check if all cells have valid numbers.
128+
"""
103129
for y in range(self.IMAGE_SHAPE[0]):
104130
for x in range(self.IMAGE_SHAPE[1]):
105131
for rgb in range(self.IMAGE_SHAPE[2]):

0 commit comments

Comments
 (0)