basedir.py :  » Installer » Zero-Install » zeroinstall-injector-0.47 » zeroinstall » support » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Installer » Zero Install 
Zero Install » zeroinstall injector 0.47 » zeroinstall » support » basedir.py
"""
Support code for the freedesktop.org basedir spec.

This module provides functions for locating configuration files.

@see: U{http://freedesktop.org/wiki/Standards/basedir-spec}

@var home: The value of $HOME (or '/' if not set). If we're running as root and
$HOME isn't owned by root, then this will be root's home from /etc/passwd
instead.
"""

# Copyright (C) 2009, Thomas Leonard
# See the README file for details, or visit http://0install.net.

from zeroinstall import _
import os

home = os.environ.get('HOME', '/')

try:
  _euid = os.geteuid()
except AttributeError:
  pass  # Windows?
else:
  if _euid == 0:
    # We're running as root. Ensure that $HOME really is root's home,
    # not the user's home, or we're likely to fill it will unreadable
    # root-owned files.
    home_owner = os.stat(home).st_uid
    if home_owner != 0:
      import pwd
      from logging import info
      old_home = home
      home = pwd.getpwuid(0).pw_dir or '/'
      info(_("$HOME (%(home)s) is owned by user %(user)d, but we are root (0). Using %(root_home)s instead."), {'old_home': old_home, 'user': home_owner, 'root_home': home})
      del old_home
      del home_owner

xdg_data_home = os.environ.get('XDG_DATA_HOME',
      os.path.join(home, '.local', 'share'))

xdg_data_dirs = [xdg_data_home] + \
  os.environ.get('XDG_DATA_DIRS', '/usr/local/share:/usr/share').split(':')

xdg_cache_home = os.environ.get('XDG_CACHE_HOME',
      os.path.join(home, '.cache'))

xdg_cache_dirs = [xdg_cache_home] + \
  os.environ.get('XDG_CACHE_DIRS', '/var/cache').split(':')

xdg_config_home = os.environ.get('XDG_CONFIG_HOME',
      os.path.join(home, '.config'))

xdg_config_dirs = [xdg_config_home] + \
  os.environ.get('XDG_CONFIG_DIRS', '/etc/xdg').split(':')

xdg_data_dirs = filter(lambda x: x, xdg_data_dirs)
xdg_cache_dirs = filter(lambda x: x, xdg_cache_dirs)
xdg_config_dirs = filter(lambda x: x, xdg_config_dirs)

def save_config_path(*resource):
  """Ensure $XDG_CONFIG_HOME/<resource>/ exists, and return its path.
  'resource' should normally be the name of your application. Use this
  when SAVING configuration settings. Use the xdg_config_dirs variable
  for loading."""
  resource = os.path.join(*resource)
  assert not resource.startswith('/')
  path = os.path.join(xdg_config_home, resource)
  if not os.path.isdir(path):
    os.makedirs(path, 0700)
  return path

def load_config_paths(*resource):
  """Returns an iterator which gives each directory named 'resource' in the
  configuration search path. Information provided by earlier directories should
  take precedence over later ones (ie, the user's config dir comes first)."""
  resource = os.path.join(*resource)
  for config_dir in xdg_config_dirs:
    path = os.path.join(config_dir, resource)
    if os.path.exists(path): yield path

def load_first_config(*resource):
  """Returns the first result from load_config_paths, or None if there is nothing
  to load."""
  for x in load_config_paths(*resource):
    return x
  return None

def save_cache_path(*resource):
  """Ensure $XDG_CACHE_HOME/<resource>/ exists, and return its path.
  'resource' should normally be the name of your application."""
  resource = os.path.join(*resource)
  assert not resource.startswith('/')
  path = os.path.join(xdg_cache_home, resource)
  if not os.path.isdir(path):
    os.makedirs(path, 0700)
  return path

def load_cache_paths(*resource):
  """Returns an iterator which gives each directory named 'resource' in the
  cache search path. Information provided by earlier directories should
  take precedence over later ones (ie, the user's cache dir comes first)."""
  resource = os.path.join(*resource)
  for cache_dir in xdg_cache_dirs:
    path = os.path.join(cache_dir, resource)
    if os.path.exists(path): yield path

def load_first_cache(*resource):
  """Returns the first result from load_cache_paths, or None if there is nothing
  to load."""
  for x in load_cache_paths(*resource):
    return x
  return None

def load_data_paths(*resource):
  """Returns an iterator which gives each directory named 'resource' in the
  shared data search path. Information provided by earlier directories should
  take precedence over later ones.
  @since: 0.28"""
  resource = os.path.join(*resource)
  for data_dir in xdg_data_dirs:
    path = os.path.join(data_dir, resource)
    if os.path.exists(path): yield path

def load_first_data(*resource):
  """Returns the first result from load_data_paths, or None if there is nothing
  to load.
  @since: 0.28"""
  for x in load_data_paths(*resource):
    return x
  return None
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.