[PythonCAD] Post nineteenth release plans

Art Haas ahaas at airmail.net
Wed Nov 17 21:46:55 CET 2004


On Tue, Nov 16, 2004 at 06:33:18PM -0600, Art Haas wrote:
>
> I'll hopefully have some code to show this week and having a few people
> take a look and offer suggestions would be great. If someone could post
> some pseudo-code for ways they've implemented file saving routines in
> programs they have written, or routines that they've found to be robust,
> that would be great too. Maybe a few examples of good examples could be
> listed as well - possibly people can recommend some KDE, GNOME, or other
> open source program.
> 

Here's the first shot at how file saving will be done. I'll be modifying
the code in the 'gtkmenus.py' file to use this routine:

def _save_file(gtkimage, filename):
    _abs = os.path.abspath(filename)
    _bname = os.path.basename(_abs)
    _newfile = _abs + '.new'
    _handle = fileio.CompFile(_newfile, "w", truename=_bname)
    try:
        imageio.new_save_image(gtkimage, _handle)
    finally:
        _handle.close()
    # print "saved new file %s" % _newfile
    _backup = _abs + '~'
    if os.path.exists(_backup):
        # print "removing existing backup %s" % _backup
        os.unlink(_backup)
    _mode = None
    if os.path.exists(_abs):
        _st = os.stat(_abs)
        _mode = stat.S_IMODE(_st.st_mode)
        os.rename(_abs, _backup)
        # print "renaming existing to backup %s" % _backup
    try:
        os.rename(_newfile, _abs)
    except:
        os.rename(_abs, _backup)
        raise
    # print "renaming new file to filename %s" % _abs
    if _mode is not None and hasattr(os, 'chmod'):
        # print "restoring file permissions"
        os.chmod(_abs, _mode)
    if gtkimage.getFilename() is None:
        gtkimage.setFilename(_abs)

The file is first saved with '.new' extension, then any existing backup
file is removed. If there is currently an existing file, the permissions
are saved and the file is renamed to the backup name, then the '.new'
file is renamed to the correct name. If there was an existing file
found, the permissions are restored to the newly written copy.

The code doesn't play nicely with symbolic links or hardlinks.

Comments?

Art
-- 
Man once surrendering his reason, has no remaining guard against absurdities
the most monstrous, and like a ship without rudder, is the sport of every wind.

-Thomas Jefferson to James Smith, 1822


More information about the PythonCAD mailing list