1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
# (C) Copyright 2005-2025 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" The interface for a node in a preferences hierarchy. """
# Enthought library imports.
from traits.api import Instance, Interface, Str
class IPreferences(Interface):
""" The interface for a node in a preferences hierarchy. """
# The absolute path to this node from the root node (the empty string if
# this node *is* the root node).
path = Str
# The parent node (None if this node *is* the root node).
parent = Instance("IPreferences")
# The name of the node relative to its parent (the empty string if this
# node *is* the root node).
name = Str
#### Methods where 'path' refers to a preference ####
def get(self, path, default=None, inherit=False):
"""Get the value of the preference at the specified path.
If no value exists for the path (or any part of the path does not
exist) then return the default value.
Preference values are *always* returned as strings.
e.g::
preferences.set('acme.ui.bgcolor', 'blue')
preferences.get('acme.ui.bgcolor') -> 'blue'
preferences.set('acme.ui.width', 100)
preferences.get('acme.ui.width') -> '100'
preferences.set('acme.ui.visible', True)
preferences.get('acme.ui.visible') -> 'True'
If 'inherit' is True then we allow 'inherited' preference values.
e.g. If we are looking up::
'acme.ui.widget.bgcolor'
and it does not exist then we will also try::
'acme.ui.bgcolor'
'acme.bgcolor'
'bgcolor'
Raise a 'ValueError' exception if the path is the empty string.
"""
def remove(self, path):
"""Remove the preference at the specified path.
Does nothing if no value exists for the path (or any part of the path
does not exist.
Raise a 'ValueError' exception if the path is the empty string.
e.g.::
preferences.remove('acme.ui.bgcolor')
"""
def set(self, path, value):
"""Set the value of the preference at the specified path.
Any missing nodes are created automatically.
Primitive Python types can be set, but preferences are *always*
stored and returned as strings.
e.g::
preferences.set('acme.ui.bgcolor', 'blue')
preferences.get('acme.ui.bgcolor') -> 'blue'
preferences.set('acme.ui.width', 100)
preferences.get('acme.ui.width') -> '100'
preferences.set('acme.ui.visible', True)
preferences.get('acme.ui.visible') -> 'True'
Raise a 'ValueError' exception if the path is the empty string.
"""
#### Methods where 'path' refers to a node ####
def clear(self, path=""):
"""Remove all preference from the node at the specified path.
If the path is the empty string (the default) then remove the
preferences in *this* node.
This does not affect any of the node's children.
e.g. To clear the preferences out of a node directly::
preferences.clear()
Or to clear the preferences of a node at a given path::
preferences.clear('acme.ui')
"""
def keys(self, path=""):
"""Return the preference keys of the node at the specified path.
If the path is the empty string (the default) then return the
preference keys of *this* node.
e.g::
keys = preferences.keys('acme.ui')
"""
def node(self, path=""):
"""Return the node at the specified path.
If the path is the empty string (the default) then return *this* node.
Any missing nodes are created automatically.
e.g::
node = preferences.node('acme.ui')
bgcolor = node.get('bgcolor')
"""
def node_exists(self, path=""):
"""Return True if the node at the specified path exists
If the path is the empty string (the default) then return True.
e.g::
exists = preferences.exists('acme.ui')
"""
def node_names(self, path=""):
"""Return the names of the children of the node at the specified path.
If the path is the empty string (the default) then return the names of
the children of *this* node.
e.g::
names = preferences.node_names('acme.ui')
"""
#### Persistence methods ####
def flush(self):
"""Force any changes in the node to the backing store.
This includes any changes to the node's descendants.
"""
|