brennivin.preferences module

Contains the Preferences class for serializing preferences. It is very simple (prefs are a dict of dicts) and flexible (can be serialized using user-provided functions).

Members

class brennivin.preferences.Preferences(filename, onloaderror=None)

Handles the serialization of preferences values. Pickled makes the following guarantees about its data:

  • If the file or directories does not exist, they will be created.
  • If the file exists but is corrupt (either the data is corrupt, or it is filled with not-a-dict), preferences will be reset.
Parameters:
  • filename – The filename where preferences will be saved. Directories will be created automatically on init if they do not exist.
  • onloaderror – If provided, invoke this function in the case of an error on Load. If None, just log out that an error occured. Errors on Save will still be raised, of course.

Override the loader(), dumper(), and openmode() methods to use a serializaer other than json.

dumper(obj, fp)

Like json.dump(obj, fp)

get(region, variable, defaultValue)

Get a preference value from the pickled data.

Parameters:
  • region – The parent group that owns the variable.
  • variable – The name of the stored variable.
  • defaultValue – Value to return if the key or region do not exist.
load()

Load a pickled file into the local prefs dict. If the data in the file is not a dict, reset all prefs to be a dict.

loader(fp)

Like json.load(fp)

openmode()

‘t’ or ‘b’ indicating the way to open the persisted file.

save()

Save the internal data in a pickle file.

set(region, variable, value)

Register a value to be stored in a cPickle file.

Parameters:
  • region – The parent group that owns the variable.
  • variable – The name of the variable.
  • value – The value to be stored as region.variable.
setdefault(region, variable, defaultValue)

If get() (region, variable) is not set, performs a set() (region, variable, defaultValue) and returns defaultValue.