From 2db91358def94cf8081f27b736988320d14eba39 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sun, 18 Oct 1992 17:09:59 +0000 Subject: Misc changes and new modules. whrandom is "objectified". SOCKET.py is moved to the sgi subdirectory. --- Lib/calendar.py | 8 +- Lib/imghdr.py | 26 ++++- Lib/irix5/torgb.py | 91 +++++++++++++++ Lib/pipes.py | 302 ++++++++++++++++++++++++++++++++++++++++++++++++ Lib/plat-irix5/torgb.py | 91 +++++++++++++++ Lib/string.py | 12 ++ Lib/stringold.py | 12 ++ Lib/toaiff.py | 101 ++++++++++++++++ Lib/tzparse.py | 76 ++++++++++++ Lib/whrandom.py | 105 +++++++++++------ 10 files changed, 778 insertions(+), 46 deletions(-) create mode 100755 Lib/irix5/torgb.py create mode 100644 Lib/pipes.py create mode 100755 Lib/plat-irix5/torgb.py create mode 100644 Lib/toaiff.py create mode 100644 Lib/tzparse.py diff --git a/Lib/calendar.py b/Lib/calendar.py index 4e02e0e..62fb27f 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -11,7 +11,7 @@ # - Monday is the first day of the week (numbered 0) # These are really parameters of the 'time' module: -epoch = 1970 # Time began on January 1 of this year (00:00:00 UCT) +epoch = 1970 # Time began on January 1 of this year (00:00:00 UTC) day_0 = 3 # The epoch begins on a Thursday (Monday = 0) # Return 1 for leap years, 0 for non-leap years @@ -58,7 +58,7 @@ def leapdays(y1, y2): return (y2+3)/4 - (y1+3)/4 # Inverse of gmtime(): -# Turn UCT calendar time (less yday, wday) into seconds since epoch +# Turn UTC calendar time (less yday, wday) into seconds since epoch def mktime(year, month, day, hours, mins, secs): days = day - 1 for m in range(January, month): days = days + mdays[m] @@ -96,8 +96,8 @@ def asctime(year, month, day, hours, mins, secs, yday, wday): return s + ' ' + `year` # Localization: Minutes West from Greenwich -# timezone = -2*60 # Middle-European time with DST on -timezone = 5*60 # EST (sigh -- THINK time() doesn't return UCT) +timezone = -2*60 # Middle-European time with DST on +# timezone = 5*60 # EST (sigh -- THINK time() doesn't return UTC) # Local time ignores DST issues for now -- adjust 'timezone' to fake it def localtime(secs): diff --git a/Lib/imghdr.py b/Lib/imghdr.py index 981340f..063bfe8 100644 --- a/Lib/imghdr.py +++ b/Lib/imghdr.py @@ -35,13 +35,29 @@ def test_gif(h, f): tests.append(test_gif) -def test_pnm(h, f): - # PBM, PGM, PPM (portable {bit,gray,pix}map; together portable anymap) +def test_pbm(h, f): + # PBM (portable bitmap) if len(h) >= 3 and \ - h[0] == 'P' and h[1] in '123456' and h[2] in ' \t\n\r': - return 'pnm' + h[0] == 'P' and h[1] in '14' and h[2] in ' \t\n\r': + return 'pbm' -tests.append(test_pnm) +tests.append(test_pbm) + +def test_pgm(h, f): + # PGM (portable graymap) + if len(h) >= 3 and \ + h[0] == 'P' and h[1] in '25' and h[2] in ' \t\n\r': + return 'pgm' + +tests.append(test_pgm) + +def test_ppm(h, f): + # PPM (portable pixmap) + if len(h) >= 3 and \ + h[0] == 'P' and h[1] in '36' and h[2] in ' \t\n\r': + return 'ppm' + +tests.append(test_ppm) def test_tiff(h, f): # TIFF (can be in Motorola or Intel byte order) diff --git a/Lib/irix5/torgb.py b/Lib/irix5/torgb.py new file mode 100755 index 0000000..e46aca3 --- /dev/null +++ b/Lib/irix5/torgb.py @@ -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 diff --git a/Lib/pipes.py b/Lib/pipes.py new file mode 100644 index 0000000..426c377 --- /dev/null +++ b/Lib/pipes.py @@ -0,0 +1,302 @@ +# Conversion pipeline templates +# ============================= + + +# The problem: +# ------------ +# +# Suppose you have some data that you want to convert to another format +# (e.g. from GIF image format to PPM image format). Maybe the +# conversion involves several steps (e.g. piping it through compress or +# uuencode). Some of the conversion steps may require that their input +# is a disk file, others may be able to read standard input; similar for +# their output. The input to the entire conversion may also be read +# from a disk file or from an open file, and similar for its output. +# +# The module lets you construct a pipeline template by sticking one or +# more conversion steps together. It will take care of creating and +# removing temporary files if they are necessary to hold intermediate +# data. You can then use the template to do conversions from many +# different sources to many different destinations. The temporary +# file names used are different each time the template is used. +# +# The templates are objects so you can create templates for many +# different conversion steps and store them in a dictionary, for +# instance. + + +# Directions: +# ----------- +# +# To create a template: +# t = Template().init() +# +# To add a conversion step to a template: +# t.append(command, kind) +# where kind is a string of two characters: the first is '-' if the +# command reads its standard input or 'f' if it requires a file; the +# second likewise for the output. The command must be valid /bin/sh +# syntax. If input or output files are required, they are passed as +# $IN and $OUT; otherwise, it must be possible to use the command in +# a pipeline. +# +# To add a conversion step at the beginning: +# t.prepend(command, kind) +# +# To convert a file to another file using a template: +# sts = t.copy(infile, outfile) +# If infile or outfile are the empty string, standard input is read or +# standard output is written, respectively. The return value is the +# exit status of the conversion pipeline. +# +# To open a file for reading or writing through a conversion pipeline: +# fp = t.open(file, mode) +# where mode is 'r' to read the file, or 'w' to write it -- just like +# for the built-in function open() or for os.popen(). +# +# To create a new template object initialized to a given one: +# t2 = t.clone() +# +# For an example, see the function test() at the end of the file. + + +import sys +import regex + +import os +import tempfile +import string + + +# Conversion step kinds + +FILEIN_FILEOUT = 'ff' # Must read & write real files +STDIN_FILEOUT = '-f' # Must write a real file +FILEIN_STDOUT = 'f-' # Must read a real file +STDIN_STDOUT = '--' # Normal pipeline element +SOURCE = '.-' # Must be first, writes stdout +SINK = '-.' # Must be last, reads stdin + +stepkinds = [FILEIN_FILEOUT, STDIN_FILEOUT, FILEIN_STDOUT, STDIN_STDOUT, \ + SOURCE, SINK] + + +# A pipeline template is a Template object: + +class Template: + + # Template().init() returns a fresh pipeline template + def init(self): + self.debugging = 0 + self.reset() + return self + + # t.__repr__() implements `t` + def __repr__(self): + return '