Skip to content

Commit 67641f1

Browse files
authored
Merge pull request #21 from Code-Partners/feat-docstrings
Feat docstrings
2 parents d63a6a8 + 6c82e2d commit 67641f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+6043
-73
lines changed

common/context/binary_context.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,54 @@
55

66

77
class BinaryContext(ViewerContext):
8+
"""
9+
This is the base class for all viewer contexts, which deal with
10+
binary data. A viewer context is the library-side representation
11+
of a viewer in the Console.
12+
13+
.. note::
14+
This class is not guaranteed to be threadsafe.
15+
"""
16+
817
def __init__(self, viewer_id: ViewerId):
18+
"""
19+
Initializes a BinaryContext instance.
20+
21+
:param viewer_id: The viewer ID to use.
22+
"""
923
super().__init__(viewer_id)
1024
self.__data: BytesIO = BytesIO()
1125

1226
@property
13-
def viewer_data(self):
27+
def viewer_data(self) -> bytes:
28+
"""
29+
Overridden. Returns the actual binary data which will be
30+
displayed in the viewer specified by the ViewerId.
31+
32+
:rtype: binary
33+
"""
1434
return self.__data.getvalue()
1535

1636
def reset_data(self):
37+
"""
38+
Resets the internal data stream.
39+
40+
.. note::
41+
This method is intended to reset the internal data stream
42+
if custom handling of data is needed by derived classes.
43+
"""
44+
1745
self.__data.seek(0)
1846
self.__data.truncate()
1947

2048
def load_from_file(self, filename: str):
49+
"""
50+
Loads the binary data from a file.
51+
52+
:param filename: The name of the file to load the binary data from.
53+
:raises ValueError: The filename string is empty.
54+
"""
55+
2156
if filename == "":
2257
raise ValueError("filename not provided")
2358
else:
@@ -27,6 +62,17 @@ def load_from_file(self, filename: str):
2762
self.__data.write(content)
2863

