home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 April / com_0405_1.iso / opensource / BTpp-0.5.4-bin.exe / $INSTDIR / BT++.exe / uu.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-19  |  4.2 KB  |  173 lines

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