gui.io
Contains a number of utility functions related to file input/output.
Does not contain any functions related to any daughter of Inachus,
moon, genus of freshwater snail, band, or editor from Solstice,
although the latter would be awesome.
-
gui.io.FILE_TYPES
- A dictionary containing information about the file types used by NOSE.
Keys are internal names for different types of files (which will not be
displayed to the user). The associated values are tupels containing a
human-readable description of the file type, the type’s extension, and
the module that contains the toXML() and fromXML() functions
used for the objects saved in files of that type.
-
gui.io.load(type, fileName=None, parent=None, *parserParameters)
Loads an object of the given type from a file. type must be one of the
file types listed in FILE_TYPES. fileName must be either the
name of the file the object that is to be loaded is saved in, or None,
in which case the user is queried for a file name. parent is used as
the transient parent of any windows that are shown during loading. Any
extra arguments are passed to the fromXML() function for type.
If the object has a fileName attribute, it is set to the file name
the object was loaded from.
If the user decline to choose a file, or if an error occurs while reading
or parsing the file, this function returns None. In the case of an
error, an error message is shown to the user.
-
gui.io.save(obj, type, fileName=None, parent=None)
Saves an object of the given type to a file. type must be one of the
file types listed in FILE_TYPES. fileName must be either the
name of athe file the object is to be saved in, or None, in which case
the user is queried for a file name. parent is used as the transient
parent of any windows that are shown during loading.
The function returns True if the object has been successfully saved.
If the user declines to choose a file, or if an error occurs while writing
to the file, it returns False. In the case of an error, an error
message is shown to the user.
If the object has a fileName attribute, it is set to the file name the
object was saved to.
-
gui.io.read(fileName, parent=None)
- Returns the contents of the file indicated by fileName as a string.
If an error occurs while reading the file, it is reported to the user,
using parent as the transient parent of the error dialog, and
this function returns None.
-
gui.io.write(text, fileName, parent=None)
- Writes text to the file indicated by fileName. If an error occurs
while writing to the file, it is reported to the user, using parent
as the transient parent of the error dialog. The function returns
True if the text has been successfully saved.
-
gui.io.chooseFile(type, mode, parent=None)
Displays a dialog that allows the user to choose a file. type must
be one of the file types listed in FILE_TYPES and is used to
filter the files that are presented to the user (which does not stop
the user from selecting a file of another type, though). mode must be
either 'r' for reading or 'w' for writing. parent is used
as the transient parent of all windows that are displayed.
The function returns either the name of the file the user selected,
or None if the user cancels the file selection procedure.
-
gui.io.stripPath(fileName)
Returns the last component of fileName.
>>> stripPath('foo/bar/baz.qux')
'baz.qux'
Module Data
-
gui.io.TESTING_USE_DEFAULT_FILE_NAME
-
gui.io.TESTING_DEFAULT_FILE_NAME
- If TESTING_USE_DEFAULT_FILE_NAME is set to True,
chooseFile() returns TESTING_DEFAULT_FILE_NAME
rather than asking the user to choose a file. Intended for
unit testing.
-
gui.io.READ_ERROR_MESSAGE
- The message shown when an error occurs while reading from a file.
-
gui.io.WRITE_ERROR_MESSAGE
- The message shown when an error occurs while writing to a file.
-
gui.io.PARSE_ERROR_MESSAGE
- The message shown when an error occurs while parsing a file.
Private Utiltiy Functions
-
gui.io._createFileChooser(type, mode, parent)
- Creates a gtk.FileChooserDialog that has the user choose
a file of the given type (which must be one of the file types listed
in FILE_TYPES) for either reading (if mode is 'r')
or writing (if mode is 'w'). parent is used as the transient
parent of the dialog.
-
gui.io._runFileChooser(chooser)
- Calls the run() method of the given gtk.FileChooserDialog
and returns the name of the file the user chose.
-
gui.io._addFileFilters(chooser, type)
- Creates two gtk.FileFilters – one that matches all files,
and one that matches a known type of files – and adds them to the given
gtk.FileChooserDialog. type must be one of the file types
listed in FILE_TYPES, where this function looks up a human-readable
description for that type of file and its extension.