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
6 The choices system is DEPRECATED. See choices.migrate().
10 from os
.path
import exists
15 path
= os
.environ
['CHOICESPATH']
16 paths
= path
.split(':')
18 paths
= [ os
.environ
['HOME'] + '/Choices',
19 '/usr/local/share/Choices',
20 '/usr/share/Choices' ]
23 """When you want to load user choices, use this function. 'dir' is
24 the subdirectory within Choices where the choices are saved (usually
25 this will be the name of your program). 'leaf' is the file within it.
26 If serveral files are present, the most important one is returned. If
27 no files are there, returns None.
28 Eg ('Edit', 'Options') - > '/usr/local/share/Choices/Edit/Options'"""
30 assert dir not in _migrated
34 full
= path
+ '/' + dir + '/' + leaf
40 def save(dir, leaf
, create
= 1):
41 """Returns a path to save to, or None if saving is disabled.
42 If 'create' is FALSE then no directories are created. 'dir' and
43 'leaf' are as for load()."""
45 assert dir not in _migrated
51 if create
and not os
.path
.exists(p
):
54 if create
and not os
.path
.exists(p
):
59 def migrate(dir, site
):
60 """Move <Choices>/dir (if it exists) to $XDG_CONFIG_HOME/site/dir, and
61 put a symlink in its place. The choices load and save functions cannot
62 be used for 'dir' after this; use the basedir module instead. 'site'
63 should be a domain name owned or managed by you. Eg:
64 choices.migrate('Edit', 'rox.sourceforge.net')"""
65 assert dir not in _migrated
69 home_choices
= paths
[0]
71 return # Saving disabled
73 full
= home_choices
+ '/' + dir
74 if os
.path
.islink(full
) or not os
.path
.exists(full
):
78 dest
= os
.path
.join(basedir
.xdg_config_home
, site
, dir)
79 if os
.path
.exists(dest
):
81 "Old config directory '%s' and new config " \
82 "directory '%s' both exist. Not migrating settings!" % \
86 site_dir
= os
.path
.join(basedir
.xdg_config_home
, site
)
87 if not os
.path
.isdir(site_dir
):
90 os
.symlink(dest
, full
)