home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / lxml / html / _diffcommand.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  2.6 KB  |  77 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  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.     
  22.     if len(args) != 2:
  23.         print 'Error: you must give two files'
  24.         parser.print_help()
  25.         sys.exit(1)
  26.     
  27.     (file1, file2) = args
  28.     input1 = read_file(file1)
  29.     input2 = read_file(file2)
  30.     body1 = split_body(input1)[1]
  31.     (pre, body2, post) = split_body(input2)
  32.     result = htmldiff(body1, body2)
  33.     result = pre + result + post
  34.     if options.output == '-':
  35.         if not result.endswith('\n'):
  36.             result += '\n'
  37.         
  38.         sys.stdout.write(result)
  39.     else:
  40.         f = open(options.output, 'wb')
  41.         f.write(result)
  42.         f.close()
  43.  
  44.  
  45. def read_file(filename):
  46.     if filename == '-':
  47.         c = sys.stdin.read()
  48.     elif not os.path.exists(filename):
  49.         raise OSError('Input file %s does not exist' % filename)
  50.     else:
  51.         f = open(filename, 'rb')
  52.         c = f.read()
  53.         f.close()
  54.     return c
  55.  
  56. body_start_re = re.compile('<body.*?>', re.I | re.S)
  57. body_end_re = re.compile('</body.*?>', re.I | re.S)
  58.  
  59. def split_body(html):
  60.     match = body_start_re.search(html)
  61.     if match:
  62.         pre = html[:match.end()]
  63.         html = html[match.end():]
  64.     
  65.     match = body_end_re.search(html)
  66.     if match:
  67.         post = html[match.start():]
  68.         html = html[:match.start()]
  69.     
  70.     return (pre, html, post)
  71.  
  72.  
  73. def annotate(options, args):
  74.     print 'Not yet implemented'
  75.     sys.exit(1)
  76.  
  77.