Moved python files into a 'rox' subdirectory so they don't clash with
[rox-lib.git] / python / rox / choices.py
blob91d9aa9c0f082c4a5bea568e58c7afa54c30bf36
1 import os
2 from os.path import exists
3 import string
5 paths = None
7 def init():
8 global paths
10 try:
11 path = os.environ['CHOICESPATH']
12 paths = string.split(path, ':')
13 except KeyError:
14 paths = [ os.environ['HOME'] + '/Choices',
15 '/usr/local/share/Choices',
16 '/usr/share/Choices' ]
18 def load(dir, leaf):
19 "Eg ('Edit', 'Options') - > '/usr/local/share/Choices/Edit/Options'"
21 if paths == None:
22 init()
24 for path in paths:
25 if path:
26 full = path + '/' + dir + '/' + leaf
27 if exists(full):
28 return full
30 return None
32 def save(dir, leaf, create = 1):
33 "As for load. Returns a path to save to, or None if saving is disabled."
34 "If 'create' is FALSE then no directories are created."
35 if paths == None:
36 init()
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