Better help for processes.
[rox-lib.git] / python / rox / choices.py
blob68b623b0a9c75aace1a50c74169c284ba4d75b45
1 """This module implements the Choices system for user preferences.
2 The environment variable CHOICESPATH gives a list of directories to search
3 for choices. Changed choices are saved back to the first directory in the
4 list."""
6 import os
7 from os.path import exists
9 try:
10 path = os.environ['CHOICESPATH']
11 paths = path.split(':')
12 except KeyError:
13 paths = [ os.environ['HOME'] + '/Choices',
14 '/usr/local/share/Choices',
15 '/usr/share/Choices' ]
17 def load(dir, leaf):
18 """When you want to load user choices, use this function. 'dir' is
19 the subdirectory within Choices where the choices are saved (usually
20 this will be the name of your program). 'leaf' is the file within it.
21 If serveral files are present, the most important one is returned. If
22 no files are there, returns None.
23 Eg ('Edit', 'Options') - > '/usr/local/share/Choices/Edit/Options'"""
25 for path in paths:
26 if path:
27 full = path + '/' + dir + '/' + leaf
28 if exists(full):
29 return full
31 return None
33 def save(dir, leaf, create = 1):
34 """Returns a path to save to, or None if saving is disabled.
35 If 'create' is FALSE then no directories are created. 'dir' and
36 'leaf' are as for load()."""
38 p = paths[0]
39 if not p:
40 return None
42 if create and not os.path.exists(p):
43 os.mkdir(p, 0x1ff)
44 p = p + '/' + dir
45 if create and not os.path.exists(p):
46 os.mkdir(p, 0x1ff)
48 return p + '/' + leaf