home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1394 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  2.8 KB  |  93 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. from math import ceil
  6. from calibre.ebooks.unidecode.unidecoder import Unidecoder
  7. from calibre import sanitize_file_name
  8. from calibre.constants import preferred_encoding, iswindows
  9. udc = Unidecoder()
  10.  
  11. def ascii_text(orig):
  12.     
  13.     try:
  14.         ascii = udc.decode(orig)
  15.     except:
  16.         if isinstance(orig, unicode):
  17.             ascii = orig.encode('ascii', 'replace')
  18.         
  19.         ascii = orig.decode(preferred_encoding, 'replace').encode('ascii', 'replace')
  20.  
  21.     return ascii
  22.  
  23.  
  24. def ascii_filename(orig, substitute = '_'):
  25.     ans = []
  26.     orig = ascii_text(orig).replace('?', '_')
  27.     for x in orig:
  28.         if ord(x) < 32:
  29.             x = substitute
  30.         
  31.         ans.append(x)
  32.     
  33.     return sanitize_file_name(''.join(ans), substitute = substitute)
  34.  
  35.  
  36. def supports_long_names(path):
  37.     t = 'a' * 300 + '.txt'
  38.     
  39.     try:
  40.         p = os.path.join(path, t)
  41.         open(p, 'wb').close()
  42.         os.remove(p)
  43.     except:
  44.         return False
  45.  
  46.     return True
  47.  
  48.  
  49. def shorten_components_to(length, components):
  50.     filepath = os.sep.join(components)
  51.     extra = len(filepath) - length
  52.     if extra < 1:
  53.         return components
  54.     delta = int(ceil(extra / float(len(components))))
  55.     ans = []
  56.     for x in components:
  57.         if delta > len(x):
  58.             r = extra < 1 if x is components[-1] else ''
  59.         elif x is components[-1]:
  60.             (b, e) = os.path.splitext(x)
  61.             r = b[:-delta] + e
  62.             if r.startswith('.'):
  63.                 r = x[0] + r
  64.             
  65.         else:
  66.             r = x[:-delta]
  67.         r = r.strip()
  68.         if not r:
  69.             r = None if x.strip() else 'x'
  70.         
  71.         ans.append(r)
  72.     
  73.     if len(os.sep.join(ans)) > length:
  74.         return shorten_components_to(length, ans)
  75.     return ans
  76.  
  77.  
  78. def find_executable_in_path(name, path = None):
  79.     if path is None:
  80.         path = os.environ.get('PATH', '')
  81.     
  82.     sep = None if iswindows else ':'
  83.     if iswindows and not name.endswith('.exe'):
  84.         name += '.exe'
  85.     
  86.     path = path.split(sep)
  87.     for x in path:
  88.         q = os.path.abspath(os.path.join(x, name))
  89.         if os.access(q, os.X_OK):
  90.             return q
  91.     
  92.  
  93.