mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Misc changes and new modules. whrandom is "objectified". SOCKET.py
is moved to the sgi subdirectory.
This commit is contained in:
parent
0cb8e8cfc0
commit
2db91358de
10 changed files with 778 additions and 46 deletions
91
Lib/irix5/torgb.py
Executable file
91
Lib/irix5/torgb.py
Executable file
|
@ -0,0 +1,91 @@
|
|||
# Convert "arbitrary" image files to rgb files (SGI's image format).
|
||||
# Input may be compressed.
|
||||
# The uncompressed file type may be PBM, PGM, PPM, GIF, TIFF, or Sun raster.
|
||||
# An exception is raised if the file is not of a recognized type.
|
||||
# Returned filename is either the input filename or a temporary filename;
|
||||
# in the latter case the caller must ensure that it is removed.
|
||||
# Other temporary files used are removed by the function.
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import pipes
|
||||
import imghdr
|
||||
|
||||
table = {}
|
||||
|
||||
t = pipes.Template().init()
|
||||
t.append('fromppm $IN $OUT', 'ff')
|
||||
table['ppm'] = t
|
||||
|
||||
t = pipes.Template().init()
|
||||
t.append('pnmtoppm', '--')
|
||||
t.append('fromppm $IN $OUT', 'ff')
|
||||
table['pnm'] = t
|
||||
table['pgm'] = t
|
||||
table['pbm'] = t
|
||||
|
||||
t = pipes.Template().init()
|
||||
t.append('fromgif $IN $OUT', 'ff')
|
||||
table['gif'] = t
|
||||
|
||||
t = pipes.Template().init()
|
||||
t.append('tifftopnm', '--')
|
||||
t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
|
||||
t.append('fromppm $IN $OUT', 'ff')
|
||||
table['tiff'] = t
|
||||
|
||||
t = pipes.Template().init()
|
||||
t.append('rasttopnm', '--')
|
||||
t.append('pnmtoppm', '--')
|
||||
t.append('fromppm $IN $OUT', 'ff')
|
||||
table['rast'] = t
|
||||
|
||||
uncompress = pipes.Template().init()
|
||||
uncompress.append('uncompress', '--')
|
||||
|
||||
|
||||
error = 'torgb.error' # Exception
|
||||
|
||||
def torgb(filename):
|
||||
temps = []
|
||||
ret = None
|
||||
try:
|
||||
ret = _torgb(filename, temps)
|
||||
finally:
|
||||
for temp in temps[:]:
|
||||
if temp <> ret:
|
||||
try:
|
||||
os.unlink(temp)
|
||||
except os.error:
|
||||
pass
|
||||
temps.remove(temp)
|
||||
return ret
|
||||
|
||||
def _torgb(filename, temps):
|
||||
if filename[-2:] == '.Z':
|
||||
fname = tempfile.mktemp()
|
||||
temps.append(fname)
|
||||
sts = uncompress.copy(filename, fname)
|
||||
if sts:
|
||||
raise error, filename + ': uncompress failed'
|
||||
else:
|
||||
fname = filename
|
||||
try:
|
||||
ftype = imghdr.what(fname)
|
||||
except IOError, msg:
|
||||
if type(msg) == type(()) and len(msg) == 2 and \
|
||||
type(msg[0]) == type(0) and type(msg[1]) == type(''):
|
||||
msg = msg[1]
|
||||
if type(msg) <> type(''):
|
||||
msg = `msg`
|
||||
raise error, filename + ': ' + msg
|
||||
if ftype == 'rgb':
|
||||
return fname
|
||||
if ftype == None or not table.has_key(ftype):
|
||||
raise error, \
|
||||
filename + ': unsupported image file type ' + `ftype`
|
||||
temp = tempfile.mktemp()
|
||||
sts = table[ftype].copy(fname, temp)
|
||||
if sts:
|
||||
raise error, filename + ': conversion to rgb failed'
|
||||
return temp
|
Loading…
Add table
Add a link
Reference in a new issue