Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
uu Namespace Reference

Data Structures

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 = 0 
)
Decode uuencoded file

Definition at line 84 of file uu.py.

References aifc.open(), string.rstrip(), and locale.str().

84 
85 def decode(in_file, out_file=None, mode=None, quiet=0):
86  """Decode uuencoded file"""
87  #
88  # Open the input file, if needed.
89  #
90  if in_file == '-':
91  in_file = sys.stdin
92  elif isinstance(in_file, StringType):
93  in_file = open(in_file)
94  #
95  # Read until a begin is encountered or we've exhausted the file
96  #
97  while 1:
98  hdr = in_file.readline()
99  if not hdr:
100  raise Error, 'No valid begin line found in input file'
101  if hdr[:5] != 'begin':
102  continue
103  hdrfields = hdr.split(" ", 2)
104  if len(hdrfields) == 3 and hdrfields[0] == 'begin':
105  try:
106  int(hdrfields[1], 8)
107  break
108  except ValueError:
109  pass
110  if out_file is None:
111  out_file = hdrfields[2].rstrip()
112  if os.path.exists(out_file):
113  raise Error, 'Cannot overwrite existing file: %s' % out_file
114  if mode is None:
115  mode = int(hdrfields[1], 8)
116  #
117  # Open the output file
118  #
119  if out_file == '-':
120  out_file = sys.stdout
121  elif isinstance(out_file, StringType):
122  fp = open(out_file, 'wb')
123  try:
124  os.path.chmod(out_file, mode)
125  except AttributeError:
126  pass
127  out_file = fp
128  #
129  # Main decoding loop
130  #
131  s = in_file.readline()
132  while s and s.strip() != 'end':
133  try:
134  data = binascii.a2b_uu(s)
135  except binascii.Error, v:
136  # Workaround for broken uuencoders by /Fredrik Lundh
137  nbytes = (((ord(s[0])-32) & 63) * 4 + 5) / 3
138  data = binascii.a2b_uu(s[:nbytes])
139  if not quiet:
140  sys.stderr.write("Warning: %s\n" % str(v))
141  out_file.write(data)
142  s = in_file.readline()
143  if not s:
144  raise Error, 'Truncated input file'
def uu.encode (   in_file,
  out_file,
  name = None,
  mode = None 
)
Uuencode file

Definition at line 43 of file uu.py.

43 
44 def encode(in_file, out_file, name=None, mode=None):
45  """Uuencode file"""
46  #
47  # If in_file is a pathname open it and change defaults
48  #
49  if in_file == '-':
50  in_file = sys.stdin
51  elif isinstance(in_file, StringType):
52  if name is None:
53  name = os.path.basename(in_file)
54  if mode is None:
55  try:
56  mode = os.stat(in_file)[0]
57  except AttributeError:
58  pass
59  in_file = open(in_file, 'rb')
60  #
61  # Open out_file if it is a pathname
62  #
63  if out_file == '-':
64  out_file = sys.stdout
65  elif isinstance(out_file, StringType):
66  out_file = open(out_file, 'w')
67  #
68  # Set defaults for name and mode
69  #
70  if name is None:
71  name = '-'
72  if mode is None:
73  mode = 0666
74  #
75  # Write the data
76  #
77  out_file.write('begin %o %s\n' % ((mode&0777),name))
78  str = in_file.read(45)
79  while len(str) > 0:
80  out_file.write(binascii.b2a_uu(str))
81  str = in_file.read(45)
82  out_file.write(' \nend\n')
83 
def uu.test ( )
uuencode/uudecode main program

Definition at line 145 of file uu.py.

References decode(), encode(), getopt.getopt(), and aifc.open().

146 def test():
147  """uuencode/uudecode main program"""
148  import getopt
149 
150  dopt = 0
151  topt = 0
152  input = sys.stdin
153  output = sys.stdout
154  ok = 1
155  try:
156  optlist, args = getopt.getopt(sys.argv[1:], 'dt')
157  except getopt.error:
158  ok = 0
159  if not ok or len(args) > 2:
160  print 'Usage:', sys.argv[0], '[-d] [-t] [input [output]]'
161  print ' -d: Decode (in stead of encode)'
162  print ' -t: data is text, encoded format unix-compatible text'
163  sys.exit(1)
164 
165  for o, a in optlist:
166  if o == '-d': dopt = 1
167  if o == '-t': topt = 1
168 
169  if len(args) > 0:
170  input = args[0]
171  if len(args) > 1:
172  output = args[1]
173 
174  if dopt:
175  if topt:
176  if isinstance(output, StringType):
177  output = open(output, 'w')
178  else:
179  print sys.argv[0], ': cannot do -t to stdout'
180  sys.exit(1)
181  decode(input, output)
182  else:
183  if topt:
184  if isinstance(input, StringType):
185  input = open(input, 'r')
186  else:
187  print sys.argv[0], ': cannot do -t from stdin'
188  sys.exit(1)
189  encode(input, output)

Variable Documentation

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

Definition at line 38 of file uu.py.