home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1421 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  2.5 KB  |  77 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.         dev_path = os.environ.get('CALIBRE_DEVELOP_FROM', None)
  32.         if dev_path is not None:
  33.             dev_path = os.path.join(os.path.abspath(os.path.dirname(dev_path)), 'resources')
  34.             if suitable(dev_path):
  35.                 self.locations.insert(0, dev_path)
  36.             
  37.         
  38.         user_path = os.path.join(config_dir, 'resources')
  39.         if suitable(user_path):
  40.             self.locations.insert(0, user_path)
  41.         
  42.  
  43.     
  44.     def __call__(self, path):
  45.         path = path.replace(os.sep, '/')
  46.         ans = self.cache.get(path, None)
  47.         if ans is None:
  48.             for base in self.locations:
  49.                 fpath = os.path.join(base, *path.split('/'))
  50.                 if os.path.exists(fpath):
  51.                     ans = fpath
  52.                     break
  53.                     continue
  54.             
  55.             if ans is None:
  56.                 ans = os.path.join(self.locations[0], *path.split('/'))
  57.             
  58.             self.cache[path] = ans
  59.         
  60.         return ans
  61.  
  62.  
  63. _resolver = PathResolver()
  64.  
  65. def get_path(path, data = False):
  66.     fpath = _resolver(path)
  67.     if data:
  68.         return open(fpath, 'rb').read()
  69.     return fpath
  70.  
  71.  
  72. def get_image_path(path, data = False):
  73.     return get_path('images/' + path, data = data)
  74.  
  75. __builtin__.__dict__['P'] = get_path
  76. __builtin__.__dict__['I'] = get_image_path
  77.