home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __license__ = 'GPL 3'
- __copyright__ = '2009, John Schember <john@nachtimwald.com>'
- __docformat__ = 'restructuredtext en'
- import os
- import re
- import time
- import sys
- from calibre.ebooks.metadata import title_sort
- from calibre.ebooks.metadata.book.base import Metadata
- from calibre.devices.mime import mime_type_ext
- from calibre.devices.interface import BookList as _BookList
- from calibre.constants import preferred_encoding
- from calibre import isbytestring
- from calibre.utils.config import prefs, tweaks
-
- class Book(Metadata):
-
- def __init__(self, prefix, lpath, size = None, other = None):
- path_to_ext = path_to_ext
- import calibre.ebooks.metadata.meta
- Metadata.__init__(self, '')
- self._new_book = False
- self.device_collections = []
- self.path = os.path.join(prefix, lpath)
- if os.sep == '\\':
- self.path = self.path.replace('/', '\\')
- self.lpath = lpath.replace('\\', '/')
- else:
- self.lpath = lpath
- self.mime = mime_type_ext(path_to_ext(lpath))
- self.size = size
-
- try:
- self.datetime = time.gmtime(os.path.getctime(self.path))
- except:
- self.datetime = time.gmtime()
-
- if other:
- self.smart_update(other)
-
-
-
- def __eq__(self, other):
- return self.lpath == getattr(other, 'lpath', None)
-
-
- def db_id(self):
- doc = 'The database id in the application database that this file corresponds to'
-
- def fget(self):
- match = re.search('_(\\d+)$', self.lpath.rpartition('.')[0])
- if match:
- return int(match.group(1))
-
- return property(fget = fget, doc = doc)
-
- db_id = dynamic_property(db_id)
-
- def title_sorter(self):
- doc = 'String to sort the title. If absent, title is returned'
-
- def fget(self):
- return title_sort(self.title)
-
- return property(doc = doc, fget = fget)
-
- title_sorter = dynamic_property(title_sorter)
-
- def thumbnail(self):
- pass
-
- thumbnail = dynamic_property(thumbnail)
-
-
- class BookList(_BookList):
-
- def __init__(self, oncard, prefix, settings):
- _BookList.__init__(self, oncard, prefix, settings)
- self._bookmap = { }
-
-
- def supports_collections(self):
- return False
-
-
- def add_book(self, book, replace_metadata):
-
- try:
- b = self.index(book)
- except (ValueError, IndexError):
- b = None
-
- if b is None:
- self.append(book)
- return book
- if replace_metadata:
- self[b].smart_update(book, replace_metadata = True)
- return self[b]
-
-
- def remove_book(self, book):
- self.remove(book)
-
-
- def get_collections(self):
- return { }
-
-
-
- class CollectionsBookList(BookList):
-
- def supports_collections(self):
- return True
-
-
- def in_category_sort_rules(self, attr):
- sorts = tweaks['sony_collection_sorting_rules']
- for attrs, sortattr in sorts:
- if attr in attrs or '*' in attrs:
- return sortattr
-
-
-
- def compute_category_name(self, attr, category, field_meta):
- renames = tweaks['sony_collection_renaming_rules']
- attr_name = renames.get(attr, None)
- if attr_name is None:
- if field_meta['is_custom']:
- attr_name = '(%s)' % field_meta['name']
- else:
- attr_name = ''
- elif attr_name != '':
- attr_name = '(%s)' % attr_name
-
- cat_name = '%s %s' % (category, attr_name)
- return cat_name.strip()
-
-
- def get_collections(self, collection_attributes):
- debug_print = debug_print
- import calibre.devices.usbms.driver
- debug_print('Starting get_collections:', prefs['manage_device_metadata'])
- debug_print('Renaming rules:', tweaks['sony_collection_renaming_rules'])
- debug_print('Sorting rules:', tweaks['sony_collection_sorting_rules'])
- use_renaming_rules = prefs['manage_device_metadata'] == 'on_connect'
- collections = { }
- for book in self:
- lpath = getattr(book, 'lpath', None)
- if lpath is None:
- continue
-
- attrs = [
- 'device_collections']
- if getattr(book, '_new_book', False):
- if prefs['manage_device_metadata'] == 'manual':
- attrs += collection_attributes
- else:
- attrs = collection_attributes
- elif prefs['manage_device_metadata'] == 'on_connect':
- attrs = collection_attributes
-
- for attr in attrs:
- attr = attr.strip()
- if attr == 'device_collections':
- doing_dc = True
- val = book.device_collections
- else:
- doing_dc = False
- (ign, val, orig_val, fm) = book.format_field_extended(attr)
- if not val:
- continue
-
- if isbytestring(val):
- val = val.decode(preferred_encoding, 'replace')
-
- if isinstance(val, (list, tuple)):
- val = list(val)
- elif fm['datatype'] == 'series':
- val = [
- orig_val]
- elif fm['datatype'] == 'text' and fm['is_multiple']:
- val = orig_val
- else:
- val = [
- val]
- sort_attr = self.in_category_sort_rules(attr)
- for category in val:
- is_series = False
- if doing_dc:
- if category == book.series:
- is_series = True
-
- elif fm['is_custom']:
- if fm['datatype'] == 'text' and len(category) > 1 and category[0] == '[' and category[-1] == ']':
- continue
-
- if fm['datatype'] == 'series':
- is_series = True
-
- elif attr == 'tags' and len(category) > 1 and category[0] == '[' and category[-1] == ']':
- continue
-
- if (attr == 'series' or 'series' in collection_attributes) and book.get('series', None) == category:
- is_series = True
-
- if use_renaming_rules:
- cat_name = self.compute_category_name(attr, category, fm)
- else:
- cat_name = category
- if cat_name not in collections:
- collections[cat_name] = { }
-
- if use_renaming_rules and sort_attr:
- sort_val = book.get(sort_attr, None)
- collections[cat_name][lpath] = (book, sort_val, book.get('title_sort', 'zzzz'))
- continue
- if is_series:
- if doing_dc:
- collections[cat_name][lpath] = (book, book.get('series_index', sys.maxint), '')
- else:
- collections[cat_name][lpath] = (book, book.get(attr + '_index', sys.maxint), '')
- doing_dc
- if lpath not in collections[cat_name]:
- collections[cat_name][lpath] = (book, book.get('title_sort', 'zzzz'), '')
- continue
-
-
-
- result = { }
-
- def none_cmp(xx, yy):
- x = xx[1]
- y = yy[1]
- if x is None and y is None:
- return cmp(xx[2], yy[2])
- if x is None:
- return 1
- if y is None:
- return -1
- c = cmp(x, y)
- if c != 0:
- return c
- return cmp(xx[2], yy[2])
-
- for category, lpaths in collections.items():
- books = lpaths.values()
- books.sort(cmp = none_cmp)
- result[category] = [ x[0] for x in books ]
-
- return result
-
-
- def rebuild_collections(self, booklist, oncard):
- pass
-
-
-