Exemplo n.º 1
0
def deletion(obj=None, name=None, dir=False):
    """Cleanly removing files and directories, handling WindowsErrors.

    The problem of MS Windows not releasing the file handle on a close() call is handled.  This method should be resilient to the strange MS Windows behaviour of not releasing the relax state files.  It should complete even when this WindowsError occurs.  A delay of 3 seconds has been added when the WindowsError occurs to give the OS some time before attempting to delete the directory or file again.  If this fails the deletion operation is skipped.


    @keyword obj:   The base object containing the file or directory name variable.
    @type obj:      Python object
    @keyword name:  The name of the file or directory name variable.
    @type name:     str
    @keyword dir:   A flag which if True indicates that a directory should be removed.  Otherwise a file should be deleted.
    """

    # No variable present.
    if not hasattr(obj, name):
        return

    # The variable.
    var = getattr(obj, name)

    # Attempt to remove the file or directory as well as the variable.
    try:
        if dir:
            rmtree(var)
        else:
            delete(var, fail=False)
        del var

    # Already deleted.
    except OSError:
        pass

    # Handle MS Windows strangeness.
    except:
        sleep(3)
        try:
            if dir:
                rmtree(var)
            else:
                delete(var, fail=False)

        # The files no longer exist?  Oh well.
        except:
            pass

        finally:
            del var
        pass
Exemplo n.º 2
0
def deletion(obj=None, name=None, dir=False):
    """Cleanly removing files and directories, handling WindowsErrors.

    The problem of MS Windows not releasing the file handle on a close() call is handled.  This method should be resilient to the strange MS Windows behaviour of not releasing the relax state files.  It should complete even when this WindowsError occurs.  A delay of 3 seconds has been added when the WindowsError occurs to give the OS some time before attempting to delete the directory or file again.  If this fails the deletion operation is skipped.


    @keyword obj:   The base object containing the file or directory name variable.
    @type obj:      Python object
    @keyword name:  The name of the file or directory name variable.
    @type name:     str
    @keyword dir:   A flag which if True indicates that a directory should be removed.  Otherwise a file should be deleted.
    """

    # No variable present.
    if not hasattr(obj, name):
        return

    # The variable.
    var = getattr(obj, name)

    # Attempt to remove the file or directory as well as the variable.
    try:
        if dir:
            rmtree(var)
        else:
            delete(var, fail=False)
        del var

    # Already deleted.
    except OSError:
        pass

    # Handle MS Windows strangeness.
    except:
        sleep(3)
        try:
            if dir:
                rmtree(var)
            else:
                delete(var, fail=False)

        # The files no longer exist?  Oh well.
        except:
            pass

        finally:
            del var
        pass
Exemplo n.º 3
0
    def tearDown(self):
        """Reset the relax data storage object."""

        # Reset relax.
        reset()

        # Delete the temporary files.
        delete(self.tmpfile_sphere, fail=False)
        delete(self.tmpfile_spheroid, fail=False)
        delete(self.tmpfile_ellipsoid, fail=False)
Exemplo n.º 4
0
    def tearDown(self):
        """Reset the relax data storage object."""

        # Reset relax.
        reset()

        # Close the open file handles on the OS level.
        close(self.tmpfile_sphere_handle)
        close(self.tmpfile_spheroid_handle)
        close(self.tmpfile_ellipsoid_handle)

        # Delete the temporary files.
        delete(self.tmpfile_sphere, fail=False)
        delete(self.tmpfile_spheroid, fail=False)
        delete(self.tmpfile_ellipsoid, fail=False)
