home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_875 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  3.7 KB  |  97 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. from calibre.ebooks.epub.fix import ePubFixer, InvalidEpub
  8. from calibre.utils.date import parse_date, strptime
  9.  
  10. class Epubcheck(ePubFixer):
  11.     name = 'Workaround epubcheck bugs'
  12.     
  13.     def short_description(self):
  14.         return _('Workaround epubcheck bugs')
  15.  
  16.     short_description = property(short_description)
  17.     
  18.     def long_description(self):
  19.         return _('Workarounds for bugs in the latest release of epubcheck. epubcheck reports many things as errors that are not actually errors. epub-fix will try to detect these and replace them with constructs that epubcheck likes. This may cause significant changes to your epub, complain to the epubcheck project.')
  20.  
  21.     long_description = property(long_description)
  22.     
  23.     def fix_name(self):
  24.         return 'epubcheck'
  25.  
  26.     fix_name = property(fix_name)
  27.     
  28.     def fix_pubdates(self):
  29.         dirtied = False
  30.         opf = self.container.opf
  31.         for dcdate in opf.xpath('//dc:date', namespaces = {
  32.             'dc': 'http://purl.org/dc/elements/1.1/' }):
  33.             raw = dcdate.text
  34.             if not raw:
  35.                 raw = ''
  36.             
  37.             default = strptime('2000-1-1', '%Y-%m-%d', as_utc = True)
  38.             
  39.             try:
  40.                 ts = parse_date(raw, assume_utc = False, as_utc = True, default = default)
  41.             except:
  42.                 raise InvalidEpub('Invalid date set in OPF', raw)
  43.  
  44.             
  45.             try:
  46.                 sval = ts.strftime('%Y-%m-%d')
  47.             except:
  48.                 strftime = strftime
  49.                 import calibre
  50.                 sval = strftime('%Y-%m-%d', ts.timetuple())
  51.  
  52.             if sval != raw:
  53.                 self.log.error('OPF contains date', raw, 'that epubcheck does not like')
  54.                 if self.fix:
  55.                     dcdate.text = sval
  56.                     self.log('\tReplaced', raw, 'with', sval)
  57.                     dirtied = True
  58.                 
  59.             self.fix
  60.         
  61.         if dirtied:
  62.             self.container.set(self.container.opf_name, opf)
  63.         
  64.  
  65.     
  66.     def fix_preserve_aspect_ratio(self):
  67.         for name in self.container.name_map:
  68.             mt = self.container.mime_map.get(name, '')
  69.             if mt.lower() == 'application/xhtml+xml':
  70.                 root = self.container.get(name)
  71.                 dirtied = False
  72.                 for svg in root.xpath('//svg:svg[@preserveAspectRatio="none"]', namespaces = {
  73.                     'svg': 'http://www.w3.org/2000/svg' }):
  74.                     self.log.error('Found <svg> element with preserveAspectRatio="none" which epubcheck cannot handle')
  75.                     if self.fix:
  76.                         svg.set('preserveAspectRatio', 'xMidYMid meet')
  77.                         dirtied = True
  78.                         self.log('\tReplaced none with xMidYMid meet')
  79.                         continue
  80.                 
  81.                 if dirtied:
  82.                     self.container.set(name, root)
  83.                 
  84.             dirtied
  85.         
  86.  
  87.     
  88.     def run(self, container, opts, log, fix = False):
  89.         self.container = container
  90.         self.opts = opts
  91.         self.log = log
  92.         self.fix = fix
  93.         self.fix_pubdates()
  94.         self.fix_preserve_aspect_ratio()
  95.  
  96.  
  97.