Package Camelot :: Package camelot :: Package core :: Module resources |
[frames] | no frames]

# Source Code for Module Camelot.camelot.core.resources

     1  #  ============================================================================
     2  #
     3  #  Copyright (C) 2007-2008 Conceptive Engineering bvba. All rights reserved.
     4  #  www.conceptive.be / project-camelot@conceptive.be
     5  #
     6  #  This file is part of the Camelot Library.
     7  #
     8  #  This file may be used under the terms of the GNU General Public
     9  #  License version 2.0 as published by the Free Software Foundation
    10  #  and appearing in the file LICENSE.GPL included in the packaging of
    11  #  this file.  Please review the following information to ensure GNU
    12  #  General Public Licensing requirements will be met:
    13  #  http://www.trolltech.com/products/qt/opensource.html
    14  #
    15  #  If you are unsure which license is appropriate for your use, please
    16  #  review the following information:
    17  #  http://www.trolltech.com/products/qt/licensing.html or contact
    18  #  project-camelot@conceptive.be.
    19  #
    20  #  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    21  #  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    22  #
    23  #  For use of this library in commercial applications, please contact
    24  #  project-camelot@conceptive.be
    25  #
    26  #  ============================================================================
    27
    28  """wrapper around pkg_resources, with fallback to using directories specified
    29  in the settings file if pkg_resources cannot be used.
    30
    31  to allow fallback to the settings file, specify the settings_attribute method,
    32  this is the attribute in the settings file that contains the folder with the
    33  resources as opposed to the folder containing the module itself.
    34
    35  this mechanism will probably be rewritten to support the loading of resources
    36  from zip files instead of falling back to settings.
    37
    38  when running from a bootstrapper, we'll try to use pgk_resources, even when
    39  runnin from within a zip file.
    40  """
    41
    42  import pkg_resources
    43  import sys
    44  import os
    45  import logging
    46
    47  logger = logging.getLogger('camelot.core.resources')
    48
    49 -def resource_filename(module_name, filename, settings_attribute=None):
    50      """Return the absolute path to a file in a directory
    51      if the directory for the module cannot be accessed through pkg_resources,
    52      fall back to the settings attribute
    53      """
    54      import settings
    55      if sys.path[0].endswith('.zip') and not hasattr(settings, 'BOOTSTRAPPER'):
    56          # we're running from a zip file, pkg_resources won't work
    57          if not settings_attribute:
    58              logger.error('resources of module %s cannot be loaded because no settings_attribute is specified and the module is inside a zip file')
    59              return ''
    60          absolute_path = os.path.join(getattr(settings, settings_attribute), filename)
    61          if not os.path.exists(absolute_path):
    62              logger.error('resources of module %s cannot be loaded because %s does not exist'%(module_name, absolute_path))
    63              return ''
    64          return os.path.join(absolute_path)
    65      else:
    66          return pkg_resources.resource_filename(module_name, filename)
    67
    68 -def resource_string(module_name, filename, settings_attribute):
    69      import settings
    70      if sys.path[0].endswith('.zip') and not hasattr(settings, 'BOOTSTRAPPER'):
    71          return open(resource_filename(module_name, filename, settings_attribute), 'rb').read()
    72      else:
    73          return pkg_resources.resource_string(module_name, filename)
    74