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