Handle thumbnail creation failure according to the standard.
[rox-lib.git] / ROX-Lib2 / python / rox / choices.py
blob09be08e920a3f827708ec95e7b88707d2d66aa38
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 The choices system is DEPRECATED. See choices.migrate().
7 """
9 import os, sys
10 from os.path import exists
12 _migrated = {}
14 try:
15 path = os.environ['CHOICESPATH']
16 paths = path.split(':')
17 except KeyError:
18 paths = [ os.environ['HOME'] + '/Choices',
19 '/usr/local/share/Choices',
20 '/usr/share/Choices' ]
22 def load(dir, leaf):
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
32 for path in paths:
33 if path:
34 full = path + '/' + dir + '/' + leaf
35 if exists(full):
36 return full
38 return None
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
47 p = paths[0]
48 if not p:
49 return None
51 if create and not os.path.exists(p):
52 os.mkdir(p, 0x1ff)
53 p = p + '/' + dir
54 if create and not os.path.exists(p):
55 os.mkdir(p, 0x1ff)
57 return p + '/' + leaf
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
67 _migrated[dir] = True
69 home_choices = paths[0]
70 if not home_choices:
71 return # Saving disabled
73 full = home_choices + '/' + dir
74 if os.path.islink(full) or not os.path.exists(full):
75 return
77 import basedir
78 dest = os.path.join(basedir.xdg_config_home, site, dir)
79 if os.path.exists(dest):
80 print >>sys.stderr, \
81 "Old config directory '%s' and new config " \
82 "directory '%s' both exist. Not migrating settings!" % \
83 (full, dest)
84 return
86 site_dir = os.path.join(basedir.xdg_config_home, site)
87 if not os.path.isdir(site_dir):
88 os.makedirs(site_dir)
89 os.rename(full, dest)
90 os.symlink(dest, full)