From aa4d536f5ea480929d1d4c5a562a54240b71e68d Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Thu, 9 Aug 2012 09:39:23 -0700 Subject: [PATCH] add get_user_dir(), get_user_file() to AppConfigParser --- edbob/configuration.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/edbob/configuration.py b/edbob/configuration.py index 52554ac..4d8edbe 100644 --- a/edbob/configuration.py +++ b/edbob/configuration.py @@ -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()``.