add get_user_dir(), get_user_file() to AppConfigParser
This commit is contained in:
		
							parent
							
								
									411ed8d31f
								
							
						
					
					
						commit
						aa4d536f5e
					
				
					 1 changed files with 33 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -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()``.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue