tallforasmurf / PPQT

A post-processing tool for PGDP written in Python, PyQt4, and Qt
GNU General Public License v3.0
4 stars 2 forks source link

Ensure meta file and text file are in sync #114

Closed bibimbop closed 11 years ago

bibimbop commented 11 years ago

A couple time I went back to a previous version of the text while forgetting to also restore the meta file, and ended-up with discrepancies with the page markers (or so I thought, see issue #113). guiguts has the same issue.

I suggest that when saving, a checksum of the text should be computed and saved in the meta file. When opening the meta file, the checksum should be checked against the text file, and a window warning should be issued if the files don't match.

tallforasmurf commented 11 years ago

This is an excellent suggestion. I will investigate next week. I think it may be sufficient to just compare the save timestamps of the document and the meta. Depends if Qt or Python give me a save timestamp that is reliable on all platforms.

bibimbop commented 11 years ago

Well, the timestamp won't work if the files are copied. Some PPer use directories to store older versions. It's easy to copy the wrong files.

A checksum is the best way to ensure both files are in sync. Python provides the hashlib for that purpose (http://docs.python.org/2/library/hashlib.html).

tallforasmurf commented 11 years ago

OK having thought this through I propose:

  1. Add members documentHash and metaHash to pqIMC, initialized as null strings.
  2. On File>New, clear both hashes to null strings.
  3. On File>Save and File>Save-As: if the hashes are different, display an ok/cancel message warning that the file should not be saved. If the user clicks Cancel, don't. Otherwise, continue as normal.
  4. On File>Save/Save-As, in pqEdit:
    • compute a hash over document and store it in both strings documentHash and metaHash.
    • (note this means that an actual save legitimizes the file for the future).
    • while writing metadata, write {DOCHASH xx..xx} with the hash string in it.
  5. In ohWaitAreWeDirty check, called before Quit, New, and Open:
    • if file is not dirty, return true as normal
    • if file is dirty, compare the hashes. If they are different, return true (quit/new/open may proceed ignoring dirty flag)
    • otherwise warn the user of the need to save, as normal
  6. On File>Open, in pqEdit:
    • read the document file.
    • compute a hash over document and store it in both strings documentHash and metaHash.
    • if there is no metadata file, proceed as usual to generate the metadata.
    • process the metadata file. Allow (but do not require) a {DOCHASH xx..xx} entry.
    • when {DOCHASH xx..xx} is seen, store xx..xx in the metaHash string.
    • after meta finished, compare the hash strings
    • if different, issue a warning message explaining the problem and recommending not saving, but continue

This takes care of the case where a file was saved before this version and has no DOCHASH metadata.

tallforasmurf commented 11 years ago

Going to use QCryptographicHash. Its output is a QByteArray, converts directly to python bytes. Since hash result can contain any character we convert it to a string with repr which is smart enough to use double-quote if the string contains a single-quote (and vice versa) and presumably smart enough to \escape one or the other if needed. But still have to expect that the document hash could be like 'blah blah"\' etc'. However, the way the sectionRE works, the cap(2).trimmed() will pick it up ok.

tallforasmurf commented 11 years ago

63819ba