home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_806 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  8.3 KB  |  260 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL 3'
  5. __copyright__ = '2009, John Schember <john@nachtimwald.com>'
  6. __docformat__ = 'restructuredtext en'
  7. import os
  8. import re
  9. import time
  10. import sys
  11. from calibre.ebooks.metadata import title_sort
  12. from calibre.ebooks.metadata.book.base import Metadata
  13. from calibre.devices.mime import mime_type_ext
  14. from calibre.devices.interface import BookList as _BookList
  15. from calibre.constants import preferred_encoding
  16. from calibre import isbytestring
  17. from calibre.utils.config import prefs, tweaks
  18.  
  19. class Book(Metadata):
  20.     
  21.     def __init__(self, prefix, lpath, size = None, other = None):
  22.         path_to_ext = path_to_ext
  23.         import calibre.ebooks.metadata.meta
  24.         Metadata.__init__(self, '')
  25.         self._new_book = False
  26.         self.device_collections = []
  27.         self.path = os.path.join(prefix, lpath)
  28.         if os.sep == '\\':
  29.             self.path = self.path.replace('/', '\\')
  30.             self.lpath = lpath.replace('\\', '/')
  31.         else:
  32.             self.lpath = lpath
  33.         self.mime = mime_type_ext(path_to_ext(lpath))
  34.         self.size = size
  35.         
  36.         try:
  37.             self.datetime = time.gmtime(os.path.getctime(self.path))
  38.         except:
  39.             self.datetime = time.gmtime()
  40.  
  41.         if other:
  42.             self.smart_update(other)
  43.         
  44.  
  45.     
  46.     def __eq__(self, other):
  47.         return self.lpath == getattr(other, 'lpath', None)
  48.  
  49.     
  50.     def db_id(self):
  51.         doc = 'The database id in the application database that this file corresponds to'
  52.         
  53.         def fget(self):
  54.             match = re.search('_(\\d+)$', self.lpath.rpartition('.')[0])
  55.             if match:
  56.                 return int(match.group(1))
  57.  
  58.         return property(fget = fget, doc = doc)
  59.  
  60.     db_id = dynamic_property(db_id)
  61.     
  62.     def title_sorter(self):
  63.         doc = 'String to sort the title. If absent, title is returned'
  64.         
  65.         def fget(self):
  66.             return title_sort(self.title)
  67.  
  68.         return property(doc = doc, fget = fget)
  69.  
  70.     title_sorter = dynamic_property(title_sorter)
  71.     
  72.     def thumbnail(self):
  73.         pass
  74.  
  75.     thumbnail = dynamic_property(thumbnail)
  76.  
  77.  
  78. class BookList(_BookList):
  79.     
  80.     def __init__(self, oncard, prefix, settings):
  81.         _BookList.__init__(self, oncard, prefix, settings)
  82.         self._bookmap = { }
  83.  
  84.     
  85.     def supports_collections(self):
  86.         return False
  87.  
  88.     
  89.     def add_book(self, book, replace_metadata):
  90.         
  91.         try:
  92.             b = self.index(book)
  93.         except (ValueError, IndexError):
  94.             b = None
  95.  
  96.         if b is None:
  97.             self.append(book)
  98.             return book
  99.         if replace_metadata:
  100.             self[b].smart_update(book, replace_metadata = True)
  101.             return self[b]
  102.  
  103.     
  104.     def remove_book(self, book):
  105.         self.remove(book)
  106.  
  107.     
  108.     def get_collections(self):
  109.         return { }
  110.  
  111.  
  112.  
  113. class CollectionsBookList(BookList):
  114.     
  115.     def supports_collections(self):
  116.         return True
  117.  
  118.     
  119.     def in_category_sort_rules(self, attr):
  120.         sorts = tweaks['sony_collection_sorting_rules']
  121.         for attrs, sortattr in sorts:
  122.             if attr in attrs or '*' in attrs:
  123.                 return sortattr
  124.         
  125.  
  126.     
  127.     def compute_category_name(self, attr, category, field_meta):
  128.         renames = tweaks['sony_collection_renaming_rules']
  129.         attr_name = renames.get(attr, None)
  130.         if attr_name is None:
  131.             if field_meta['is_custom']:
  132.                 attr_name = '(%s)' % field_meta['name']
  133.             else:
  134.                 attr_name = ''
  135.         elif attr_name != '':
  136.             attr_name = '(%s)' % attr_name
  137.         
  138.         cat_name = '%s %s' % (category, attr_name)
  139.         return cat_name.strip()
  140.  
  141.     
  142.     def get_collections(self, collection_attributes):
  143.         debug_print = debug_print
  144.         import calibre.devices.usbms.driver
  145.         debug_print('Starting get_collections:', prefs['manage_device_metadata'])
  146.         debug_print('Renaming rules:', tweaks['sony_collection_renaming_rules'])
  147.         debug_print('Sorting rules:', tweaks['sony_collection_sorting_rules'])
  148.         use_renaming_rules = prefs['manage_device_metadata'] == 'on_connect'
  149.         collections = { }
  150.         for book in self:
  151.             lpath = getattr(book, 'lpath', None)
  152.             if lpath is None:
  153.                 continue
  154.             
  155.             attrs = [
  156.                 'device_collections']
  157.             if getattr(book, '_new_book', False):
  158.                 if prefs['manage_device_metadata'] == 'manual':
  159.                     attrs += collection_attributes
  160.                 else:
  161.                     attrs = collection_attributes
  162.             elif prefs['manage_device_metadata'] == 'on_connect':
  163.                 attrs = collection_attributes
  164.             
  165.             for attr in attrs:
  166.                 attr = attr.strip()
  167.                 if attr == 'device_collections':
  168.                     doing_dc = True
  169.                     val = book.device_collections
  170.                 else:
  171.                     doing_dc = False
  172.                     (ign, val, orig_val, fm) = book.format_field_extended(attr)
  173.                 if not val:
  174.                     continue
  175.                 
  176.                 if isbytestring(val):
  177.                     val = val.decode(preferred_encoding, 'replace')
  178.                 
  179.                 if isinstance(val, (list, tuple)):
  180.                     val = list(val)
  181.                 elif fm['datatype'] == 'series':
  182.                     val = [
  183.                         orig_val]
  184.                 elif fm['datatype'] == 'text' and fm['is_multiple']:
  185.                     val = orig_val
  186.                 else:
  187.                     val = [
  188.                         val]
  189.                 sort_attr = self.in_category_sort_rules(attr)
  190.                 for category in val:
  191.                     is_series = False
  192.                     if doing_dc:
  193.                         if category == book.series:
  194.                             is_series = True
  195.                         
  196.                     elif fm['is_custom']:
  197.                         if fm['datatype'] == 'text' and len(category) > 1 and category[0] == '[' and category[-1] == ']':
  198.                             continue
  199.                         
  200.                         if fm['datatype'] == 'series':
  201.                             is_series = True
  202.                         
  203.                     elif attr == 'tags' and len(category) > 1 and category[0] == '[' and category[-1] == ']':
  204.                         continue
  205.                     
  206.                     if (attr == 'series' or 'series' in collection_attributes) and book.get('series', None) == category:
  207.                         is_series = True
  208.                     
  209.                     if use_renaming_rules:
  210.                         cat_name = self.compute_category_name(attr, category, fm)
  211.                     else:
  212.                         cat_name = category
  213.                     if cat_name not in collections:
  214.                         collections[cat_name] = { }
  215.                     
  216.                     if use_renaming_rules and sort_attr:
  217.                         sort_val = book.get(sort_attr, None)
  218.                         collections[cat_name][lpath] = (book, sort_val, book.get('title_sort', 'zzzz'))
  219.                         continue
  220.                     if is_series:
  221.                         if doing_dc:
  222.                             collections[cat_name][lpath] = (book, book.get('series_index', sys.maxint), '')
  223.                         else:
  224.                             collections[cat_name][lpath] = (book, book.get(attr + '_index', sys.maxint), '')
  225.                     doing_dc
  226.                     if lpath not in collections[cat_name]:
  227.                         collections[cat_name][lpath] = (book, book.get('title_sort', 'zzzz'), '')
  228.                         continue
  229.                 
  230.             
  231.         
  232.         result = { }
  233.         
  234.         def none_cmp(xx, yy):
  235.             x = xx[1]
  236.             y = yy[1]
  237.             if x is None and y is None:
  238.                 return cmp(xx[2], yy[2])
  239.             if x is None:
  240.                 return 1
  241.             if y is None:
  242.                 return -1
  243.             c = cmp(x, y)
  244.             if c != 0:
  245.                 return c
  246.             return cmp(xx[2], yy[2])
  247.  
  248.         for category, lpaths in collections.items():
  249.             books = lpaths.values()
  250.             books.sort(cmp = none_cmp)
  251.             result[category] = [ x[0] for x in books ]
  252.         
  253.         return result
  254.  
  255.     
  256.     def rebuild_collections(self, booklist, oncard):
  257.         pass
  258.  
  259.  
  260.