home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1493 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  2.6 KB  |  84 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. import __builtin__
  9. import sys
  10. import os
  11. from calibre import config_dir
  12.  
  13. class PathResolver(object):
  14.     
  15.     def __init__(self):
  16.         self.locations = [
  17.             sys.resources_location]
  18.         self.cache = { }
  19.         
  20.         def suitable(path):
  21.             
  22.             try:
  23.                 if os.path.exists(path) and os.path.isdir(path):
  24.                     pass
  25.                 return os.listdir(path)
  26.             except:
  27.                 pass
  28.  
  29.             return False
  30.  
  31.         self.default_path = sys.resources_location
  32.         dev_path = os.environ.get('CALIBRE_DEVELOP_FROM', None)
  33.         if dev_path is not None:
  34.             dev_path = os.path.join(os.path.abspath(os.path.dirname(dev_path)), 'resources')
  35.             if suitable(dev_path):
  36.                 self.locations.insert(0, dev_path)
  37.                 self.default_path = dev_path
  38.             
  39.         
  40.         user_path = os.path.join(config_dir, 'resources')
  41.         self.user_path = None
  42.         if suitable(user_path):
  43.             self.locations.insert(0, user_path)
  44.             self.user_path = user_path
  45.         
  46.  
  47.     
  48.     def __call__(self, path, allow_user_override = True):
  49.         path = path.replace(os.sep, '/')
  50.         ans = self.cache.get(path, None)
  51.         if ans is None:
  52.             for base in self.locations:
  53.                 if not allow_user_override and base == self.user_path:
  54.                     continue
  55.                 
  56.                 fpath = os.path.join(base, *path.split('/'))
  57.                 if os.path.exists(fpath):
  58.                     ans = fpath
  59.                     break
  60.                     continue
  61.             
  62.             if ans is None:
  63.                 ans = os.path.join(self.default_path, *path.split('/'))
  64.             
  65.             self.cache[path] = ans
  66.         
  67.         return ans
  68.  
  69.  
  70. _resolver = PathResolver()
  71.  
  72. def get_path(path, data = False, allow_user_override = True):
  73.     fpath = _resolver(path, allow_user_override = allow_user_override)
  74.     if data:
  75.         return open(fpath, 'rb').read()
  76.     return fpath
  77.  
  78.  
  79. def get_image_path(path, data = False, allow_user_override = True):
  80.     return get_path('images/' + path, data = data)
  81.  
  82. __builtin__.__dict__['P'] = get_path
  83. __builtin__.__dict__['I'] = get_image_path
  84.