]>
Commit | Line | Data |
---|---|---|
d14a1e28 | 1 | """Provides a variety of classes to create pseudo keywords and pseudo files.""" |
1fded56b | 2 | |
d14a1e28 RD |
3 | __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>" |
4 | __cvsid__ = "$Id$" | |
5 | __revision__ = "$Revision$"[11:-2] | |
1fded56b | 6 | |
d14a1e28 RD |
7 | |
8 | class PseudoKeyword: | |
9 | """A callable class that calls a method passed as a parameter. | |
10 | ||
11 | Good for creating a pseudo keyword in the python runtime | |
12 | environment. The keyword is really an object that has a repr() | |
13 | that calls itself which calls the method that was passed in the | |
14 | init of the object. All this just to avoid having to type in the | |
15 | closing parens on a method. So, for example: | |
16 | ||
17 | >>> quit = PseudoKeyword(SomeObject.someMethod) | |
18 | >>> quit | |
19 | ||
20 | SomeObject.someMethod gets executed as if it had been called | |
21 | directly and the user didn't have to type the parens, like | |
22 | 'quit()'. This technique is most applicable for pseudo keywords | |
23 | like quit, exit and help. | |
24 | ||
25 | If SomeObject.someMethod can take parameters, they can still be | |
26 | passed by using the keyword in the traditional way with parens.""" | |
27 | ||
28 | def __init__(self, method): | |
29 | """Create a callable object that executes method when called.""" | |
30 | ||
31 | if callable(method): | |
32 | self.method = method | |
33 | else: | |
34 | raise ValueError, 'method must be callable' | |
35 | ||
36 | def __call__(self, *args, **kwds): | |
37 | self.method(*args, **kwds) | |
38 | ||
39 | def __repr__(self): | |
40 | self() | |
41 | return '' | |
42 | ||
43 | ||
44 | class PseudoFile: | |
45 | ||
46 | def __init__(self): | |
47 | """Create a file-like object.""" | |
48 | pass | |
49 | ||
50 | def readline(self): | |
51 | pass | |
52 | ||
53 | def write(self, s): | |
54 | pass | |
55 | ||
56 | def writelines(self, l): | |
57 | map(self.write, l) | |
58 | ||
59 | def flush(self): | |
60 | pass | |
61 | ||
62 | def isatty(self): | |
63 | pass | |
64 | ||
65 | ||
66 | class PseudoFileIn(PseudoFile): | |
67 | ||
68 | def __init__(self, readline, readlines=None): | |
69 | if callable(readline): | |
70 | self.readline = readline | |
71 | else: | |
72 | raise ValueError, 'readline must be callable' | |
73 | if callable(readlines): | |
74 | self.readlines = readlines | |
75 | ||
76 | def isatty(self): | |
77 | return 1 | |
78 | ||
79 | ||
80 | class PseudoFileOut(PseudoFile): | |
81 | ||
82 | def __init__(self, write): | |
83 | if callable(write): | |
84 | self.write = write | |
85 | else: | |
86 | raise ValueError, 'write must be callable' | |
87 | ||
88 | def isatty(self): | |
89 | return 1 | |
90 | ||
91 | ||
92 | class PseudoFileErr(PseudoFile): | |
93 | ||
94 | def __init__(self, write): | |
95 | if callable(write): | |
96 | self.write = write | |
97 | else: | |
98 | raise ValueError, 'write must be callable' | |
99 | ||
100 | def isatty(self): | |
101 | return 1 |