Menu

#114 rst2odt_prepstyles.py: Port to ElementTree

None
closed-accepted
nobody
None
5
2023-05-10
2013-08-05
No

rst2odt_prepstyles.py currently uses lxml to parse, modify and write XML. The attached patch makes it use ElementTree instead, which has been part of Python's standard library since 2.5.

1 Attachments

Discussion

  • Dmitry Shachnev

    Dmitry Shachnev - 2013-08-06

    Here is a revised version of this patch, which also adds Python 3 compatibility (by using correct Except: statements and sys.stderr.write).

     
  • Dmitry Shachnev

    Dmitry Shachnev - 2014-05-04

    Dear Docutils developers, can you please comment on this ticket? (And probably other ones in "patches" group).

     
  • engelbert gruber

    i am trying.
    please (leave the patch here) and move discussion to
    https://sourceforge.net/p/docutils/bugs/255/

     
  • Günter Milde

    Günter Milde - 2023-04-18
    • status: open --> pending-remind
     
  • Günter Milde

    Günter Milde - 2023-04-18

    With [bugs:#255] solved and changes to the source (to make it Py3k-conform), is there an advantage of using elementtree that makes it worth updating, testing, and applying the patch ?

     

    Related

    Bugs: #255

    • Dmitry Shachnev

      Dmitry Shachnev - 2023-04-21

      lxml is not part of standard library, while xml.etree is. So we are adding a dependency on a 3rdparty project for no good reason.

      I am attaching a version of this patch which applies cleanly to master branch of git mirror.

       
  • Günter Milde

    Günter Milde - 2023-04-26

    lxml is not part of standard library, while xml.etree is.

    I see. Thanks for the clarification.

    When I try to use the patched script on a copy of docutils/writers/odf_odt/styles.odt, I get an error:

    /usr/local/src/docutils-git-svn/docutils/tools/rst2odt_prepstyles.py styles.odt 
    Traceback (most recent call last):
      File "/usr/local/src/docutils-git-svn/docutils/tools/rst2odt_prepstyles.py", line 78, in <module>
    main()
      File "/usr/local/src/docutils-git-svn/docutils/tools/rst2odt_prepstyles.py", line 74, in main
    prepstyle(filename)
      File "/usr/local/src/docutils-git-svn/docutils/tools/rst2odt_prepstyles.py", line 48, in prepstyle
    for attr in el.attrib.keys():
    RuntimeError: dictionary changed size during iteration
    

    BTW, the non-patched version fails with:

    /usr/local/src/docutils-git-svn/docutils/tools/rst2odt_prepstyles.py styles.odt 
    Traceback (most recent call last):
      File "/usr/local/src/docutils-git-svn/docutils/tools/rst2odt_prepstyles.py", line 67, in <module>
        main()
      File "/usr/local/src/docutils-git-svn/docutils/tools/rst2odt_prepstyles.py", line 63, in main
        prepstyle(filename)
      File "/usr/local/src/docutils-git-svn/docutils/tools/rst2odt_prepstyles.py", line 49, in prepstyle
        zout.writestr(item, zin.read(item.filename))
      File "/usr/lib/python3.9/zipfile.py", line 1802, in writestr
        with self.open(zinfo, mode='w') as dest:
      File "/usr/lib/python3.9/zipfile.py", line 1505, in open
        return self._open_to_write(zinfo, force_zip64=force_zip64)
      File "/usr/lib/python3.9/zipfile.py", line 1600, in _open_to_write
        self.fp.write(zinfo.FileHeader(zip64))
    TypeError: write() argument must be str, not bytes
    Exception ignored in: <function ZipFile.__del__ at 0x7f7e5b555700>
    Traceback (most recent call last):
      File "/usr/lib/python3.9/zipfile.py", line 1807, in __del__
        self.close()
      File "/usr/lib/python3.9/zipfile.py", line 1825, in close
        self._write_end_record()
      File "/usr/lib/python3.9/zipfile.py", line 1919, in _write_end_record
        self.fp.write(endrec)
    TypeError: write() argument must be str, not bytes
    
     
    • Dmitry Shachnev

      Dmitry Shachnev - 2023-04-26

      First error can be fixed by replacing el.attrib.keys() with list(el.attrib). Second error can be fixed by changing argument of os.fdopen from w to wb.

      I am attaching an updated version of the patch. Although you may want to commit w to wb change as a separate revision, for ease of backporting.

       
  • Günter Milde

    Günter Milde - 2023-04-26
    • status: pending-remind --> open-accepted
     
  • Günter Milde

    Günter Milde - 2023-04-26

    Applied in [r9365].
    (Without the handling of Python <2.5 as we no longer support Python 2.)

    Thank you for the patch.

     

    Related

    Commit: [r9365]

  • Günter Milde

    Günter Milde - 2023-05-10
    • status: open-accepted --> closed-accepted
     
  • Günter Milde

    Günter Milde - 2023-05-10

    Fixed in Docutils 0.20
    Thanks for the patch and explanations.

     

Log in to post a comment.