Bumped to version 1.0.0
[be.git] / libbe / storage / util / config.py
1 # Copyright (C) 2005-2011 Aaron Bentley <abentley@panoramicfeedback.com>
2 #                         Gianluca Montecchi <gian@grys.it>
3 #                         W. Trevor King <wking@drexel.edu>
4 #
5 # This file is part of Bugs Everywhere.
6 #
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.
11 #
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.
16 #
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/>.
19
20 """Create, save, and load the per-user config file at :func:`path`.
21 """
22
23 import ConfigParser
24 import codecs
25 import os.path
26
27 import libbe
28 import libbe.util.encoding
29 if libbe.TESTING == True:
30     import doctest
31
32
33 default_encoding = libbe.util.encoding.get_filesystem_encoding()
34 """Default filesystem encoding.
35
36 Initialized with :func:`libbe.util.encoding.get_filesystem_encoding`.
37 """
38
39 def path():
40     """Return the path to the per-user config file.
41
42     Defaults to :file:`~/.bugs_everywhere`.
43     """
44     return os.path.expanduser(os.path.join('~','.bugs_everywhere'))
45
46 def set_val(name, value, section="DEFAULT", encoding=None):
47     """Set a value in the per-user config file.
48
49     Parameters
50     ----------
51     name : str
52       The name of the value to set.
53     value : str or None
54       The new value to set (or None to delete the value).
55     section : str
56       The section to store the name/value in.
57     encoding : str
58       The config file's encoding, defaults to :data:`default_encoding`.
59     """
60     if encoding == None:
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())
67     f.close()
68     if value is not None:
69         config.set(section, name, value)
70     else:
71         config.remove_option(section, name)
72     f = codecs.open(path(), 'w', encoding)
73     config.write(f)
74     f.close()
75
76 def get_val(name, section="DEFAULT", default=None, encoding=None):
77     """Get a value from the per-user config file
78
79     Parameters
80     ----------
81     name : str
82       The name of the value to set.
83     section : str
84       The section to store the name/value in.
85     default :
86       The value to return if `name` is not set.
87     encoding : str
88       The config file's encoding, defaults to :data:`default_encoding`.
89
90     Examples
91     --------
92
93     >>> get_val("junk") is None
94     True
95     >>> set_val("junk", "random")
96     >>> get_val("junk")
97     u'random'
98     >>> set_val("junk", None)
99     >>> get_val("junk") is None
100     True
101     """
102     if os.path.exists(path()):
103         if encoding == None:
104             encoding = default_encoding
105         config = ConfigParser.ConfigParser()
106         f = codecs.open(path(), 'r', encoding)
107         config.readfp(f, path())
108         f.close()
109         try:
110             return config.get(section, name)
111         except ConfigParser.NoOptionError:
112             return default
113     else:
114         return default
115
116 if libbe.TESTING == True:
117     suite = doctest.DocTestSuite()