sanette/intuos4-oled

Setting text for buttons is broken in python 3.10

ventillo opened this issue · 2 comments

When trying
intuos4oled.py -t 'xxx' -b 2 --font DejaVuSans.ttf set

An error is thrown:
Using Wacom Intuos4 (6x9) (PTK-640)
Active led = 0
Loading datafile /root/.intuos
Sending "xxx" to button 2
Traceback (most recent call last):
File "/usr/local/bin/intuos4oled.py", line 488, in
send_text(args.text, args.button, screen, flip = args.flip,
File "/usr/local/bin/intuos4oled.py", line 407, in send_text
text_to_img(text, filename, font, size, span)
File "/usr/local/bin/intuos4oled.py", line 382, in text_to_img
font_path = get_font_path(font)
File "/usr/local/bin/intuos4oled.py", line 369, in get_font_path
f = [x for x in l if font in x]
File "/usr/local/bin/intuos4oled.py", line 369, in
f = [x for x in l if font in x]
TypeError: a bytes-like object is required, not 'str'

This seems to be due to fc-list subprocess returning a byte array, instead of a str()
By modifying the function responsible, I managed to get this working, but it might be a good idea to resolve it in a more pythonic way.

Python version:
Python 3.10.8 (main, Nov 1 2022, 14:18:21) [GCC 12.2.0] on linux

Kernel:
Linux cerium 5.15.85-1-MANJARO #1 SMP PREEMPT Wed Dec 21 21:15:06 UTC 2022 x86_64 GNU/Linux

Amended line 366:
def get_font_path (font):
# fast and dirty
l = str(subprocess.check_output(["fc-list"]))
# more methodic, unicode approach
l = subprocess.check_output(["fc-list"]).decode("utf-8")
l = l.splitlines()
f = [x for x in l if font in x]
if f == []:
print ("ERROR: font %s not found"%font)
return (None)
return (f[0].split(':')[0])

Thanks for the report! Could you try this instead?:

def get_font_path (font):
    l = subprocess.check_output(["fc-list", font], text=True, encoding="utf8")
    l = l.splitlines()
    f = [x for x in l if font in x]
    if f == []:
        print ("ERROR: font %s not found"%font)
        return (None)
    return (f[0].split(':')[0])

A nice addition, didn't research the options for fopen:
subprocess.check_output(["fc-list", font], text=True)

And actually, the encoding utf-8 can be left out, if not strictly used in the rest of the code. text=True should be more than enough.