tjfontaine / airprint-generate

Automatically generate AirPrint Avahi service files for CUPS printers
http://www.atxconsulting.com/blog/tjfontaine/2010/11/21/automatically-generate-airprint-avahi-service-files-cups-printers
405 stars 119 forks source link

Script fails with Python 3.x when py-lxml installed #28

Open freultwah opened 3 years ago

freultwah commented 3 years ago

I stumbled upon a Python 2 to Python 3 conversion issue when trying to run the script with py-lxml installed.

# ./airprint-generate.py
Traceback (most recent call last):
  File "/root/./airprint-generate.py", line 279, in <module>
    apg.generate()
  File "/root/./airprint-generate.py", line 226, in generate
    tree.write(f, pretty_print=True, xml_declaration=True, encoding="UTF-8")
  File "src/lxml/etree.pyx", line 2057, in lxml.etree._ElementTree.write
  File "src/lxml/serializer.pxi", line 758, in lxml.etree._tofilelike
  File "src/lxml/etree.pyx", line 318, in lxml.etree._ExceptionContext._raise_if_stored
  File "src/lxml/serializer.pxi", line 682, in lxml.etree._FilelikeWriter.write
TypeError: write() argument must be str, not bytes

I fixed it by opening the file as binary:

--- airprint-generate.py.us.bak 2020-12-02 14:00:09.404088000 +0200
+++ airprint-generate.py        2020-12-02 14:05:06.222074000 +0200
@@ -220,7 +220,7 @@
                 if self.directory:
                     fname = os.path.join(self.directory, fname)

-                f = open(fname, 'w')
+                f = open(fname, 'wb')

                 if etree:
                     tree.write(f, pretty_print=True, xml_declaration=True, encoding="UTF-8")

I could see no adverse effects when py-lxml was not installed.

abbsi commented 3 years ago

Same issue, and the fix above resolved it. Thanks!

jhayashi commented 3 years ago

Ditto. Thanks!

undrwater commented 3 years ago

Same here. Is there any reason not to make this change globally? IOW, will it break something?

gearfox commented 1 year ago

pip3 uninstall lxml ./airprint-generate.py -d /etc/avahi/services/ ls /etc/avahi/services/