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 re
- import os
- import traceback
- import fnmatch
- from calibre import isbytestring
- from calibre.constants import filesystem_encoding
- from calibre.ebooks import BOOK_EXTENSIONS
- EBOOK_EXTENSIONS = frozenset(BOOK_EXTENSIONS)
- NORMALS = frozenset([
- 'metadata.opf',
- 'cover.jpg'])
- CHECKS = [
- ('invalid_titles', _('Invalid titles'), True),
- ('extra_titles', _('Extra titles'), True),
- ('invalid_authors', _('Invalid authors'), True),
- ('extra_authors', _('Extra authors'), True),
- ('missing_formats', _('Missing book formats'), False),
- ('extra_formats', _('Extra book formats'), True),
- ('extra_files', _('Unknown files in books'), True),
- ('failed_folders', _('Folders raising exception'), False)]
-
- class CheckLibrary(object):
-
- def __init__(self, library_path, db):
- if isbytestring(library_path):
- library_path = library_path.decode(filesystem_encoding)
-
- self.src_library_path = os.path.abspath(library_path)
- self.db = db
- self.is_case_sensitive = db.is_case_sensitive
- self.all_authors = []([ x[1] for x in db.all_authors() ])
- self.all_ids = []([ id for id in db.all_ids() ])
- self.all_dbpaths = (frozenset,)((lambda .0: for id in .0:
- self.dbpath(id))(self.all_ids))
- self.all_lc_dbpaths = []([ f.lower() for f in self.all_dbpaths ])
- self.db_id_regexp = re.compile('^.* \\((\\d+)\\)$')
- self.dirs = []
- self.book_dirs = []
- self.potential_authors = { }
- self.invalid_authors = []
- self.extra_authors = []
- self.invalid_titles = []
- self.extra_titles = []
- self.unknown_book_files = []
- self.missing_formats = []
- self.extra_formats = []
- self.extra_files = []
-
-
- def dbpath(self, id):
- return self.db.path(id, index_is_id = True)
-
-
- def errors_occurred(self):
- if not self.failed_folders and self.mismatched_dirs and self.conflicting_custom_cols:
- pass
- return self.failed_restores
-
- errors_occurred = property(errors_occurred)
-
- def ignore_name(self, filename):
- for filespec in self.ignore_names:
- if fnmatch.fnmatch(filename, filespec):
- return True
-
- return False
-
-
- def scan_library(self, name_ignores, extension_ignores):
- self.ignore_names = frozenset(name_ignores)
- self.ignore_ext = []([ '.' + e for e in extension_ignores ])
- lib = self.src_library_path
- for auth_dir in os.listdir(lib):
- auth_path = os.path.join(lib, auth_dir)
- if not os.path.isdir(auth_path):
- self.invalid_authors.append((auth_dir, auth_dir))
- continue
-
- self.potential_authors[auth_dir] = { }
- found_titles = False
- for title_dir in os.listdir(auth_path):
- if self.ignore_name(title_dir):
- continue
-
- title_path = os.path.join(auth_path, title_dir)
- db_path = os.path.join(auth_dir, title_dir)
- m = self.db_id_regexp.search(title_dir)
- if m is None or not os.path.isdir(title_path):
- self.invalid_titles.append((auth_dir, db_path))
- continue
-
- id = m.group(1)
- if self.is_case_sensitive:
- if int(id) not in self.all_ids or db_path not in self.all_dbpaths:
- self.extra_titles.append((title_dir, db_path))
- continue
-
- elif int(id) not in self.all_ids or db_path.lower() not in self.all_lc_dbpaths:
- self.extra_titles.append((title_dir, db_path))
- continue
-
- self.book_dirs.append((db_path, title_dir, id))
- found_titles = True
-
- if not found_titles:
- self.extra_authors.append((auth_dir, auth_dir))
- continue
-
- for x in self.book_dirs:
-
- try:
- self.process_book(lib, x)
- continue
- traceback.print_exc()
- self.failed_folders.append((title_path, traceback.format_exc(), []))
- continue
-
-
-
-
- def is_ebook_file(self, filename):
- ext = os.path.splitext(filename)[1]
- if not ext:
- return False
- ext = ext[1:].lower()
- if ext in EBOOK_EXTENSIONS:
- return True
- return False
-
-
- def process_book(self, lib, book_info):
- (db_path, title_dir, book_id) = book_info
- filenames = [](_[1])
- book_id = int(book_id)
- formats = frozenset(filter(self.is_ebook_file, filenames))
- book_formats = []([ x[0] + '.' + x[1].lower() for x in self.db.format_files(book_id, index_is_id = True) ])
-
-
-