I'm writing a python3 program that generates a text file that is post-procesed with asciidoc for the final report in html and pdf.
The python program generates thousands files with graphics to be included in the final report. The filenames for the files are generated with tempfile.NamedTemporaryFile
The problem it that the character set used by tempfile is defined as:
characters = "abcdefghijklmnopqrstuvwxyz0123456789_"
then I end with some files with names like "_6456_" and asciidoc interprets the "_" as formatting and inserts some html that breaks the report.
I need to either find a way to "escape" the filenames in asciidoc or control the characters in the temporary file.
My current solution is to rename the temporary file after I close it to replace the "_" with some other character (not in the list of characters used by tempfile to avoid a collision) but i have the feeling that there is a better way to do it.
I will appreciate any ideas. I'm not very proficient with python yet, i think overloading _RandomNameSequence in tempfile will work, but i'm not sure how to do it.
regards.
Hack way, based on manipulating tempfile internals:
class MyRandomSequence(tempfile._RandomNameSequence):
characters = "xyz123"
tempfile._name_sequence = MyRandomSequence()
# make your temporary file
Example:
>>> tempfile.NamedTemporaryFile()
<open file '<fdopen>', mode 'w+b' at 0x1013b5540>
>>> k=_
>>> k.name
'/var/folders/Su/SuMQtmxiE941sUwe8d91lE+++TU/-Tmp-/tmp33x22z'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With