-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytewriter.py
90 lines (70 loc) · 2.29 KB
/
bytewriter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""bytewriter.py: Write arrays in various ways"""
__author__ = "Aleksey.Zholdak"
__version__ = "1.0.1"
__email__ = "aleksey@zholdak.com"
class ByteAsBytearrayWriter(object):
bytes_per_line = 16
def __init__(self, stream, varname):
self.stream = stream
self.stream.write('{} = \\\n'.format(varname))
self.bytecount = 0 # For line breaks
def _eol(self):
self.stream.write("'\\\n")
def _eot(self):
self.stream.write("'\n")
def _bol(self):
self.stream.write(" b'")
# Output a single byte
def obyte(self, data):
if not self.bytecount:
self._bol()
self.stream.write('\\x{:02x}'.format(data))
self.bytecount += 1
self.bytecount %= self.bytes_per_line
if not self.bytecount:
self._eol()
# Output from a sequence
def out_data(self, bytelist):
for byt in bytelist:
self.obyte(byt)
# ensure a correct final line
def eot(self): # User force EOL if one hasn't occurred
if self.bytecount:
self._eot()
self.stream.write('\n')
class ByteAsIntArrayWriter(object):
bytes_per_line = 16
def __init__(self, stream, varname):
self.stream = stream
self.stream.write('{} = [\n'.format(varname))
self.bytecount = 0 # For line breaks
self.bytes_in_line = 0 # For line breaks
def _eol(self):
self.stream.write("\n")
def _eot(self):
self.stream.write("\n")
def _bol(self):
self.stream.write(" ")
# Output a single byte
def out_byte(self, byte):
if self.bytecount:
self.stream.write(',')
if self.bytecount and not self.bytes_in_line:
self._eol()
if not self.bytes_in_line:
self._bol()
self.stream.write('{:d}'.format(byte))
self.bytes_in_line += 1
self.bytes_in_line %= self.bytes_per_line
self.bytecount += 1
# Output from a sequence
def out_data(self, bytelist):
for byt in bytelist:
self.out_byte(byt)
# ensure a correct final line
def eot(self): # User force EOL if one hasn't occurred
if self.bytes_in_line:
self._eot()
self.stream.write(']\n')