home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_777 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.7 KB  |  151 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Timothy Legge <timlegge at gmail.com>'
  6. import os
  7. import re
  8. import time
  9. from calibre.ebooks.metadata import MetaInformation
  10. from calibre.constants import filesystem_encoding, preferred_encoding
  11. from calibre import isbytestring
  12.  
  13. class Book(MetaInformation):
  14.     BOOK_ATTRS = [
  15.         'lpath',
  16.         'size',
  17.         'mime',
  18.         'device_collections',
  19.         '_new_book']
  20.     JSON_ATTRS = [
  21.         'lpath',
  22.         'title',
  23.         'authors',
  24.         'mime',
  25.         'size',
  26.         'tags',
  27.         'author_sort',
  28.         'title_sort',
  29.         'comments',
  30.         'category',
  31.         'publisher',
  32.         'series',
  33.         'series_index',
  34.         'rating',
  35.         'isbn',
  36.         'language',
  37.         'application_id',
  38.         'book_producer',
  39.         'lccn',
  40.         'lcc',
  41.         'ddc',
  42.         'rights',
  43.         'publication_type',
  44.         'uuid']
  45.     
  46.     def __init__(self, prefix, lpath, title, authors, mime, date, ContentType, thumbnail_name, other = None):
  47.         MetaInformation.__init__(self, '')
  48.         self.device_collections = []
  49.         self._new_book = False
  50.         self.path = os.path.join(prefix, lpath)
  51.         if os.sep == '\\':
  52.             self.path = self.path.replace('/', '\\')
  53.             self.lpath = lpath.replace('\\', '/')
  54.         else:
  55.             self.lpath = lpath
  56.         self.title = title
  57.         if not authors:
  58.             self.authors = [
  59.                 '']
  60.         else:
  61.             self.authors = [
  62.                 authors]
  63.         self.mime = mime
  64.         
  65.         try:
  66.             self.size = os.path.getsize(self.path)
  67.         except OSError:
  68.             self.size = 0
  69.  
  70.         
  71.         try:
  72.             if ContentType == '6':
  73.                 self.datetime = time.strptime(date, '%Y-%m-%dT%H:%M:%S.%f')
  74.             else:
  75.                 self.datetime = time.gmtime(os.path.getctime(self.path))
  76.         except:
  77.             self.datetime = time.gmtime()
  78.  
  79.         if thumbnail_name is not None:
  80.             self.thumbnail = ImageWrapper(thumbnail_name)
  81.         
  82.         self.tags = []
  83.         if other:
  84.             self.smart_update(other)
  85.         
  86.  
  87.     
  88.     def __eq__(self, other):
  89.         return self.path == getattr(other, 'path', None)
  90.  
  91.     
  92.     def db_id(self):
  93.         doc = 'The database id in the application database that this file corresponds to'
  94.         
  95.         def fget(self):
  96.             match = re.search('_(\\d+)$', self.lpath.rpartition('.')[0])
  97.             if match:
  98.                 return int(match.group(1))
  99.  
  100.         return property(fget = fget, doc = doc)
  101.  
  102.     db_id = dynamic_property(db_id)
  103.     
  104.     def title_sorter(self):
  105.         doc = 'String to sort the title. If absent, title is returned'
  106.         
  107.         def fget(self):
  108.             return re.sub('^\\s*A\\s+|^\\s*The\\s+|^\\s*An\\s+', '', self.title).rstrip()
  109.  
  110.         return property(doc = doc, fget = fget)
  111.  
  112.     title_sorter = dynamic_property(title_sorter)
  113.     
  114.     def thumbnail(self):
  115.         pass
  116.  
  117.     thumbnail = dynamic_property(thumbnail)
  118.     
  119.     def smart_update(self, other, replace_metadata = False):
  120.         MetaInformation.smart_update(self, other)
  121.         for attr in self.BOOK_ATTRS:
  122.             if hasattr(other, attr):
  123.                 val = getattr(other, attr, None)
  124.                 setattr(self, attr, val)
  125.                 continue
  126.         
  127.  
  128.     
  129.     def to_json(self):
  130.         json = { }
  131.         for attr in self.JSON_ATTRS:
  132.             val = getattr(self, attr)
  133.             if isbytestring(val):
  134.                 enc = None if attr == 'lpath' else preferred_encoding
  135.                 val = val.decode(enc, 'replace')
  136.             elif isinstance(val, (list, tuple)):
  137.                 val = [ _[1] if isbytestring(x) else x for x in val ]
  138.             
  139.             json[attr] = val
  140.         
  141.         return json
  142.  
  143.  
  144.  
  145. class ImageWrapper(object):
  146.     
  147.     def __init__(self, image_path):
  148.         self.image_path = image_path
  149.  
  150.  
  151.