Package flumotion :: Package common :: Module xdg
[hide private]

Source Code for Module flumotion.common.xdg

  1  # -*- Mode: Python; test-case-name: flumotion.test.test_common_xdg -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  """Simple XDG Base Directory Specification implementation. 
 23   
 24  See http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html 
 25   
 26  Currently only configuration files handling is implemented.""" 
 27   
 28   
 29  import os 
 30  from flumotion.common.python import makedirs 
 31   
 32   
 33  APPLICATION = 'flumotion' 
 34   
 35   
36 -def config_home_path():
37 """ 38 Get the path of the config directory, taking into account the 39 XDG_CONFIG_HOME environment variable. 40 """ 41 42 # if $HOME is unset, there's not much we can do, default to the workdir 43 home = os.environ.get('HOME', '') 44 config_home = os.environ.get('XDG_CONFIG_HOME') 45 if not config_home: 46 config_home = os.path.join(home, '.config') 47 return config_home
48 49
50 -def config_read_path(name):
51 """ 52 Get the path of the config file with the given name, taking into account 53 the XDG_CONFIG_HOME and XDG_CONFIG_DIRS environment variables. 54 55 @param name: The name of the config file 56 @type name: str 57 58 @returns: full path to the file or None if it does not exist 59 """ 60 61 search_path = [config_home_path()] 62 63 config_dirs = os.environ.get('XDG_CONFIG_DIRS') 64 if config_dirs: 65 search_path.extend(config_dirs.split(':')) 66 67 for path in search_path: 68 candidate = os.path.join(path, APPLICATION, name) 69 if os.access(candidate, os.F_OK | os.R_OK): 70 return candidate 71 return None
72 73
74 -def config_write_path(name, mode='w'):
75 """ 76 Get file-like object for the config file with the given name, taking into 77 account the XDG_CONFIG_HOME environment variable. 78 Create intermidient directories and the file itself according to the XDG 79 Specification in case the file does not exist. 80 81 May raise EnvironmentError if the file or directories cannot be created. 82 83 @param name: The name of the config file 84 @type name: str 85 @param mode: The mode to use when opening the file, 'w' by default. 86 @type mode: str 87 88 @returns: a file-like object 89 """ 90 91 path = os.path.join(config_home_path(), APPLICATION, name) 92 dirname = os.path.dirname(path) 93 94 if not os.path.exists(dirname): 95 # XDG spec demands mode 0700 96 makedirs(dirname, 0700) 97 98 return file(path, mode)
99