]>
Commit | Line | Data |
---|---|---|
e9647132 PI |
1 | #!/usr/bin/env python |
2 | ## $Id: abstfilter.py,v 1.1.1.1 2003/07/01 23:28:27 euske Exp $ | |
3 | ## | |
4 | ## abstfilter.py - A framework for cascade filters. | |
5 | ## | |
9d9c1ae1 PI |
6 | ## from http://www.unixuser.org/~euske/python/index.html: |
7 | ## The following files are in public domain except where otherwise noted. THESE FILES COME WITH ABSOLUTELY NO WARRANTY. | |
e9647132 PI |
8 | |
9 | ||
10 | ## AbstractFeeder | |
11 | ## | |
12 | class AbstractFeeder(object): | |
13 | ||
14 | def __init__(self, next_filter): | |
15 | self.next_filter = next_filter | |
16 | return | |
17 | ||
18 | def feed_next(self, s): | |
19 | self.next_filter.feed(s) | |
20 | return | |
21 | ||
22 | def close(self): | |
23 | self.next_filter.close() | |
24 | return | |
25 | ||
26 | ||
27 | ## AbstractFilter | |
28 | ## | |
29 | class AbstractFilter(object): | |
30 | ||
31 | def __init__(self, next_filter): | |
32 | self.next_filter = next_filter | |
33 | return | |
34 | ||
35 | def process(self, s): | |
36 | raise NotImplementedError | |
37 | ||
38 | def feed(self, s): | |
39 | self.feed_next(self.process(s)) | |
40 | return | |
41 | ||
42 | def feed_next(self, s): | |
43 | self.next_filter.feed(s) | |
44 | return | |
45 | ||
46 | def close(self): | |
47 | self.next_filter.close() | |
48 | return | |
49 | ||
50 | ||
51 | ## AbstractConsumer | |
52 | ## | |
53 | class AbstractConsumer(object): | |
54 | ||
55 | def __init__(self): | |
56 | return | |
57 | ||
58 | def feed(self, s): | |
59 | raise NotImplementedError | |
60 | ||
61 | def close(self): | |
62 | return | |
63 | ||
64 | ||
65 | ## FileGenerator | |
66 | ## | |
67 | class FileGenerator(AbstractFeeder): | |
68 | def __init__(self, next_type): | |
69 | next_filter = next_type(self) | |
70 | AbstractFeeder.__init__(self, next_filter) | |
71 | self.results = [] | |
72 | return | |
73 | ||
74 | def feed(self, s): | |
75 | self.results.append(s) | |
76 | return | |
77 | ||
78 | def close(self): | |
79 | return | |
80 | ||
81 | def pullopen(self, f): | |
82 | while 1: | |
83 | s = f.readline() | |
84 | if not s: break | |
85 | self.feed_next(s) | |
86 | if self.results: | |
87 | for s in self.results: | |
88 | yield s | |
89 | self.results = [] | |
90 | for s in self.results: | |
91 | yield s | |
92 | self.results = [] | |
93 | return | |
94 |