1 # Copyright (C) 2005-2011 Aaron Bentley <abentley@panoramicfeedback.com>
2 # Gianluca Montecchi <gian@grys.it>
3 # W. Trevor King <wking@drexel.edu>
5 # This file is part of Bugs Everywhere.
7 # Bugs Everywhere is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation, either version 2 of the License, or (at your
10 # option) any later version.
12 # Bugs Everywhere is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Bugs Everywhere. If not, see <http://www.gnu.org/licenses/>.
20 """Create, save, and load the per-user config file at :func:`path`.
28 import libbe.util.encoding
29 if libbe.TESTING == True:
33 default_encoding = libbe.util.encoding.get_filesystem_encoding()
34 """Default filesystem encoding.
36 Initialized with :func:`libbe.util.encoding.get_filesystem_encoding`.
40 """Return the path to the per-user config file.
42 Defaults to :file:`~/.bugs_everywhere`.
44 return os.path.expanduser(os.path.join('~','.bugs_everywhere'))
46 def set_val(name, value, section="DEFAULT", encoding=None):
47 """Set a value in the per-user config file.
52 The name of the value to set.
54 The new value to set (or None to delete the value).
56 The section to store the name/value in.
58 The config file's encoding, defaults to :data:`default_encoding`.
61 encoding = default_encoding
62 config = ConfigParser.ConfigParser()
63 if os.path.exists(path()) == False: # touch file or config
64 open(path(), 'w').close() # read chokes on missing file
65 f = codecs.open(path(), 'r', encoding)
66 config.readfp(f, path())
69 config.set(section, name, value)
71 config.remove_option(section, name)
72 f = codecs.open(path(), 'w', encoding)
76 def get_val(name, section="DEFAULT", default=None, encoding=None):
77 """Get a value from the per-user config file
82 The name of the value to set.
84 The section to store the name/value in.
86 The value to return if `name` is not set.
88 The config file's encoding, defaults to :data:`default_encoding`.
93 >>> get_val("junk") is None
95 >>> set_val("junk", "random")
98 >>> set_val("junk", None)
99 >>> get_val("junk") is None
102 if os.path.exists(path()):
104 encoding = default_encoding
105 config = ConfigParser.ConfigParser()
106 f = codecs.open(path(), 'r', encoding)
107 config.readfp(f, path())
110 return config.get(section, name)
111 except ConfigParser.NoOptionError:
116 if libbe.TESTING == True:
117 suite = doctest.DocTestSuite()