2964
def append_bytes(self, bytestring: (bytes, bytearray), offset: int = 0, length: int = 0):
65+
"""
66+
Overloaded. Appends a buffer. Lets you specify the offset in
67+
the buffer and the amount of bytes to append.
68+
69+
:param bytestring: The buffer to append.
70+
:param offset: The offset at which to begin appending.
71+
:param length: The number of bytes to append.
72+
73+
:raises TypeError: if type requirements are not met by the arguments
74+
"""
75+
3076
if not isinstance(bytestring, bytes) and \
3177
not isinstance(bytestring, bytes):
3278
raise TypeError("bytestring must be bytes sequence")
@@ -41,6 +87,10 @@ def append_bytes(self, bytestring: (bytes, bytearray), offset: int = 0, length:
4187
self.__data.write(bytestring[offset: offset + length])
4288

4389
def close(self) -> None:
90+
"""
91+
Overridden. Releases any resources.
92+
"""
93+
4494
if not self.__data.closed:
4595
try:
4696
self.__data.close()
@@ -49,5 +99,11 @@ def close(self) -> None:
4999
# we are not expecting an exception
50100

51101
def load_from_stream(self, stream):
102+
"""
103+
Loads the binary data from a stream.
104+
105+
:param stream: The stream to load the binary data from.
106+
"""
107+
52108
self.reset_data()
53109
self.__data.write(stream)

common/context/binary_viewer_context.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@
33

44

55
class BinaryViewerContext(BinaryContext):
6+
"""Represents the binary viewer in the Console which can display binary
7+
data in a read-only hex editor.
68
9+
.. note::
10+
11+
The binary viewer in the Console interprets the data of a Log Entry as binary data
12+
and displays it in a read-only hex editor.
13+
14+
You can use the BinaryViewerContext class for creating custom log
15+
methods around for sending custom binary data.
16+
17+
.. note::
18+
19+
This class is not guaranteed to be threadsafe.
20+
"""
721
def __init__(self):
22+
"""
23+
Initializes a BinaryViewerContext instance.
24+
"""
825
super().__init__(ViewerId.BINARY)

common/context/data_viewer_context.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,24 @@
33

44

55
class DataViewerContext(TextContext):
6+
"""
7+
Represents the data viewer in the Console which can display simple
8+
and unformatted text.
9+
10+
.. note::
11+
The data viewer in the Console interprets the data of a Log Entry as text and displays it in a read-only text
12+
field.
13+
14+
You can use the DataViewerContext class for creating custom log
15+
methods around LogCustomContext for
16+
sending custom text data.
17+
18+
.. note::
19+
This class is not guaranteed to be threadsafe.
20+
"""
21+
622
def __init__(self):
23+
"""
24+
Initializes a DataViewerContext instance.
25+
"""
726
super().__init__(ViewerId.DATA)

common/context/inspector_viewer_context.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,50 @@
33

44

55
class InspectorViewerContext(ValueListViewerContext):
6+
"""
7+
Represents the inspector viewer in the Console which displays
8+
key/value pairs in an object inspector control.
9+
10+
The inspector viewer in the Console interprets the
11+
`data of a Log Entry <LogEntry.data>` as a key/value list with
12+
group support like object inspectors from popular IDEs. This class
13+
takes care of the necessary formatting and escaping required by the
14+
corresponding inspector viewer in the Console.
15+
16+
You can use the InspectorViewerContext class for creating custom
17+
log methods around Session.log_custom_context
18+
for sending custom data organized as grouped key/value pairs.
19+
20+
.. note::
21+
This class is not guaranteed to be threadsafe.
22+
"""
23+
624
def __init__(self):
25+
"""
26+
Initializes an InspectorViewerContext instance.
27+
"""
728
super().__init__(ViewerId.INSPECTOR)
829

930
def start_group(self, group: str):
31+
"""
32+
Starts a new group.
33+
34+
:param group: The name of the group to use.
35+
"""
1036
if isinstance(group, str):
1137
self.append_text("[")
1238
self.append_text(self.escape_item(group))
1339
self.append_text("]\r\n")
1440

1541
def escape_item(self, item: str) -> str:
42+
"""
43+
Overridden. Escapes a key or a value.
44+
45+
This method ensures that the escaped key or value does not contain any newline characters,
46+
such as the carriage return or linefeed characters.
47+
Furthermore, it escapes the '\\', '=', '[' and ']' characters.
48+
49+
:param item: The key or value to escape.
50+
:returns: The escaped key or value.
51+
"""
1652
return self.escape_line(item, "\\=[]")

common/context/list_viewer_context.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,47 @@
33

44

55
class ListViewerContext(TextContext):
6+
"""
7+
Represents the list viewer in the Console which can display simple lists of text data.
8+
9+
The list viewer in the Console interprets the data of a Log Entry as a list. Every line in the text data
10+
is interpreted as one item of the list. This class takes care of the necessary formatting and escaping required
11+
by the corresponding list viewer in the Console.
12+
13+
You can use the ListViewerContext class for creating custom log methods around LogCustomContext for sending
14+
custom data organized as simple lists.
15+
16+
.. note::
17+
This class is not guaranteed to be threadsafe.
18+
"""
19+
620
def __init__(self, viewer_id: ViewerId = ViewerId.LIST) -> None:
21+
"""
22+
Initializes a ListViewerContext instance using a viewer ID.
23+
24+
This constructor is intended for derived classes, such as the ValueListViewerContext class,
25+
which extend the capabilities of this class and use a different viewer ID.
26+
27+
:param viewer_id: The viewer ID to use.
28+
"""
729
super().__init__(viewer_id)
830

931
@staticmethod
1032
def escape_line(line: str = "", escape_chars: str = "") -> str:
33+
34+
"""
35+
Escapes a line.
36+
37+
This method ensures that the escaped line does not
38+
contain characters listed in the escape_chars parameter plus
39+
any newline characters, such as the carriage return or
40+
linefeed characters.
41+
42+
:param line: The line to escape.
43+
:param escape_chars: A string of characters which should be escaped in addition to the newline characters.
44+
Can be empty.
45+
:returns: The escaped line.
46+
"""
1147
if (
1248
not isinstance(line, str) or
1349
not isinstance(escape_chars, str)

common/context/tableviewercontext.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,45 @@
33

44

55
class TableViewerContext(ListViewerContext):
6+
"""
7+
Represents the table viewer in the Console which can display text
8+
data as a table.
9+
10+
The table viewer in the Console interprets the
11+
data of a Log Entry as a table. This class
12+
takes care of the necessary formatting and escaping required by
13+
the corresponding table viewer in the Console.
14+
15+
You can use the TableViewerContext class for creating custom
16+
log methods around Session.log_custom_context
17+
for sending custom data organized as tables.
18+
19+
.. note::
20+
This class is not guaranteed to be threadsafe.
21+
"""
22+
623
def __init__(self) -> None:
24+
"""
25+
Initializes a TableViewerContext instance.
26+
"""
727
super().__init__(ViewerId.TABLE)
828
self.__line_start: bool = True
929

1030
def append_header(self, header: str) -> None:
31+
"""
32+
Appends a header to the text data.
33+
34+
:param header: The header to append."""
35+
1136
self.append_line(header)
1237
self.append_line("")
1338

1439
def add_row_entry(self, entry: str) -> None:
40+
"""
41+
Adds a string entry to the current row.
42+
43+
:param entry: The string entry to add.
44+
"""
1545
if self.__line_start:
1646
self.__line_start = False
1747
else:
@@ -42,7 +72,9 @@ def __escape_csv_entry(entry: str) -> str:
4272
return "".join(result)
4373

4474
def begin_row(self) -> None:
75+
"""Begins a new row."""
4576
self.__line_start = True
4677

4778
def end_row(self) -> None:
79+
"""Ends the current row."""
4880
self.append_line("")

0 commit comments

Comments
 (0)