diff options
author | W. Trevor King <wking@drexel.edu> | 2008-11-25 15:47:19 -0500 |
---|---|---|
committer | W. Trevor King <wking@drexel.edu> | 2008-11-25 15:47:19 -0500 |
commit | 5699aef2a5741c5ffc24d9cb12d6bc9b085d484a (patch) | |
tree | 84ac8783ce2d1f9a28ba0bd0c256ae7179c68507 /libbe/config.py | |
parent | ed4d971d1375a692fbd3a394237f56e851bb5d0e (diff) | |
download | bugseverywhere-5699aef2a5741c5ffc24d9cb12d6bc9b085d484a.tar.gz |
Added libbe/encoding.py to wrap input/output/file access appropriately.
I borrowed most of the code for this.
get_encoding() is from Trac
http://trac.edgewall.org/browser/trunk/trac/util/datefmt.py
format_datetime()
Trac has a BSD license
http://trac.edgewall.org/wiki/TracLicense
I don't know if such a small snippet requires us to "reproduce the
above copyright" or where we need to reproduce it if it is needed.
The stdout/stdin replacement code follows
http://wiki.python.org/moin/ShellRedirectionFails
Because of the stdout replacement, the doctests executes now need an
optional 'test' argument to turn off replacement during the doctests,
otherwise doctest flips out (since it had set up stdout to catch
output, and then we clobbered it's setup).
References:
http://wiki.python.org/moin/Unicode
http://www.amk.ca/python/howto/unicode
http://www.python.org/dev/peps/pep-0100/
I also split libbe/editor.py off from libbe.utility.py and started
explaining the motivation for the BugDir init flags in it's docstring.
Diffstat (limited to 'libbe/config.py')
-rw-r--r-- | libbe/config.py | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/libbe/config.py b/libbe/config.py index 79c0d6f..94c700e 100644 --- a/libbe/config.py +++ b/libbe/config.py @@ -15,30 +15,40 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import ConfigParser +import codecs +import locale import os.path +import sys import doctest +default_encoding = sys.getfilesystemencoding() or locale.getpreferredencoding() + def path(): """Return the path to the per-user config file""" return os.path.expanduser("~/.bugs_everywhere") -def set_val(name, value, section="DEFAULT"): +def set_val(name, value, section="DEFAULT", encoding=None): """Set a value in the per-user config file :param name: The name of the value to set :param value: The new value to set (or None to delete the value) :param section: The section to store the name/value in """ + if encoding == None: + encoding = default_encoding config = ConfigParser.ConfigParser() - config.read(path()) + f = codecs.open(path(), "r", encoding) + config.readfp(f, path()) + f.close() if value is not None: config.set(section, name, value) else: config.remove_option(section, name) - config.write(file(path(), "wb")) - pass + f = codecs.open(path(), "w", encoding) + config.write(f) + f.close() -def get_val(name, section="DEFAULT"): +def get_val(name, section="DEFAULT", encoding=None): """ Get a value from the per-user config file @@ -49,13 +59,17 @@ def get_val(name, section="DEFAULT"): True >>> set_val("junk", "random") >>> get_val("junk") - 'random' + u'random' >>> set_val("junk", None) >>> get_val("junk") is None True """ + if encoding == None: + encoding = default_encoding config = ConfigParser.ConfigParser() - config.read(path()) + f = codecs.open(path(), "r", encoding) + config.readfp(f, path()) + f.close() try: return config.get(section, name) except ConfigParser.NoOptionError: |