home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_881 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.6 KB  |  90 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.             sval = ts.strftime('%Y-%m-%d')
  45.             if sval != raw:
  46.                 self.log.error('OPF contains date', raw, 'that epubcheck does not like')
  47.                 if self.fix:
  48.                     dcdate.text = sval
  49.                     self.log('\tReplaced', raw, 'with', sval)
  50.                     dirtied = True
  51.                 
  52.             self.fix
  53.         
  54.         if dirtied:
  55.             self.container.set(self.container.opf_name, opf)
  56.         
  57.  
  58.     
  59.     def fix_preserve_aspect_ratio(self):
  60.         for name in self.container.name_map:
  61.             mt = self.container.mime_map.get(name, '')
  62.             if mt.lower() == 'application/xhtml+xml':
  63.                 root = self.container.get(name)
  64.                 dirtied = False
  65.                 for svg in root.xpath('//svg:svg[@preserveAspectRatio="none"]', namespaces = {
  66.                     'svg': 'http://www.w3.org/2000/svg' }):
  67.                     self.log.error('Found <svg> element with preserveAspectRatio="none" which epubcheck cannot handle')
  68.                     if self.fix:
  69.                         svg.set('preserveAspectRatio', 'xMidYMid meet')
  70.                         dirtied = True
  71.                         self.log('\tReplaced none with xMidYMid meet')
  72.                         continue
  73.                 
  74.                 if dirtied:
  75.                     self.container.set(name, root)
  76.                 
  77.             dirtied
  78.         
  79.  
  80.     
  81.     def run(self, container, opts, log, fix = False):
  82.         self.container = container
  83.         self.opts = opts
  84.         self.log = log
  85.         self.fix = fix
  86.         self.fix_pubdates()
  87.         self.fix_preserve_aspect_ratio()
  88.  
  89.  
  90.