home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1406 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  6.5 KB  |  145 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 re
  8. import os
  9. import traceback
  10. import fnmatch
  11. from calibre import isbytestring
  12. from calibre.constants import filesystem_encoding
  13. from calibre.ebooks import BOOK_EXTENSIONS
  14. EBOOK_EXTENSIONS = frozenset(BOOK_EXTENSIONS)
  15. NORMALS = frozenset([
  16.     'metadata.opf',
  17.     'cover.jpg'])
  18. CHECKS = [
  19.     ('invalid_titles', _('Invalid titles'), True),
  20.     ('extra_titles', _('Extra titles'), True),
  21.     ('invalid_authors', _('Invalid authors'), True),
  22.     ('extra_authors', _('Extra authors'), True),
  23.     ('missing_formats', _('Missing book formats'), False),
  24.     ('extra_formats', _('Extra book formats'), True),
  25.     ('extra_files', _('Unknown files in books'), True),
  26.     ('failed_folders', _('Folders raising exception'), False)]
  27.  
  28. class CheckLibrary(object):
  29.     
  30.     def __init__(self, library_path, db):
  31.         if isbytestring(library_path):
  32.             library_path = library_path.decode(filesystem_encoding)
  33.         
  34.         self.src_library_path = os.path.abspath(library_path)
  35.         self.db = db
  36.         self.is_case_sensitive = db.is_case_sensitive
  37.         self.all_authors = []([ x[1] for x in db.all_authors() ])
  38.         self.all_ids = []([ id for id in db.all_ids() ])
  39.         self.all_dbpaths = (frozenset,)((lambda .0: for id in .0:
  40. self.dbpath(id))(self.all_ids))
  41.         self.all_lc_dbpaths = []([ f.lower() for f in self.all_dbpaths ])
  42.         self.db_id_regexp = re.compile('^.* \\((\\d+)\\)$')
  43.         self.dirs = []
  44.         self.book_dirs = []
  45.         self.potential_authors = { }
  46.         self.invalid_authors = []
  47.         self.extra_authors = []
  48.         self.invalid_titles = []
  49.         self.extra_titles = []
  50.         self.unknown_book_files = []
  51.         self.missing_formats = []
  52.         self.extra_formats = []
  53.         self.extra_files = []
  54.  
  55.     
  56.     def dbpath(self, id):
  57.         return self.db.path(id, index_is_id = True)
  58.  
  59.     
  60.     def errors_occurred(self):
  61.         if not self.failed_folders and self.mismatched_dirs and self.conflicting_custom_cols:
  62.             pass
  63.         return self.failed_restores
  64.  
  65.     errors_occurred = property(errors_occurred)
  66.     
  67.     def ignore_name(self, filename):
  68.         for filespec in self.ignore_names:
  69.             if fnmatch.fnmatch(filename, filespec):
  70.                 return True
  71.         
  72.         return False
  73.  
  74.     
  75.     def scan_library(self, name_ignores, extension_ignores):
  76.         self.ignore_names = frozenset(name_ignores)
  77.         self.ignore_ext = []([ '.' + e for e in extension_ignores ])
  78.         lib = self.src_library_path
  79.         for auth_dir in os.listdir(lib):
  80.             auth_path = os.path.join(lib, auth_dir)
  81.             if not os.path.isdir(auth_path):
  82.                 self.invalid_authors.append((auth_dir, auth_dir))
  83.                 continue
  84.             
  85.             self.potential_authors[auth_dir] = { }
  86.             found_titles = False
  87.             for title_dir in os.listdir(auth_path):
  88.                 if self.ignore_name(title_dir):
  89.                     continue
  90.                 
  91.                 title_path = os.path.join(auth_path, title_dir)
  92.                 db_path = os.path.join(auth_dir, title_dir)
  93.                 m = self.db_id_regexp.search(title_dir)
  94.                 if m is None or not os.path.isdir(title_path):
  95.                     self.invalid_titles.append((auth_dir, db_path))
  96.                     continue
  97.                 
  98.                 id = m.group(1)
  99.                 if self.is_case_sensitive:
  100.                     if int(id) not in self.all_ids or db_path not in self.all_dbpaths:
  101.                         self.extra_titles.append((title_dir, db_path))
  102.                         continue
  103.                     
  104.                 elif int(id) not in self.all_ids or db_path.lower() not in self.all_lc_dbpaths:
  105.                     self.extra_titles.append((title_dir, db_path))
  106.                     continue
  107.                 
  108.                 self.book_dirs.append((db_path, title_dir, id))
  109.                 found_titles = True
  110.             
  111.             if not found_titles:
  112.                 self.extra_authors.append((auth_dir, auth_dir))
  113.                 continue
  114.         
  115.         for x in self.book_dirs:
  116.             
  117.             try:
  118.                 self.process_book(lib, x)
  119.             continue
  120.             traceback.print_exc()
  121.             self.failed_folders.append((title_path, traceback.format_exc(), []))
  122.             continue
  123.  
  124.         
  125.  
  126.     
  127.     def is_ebook_file(self, filename):
  128.         ext = os.path.splitext(filename)[1]
  129.         if not ext:
  130.             return False
  131.         ext = ext[1:].lower()
  132.         if ext in EBOOK_EXTENSIONS:
  133.             return True
  134.         return False
  135.  
  136.     
  137.     def process_book(self, lib, book_info):
  138.         (db_path, title_dir, book_id) = book_info
  139.         filenames = [](_[1])
  140.         book_id = int(book_id)
  141.         formats = frozenset(filter(self.is_ebook_file, filenames))
  142.         book_formats = []([ x[0] + '.' + x[1].lower() for x in self.db.format_files(book_id, index_is_id = True) ])
  143.  
  144.  
  145.