QA: Lint Python tools.

pull/1075/head
Phil Howard 2025-04-25 17:04:26 +01:00
rodzic c7461571ac
commit 4ebdff414e
3 zmienionych plików z 32 dodań i 33 usunięć

Wyświetl plik

@ -72,7 +72,7 @@ def convert_font(data):
while data.find("\n", char_data_len) != -1:
data = data.replace("\n", "", 1)
for i in range(0, vertex_count):
for i in range(vertex_count):
offset = 10 + (i * 2)
if data[offset:offset + 2] == " R":
# pen up for one
@ -106,7 +106,7 @@ def convert_font(data):
# int8_t *vertices; // vertex data (indices: even = x, odd = y)
# };
chars = []
for i in range(0, 95):
for i in range(95):
if i in char_data:
chars.append(
" {{.width={width}, .vertex_count={vertex_count}, .vertices=&{font_name}_vertices[{offset}]}}".format(

Wyświetl plik

@ -28,12 +28,12 @@ def data():
"""
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('--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('--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 = argparse.ArgumentParser(description="Converts images into the format used by Badger2040.")
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("--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("--resize", action="store_true", help="force images to 296x128 pixels")
options = parser.parse_args()
@ -43,7 +43,7 @@ class ByteWriter(object):
def __init__(self, stream, varname):
self.stream = stream
self.stream.write('{} =\\\n'.format(varname))
self.stream.write("{} =\\\n".format(varname))
self.bytecount = 0 # For line breaks
def _eol(self):
@ -59,7 +59,7 @@ class ByteWriter(object):
def obyte(self, data):
if not self.bytecount:
self._bol()
self.stream.write('\\x{:02x}'.format(data))
self.stream.write("\\x{:02x}".format(data))
self.bytecount += 1
self.bytecount %= self.bytes_per_line
if not self.bytecount:
@ -74,7 +74,7 @@ class ByteWriter(object):
def eot(self): # User force EOL if one hasn't occurred
if self.bytecount:
self._eot()
self.stream.write('\n')
self.stream.write("\n")
def convert_image(img):
@ -85,15 +85,14 @@ def convert_image(img):
img = enhancer.enhance(2.0)
except ValueError:
pass
img = img.convert("1") # convert to black and white
return img
return img.convert("1") # convert to black and white
def write_stream(header, footer, ip_stream, op_stream):
op_stream.write(header)
op_stream.write('\n')
op_stream.write("\n")
data = ip_stream.read()
bw_data = ByteWriter(op_stream, '_data')
bw_data = ByteWriter(op_stream, "_data")
bw_data.odata(data)
bw_data.eot()
op_stream.write(footer)
@ -127,10 +126,10 @@ for input_filename in options.file:
with open(output_filename, "w") as out:
write_stream(PY_HEADER, PY_FOOTER, io.BytesIO(bytes(output_data)), out)
else:
image_code = '''\
image_code = """\
static const uint8_t {image_name}[{count}] = {{
{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)

Wyświetl plik

@ -41,7 +41,7 @@ class ByteWriter(object):
def __init__(self, stream, varname):
self.stream = stream
self.stream.write('{} =\\\n'.format(varname))
self.stream.write("{} =\\\n".format(varname))
self.bytecount = 0 # For line breaks
def _eol(self):
@ -57,7 +57,7 @@ class ByteWriter(object):
def obyte(self, data):
if not self.bytecount:
self._bol()
self.stream.write('\\x{:02x}'.format(data))
self.stream.write("\\x{:02x}".format(data))
self.bytecount += 1
self.bytecount %= self.bytes_per_line
if not self.bytecount:
@ -72,7 +72,7 @@ class ByteWriter(object):
def eot(self): # User force EOL if one hasn't occurred
if self.bytecount:
self._eot()
self.stream.write('\n')
self.stream.write("\n")
# PYTHON FILE WRITING
@ -90,17 +90,17 @@ def data():
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):
try:
with open(ip_path, 'rb') as ip_stream:
with open(ip_path, "rb") as ip_stream:
try:
with open(op_path, 'w') as op_stream:
with open(op_path, "w") as op_stream:
write_stream(ip_stream, op_stream)
except OSError:
print("Can't open", op_path, 'for writing')
print("Can't open", op_path, "for writing")
return False
except OSError:
print("Can't open", ip_path)
@ -110,9 +110,9 @@ def write_data(op_path, ip_path):
def write_stream(ip_stream, op_stream):
op_stream.write(STR01)
op_stream.write('\n')
op_stream.write("\n")
data = ip_stream.read()
bw_data = ByteWriter(op_stream, '_data')
bw_data = ByteWriter(op_stream, "_data")
bw_data.odata(data)
bw_data.eot()
op_stream.write(STR02)
@ -135,20 +135,20 @@ data_to_py.py image.jpg image.py
if __name__ == "__main__":
parser = argparse.ArgumentParser(__file__, description=DESC,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('infile', type=str, help='Input file path')
parser.add_argument('outfile', type=str,
help='Path and name of output file. Must have .py extension.')
parser.add_argument("infile", type=str, help="Input file path")
parser.add_argument("outfile", type=str,
help="Path and name of output file. Must have .py extension.")
args = parser.parse_args()
if not os.path.isfile(args.infile):
quit("Data filename does not exist")
if not os.path.splitext(args.outfile)[1].upper() == '.PY':
quit('Output filename must have a .py extension.')
if not os.path.splitext(args.outfile)[1].upper() == ".PY":
quit("Output filename must have a .py extension.")
print('Writing Python file.')
print("Writing Python file.")
if not write_data(args.outfile, args.infile):
sys.exit(1)
print(args.outfile, 'written successfully.')
print(args.outfile, "written successfully.")