annotate Lib/pipes.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 560ec7030817
children 24da035ceb6e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Conversion pipeline templates
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # =============================
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # The problem:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # ------------
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # Suppose you have some data that you want to convert to another format
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # (e.g. from GIF image format to PPM image format). Maybe the
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # conversion involves several steps (e.g. piping it through compress or
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # uuencode). Some of the conversion steps may require that their input
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # is a disk file, others may be able to read standard input; similar for
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # their output. The input to the entire conversion may also be read
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # from a disk file or from an open file, and similar for its output.
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 # The module lets you construct a pipeline template by sticking one or
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # more conversion steps together. It will take care of creating and
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # removing temporary files if they are necessary to hold intermediate
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # data. You can then use the template to do conversions from many
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # different sources to many different destinations. The temporary
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # file names used are different each time the template is used.
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # The templates are objects so you can create templates for many
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # different conversion steps and store them in a dictionary, for
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # instance.
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # Directions:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # -----------
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # To create a template:
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1078
diff changeset
32 # t = Template()
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 # To add a conversion step to a template:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 # t.append(command, kind)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 # where kind is a string of two characters: the first is '-' if the
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 # command reads its standard input or 'f' if it requires a file; the
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 # second likewise for the output. The command must be valid /bin/sh
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 # syntax. If input or output files are required, they are passed as
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 # $IN and $OUT; otherwise, it must be possible to use the command in
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 # a pipeline.
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 # To add a conversion step at the beginning:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 # t.prepend(command, kind)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 # To convert a file to another file using a template:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 # sts = t.copy(infile, outfile)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 # If infile or outfile are the empty string, standard input is read or
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 # standard output is written, respectively. The return value is the
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # exit status of the conversion pipeline.
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 # To open a file for reading or writing through a conversion pipeline:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 # fp = t.open(file, mode)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 # where mode is 'r' to read the file, or 'w' to write it -- just like
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 # for the built-in function open() or for os.popen().
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 # To create a new template object initialized to a given one:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 # t2 = t.clone()
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 # For an example, see the function test() at the end of the file.
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 import sys
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 import regex
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 import os
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 import tempfile
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 import string
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 # Conversion step kinds
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 FILEIN_FILEOUT = 'ff' # Must read & write real files
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 STDIN_FILEOUT = '-f' # Must write a real file
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 FILEIN_STDOUT = 'f-' # Must read a real file
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 STDIN_STDOUT = '--' # Normal pipeline element
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 SOURCE = '.-' # Must be first, writes stdout
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 SINK = '-.' # Must be last, reads stdin
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 stepkinds = [FILEIN_FILEOUT, STDIN_FILEOUT, FILEIN_STDOUT, STDIN_STDOUT, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 SOURCE, SINK]
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 # A pipeline template is a Template object:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 class Template:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1078
diff changeset
88 # Template() returns a fresh pipeline template
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1078
diff changeset
89 def __init__(self):
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 self.debugging = 0
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 self.reset()
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 # t.__repr__() implements `t`
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 def __repr__(self):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 return '<Template instance, steps=' + `self.steps` + '>'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 # t.reset() restores a pipeline template to its initial state
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 def reset(self):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 self.steps = []
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 # t.clone() returns a new pipeline template with identical
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 # initial state as the current one
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 def clone(self):
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1078
diff changeset
104 t = Template()
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 t.steps = self.steps[:]
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 t.debugging = self.debugging
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 return t
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 # t.debug(flag) turns debugging on or off
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 def debug(self, flag):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 self.debugging = flag
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 # t.append(cmd, kind) adds a new step at the end
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 def append(self, cmd, kind):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 if type(cmd) <> type(''):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 raise TypeError, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 'Template.append: cmd must be a string'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 if kind not in stepkinds:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 raise ValueError, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 'Template.append: bad kind ' + `kind`
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 if kind == SOURCE:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 raise ValueError, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 'Template.append: SOURCE can only be prepended'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 if self.steps <> [] and self.steps[-1][1] == SINK:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 raise ValueError, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 'Template.append: already ends with SINK'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 if kind[0] == 'f' and regex.search('\$IN', cmd) < 0:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 raise ValueError, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 'Template.append: missing $IN in cmd'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 if kind[1] == 'f' and regex.search('\$OUT', cmd) < 0:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 raise ValueError, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 'Template.append: missing $OUT in cmd'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 self.steps.append((cmd, kind))
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 # t.prepend(cmd, kind) adds a new step at the front
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 def prepend(self, cmd, kind):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 if type(cmd) <> type(''):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 raise TypeError, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 'Template.prepend: cmd must be a string'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 if kind not in stepkinds:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 raise ValueError, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 'Template.prepend: bad kind ' + `kind`
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 if kind == SINK:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 raise ValueError, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 'Template.prepend: SINK can only be appended'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 if self.steps <> [] and self.steps[0][1] == SOURCE:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 raise ValueError, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 'Template.prepend: already begins with SOURCE'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 if kind[0] == 'f' and regex.search('\$IN\>', cmd) < 0:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 raise ValueError, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 'Template.prepend: missing $IN in cmd'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 if kind[1] == 'f' and regex.search('\$OUT\>', cmd) < 0:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 raise ValueError, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 'Template.prepend: missing $OUT in cmd'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 self.steps.insert(0, (cmd, kind))
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 # t.open(file, rw) returns a pipe or file object open for
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 # reading or writing; the file is the other end of the pipeline
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 def open(self, file, rw):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 if rw == 'r':
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 return self.open_r(file)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 if rw == 'w':
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 return self.open_w(file)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 raise ValueError, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 'Template.open: rw must be \'r\' or \'w\', not ' + `rw`
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
166
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 # t.open_r(file) and t.open_w(file) implement
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 # t.open(file, 'r') and t.open(file, 'w') respectively
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 def open_r(self, file):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 if self.steps == []:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 return open(file, 'r')
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 if self.steps[-1][1] == SINK:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 raise ValueError, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 'Template.open_r: pipeline ends width SINK'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 cmd = self.makepipeline(file, '')
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 return os.popen(cmd, 'r')
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
178
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 def open_w(self, file):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 if self.steps == []:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 return open(file, 'w')
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 if self.steps[0][1] == SOURCE:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 raise ValueError, \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 'Template.open_w: pipeline begins with SOURCE'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 cmd = self.makepipeline('', file)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 return os.popen(cmd, 'w')
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 def copy(self, infile, outfile):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 return os.system(self.makepipeline(infile, outfile))
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
190
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 def makepipeline(self, infile, outfile):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 cmd = makepipeline(infile, self.steps, outfile)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 if self.debugging:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 print cmd
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 cmd = 'set -x; ' + cmd
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 return cmd
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
197
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 def makepipeline(infile, steps, outfile):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 # Build a list with for each command:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 # [input filename or '', command string, kind, output filename or '']
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 list = []
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 for cmd, kind in steps:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 list.append(['', cmd, kind, ''])
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 # Make sure there is at least one step
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 if list == []:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 list.append(['', 'cat', '--', ''])
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 # Take care of the input and output ends
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 [cmd, kind] = list[0][1:3]
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 if kind[0] == 'f' and not infile:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 list.insert(0, ['', 'cat', '--', ''])
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 list[0][0] = infile
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 [cmd, kind] = list[-1][1:3]
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 if kind[1] == 'f' and not outfile:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 list.append(['', 'cat', '--', ''])
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 list[-1][-1] = outfile
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 # Invent temporary files to connect stages that need files
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 garbage = []
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 for i in range(1, len(list)):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 lkind = list[i-1][2]
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 rkind = list[i][2]
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 if lkind[1] == 'f' or rkind[0] == 'f':
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 temp = tempfile.mktemp()
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 garbage.append(temp)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 list[i-1][-1] = list[i][0] = temp
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 for item in list:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 [inf, cmd, kind, outf] = item
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 if kind[1] == 'f':
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 cmd = 'OUT=' + quote(outf) + '; ' + cmd
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 if kind[0] == 'f':
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 cmd = 'IN=' + quote(inf) + '; ' + cmd
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 if kind[0] == '-' and inf:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 cmd = cmd + ' <' + quote(inf)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 if kind[1] == '-' and outf:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 cmd = cmd + ' >' + quote(outf)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 item[1] = cmd
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 cmdlist = list[0][1]
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 for item in list[1:]:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 [cmd, kind] = item[1:3]
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 if item[0] == '':
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 if 'f' in kind:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 cmd = '{ ' + cmd + '; }'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 cmdlist = cmdlist + ' |\n' + cmd
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 else:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 cmdlist = cmdlist + '\n' + cmd
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 if garbage:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 rmcmd = 'rm -f'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 for file in garbage:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 rmcmd = rmcmd + ' ' + quote(file)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 trapcmd = 'trap ' + quote(rmcmd + '; exit') + ' 1 2 3 13 14 15'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 cmdlist = trapcmd + '\n' + cmdlist + '\n' + rmcmd
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 return cmdlist
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
265
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
266
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 # Reliably quote a string as a single argument for /bin/sh
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
268
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 _safechars = string.letters + string.digits + '!@%_-+=:,./' # Safe unquoted
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 _funnychars = '"`$\\' # Unsafe inside "double quotes"
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
271
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 def quote(file):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 for c in file:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 if c not in _safechars:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 break
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 else:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 return file
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 if '\'' not in file:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 return '\'' + file + '\''
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 res = ''
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 for c in file:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 if c in _funnychars:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 c = '\\' + c
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 res = res + c
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 return '"' + res + '"'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
286
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
287
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 # Small test program and example
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
289
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 def test():
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 import os
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 print 'Testing...'
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1078
diff changeset
293 t = Template()
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 t.append('togif $IN $OUT', 'ff')
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 t.append('giftoppm', '--')
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 t.append('ppmtogif >$OUT', '-f')
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 t.append('fromgif $IN $OUT', 'ff')
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 t.debug(1)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 FILE = '/usr/local/images/rgb/rogues/guido.rgb'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 t.copy(FILE, '@temp')
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 print 'Done.'