Package schrodinger :: Package utils :: Module preferences :: Class Preferences
[hide private]
[frames] | no frames]

Class Preferences

object --+
         |
        Preferences

A class that allows Pythonic access to the mmpref library.

Instance Methods [hide private]
 
__init__(self, product)
Create a new Preferences object
 
__del__(self)
Delete the handle and write the preferences to the preference file.
 
sync(self)
Sync the settings to file and reload settings modified by other threads/process.
 
clear(self)
This will clear all preferences associated with this handle.
constant
getKeyType(self, key)
Gets the type of data stored for this key.
int
remove(self, key, key_type=None)
Remove the key or group and its values.
bool
contains(self, key, key_type=None)
Check for the presence of key.
 
beginGroup(self, group, toplevel=False)
Indicate the group for future key searches by appending group to the current group path.
str
getGroup(self)
Get the current group set via the beginGroup method.
 
endGroup(self)
Remove the current group set via the beginGroup method from the group search path.
 
resetGroup(self, retain=0)
Unset the group path so that searches begin at the top level for the product.
 
beginOS(self, platform=None)
Set the OS.
 
endOS(self)
Unset the OS.
 
set(self, key, value)
Set the value of key.
 
get(self, key, default=object())
Get the value of key.
dict
getAllPreferences(self, group='')
Get a dictionary of all preferences in the current group path.
 
setCommentForKey(self, key, comment, key_type=None)
Set a comment for the given key.
 
_check_pref_status(self)
Raise appropriate exception based on status.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, product)
(Constructor)

 

Create a new Preferences object

Parameters:
  • product (constant) - A product name constant supplied in this module or one from the pymmlibs module. Constants supplied in this module:
    • CANVAS
    • COMBIGLIDE
    • DESMOND
    • EPIK
    • IMPACT
    • JAGUAR
    • KNIME
    • MACROMODEL
    • MAESTRO
    • MCPRO
    • PHASE
    • PSP
    • PYMOL
    • QIKPROP
    • SCRIPTS
    • SHARED
    • WATERMAP
Raises:
  • ValueError - If product is not a recognized constant
  • IOError - If the preferences file is inaccessible.
  • IOError - If the preferences file is inaccessible.
  • RuntimeError - If another error is encountered.
Overrides: object.__init__

getKeyType(self, key)

 

Gets the type of data stored for this key.

Parameters:
  • key (str) - key or group to remove
Returns: constant
A constant indicating the data type stored for this key. Valid constants are
  • INT
  • STRING
  • FLOAT
  • BOOL

If the key is not found, None is returned.

remove(self, key, key_type=None)

 

Remove the key or group and its values.

To remove a group and its associated keys, set type to NO_TYPE.

If a group is set using beginGroup, the search will happen within the group. If a comment is set for the key, it will be removed automatically. The key matching the running platform is given higher preference over the general one.

Parameters:
  • key (str) - key or group to remove
  • key_type (constant) - A constant indicating the data type of this key or if it is a group. If not given, an attempt will be made to remove key if it is found as a key and if not it will be treated as a group. Valid constants are
    • INT
    • STRING
    • FLOAT
    • BOOL
    • NO_TYPE (groups)
Returns: int
The number of keys removed
Raises:
  • ValueError - If key_type is not a recognized type

contains(self, key, key_type=None)

 

Check for the presence of key.

Check if the given key is available matching the give datatype.

If beginGroup is called before this function, the given key will be searched for relative to the group.

Keys specific to the running platform and non-platform specific keys attribute will be considered in the search. Note that the platform searched for the key is the running platform and not the platform set by beginOS.

Parameters:
  • key (str) - key to search for
  • key_type (constant) - A constant indicating the data type of this key. If not given, an attempt will be made to find a key of any data type. Valid values are
    • INT
    • STRING
    • FLOAT
    • BOOL
Returns: bool
True if the key is found, False if not
Raises:
  • ValueError - If key_type is not a valid type

beginGroup(self, group, toplevel=False)

 

Indicate the group for future key searches by appending group to the current group path. This group will be used for future group/key searches until endGroup is called. This is useful to avoid typing the common path again and again to query sets of keys.