Exemplo n.º 5
0
def macro_apply(data_type=None, style="classic", colour_start_name=None, colour_start_rgb=None, colour_end_name=None, colour_end_rgb=None, colour_list=None):
    """Execute a PyMOL macro.

    @keyword data_type:         The data type to map to the structure.
    @type data_type:            str
    @keyword style:             The style of the macro.
    @type style:                str
    @keyword colour_start_name: The name of the starting colour of the linear gradient.
    @type colour_start_name:    str
    @keyword colour_start_rgb:  The RGB array starting colour of the linear gradient.
    @type colour_start_rgb:     RBG colour array (len 3 with vals from 0 to 1)
    @keyword colour_end_name:   The name of the ending colour of the linear gradient.
    @type colour_end_name:      str
    @keyword colour_end_rgb:    The RGB array ending colour of the linear gradient.
    @type colour_end_rgb:       RBG colour array (len 3 with vals from 0 to 1)
    @keyword colour_list:       The colour list to search for the colour names.  Can be either 'molmol' or 'x11'.
    @type colour_list:          str or None
    """

    # Test if the current data pipe exists.
    pipes.test()

    # Test if sequence data exists.
    if not exists_mol_res_spin_data():
        raise RelaxNoSequenceError

    # Check the arguments.
    if colour_start_name != None and colour_start_rgb != None:
        raise RelaxError("The starting colour name and RGB colour array cannot both be supplied.")
    if colour_end_name != None and colour_end_rgb != None:
        raise RelaxError("The ending colour name and RGB colour array cannot both be supplied.")

    # Merge the colour args.
    if colour_start_name != None:
        colour_start = colour_start_name
    else:
        colour_start = colour_start_rgb
    if colour_end_name != None:
        colour_end = colour_end_name
    else:
        colour_end = colour_end_rgb

    # Clear the PyMOL history first.
    pymol_obj.clear_history()

    # Create the macro.
    commands = create_macro(data_type=data_type, style=style, colour_start=colour_start, colour_end=colour_end, colour_list=colour_list)

    # Save the commands as a temporary file, execute it, then delete it.
    try:
        # Temp file name.
        tmpfile = "%s.pml" % mktemp()

        # Open the file.
        file = open(tmpfile, 'w')

        # Loop over the commands and write them.
        for command in commands:
            file.write("%s\n" % command)
        file.close()

        # Execute the macro.
        pymol_obj.exec_cmd("@%s" % tmpfile)

        # Wait a bit for PyMOL to catch up (it takes time for PyMOL to start and the macro to execute).
        sleep(3)

    # Delete the temporary file (no matter what).
    finally:
        # Delete the file.
        delete(tmpfile, fail=False)
Exemplo n.º 6
0
def macro_apply(data_type=None,
                style="classic",
                colour_start_name=None,
                colour_start_rgb=None,
                colour_end_name=None,
                colour_end_rgb=None,
                colour_list=None):
    """Execute a PyMOL macro.

    @keyword data_type:         The data type to map to the structure.
    @type data_type:            str
    @keyword style:             The style of the macro.
    @type style:                str
    @keyword colour_start_name: The name of the starting colour of the linear gradient.
    @type colour_start_name:    str
    @keyword colour_start_rgb:  The RGB array starting colour of the linear gradient.
    @type colour_start_rgb:     RBG colour array (len 3 with vals from 0 to 1)
    @keyword colour_end_name:   The name of the ending colour of the linear gradient.
    @type colour_end_name:      str
    @keyword colour_end_rgb:    The RGB array ending colour of the linear gradient.
    @type colour_end_rgb:       RBG colour array (len 3 with vals from 0 to 1)
    @keyword colour_list:       The colour list to search for the colour names.  Can be either 'molmol' or 'x11'.
    @type colour_list:          str or None
    """

    # Test if the current data pipe exists.
    check_pipe()

    # Test if sequence data exists.
    if not exists_mol_res_spin_data():
        raise RelaxNoSequenceError

    # Check the arguments.
    if colour_start_name != None and colour_start_rgb != None:
        raise RelaxError(
            "The starting colour name and RGB colour array cannot both be supplied."
        )
    if colour_end_name != None and colour_end_rgb != None:
        raise RelaxError(
            "The ending colour name and RGB colour array cannot both be supplied."
        )

    # Merge the colour args.
    if colour_start_name != None:
        colour_start = colour_start_name
    else:
        colour_start = colour_start_rgb
    if colour_end_name != None:
        colour_end = colour_end_name
    else:
        colour_end = colour_end_rgb

    # Clear the PyMOL history first.
    pymol_obj.clear_history()

    # Create the macro.
    commands = create_macro(data_type=data_type,
                            style=style,
                            colour_start=colour_start,
                            colour_end=colour_end,
                            colour_list=colour_list)

    # Save the commands as a temporary file, execute it, then delete it.
    try:
        # Temp file name.
        tmpfile_handle, tmpfile = mkstemp(suffix='.pml')

        # Open the file.
        file = open(tmpfile, 'w')

        # Loop over the commands and write them.
        for command in commands:
            file.write("%s\n" % command)
        file.close()

        # Execute the macro.
        pymol_obj.exec_cmd("@%s" % tmpfile)

        # Wait a bit for PyMOL to catch up (it takes time for PyMOL to start and the macro to execute).
        sleep(3)

    # Delete the temporary file (no matter what).
    finally:
        # Close the open file handle on the OS level.
        close(tmpfile_handle)

        # Delete the temporary file.
        delete(tmpfile, fail=False)