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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import optparse
  5. import sys
  6. import re
  7. import os
  8. from lxml.html.diff import htmldiff
  9. description = ''
  10. parser = optparse.OptionParser(usage = '%prog [OPTIONS] FILE1 FILE2\n%prog --annotate [OPTIONS] INFO1 FILE1 INFO2 FILE2 ...', description = description)
  11. parser.add_option('-o', '--output', metavar = 'FILE', dest = 'output', default = '-', help = 'File to write the difference to')
  12. parser.add_option('-a', '--annotation', action = 'store_true', dest = 'annotation', help = 'Do an annotation')
  13.  
  14. def main(args = None):
  15.     if args is None:
  16.         args = sys.argv[1:]
  17.     
  18.     (options, args) = parser.parse_args(args)
  19.     if options.annotation:
  20.         return annotate(options, args)
  21.     (file1, file2) = args
  22.     input1 = read_file(file1)
  23.     input2 = read_file(file2)
  24.     body1 = split_body(input1)[1]
  25.     (pre, body2, post) = split_body(input2)
  26.     result = htmldiff(body1, body2)
  27.     result = pre + result + post
  28.  
  29.  
  30. def read_file(filename):
  31.     if filename == '-':
  32.         c = sys.stdin.read()
  33.     elif not os.path.exists(filename):
  34.         raise OSError('Input file %s does not exist' % filename)
  35.     else:
  36.         f = open(filename, 'rb')
  37.         c = f.read()
  38.         f.close()
  39.     return c
  40.  
  41. body_start_re = re.compile('<body.*?>', re.I | re.S)
  42. body_end_re = re.compile('</body.*?>', re.I | re.S)
  43.  
  44. def split_body(html):
  45.     match = body_start_re.search(html)
  46.     if match:
  47.         pre = html[:match.end()]
  48.         html = html[match.end():]
  49.     
  50.     match = body_end_re.search(html)
  51.     if match:
  52.         post = html[match.start():]
  53.         html = html[:match.start()]
  54.     
  55.     return (pre, html, post)
  56.  
  57.  
  58. def annotate(options, args):
  59.     print 'Not yet implemented'
  60.     sys.exit(1)
  61.  
  62.