Back to index

python3.2  3.2.2
Classes | Functions | Variables
uu Namespace Reference

Classes

class  Error

Functions

def encode
def decode
def test

Variables

list __all__ = ["Error", "encode", "decode"]

Function Documentation

def uu.decode (   in_file,
  out_file = None,
  mode = None,
  quiet = False 
)
Decode uuencoded file

Definition at line 90 of file uu.py.

00090 
00091 def decode(in_file, out_file=None, mode=None, quiet=False):
00092     """Decode uuencoded file"""
00093     #
00094     # Open the input file, if needed.
00095     #
00096     opened_files = []
00097     if in_file == '-':
00098         in_file = sys.stdin.buffer
00099     elif isinstance(in_file, str):
00100         in_file = open(in_file, 'rb')
00101         opened_files.append(in_file)
00102 
00103     try:
00104         #
00105         # Read until a begin is encountered or we've exhausted the file
00106         #
00107         while True:
00108             hdr = in_file.readline()
00109             if not hdr:
00110                 raise Error('No valid begin line found in input file')
00111             if not hdr.startswith(b'begin'):
00112                 continue
00113             hdrfields = hdr.split(b' ', 2)
00114             if len(hdrfields) == 3 and hdrfields[0] == b'begin':
00115                 try:
00116                     int(hdrfields[1], 8)
00117                     break
00118                 except ValueError:
00119                     pass
00120         if out_file is None:
00121             # If the filename isn't ASCII, what's up with that?!?
00122             out_file = hdrfields[2].rstrip(b' \t\r\n\f').decode("ascii")
00123             if os.path.exists(out_file):
00124                 raise Error('Cannot overwrite existing file: %s' % out_file)
00125         if mode is None:
00126             mode = int(hdrfields[1], 8)
00127         #
00128         # Open the output file
00129         #
00130         if out_file == '-':
00131             out_file = sys.stdout.buffer
00132         elif isinstance(out_file, str):
00133             fp = open(out_file, 'wb')
00134             try:
00135                 os.path.chmod(out_file, mode)
00136             except AttributeError:
00137                 pass
00138             out_file = fp
00139             opened_files.append(out_file)
00140         #
00141         # Main decoding loop
00142         #
00143         s = in_file.readline()
00144         while s and s.strip(b' \t\r\n\f') != b'end':
00145             try:
00146                 data = binascii.a2b_uu(s)
00147             except binascii.Error as v:
00148                 # Workaround for broken uuencoders by /Fredrik Lundh
00149                 nbytes = (((s[0]-32) & 63) * 4 + 5) // 3
00150                 data = binascii.a2b_uu(s[:nbytes])
00151                 if not quiet:
00152                     sys.stderr.write("Warning: %s\n" % v)
00153             out_file.write(data)
00154             s = in_file.readline()
00155         if not s:
00156             raise Error('Truncated input file')
00157     finally:
00158         for f in opened_files:
00159             f.close()

Here is the caller graph for this function:

def uu.encode (   in_file,
  out_file,
  name = None,
  mode = None 
)
Uuencode file

Definition at line 42 of file uu.py.

00042 
00043 def encode(in_file, out_file, name=None, mode=None):
00044     """Uuencode file"""
00045     #
00046     # If in_file is a pathname open it and change defaults
00047     #
00048     opened_files = []
00049     try:
00050         if in_file == '-':
00051             in_file = sys.stdin.buffer
00052         elif isinstance(in_file, str):
00053             if name is None:
00054                 name = os.path.basename(in_file)
00055             if mode is None:
00056                 try:
00057                     mode = os.stat(in_file).st_mode
00058                 except AttributeError:
00059                     pass
00060             in_file = open(in_file, 'rb')
00061             opened_files.append(in_file)
00062         #
00063         # Open out_file if it is a pathname
00064         #
00065         if out_file == '-':
00066             out_file = sys.stdout.buffer
00067         elif isinstance(out_file, str):
00068             out_file = open(out_file, 'wb')
00069             opened_files.append(out_file)
00070         #
00071         # Set defaults for name and mode
00072         #
00073         if name is None:
00074             name = '-'
00075         if mode is None:
00076             mode = 0o666
00077         #
00078         # Write the data
00079         #
00080         out_file.write(('begin %o %s\n' % ((mode & 0o777), name)).encode("ascii"))
00081         data = in_file.read(45)
00082         while len(data) > 0:
00083             out_file.write(binascii.b2a_uu(data))
00084             data = in_file.read(45)
00085         out_file.write(b' \nend\n')
00086     finally:
00087         for f in opened_files:
00088             f.close()
00089 

Here is the caller graph for this function:

def uu.test ( )
uuencode/uudecode main program

Definition at line 160 of file uu.py.

00160 
00161 def test():
00162     """uuencode/uudecode main program"""
00163 
00164     import optparse
00165     parser = optparse.OptionParser(usage='usage: %prog [-d] [-t] [input [output]]')
00166     parser.add_option('-d', '--decode', dest='decode', help='Decode (instead of encode)?', default=False, action='store_true')
00167     parser.add_option('-t', '--text', dest='text', help='data is text, encoded format unix-compatible text?', default=False, action='store_true')
00168 
00169     (options, args) = parser.parse_args()
00170     if len(args) > 2:
00171         parser.error('incorrect number of arguments')
00172         sys.exit(1)
00173 
00174     # Use the binary streams underlying stdin/stdout
00175     input = sys.stdin.buffer
00176     output = sys.stdout.buffer
00177     if len(args) > 0:
00178         input = args[0]
00179     if len(args) > 1:
00180         output = args[1]
00181 
00182     if options.decode:
00183         if options.text:
00184             if isinstance(output, str):
00185                 output = open(output, 'wb')
00186             else:
00187                 print(sys.argv[0], ': cannot do -t to stdout')
00188                 sys.exit(1)
00189         decode(input, output)
00190     else:
00191         if options.text:
00192             if isinstance(input, str):
00193                 input = open(input, 'rb')
00194             else:
00195                 print(sys.argv[0], ': cannot do -t from stdin')
00196                 sys.exit(1)
00197         encode(input, output)

Here is the call graph for this function:


Variable Documentation

list uu.__all__ = ["Error", "encode", "decode"]

Definition at line 37 of file uu.py.