HartyBart opened this issue on Sep 08, 2021 ยท 7 posts
HartyBart posted Wed, 08 September 2021 at 8:19 PM
This script is working with Poser 11, and is a Python 2.7 script to replace a string of multiline text between two named delimiters. In this case the delimiters are imaginary XML tags, and the replacement that is being made between them is a multiline block. The script shows how to correctly format this block in Python, so as to replace the existing block without mangling the XML indenting and line-wrapping.
Does not appear to work on XML that is live and in use by Poser, as that has probably already been taken up into memory.
Not tested on other types of file, but I expect it may work on various Poser formats that are really only text files and are not actively in use and in-memory inside Poser.
Learn the Secrets of Poser 11 and Line-art Filters.
bagginsbill posted Thu, 09 September 2021 at 1:37 PM
While this is correct, it can be a disaster if something goes wrong. If some other user is starting with this as a template and they change the code at line 18 to suit their purposes, it is easily possible that the program throws an exception (due to some temporary mistake on the user's part) which will result in the loss of all the file contents after the start tag.
It would be safer to open a DIFFERENT file for writing, write all the lines and close it, and ONLY THEN perform a delete and a couple file renames to safeguard that the original file BEFORE AND AFTER the replacement is retained, and that if a new content isn't produced properly, the original is not touched at all.
For similar reasons the f.close() should not be explicitly done by the program. Instead, perform the temp file production with a "with" statement. That will ensure that even if the program aborts, the file is properly closed. Otherwise, the Poser process will continue to have the file "open" and it may be difficult for the user to delete the file.
Renderosity forum reply notifications are wonky. If I read a follow-up in a thread, but I don't myself reply, then notifications no longer happen AT ALL on that thread. So if I seem to be ignoring a question, that's why. (Updated September 23, 2019)
bagginsbill posted Thu, 09 September 2021 at 1:44 PM
Because I'm lazy, I'm screen grabbing from some other python tutorial.
You do the same on the write as on the read. You'll never accidentally leave a file open that way.
Renderosity forum reply notifications are wonky. If I read a follow-up in a thread, but I don't myself reply, then notifications no longer happen AT ALL on that thread. So if I seem to be ignoring a question, that's why. (Updated September 23, 2019)
bagginsbill posted Thu, 09 September 2021 at 1:47 PM
This article explains in more detail https://stackoverflow.com/questions/1812115/how-to-safely-write-to-a-file
Renderosity forum reply notifications are wonky. If I read a follow-up in a thread, but I don't myself reply, then notifications no longer happen AT ALL on that thread. So if I seem to be ignoring a question, that's why. (Updated September 23, 2019)
HartyBart posted Thu, 09 September 2021 at 2:59 PM
Many thanks, especially for the advice on closing files properly if using this method. I'm assuming the advanced user has of course made a safe backup of the target file. I did think of going further and having the script:
But since changing the XML does not affect Poser's UI when running (I assume it's loaded at start and then resides in memory), there seemed no point going further. But obviously if someone had need for this sort of search-and-replace between X and Y, then they might put those sort of safety measure in place.
Learn the Secrets of Poser 11 and Line-art Filters.
adp001 posted Fri, 10 September 2021 at 1:14 AM
Since you are reading the XML file completely into memory anyway, you might as well process the data in memory.
Like this:
adp001 posted Fri, 10 September 2021 at 1:22 AM
Use os.access(...) to check if the user is allowed to read/write that file:
if os.access(os.R_OK) ... # read ok
if os.access(os.W_OK) ... # write ok
if os.access(os.R_OK | os.W_OK) ... # read and write ok
https://docs.python.org/2.7/library/os.html#os.access