1""" 2Support code for the freedesktop.org basedir spec. 3 4This module provides functions for locating configuration files. 5 6@see: U{http://freedesktop.org/wiki/Standards/basedir-spec} 7 8@var home: The value of $HOME (or '/' if not set). If we're running as root and 9$HOME isn't owned by root, then this will be root's home from /etc/passwd 10instead. 11""" 12 13# Copyright (C) 2009, Thomas Leonard 14# See the README file for details, or visit http://0install.net. 15 16fromzeroinstallimport_,logger 17importos 18 19home=os.environ.get('HOME','/') 20 21try: 22_euid=os.geteuid() 23exceptAttributeError: 24pass# Windows? 25else: 26if_euid==0: 27# We're running as root. Ensure that $HOME really is root's home, 28# not the user's home, or we're likely to fill it will unreadable 29# root-owned files. 30home_owner=os.stat(home).st_uid 31ifhome_owner!=0: 32importpwd 33old_home=home 34home=pwd.getpwuid(0).pw_diror'/' 35logger.info(_("$HOME (%(home)s) is owned by user %(user)d, but we are root (0). Using %(root_home)s instead."),{'home':old_home,'user':home_owner,'root_home':home}) 36delold_home 37delhome_owner 38 39portable_base=os.environ.get('ZEROINSTALL_PORTABLE_BASE') 40ifportable_base: 41xdg_data_dirs=[os.path.join(portable_base,"data")] 42xdg_cache_dirs=[os.path.join(portable_base,"cache")] 43xdg_config_dirs=[os.path.join(portable_base,"config")] 44else: 45ifos.name=="nt": 46fromwin32com.shellimportshell,shellcon 47appData=shell.SHGetFolderPath(0,shellcon.CSIDL_APPDATA,0,0) 48localAppData=shell.SHGetFolderPath(0,shellcon.CSIDL_LOCAL_APPDATA,0,0) 49commonAppData=shell.SHGetFolderPath(0,shellcon.CSIDL_COMMON_APPDATA,0,0) 50 51_default_paths={ 52'DATA':[appData,commonAppData], 53'CACHE':[localAppData,commonAppData], 54'CONFIG':[appData,commonAppData], 55} 56else: 57_default_paths={ 58'DATA':[os.path.join(home,'.local','share'),'/usr/local/share','/usr/share'], 59'CACHE':[os.path.join(home,'.cache'),'/var/cache'], 60'CONFIG':[os.path.join(home,'.config'),'/etc/xdg'], 61} 62
90"""Ensure $XDG_CONFIG_HOME/<resource>/ exists, and return its path. 91 'resource' should normally be the name of your application. Use this 92 when SAVING configuration settings. Use the xdg_config_dirs variable 93 for loading. 94 @rtype: str""" 95resource=os.path.join(*resource) 96assertnotos.path.isabs(resource) 97path=os.path.join(xdg_config_home,resource) 98ifnotos.path.isdir(path): 99os.makedirs(path,0o700)100returnpath
103"""Returns an iterator which gives each directory named 'resource' in the104 configuration search path. Information provided by earlier directories should105 take precedence over later ones (ie, the user's config dir comes first)."""106resource=os.path.join(*resource)107forconfig_dirinxdg_config_dirs:108path=os.path.join(config_dir,resource)109ifos.path.exists(path):yieldpath
112"""Returns the first result from load_config_paths, or None if there is nothing113 to load.114 @rtype: str | None"""115forxinload_config_paths(*resource):116returnx117returnNone
120"""Ensure $XDG_CACHE_HOME/<resource>/ exists, and return its path.121 'resource' should normally be the name of your application.122 @rtype: str"""123resource=os.path.join(*resource)124assertnotos.path.isabs(resource)125path=os.path.join(xdg_cache_home,resource)126ifnotos.path.isdir(path):127os.makedirs(path,0o700)128returnpath
131"""Returns an iterator which gives each directory named 'resource' in the132 cache search path. Information provided by earlier directories should133 take precedence over later ones (ie, the user's cache dir comes first)."""134resource=os.path.join(*resource)135forcache_dirinxdg_cache_dirs:136path=os.path.join(cache_dir,resource)137ifos.path.exists(path):yieldpath
140"""Returns the first result from load_cache_paths, or None if there is nothing141 to load.142 @rtype: str | None"""143forxinload_cache_paths(*resource):144returnx145returnNone
148"""Returns an iterator which gives each directory named 'resource' in the149 shared data search path. Information provided by earlier directories should150 take precedence over later ones.151 @since: 0.28"""152resource=os.path.join(*resource)153fordata_dirinxdg_data_dirs:154path=os.path.join(data_dir,resource)155ifos.path.exists(path):yieldpath
158"""Returns the first result from load_data_paths, or None if there is nothing159 to load.160 @rtype: str | None161 @since: 0.28"""162forxinload_data_paths(*resource):163returnx164returnNone
167"""Ensure $XDG_DATA_HOME/<resource>/ exists, and return its path.168 'resource' should normally be the name of your application.169 @rtype: str"""170resource=os.path.join(*resource)171assertnotos.path.isabs(resource)172path=os.path.join(xdg_data_home,resource)173ifnotos.path.isdir(path):174os.makedirs(path,0o700)175returnpath