Package translate :: Package misc :: Module file_discovery

[hide private]
[frames] | no frames]

# Source Code for Module translate.misc.file_discovery

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2008 Zuza Software Foundation
     5  #
     6  # This file is part of translate.
     7  #
     8  # translate is free software; you can redistribute it and/or modify
     9  # it under the terms of the GNU General Public License as published by
    10  # the Free Software Foundation; either version 2 of the License, or
    11  # (at your option) any later version.
    12  #
    13  # translate is distributed in the hope that it will be useful,
    14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  # GNU General Public License for more details.
    17  #
    18  # You should have received a copy of the GNU General Public License
    19  # along with translate; if not, write to the Free Software
    20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    21  #
    22
    23  __all__ = ['get_abs_data_filename']
    24
    25  import sys
    26  import os
    27
    28 -def get_abs_data_filename(path_parts, basedirs=None):
    29      """Get the absolute path to the given file- or directory name in the current
    30          running application's data directory.
    31
    32          @type  path_parts: list
    33          @param path_parts: The path parts that can be joined by os.path.join().
    34          """
    35      if basedirs is None:
    36          basedirs = []
    37
    38      if isinstance(path_parts, str):
    39          path_parts = [path_parts]
    40
    41      BASE_DIRS = basedirs + [
    42          os.path.dirname(unicode(__file__, sys.getfilesystemencoding())),
    43          os.path.dirname(unicode(sys.executable, sys.getfilesystemencoding()))
    44      ]
    45
    46      # Freedesktop standard
    47      if 'XDG_DATA_HOME' in os.environ:
    48          BASE_DIRS += [os.environ['XDG_DATA_HOME']]
    49      if 'XDG_DATA_DIRS' in os.environ:
    50          BASE_DIRS += os.environ['XDG_DATA_DIRS'].split(os.path.pathsep)
    51
    52      # Mac OSX app bundles
    53      if 'RESOURCEPATH' in os.environ:
    54          BASE_DIRS += os.environ['RESOURCEPATH'].split(os.path.pathsep)
    55
    56      DATA_DIRS = [
    57          ["..", "share"],
    58          ["share"]
    59      ]
    60
    61      for basepath, data_dir in ((x, y) for x in BASE_DIRS for y in DATA_DIRS):
    62          dir_and_filename = data_dir + path_parts
    63          datafile = os.path.join(basepath or os.path.dirname(__file__), *dir_and_filename)
    64          if os.path.exists(datafile):
    65              return datafile
    66      raise Exception('Could not find "%s"' % (os.path.join(*path_parts)))
    67