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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. import os
  8. from hashlib import sha1
  9. from calibre.constants import filesystem_encoding
  10. from calibre.ebooks import BOOK_EXTENSIONS
  11.  
  12. def find_folders_under(root, db, add_root = True, follow_links = False, cancel_callback = (lambda : False)):
  13.     lp = db.library_path
  14.     if isinstance(lp, unicode):
  15.         
  16.         try:
  17.             lp = lp.encode(filesystem_encoding)
  18.         lp = None
  19.  
  20.     
  21.     if lp:
  22.         lp = os.path.abspath(lp)
  23.     
  24.     root = os.path.abspath(root)
  25.     ans = set([])
  26.     for dirpath, dirnames, __ in os.walk(root, topdown = True, followlinks = follow_links):
  27.         if cancel_callback():
  28.             break
  29.         
  30.         for x in list(dirnames):
  31.             path = os.path.join(dirpath, x)
  32.             if lp and path.startswith(lp):
  33.                 dirnames.remove(x)
  34.                 continue
  35.         
  36.         if lp and dirpath.startswith(lp):
  37.             continue
  38.         
  39.         ans.add(dirpath)
  40.     
  41.     if not add_root:
  42.         ans.remove(root)
  43.     
  44.     return ans
  45.  
  46.  
  47. class FormatCollection(object):
  48.     
  49.     def __init__(self, parent_folder, formats):
  50.         self.path_map = { }
  51.         for x in set(formats):
  52.             fmt = os.path.splitext(x)[1].lower()
  53.             if fmt:
  54.                 fmt = fmt[1:]
  55.                 self.path_map[fmt] = x
  56.                 continue
  57.         
  58.         self.parent_folder = None
  59.         self.hash_map = { }
  60.         for fmt, path in self.format_map.items():
  61.             self.hash_map[fmt] = self.hash_of_file(path)
  62.         
  63.  
  64.     
  65.     def hash_of_file(self, path):
  66.         
  67.         try:
  68.             f = _[1]
  69.             return sha1(f.read()).digest()
  70.         finally:
  71.             pass
  72.  
  73.  
  74.     
  75.     def hashes(self):
  76.         return frozenset(self.formats.values())
  77.  
  78.     hashes = property(hashes)
  79.     
  80.     def is_empty(self):
  81.         return len(self) == 0
  82.  
  83.     is_empty = property(is_empty)
  84.     
  85.     def __iter__(self):
  86.         for x in self.path_map:
  87.             yield x
  88.         
  89.  
  90.     
  91.     def __len__(self):
  92.         return len(self.path_map)
  93.  
  94.     
  95.     def remove(self, fmt):
  96.         self.hash_map.pop(fmt, None)
  97.         self.path_map.pop(fmt, None)
  98.  
  99.     
  100.     def matches(self, other):
  101.         if not self.hashes.intersection(other.hashes):
  102.             return False
  103.         for fmt in self:
  104.             if self.hash_map[fmt] != other.hash_map.get(fmt, False):
  105.                 return False
  106.         
  107.         return True
  108.  
  109.     
  110.     def merge(self, other):
  111.         for fmt in list(other):
  112.             self.path_map[fmt] = other.path_map[fmt]
  113.             self.hash_map[fmt] = other.hash_map[fmt]
  114.             other.remove(fmt)
  115.         
  116.  
  117.  
  118.  
  119. def books_in_folder(folder, one_per_folder, cancel_callback = (lambda : False)):
  120.     dirpath = os.path.abspath(folder)
  121.     if one_per_folder:
  122.         formats = set([])
  123.         for path in os.listdir(dirpath):
  124.             if cancel_callback():
  125.                 return []
  126.             path = os.path.abspath(os.path.join(dirpath, path))
  127.             if os.path.isdir(path) or not os.access(path, os.R_OK):
  128.                 continue
  129.             
  130.             ext = os.path.splitext(path)[1]
  131.             if not ext:
  132.                 continue
  133.             
  134.             ext = ext[1:].lower()
  135.             if ext not in BOOK_EXTENSIONS and ext != 'opf':
  136.                 continue
  137.             
  138.             formats.add(path)
  139.         
  140.         return [
  141.             FormatCollection(folder, formats)]
  142.     books = { }
  143.     for path in os.listdir(dirpath):
  144.         if cancel_callback():
  145.             return None
  146.         path = os.path.abspath(os.path.join(dirpath, path))
  147.         ext = os.path.splitext(path)[1]
  148.         if not ext:
  149.             continue
  150.         
  151.         ext = ext[1:].lower()
  152.         if ext not in BOOK_EXTENSIONS:
  153.             continue
  154.         
  155.         key = os.path.splitext(path)[0]
  156.         if not books.has_key(key):
  157.             books[key] = set([])
  158.         
  159.         books[key].add(path)
  160.     
  161.     return _[1]
  162.  
  163.  
  164. def hash_merge_format_collections(collections, cancel_callback = (lambda : False)):
  165.     ans = []
  166.     collections = list(collections)
  167.     l = len(collections)
  168.     for i in range(l):
  169.         if cancel_callback():
  170.             return collections
  171.         one = collections[i]
  172.         if one.is_empty:
  173.             continue
  174.         
  175.         for j in range(i + 1, l):
  176.             if cancel_callback():
  177.                 return collections
  178.             two = collections[j]
  179.             if two.is_empty:
  180.                 continue
  181.             
  182.             if one.matches(two):
  183.                 one.merge(two)
  184.                 continue
  185.         
  186.         ans.append(one)
  187.     
  188.     return ans
  189.  
  190.