home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / uu.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  3.9 KB  |  156 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. import binascii
  5. import os
  6. import sys
  7. __all__ = [
  8.     'Error',
  9.     'encode',
  10.     'decode']
  11.  
  12. class Error(Exception):
  13.     pass
  14.  
  15.  
  16. def encode(in_file, out_file, name = None, mode = None):
  17.     opened_files = []
  18.     
  19.     try:
  20.         if in_file == '-':
  21.             in_file = sys.stdin
  22.         elif isinstance(in_file, basestring):
  23.             if name is None:
  24.                 name = os.path.basename(in_file)
  25.             if mode is None:
  26.                 
  27.                 try:
  28.                     mode = os.stat(in_file).st_mode
  29.                 except AttributeError:
  30.                     pass
  31.                 
  32.  
  33.             in_file = open(in_file, 'rb')
  34.             opened_files.append(in_file)
  35.         if out_file == '-':
  36.             out_file = sys.stdout
  37.         elif isinstance(out_file, basestring):
  38.             out_file = open(out_file, 'wb')
  39.             opened_files.append(out_file)
  40.         if name is None:
  41.             name = '-'
  42.         if mode is None:
  43.             mode = 438
  44.         out_file.write('begin %o %s\n' % (mode & 511, name))
  45.         data = in_file.read(45)
  46.         while len(data) > 0:
  47.             out_file.write(binascii.b2a_uu(data))
  48.             data = in_file.read(45)
  49.         out_file.write(' \nend\n')
  50.     finally:
  51.         for f in opened_files:
  52.             f.close()
  53.         
  54.  
  55.  
  56.  
  57. def decode(in_file, out_file = None, mode = None, quiet = 0):
  58.     opened_files = []
  59.     if in_file == '-':
  60.         in_file = sys.stdin
  61.     elif isinstance(in_file, basestring):
  62.         in_file = open(in_file)
  63.         opened_files.append(in_file)
  64.     
  65.     try:
  66.         while True:
  67.             hdr = in_file.readline()
  68.             if not hdr:
  69.                 raise Error('No valid begin line found in input file')
  70.             if not hdr.startswith('begin'):
  71.                 continue
  72.             hdrfields = hdr.split(' ', 2)
  73.             if len(hdrfields) == 3 and hdrfields[0] == 'begin':
  74.                 
  75.                 try:
  76.                     int(hdrfields[1], 8)
  77.                 except ValueError:
  78.                     pass
  79.                 
  80.  
  81.             if out_file is None:
  82.                 out_file = hdrfields[2].rstrip()
  83.                 if os.path.exists(out_file):
  84.                     raise Error('Cannot overwrite existing file: %s' % out_file)
  85.             if mode is None:
  86.                 mode = int(hdrfields[1], 8)
  87.             if out_file == '-':
  88.                 out_file = sys.stdout
  89.             elif isinstance(out_file, basestring):
  90.                 fp = open(out_file, 'wb')
  91.                 
  92.                 try:
  93.                     os.path.chmod(out_file, mode)
  94.                 except AttributeError:
  95.                     pass
  96.  
  97.                 out_file = fp
  98.                 opened_files.append(out_file)
  99.             s = in_file.readline()
  100.             while s and s.strip() != 'end':
  101.                 
  102.                 try:
  103.                     data = binascii.a2b_uu(s)
  104.                 except binascii.Error:
  105.                     v = None
  106.                     nbytes = ((ord(s[0]) - 32 & 63) * 4 + 5) // 3
  107.                     data = binascii.a2b_uu(s[:nbytes])
  108.                     if not quiet:
  109.                         sys.stderr.write('Warning: %s\n' % v)
  110.                     
  111.  
  112.                 out_file.write(data)
  113.                 s = in_file.readline()
  114.             if not s:
  115.                 raise Error('Truncated input file')
  116.         for f in opened_files:
  117.             f.close()
  118.         
  119.         return None
  120.  
  121.  
  122.  
  123. def test():
  124.     import optparse
  125.     parser = optparse.OptionParser(usage = 'usage: %prog [-d] [-t] [input [output]]')
  126.     parser.add_option('-d', '--decode', dest = 'decode', help = 'Decode (instead of encode)?', default = False, action = 'store_true')
  127.     parser.add_option('-t', '--text', dest = 'text', help = 'data is text, encoded format unix-compatible text?', default = False, action = 'store_true')
  128.     (options, args) = parser.parse_args()
  129.     if len(args) > 2:
  130.         parser.error('incorrect number of arguments')
  131.         sys.exit(1)
  132.     input = sys.stdin
  133.     output = sys.stdout
  134.     if len(args) > 0:
  135.         input = args[0]
  136.     if len(args) > 1:
  137.         output = args[1]
  138.     if options.decode:
  139.         if options.text:
  140.             if isinstance(output, basestring):
  141.                 output = open(output, 'w')
  142.             else:
  143.                 print sys.argv[0], ': cannot do -t to stdout'
  144.                 sys.exit(1)
  145.         decode(input, output)
  146.     elif options.text:
  147.         if isinstance(input, basestring):
  148.             input = open(input, 'r')
  149.         else:
  150.             print sys.argv[0], ': cannot do -t from stdin'
  151.             sys.exit(1)
  152.     encode(input, output)
  153.  
  154. if __name__ == '__main__':
  155.     test()
  156.