traitsui.extras.saving module

Provides a lightweight framework that removes some of the drudge work involved with implementing user-friendly saving behavior in a Traits UI application.

class traitsui.extras.saving.CanSaveMixin[source]

Bases: HasTraits

A mixin-class for objects that wish to support GUI saving via a SaveHandler. It is the responsiblity of the child class to manage its dirty flag, which describes whether its information has changed since its last save.

save()[source]

Saves the object to the path specified by its ‘filepath’ trait. This method should also reset the dirty flag on this object.

validate()[source]

Returns whether the information in the object is valid to be saved in tuple form. The first item is the validation state (boolean) and the second item is the message to display if the object did not validate.

By default, an object always validates.

class traitsui.extras.saving.SaveHandler[source]

Bases: Handler

A Handler that facilates adding saving to a Traits UI application.

allowValidationBypass = Bool(False)

Whether to allow the user to override a validation failure through a confirmation dialog. By default, validation errors cannot be overriden.

autosave = Bool(False)

Whether to automatically save after a certain amount of time has passed since the last save

autosaveInterval = Int(300)

Number of seconds between each autosave. Default is 5 minutes.

autosaveValidationBypass = Bool(True)

If it is possible to override validation failures, this specifies whether autosave will do so. If False and a validation errors occurs, no save will occur.

close(info, is_ok)[source]

Called when the user requests to close the interface. Returns a boolean indicating whether the window should be allowed to close.

closed(info, is_ok)[source]

Called after the window is destroyed. Makes sure that the autosave timer is stopped.

exit(info)[source]

Closes the UI unless a save prompt is cancelled. Provided for convenience to be used with a Menu action.

extension = Str()

The option extension which should appear at the end of all filenames. If the user does not explicitly specifiy it, it is appended to the filename.

init(info)[source]

Set the default save object (the object being handled). Also, perform a questionable hack by which we remove the handled object from the keybinding’s controllers. This means that a keybinding to ‘save’ only calls this object, not the object being edited as well. (For reasons unclear, the KeyBinding handler API is radically different from the Action API, which is the reason that this problem exists. Keybindings are a UI concept–they should not call the model by default.)

promptForSave(info, cancel=True)[source]

Prompts the user to save the object, if appropriate. Returns whether the user canceled the action that invoked this prompt.

promptOnExit = Bool(True)

Whether to prompt for a save on exit if the save object is dirty

save(info)[source]

Saves the object to its current filepath. If this is not specified, opens a dialog to select this path. Returns whether the save actually occurred.

saveAs(info)[source]

Saves the object to a new path, and sets this as the ‘filepath’ on the object. Returns whether the save actually occurred.

saveObject = Any()

The object which is to be saved (subclass of CanSaveMixin). It is assigned to info.object in the ‘init’ method, which in many cases is what you want. If not, override that method to set it to something else.

savePromptMessage = Str("Would you like to save?")

This message to display when the Handler requests a save

wildcard = Str("All files (*.*)|*.*")

The type of files to show in the save dialogs