kopia lustrzana https://github.com/pimoroni/pimoroni-pico
QA: Lint Python tools.
rodzic
c7461571ac
commit
4ebdff414e
|
@ -72,7 +72,7 @@ def convert_font(data):
|
||||||
while data.find("\n", char_data_len) != -1:
|
while data.find("\n", char_data_len) != -1:
|
||||||
data = data.replace("\n", "", 1)
|
data = data.replace("\n", "", 1)
|
||||||
|
|
||||||
for i in range(0, vertex_count):
|
for i in range(vertex_count):
|
||||||
offset = 10 + (i * 2)
|
offset = 10 + (i * 2)
|
||||||
if data[offset:offset + 2] == " R":
|
if data[offset:offset + 2] == " R":
|
||||||
# pen up for one
|
# pen up for one
|
||||||
|
@ -106,7 +106,7 @@ def convert_font(data):
|
||||||
# int8_t *vertices; // vertex data (indices: even = x, odd = y)
|
# int8_t *vertices; // vertex data (indices: even = x, odd = y)
|
||||||
# };
|
# };
|
||||||
chars = []
|
chars = []
|
||||||
for i in range(0, 95):
|
for i in range(95):
|
||||||
if i in char_data:
|
if i in char_data:
|
||||||
chars.append(
|
chars.append(
|
||||||
" {{.width={width}, .vertex_count={vertex_count}, .vertices=&{font_name}_vertices[{offset}]}}".format(
|
" {{.width={width}, .vertex_count={vertex_count}, .vertices=&{font_name}_vertices[{offset}]}}".format(
|
||||||
|
|
|
@ -28,12 +28,12 @@ def data():
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Converts images into the format used by Badger2040.')
|
parser = argparse.ArgumentParser(description="Converts images into the format used by Badger2040.")
|
||||||
parser.add_argument('file', nargs="+", help='input files to convert')
|
parser.add_argument("file", nargs="+", help="input files to convert")
|
||||||
parser.add_argument('--out_dir', type=Path, default=None, help='output directory')
|
parser.add_argument("--out_dir", type=Path, default=None, help="output directory")
|
||||||
parser.add_argument('--binary', action="store_true", help='output binary file for MicroPython')
|
parser.add_argument("--binary", action="store_true", help="output binary file for MicroPython")
|
||||||
parser.add_argument('--py', action="store_true", help='output .py file for MicroPython embedding')
|
parser.add_argument("--py", action="store_true", help="output .py file for MicroPython embedding")
|
||||||
parser.add_argument('--resize', action="store_true", help='force images to 296x128 pixels')
|
parser.add_argument("--resize", action="store_true", help="force images to 296x128 pixels")
|
||||||
|
|
||||||
options = parser.parse_args()
|
options = parser.parse_args()
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ class ByteWriter(object):
|
||||||
|
|
||||||
def __init__(self, stream, varname):
|
def __init__(self, stream, varname):
|
||||||
self.stream = stream
|
self.stream = stream
|
||||||
self.stream.write('{} =\\\n'.format(varname))
|
self.stream.write("{} =\\\n".format(varname))
|
||||||
self.bytecount = 0 # For line breaks
|
self.bytecount = 0 # For line breaks
|
||||||
|
|
||||||
def _eol(self):
|
def _eol(self):
|
||||||
|
@ -59,7 +59,7 @@ class ByteWriter(object):
|
||||||
def obyte(self, data):
|
def obyte(self, data):
|
||||||
if not self.bytecount:
|
if not self.bytecount:
|
||||||
self._bol()
|
self._bol()
|
||||||
self.stream.write('\\x{:02x}'.format(data))
|
self.stream.write("\\x{:02x}".format(data))
|
||||||
self.bytecount += 1
|
self.bytecount += 1
|
||||||
self.bytecount %= self.bytes_per_line
|
self.bytecount %= self.bytes_per_line
|
||||||
if not self.bytecount:
|
if not self.bytecount:
|
||||||
|
@ -74,7 +74,7 @@ class ByteWriter(object):
|
||||||
def eot(self): # User force EOL if one hasn't occurred
|
def eot(self): # User force EOL if one hasn't occurred
|
||||||
if self.bytecount:
|
if self.bytecount:
|
||||||
self._eot()
|
self._eot()
|
||||||
self.stream.write('\n')
|
self.stream.write("\n")
|
||||||
|
|
||||||
|
|
||||||
def convert_image(img):
|
def convert_image(img):
|
||||||
|
@ -85,15 +85,14 @@ def convert_image(img):
|
||||||
img = enhancer.enhance(2.0)
|
img = enhancer.enhance(2.0)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
img = img.convert("1") # convert to black and white
|
return img.convert("1") # convert to black and white
|
||||||
return img
|
|
||||||
|
|
||||||
|
|
||||||
def write_stream(header, footer, ip_stream, op_stream):
|
def write_stream(header, footer, ip_stream, op_stream):
|
||||||
op_stream.write(header)
|
op_stream.write(header)
|
||||||
op_stream.write('\n')
|
op_stream.write("\n")
|
||||||
data = ip_stream.read()
|
data = ip_stream.read()
|
||||||
bw_data = ByteWriter(op_stream, '_data')
|
bw_data = ByteWriter(op_stream, "_data")
|
||||||
bw_data.odata(data)
|
bw_data.odata(data)
|
||||||
bw_data.eot()
|
bw_data.eot()
|
||||||
op_stream.write(footer)
|
op_stream.write(footer)
|
||||||
|
@ -127,10 +126,10 @@ for input_filename in options.file:
|
||||||
with open(output_filename, "w") as out:
|
with open(output_filename, "w") as out:
|
||||||
write_stream(PY_HEADER, PY_FOOTER, io.BytesIO(bytes(output_data)), out)
|
write_stream(PY_HEADER, PY_FOOTER, io.BytesIO(bytes(output_data)), out)
|
||||||
else:
|
else:
|
||||||
image_code = '''\
|
image_code = """\
|
||||||
static const uint8_t {image_name}[{count}] = {{
|
static const uint8_t {image_name}[{count}] = {{
|
||||||
{byte_data}
|
{byte_data}
|
||||||
}};
|
}};
|
||||||
'''.format(image_name=image_name, count=len(output_data), byte_data=", ".join(str(b) for b in output_data))
|
""".format(image_name=image_name, count=len(output_data), byte_data=", ".join(str(b) for b in output_data))
|
||||||
|
|
||||||
print(image_code)
|
print(image_code)
|
||||||
|
|
|
@ -41,7 +41,7 @@ class ByteWriter(object):
|
||||||
|
|
||||||
def __init__(self, stream, varname):
|
def __init__(self, stream, varname):
|
||||||
self.stream = stream
|
self.stream = stream
|
||||||
self.stream.write('{} =\\\n'.format(varname))
|
self.stream.write("{} =\\\n".format(varname))
|
||||||
self.bytecount = 0 # For line breaks
|
self.bytecount = 0 # For line breaks
|
||||||
|
|
||||||
def _eol(self):
|
def _eol(self):
|
||||||
|
@ -57,7 +57,7 @@ class ByteWriter(object):
|
||||||
def obyte(self, data):
|
def obyte(self, data):
|
||||||
if not self.bytecount:
|
if not self.bytecount:
|
||||||
self._bol()
|
self._bol()
|
||||||
self.stream.write('\\x{:02x}'.format(data))
|
self.stream.write("\\x{:02x}".format(data))
|
||||||
self.bytecount += 1
|
self.bytecount += 1
|
||||||
self.bytecount %= self.bytes_per_line
|
self.bytecount %= self.bytes_per_line
|
||||||
if not self.bytecount:
|
if not self.bytecount:
|
||||||
|
@ -72,7 +72,7 @@ class ByteWriter(object):
|
||||||
def eot(self): # User force EOL if one hasn't occurred
|
def eot(self): # User force EOL if one hasn't occurred
|
||||||
if self.bytecount:
|
if self.bytecount:
|
||||||
self._eot()
|
self._eot()
|
||||||
self.stream.write('\n')
|
self.stream.write("\n")
|
||||||
|
|
||||||
|
|
||||||
# PYTHON FILE WRITING
|
# PYTHON FILE WRITING
|
||||||
|
@ -90,17 +90,17 @@ def data():
|
||||||
|
|
||||||
|
|
||||||
def write_func(stream, name, arg):
|
def write_func(stream, name, arg):
|
||||||
stream.write('def {}():\n return {}\n\n'.format(name, arg))
|
stream.write("def {}():\n return {}\n\n".format(name, arg))
|
||||||
|
|
||||||
|
|
||||||
def write_data(op_path, ip_path):
|
def write_data(op_path, ip_path):
|
||||||
try:
|
try:
|
||||||
with open(ip_path, 'rb') as ip_stream:
|
with open(ip_path, "rb") as ip_stream:
|
||||||
try:
|
try:
|
||||||
with open(op_path, 'w') as op_stream:
|
with open(op_path, "w") as op_stream:
|
||||||
write_stream(ip_stream, op_stream)
|
write_stream(ip_stream, op_stream)
|
||||||
except OSError:
|
except OSError:
|
||||||
print("Can't open", op_path, 'for writing')
|
print("Can't open", op_path, "for writing")
|
||||||
return False
|
return False
|
||||||
except OSError:
|
except OSError:
|
||||||
print("Can't open", ip_path)
|
print("Can't open", ip_path)
|
||||||
|
@ -110,9 +110,9 @@ def write_data(op_path, ip_path):
|
||||||
|
|
||||||
def write_stream(ip_stream, op_stream):
|
def write_stream(ip_stream, op_stream):
|
||||||
op_stream.write(STR01)
|
op_stream.write(STR01)
|
||||||
op_stream.write('\n')
|
op_stream.write("\n")
|
||||||
data = ip_stream.read()
|
data = ip_stream.read()
|
||||||
bw_data = ByteWriter(op_stream, '_data')
|
bw_data = ByteWriter(op_stream, "_data")
|
||||||
bw_data.odata(data)
|
bw_data.odata(data)
|
||||||
bw_data.eot()
|
bw_data.eot()
|
||||||
op_stream.write(STR02)
|
op_stream.write(STR02)
|
||||||
|
@ -135,20 +135,20 @@ data_to_py.py image.jpg image.py
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(__file__, description=DESC,
|
parser = argparse.ArgumentParser(__file__, description=DESC,
|
||||||
formatter_class=argparse.RawDescriptionHelpFormatter)
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||||
parser.add_argument('infile', type=str, help='Input file path')
|
parser.add_argument("infile", type=str, help="Input file path")
|
||||||
parser.add_argument('outfile', type=str,
|
parser.add_argument("outfile", type=str,
|
||||||
help='Path and name of output file. Must have .py extension.')
|
help="Path and name of output file. Must have .py extension.")
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if not os.path.isfile(args.infile):
|
if not os.path.isfile(args.infile):
|
||||||
quit("Data filename does not exist")
|
quit("Data filename does not exist")
|
||||||
|
|
||||||
if not os.path.splitext(args.outfile)[1].upper() == '.PY':
|
if not os.path.splitext(args.outfile)[1].upper() == ".PY":
|
||||||
quit('Output filename must have a .py extension.')
|
quit("Output filename must have a .py extension.")
|
||||||
|
|
||||||
print('Writing Python file.')
|
print("Writing Python file.")
|
||||||
if not write_data(args.outfile, args.infile):
|
if not write_data(args.outfile, args.infile):
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
print(args.outfile, 'written successfully.')
|
print(args.outfile, "written successfully.")
|
||||||
|
|
Ładowanie…
Reference in New Issue