55
66
77class 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 )
0 commit comments