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