Save settings

From PyMOLWiki
Revision as of 13:06, 8 November 2011 by Speleo3 (talk | contribs) (CmdException)
Jump to navigation Jump to search

save_settings is a prototype implementation to save all settings with non-default values to a file.

By default settings will be stored to ~/ which is automatically recognised and loaded by PyMOL on startup. You have to manually delete the file if you no more want those settings to be loaded.

This answers a feature request on sourceforge.


save_settings [ filename ]

The Script

from pymol import cmd, CmdException

def save_settings(filename='~/', quiet=1):
    Dumps all settings with non-default values to ~/
    Feature Request: Save settings for later use - ID: 1009951
    from pymol.setting import get_name_list
    quiet = int(quiet)
    if not filename.endswith('.py'):
        print 'Error: filename must end with ".py"'
        raise CmdException
    # temporatily load default settings and remember them
    original = [(name, cmd.get(name)) for name in get_name_list()]
    # dump to file
    filename = cmd.exp_path(filename)
    f = open(filename, 'w')
    print >> f, '# AUTOGENERATED FILE'
    print >> f, 'from pymol import cmd, invocation'
    print >> f, 'if invocation.options.show_splash:'
    print >> f, '    print "Loading settings from",', repr(filename)
    count = 0
    for name, o_value in original:
        value = cmd.get(name)
        if value != o_value:
            print >> f, 'cmd.set("%s", %s)' % (name, repr(value))
            if not quiet:
                print 'set %s, %s # default: %s' % (name, value, o_value)
            count += 1
    if not quiet:
        print 'Dumped %d settings to %s' % (count, filename)

cmd.extend('save_settings', save_settings)

See Also