A product's keys can be grouped together under various paths. For instance, the SCRIPT product may have a PoseExplorer group, and that group may have Paths and Dialogs subgroups. One could access the 'import' key under the PoseExplorer/Paths group via:

   handler = Preferences(SCRIPTS)
   handler.beginGroup('PoseExplorer')
   handler.beginGroup('Paths')
   import_dir = handler.get('import')

or:

   handler = PreferenceHandler(SCRIPTS)
   handler.beginGroup('PoseExplorer/Paths')
   import_dir = handler.get('import')

or:

   handler = Preferences(SCRIPTS)
   import_dir = handler.get('PoseExplorer/Paths/import')
Parameters:
  • group (str) - the group to append to the current group path
  • toplevel (bool) - If True, treat this group as a toplevel group - i.e. the group path will simply be 'group' after this. If False (default), this group should be appended to the current group path.
Raises:
  • ValueError - if key contain invalid character
  • MmException - otherwise

getGroup(self)

 

Get the current group set via the beginGroup method.

Returns: str
The current group, or "" if no group is set

endGroup(self)

 

Remove the current group set via the beginGroup method from the group search path. The new group path will be the same as it was before the last invocation of beginGroup.

resetGroup(self, retain=0)

 

Unset the group path so that searches begin at the top level for the product.

Parameters:
  • retain (int) - The number of groups in the path to retain below the top level. For instance, if the current group is a/b/c/d, self.resetGroup(retain=1) would set the current group to a.

beginOS(self, platform=None)

 

Set the OS. Future set() and set_x() commands will set OS-specific values for keys. Without calling this method, set() and set_x() will set non-OS-specific values.

Keys specific to the running platform get higher preference in get functions.

To stop setting platform specific key(s), use endOS.

Parameters:
  • platform (constant) - One of the OS constants. By default, the running platform is used.
    • WINDOWS
    • LINUX
    • DARWIN
    • NO_OS (non-OS specific, same as using endOS
Raises:
  • ValueError - If platform is not a valid type

endOS(self)

 

Unset the OS. Future set() and set_x() commands will set non-OS-specific values for keys.

Keys specific to the running platform get higher preference in get functions.

set(self, key, value)

 

Set the value of key. Note that the underlying library stores keys in a type specific way, but this function and the get function abstract that away.

If key does not exist, it is created.

If the group path has been set via beginGroup, it is honored.

If the OS has been set via beginOS, it is honored. OS-specific key values get higher preference when searching for keys.

Parameters:
  • key (str) - The key to set the value for. Can be a key name or a group path/key name.
  • value (bool, int, float, or str) - The value to set key to
Raises:
  • ValueError - if key contain invalid character
  • TypeError - if type of existing key is being changed
  • MmException - otherwise

get(self, key, default=object())

 

Get the value of key. Note that the underlying library stores keys in a type specific way, but this function and the set function abstract that away.

If key does not exist, the value of the default argument is returned if it is given. If the key does not exist and default is not given, a KeyError is raised.

If the group path has been set via beginGroup, it is honored.

If the OS has been set via beginOS, it is honored. OS-specific key values get higher preference when searching for keys.

The user-specific preference file is checked for the key first, then the default installation files are checked.

Parameters:
  • key (str) - The key to get the value for. Can be a key name or a group path/key name.
Raises:
  • KeyError - If the key is not found and default is not given

getAllPreferences(self, group='')

 

Get a dictionary of all preferences in the current group path. The dictionary keys will be preference names and the dictionary values will be preference values. Note that preference names will include the relative group path starting at the current group.

If the OS has been set via beginOS, it is honored. OS-specific key values get higher preference when searching for keys.

Parameters:
  • group (str) - Append this group path to the current group path before searching for keys. Note that this persists during this operation only, after returning the dictionary the group path returns to its previous value.
Returns: dict
dictionary of preferences with preference names as keys and preference values as values.

setCommentForKey(self, key, comment, key_type=None)

 

Set a comment for the given key.

If the group path has been set via beginGroup, it is honored.

If the OS has been set via beginOS, it is honored. OS-specific key values get higher preference when searching for keys.

Parameters:
  • key (str) - The key to set the value for. Can be a key name or a group path/key name.
  • comment (str) - The commment for the key
  • key_type (constant) - A constant indicating the data type of this key. If not given, an attempt will be made to find a key of any data type. Valid values are
    • INT
    • STRING
    • FLOAT
    • BOOL
Raises:
  • KeyError - If key is not found
  • Exception - Unknown error condition