5
5
6
6
7
7
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
+
8
17
def __init__ (self , viewer_id : ViewerId ):
18
+ """
19
+ Initializes a BinaryContext instance.
20
+
21
+ :param viewer_id: The viewer ID to use.
22
+ """
9
23
super ().__init__ (viewer_id )
10
24
self .__data : BytesIO = BytesIO ()
11
25
12
26
@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
+ """
14
34
return self .__data .getvalue ()
15
35
16
36
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
+
17
45
self .__data .seek (0 )
18
46
self .__data .truncate ()
19
47
20
48
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
+
21
56
if filename == "" :
22
57
raise ValueError ("filename not provided" )
23
58
else :
@@ -27,6 +62,17 @@ def load_from_file(self, filename: str):
27
62
self .__data .write (content )
28
63
29
64
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
+
30
76
if not isinstance (bytestring , bytes ) and \
31
77
not isinstance (bytestring , bytes ):
32
78
raise TypeError ("bytestring must be bytes sequence" )
@@ -41,6 +87,10 @@ def append_bytes(self, bytestring: (bytes, bytearray), offset: int = 0, length:
41
87
self .__data .write (bytestring [offset : offset + length ])
42
88
43
89
def close (self ) -> None :
90
+ """
91
+ Overridden. Releases any resources.
92
+ """
93
+
44
94
if not self .__data .closed :
45
95
try :
46
96
self .__data .close ()
@@ -49,5 +99,11 @@ def close(self) -> None:
49
99
# we are not expecting an exception
50
100
51
101
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
+
52
108
self .reset_data ()
53
109
self .__data .write (stream )
0 commit comments