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
    29 -def get_abs_data_filename(path_parts, basedirs=None):
    30      """Get the absolute path to the given file- or directory name in the current
    31          running application's data directory.
    32
    33          @type  path_parts: list
    34          @param path_parts: The path parts that can be joined by os.path.join().
    35          """
    36      if basedirs is None:
    37          basedirs = []
    38
    39      if isinstance(path_parts, str):
    40          path_parts = [path_parts]
    41
    42      BASE_DIRS = basedirs + [
    43          os.path.dirname(unicode(__file__, sys.getfilesystemencoding())),
    44          os.path.dirname(unicode(sys.executable, sys.getfilesystemencoding())),
    45      ]
    46
    47      # Freedesktop standard
    48      if 'XDG_DATA_HOME' in os.environ:
    49          BASE_DIRS += [os.environ['XDG_DATA_HOME']]
    50      if 'XDG_DATA_DIRS' in os.environ:
    51          BASE_DIRS += os.environ['XDG_DATA_DIRS'].split(os.path.pathsep)
    52
    53      # Mac OSX app bundles
    54      if 'RESOURCEPATH' in os.environ:
    55          BASE_DIRS += os.environ['RESOURCEPATH'].split(os.path.pathsep)
    56
    57      DATA_DIRS = [
    58          ["..", "share"],
    59          ["share"],
    60      ]
    61
    62      for basepath, data_dir in ((x, y) for x in BASE_DIRS for y in DATA_DIRS):
    63          dir_and_filename = data_dir + path_parts
    64          datafile = os.path.join(basepath or os.path.dirname(__file__), *dir_and_filename)
    65          if os.path.exists(datafile):
    66              return datafile
    67      raise Exception('Could not find "%s"' % (os.path.join(*path_parts)))
    68