add get_user_dir(), get_user_file() to AppConfigParser

This commit is contained in:
Lance Edgar 2012-08-09 09:39:23 -07:00
parent 411ed8d31f
commit aa4d536f5e

View file

@ -115,6 +115,39 @@ class AppConfigParser(ConfigParser.SafeConfigParser):
d[opt] = self.get(section, opt)
return d
def get_user_dir(self, create=False):
"""
Returns a path to the "preferred" user-level folder, in which
additional config files (etc.) may be placed as needed. This
essentially returns a platform-specific variation of ``~/.appname``.
If ``create`` is ``True``, then the folder will be created if it does
not already exist.
"""
path = os.path.expanduser('~/.%s' % self.appname)
if sys.platform == 'win32':
from win32com.shell import shell, shellcon
path = os.path.join(
shell.SHGetSpecialFolderPath(0, shellcon.CSIDL_APPDATA),
self.appname)
if create and not os.path.exists(path):
os.mkdir(path)
return path
def get_user_file(self, filename, create=False):
"""
Returns a full path to a user-level config file location. This is
obtained by first calling :meth:`get_user_dir()` and then joining the
result with ``filename``.
The ``create`` argument will be passed directly to
:meth:`get_user_dir()`, and may be used to ensure the user-level folder
exists.
"""
return os.path.join(self.get_user_dir(create=create), filename)
def options(self, section):
"""
Overridden version of ``ConfigParser.SafeConfigParser.options()``.