1 # Copyright (C) 2005-2010 Aaron Bentley and Panometrics, Inc.
2 # Gianluca Montecchi <gian@grys.it>
3 # W. Trevor King <wking@drexel.edu>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 """Create, save, and load the per-user config file at :func:`path`.
27 import libbe.util.encoding
28 if libbe.TESTING == True:
32 default_encoding = libbe.util.encoding.get_filesystem_encoding()
33 """Default filesystem encoding.
35 Initialized with :func:`libbe.util.encoding.get_filesystem_encoding`.
39 """Return the path to the per-user config file.
41 return os.path.expanduser("~/.bugs_everywhere")
43 def set_val(name, value, section="DEFAULT", encoding=None):
44 """Set a value in the per-user config file.
49 The name of the value to set.
51 The new value to set (or None to delete the value).
53 The section to store the name/value in.
55 The config file's encoding, defaults to :data:`default_encoding`.
58 encoding = default_encoding
59 config = ConfigParser.ConfigParser()
60 if os.path.exists(path()) == False: # touch file or config
61 open(path(), 'w').close() # read chokes on missing file
62 f = codecs.open(path(), 'r', encoding)
63 config.readfp(f, path())
66 config.set(section, name, value)
68 config.remove_option(section, name)
69 f = codecs.open(path(), 'w', encoding)
73 def get_val(name, section="DEFAULT", default=None, encoding=None):
74 """Get a value from the per-user config file
79 The name of the value to set.
81 The section to store the name/value in.
83 The value to return if `name` is not set.
85 The config file's encoding, defaults to :data:`default_encoding`.
90 >>> get_val("junk") is None
92 >>> set_val("junk", "random")
95 >>> set_val("junk", None)
96 >>> get_val("junk") is None
99 if os.path.exists(path()):
101 encoding = default_encoding
102 config = ConfigParser.ConfigParser()
103 f = codecs.open(path(), 'r', encoding)
104 config.readfp(f, path())
107 return config.get(section, name)
108 except ConfigParser.NoOptionError:
113 if libbe.TESTING == True:
114 suite = doctest.DocTestSuite()