Example #1
0
def 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.
    """
    try:
        outFile = open(fileName, "w")
        outFile.write(text)
        outFile.close()
        return True
    except IOError, e:
        widgets.reportError(parent, WRITE_ERROR_MESSAGE, e, "io")
        return False
Example #2
0
def 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``.
    """
    try:
        inFile = open(fileName, "r")
        text = inFile.read()
        inFile.close()
        return text
    except IOError, e:
        message = READ_ERROR_MESSAGE % stripPath(fileName)
        widgets.reportError(parent, message, e, "io")
        return None
Example #3
0
    def activate(self, *parameters):
        """
        Called when the action is activated. Calls :meth:`run`, unless its not
        supposed to be possible to activate the action right now.
        """
        error = None

        # None of these cases should actually be possible, but we properly
        # handle illegal activations nonetheless.
        if self.requiresCalibrationData:
            if not self.system.calibrationData.hasMeasurements:
                error = NO_CALIBRATION_DATA_ERROR_MESSAGE
        elif self.requiresSimulation and not self.system.isSimulation:
            error = NO_SIMULATION_ERROR_MESSAGE

        if error:
            widgets.reportError(self.mainWindowHandler._window,
                error, None, 'illegal activation')
        else:
            self.run()
Example #4
0
def 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 :data:`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.
    """
    if fileName is None:
        fileName = chooseFile(type, "r", parent)

    if fileName is None:
        return None

    text = read(fileName, parent)

    if text is None:
        return None

    obj = getattr(FILE_TYPES[type][2], "fromXML")(text, *parserParameters)

    if obj is None:
        message = PARSE_ERROR_MESSAGE % stripPath(fileName)
        widgets.reportError(parent, message, None, "parse")
        return None

    if hasattr(obj, "fileName"):
        obj.fileName = fileName

    return obj