home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __license__ = 'GPL v3'
- __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
- __docformat__ = 'restructuredtext en'
- import os
- from hashlib import sha1
- from calibre.constants import filesystem_encoding
- from calibre.ebooks import BOOK_EXTENSIONS
-
- def find_folders_under(root, db, add_root = True, follow_links = False, cancel_callback = (lambda : False)):
- lp = db.library_path
- if isinstance(lp, unicode):
-
- try:
- lp = lp.encode(filesystem_encoding)
- lp = None
-
-
- if lp:
- lp = os.path.abspath(lp)
-
- root = os.path.abspath(root)
- ans = set([])
- for dirpath, dirnames, __ in os.walk(root, topdown = True, followlinks = follow_links):
- if cancel_callback():
- break
-
- for x in list(dirnames):
- path = os.path.join(dirpath, x)
- if lp and path.startswith(lp):
- dirnames.remove(x)
- continue
-
- if lp and dirpath.startswith(lp):
- continue
-
- ans.add(dirpath)
-
- if not add_root:
- ans.remove(root)
-
- return ans
-
-
- class FormatCollection(object):
-
- def __init__(self, parent_folder, formats):
- self.path_map = { }
- for x in set(formats):
- fmt = os.path.splitext(x)[1].lower()
- if fmt:
- fmt = fmt[1:]
- self.path_map[fmt] = x
- continue
-
- self.parent_folder = None
- self.hash_map = { }
- for fmt, path in self.format_map.items():
- self.hash_map[fmt] = self.hash_of_file(path)
-
-
-
- def hash_of_file(self, path):
-
- try:
- f = _[1]
- return sha1(f.read()).digest()
- finally:
- pass
-
-
-
- def hashes(self):
- return frozenset(self.formats.values())
-
- hashes = property(hashes)
-
- def is_empty(self):
- return len(self) == 0
-
- is_empty = property(is_empty)
-
- def __iter__(self):
- for x in self.path_map:
- yield x
-
-
-
- def __len__(self):
- return len(self.path_map)
-
-
- def remove(self, fmt):
- self.hash_map.pop(fmt, None)
- self.path_map.pop(fmt, None)
-
-
- def matches(self, other):
- if not self.hashes.intersection(other.hashes):
- return False
- for fmt in self:
- if self.hash_map[fmt] != other.hash_map.get(fmt, False):
- return False
-
- return True
-
-
- def merge(self, other):
- for fmt in list(other):
- self.path_map[fmt] = other.path_map[fmt]
- self.hash_map[fmt] = other.hash_map[fmt]
- other.remove(fmt)
-
-
-
-
- def books_in_folder(folder, one_per_folder, cancel_callback = (lambda : False)):
- dirpath = os.path.abspath(folder)
- if one_per_folder:
- formats = set([])
- for path in os.listdir(dirpath):
- if cancel_callback():
- return []
- path = os.path.abspath(os.path.join(dirpath, path))
- if os.path.isdir(path) or not os.access(path, os.R_OK):
- continue
-
- ext = os.path.splitext(path)[1]
- if not ext:
- continue
-
- ext = ext[1:].lower()
- if ext not in BOOK_EXTENSIONS and ext != 'opf':
- continue
-
- formats.add(path)
-
- return [
- FormatCollection(folder, formats)]
- books = { }
- for path in os.listdir(dirpath):
- if cancel_callback():
- return None
- path = os.path.abspath(os.path.join(dirpath, path))
- ext = os.path.splitext(path)[1]
- if not ext:
- continue
-
- ext = ext[1:].lower()
- if ext not in BOOK_EXTENSIONS:
- continue
-
- key = os.path.splitext(path)[0]
- if not books.has_key(key):
- books[key] = set([])
-
- books[key].add(path)
-
- return _[1]
-
-
- def hash_merge_format_collections(collections, cancel_callback = (lambda : False)):
- ans = []
- collections = list(collections)
- l = len(collections)
- for i in range(l):
- if cancel_callback():
- return collections
- one = collections[i]
- if one.is_empty:
- continue
-
- for j in range(i + 1, l):
- if cancel_callback():
- return collections
- two = collections[j]
- if two.is_empty:
- continue
-
- if one.matches(two):
- one.merge(two)
- continue
-
- ans.append(one)
-
- return ans
-
-