source: people/peter.buschman/backup_monitoring/parsing/streams/_streams.py@ 1252

Last change on this file since 1252 was 976, checked in by peter, on Dec 6, 2011 at 10:19:33 AM

Raw checkin of current NetBackup / TSM parsing code.

File size: 3.4 KB
Line 
1
2##
3## Stream Base Class
4##
5class Stream(object):
6
7 def __init__(self, data):
8
9 self.data = data
10
11 def __del__(self):
12
13 self.data.close()
14
15 def close(self):
16
17 self.data.close()
18
19 def records(self):
20
21 records = []
22
23 for record in self:
24 records.append(record)
25
26 return records
27
28##
29## ParsedStream
30##
31class ParsedStream(object):
32
33 def __init__(self, stream, parse=None, parse_args=[], parse_kwargs={}):
34
35 self.stream = stream
36 self.parse = parse
37 self.parse_args = parse_args
38 self.parse_kwargs = parse_kwargs
39
40 def __iter__(self):
41
42 return self
43
44 def next(self):
45
46 try:
47 record = self.stream.next()
48 args = self.parse_args
49 kwargs = self.parse_kwargs
50 return self.parse(record, *args, **kwargs)
51 except StopIteration:
52 self.stream.close()
53 raise StopIteration
54
55
56##
57## Stream of records separated by newlines
58##
59class NewLineStream(Stream):
60
61 def __init__(self, data, header=0):
62
63 Stream.__init__(self, data)
64
65 for i in range(header):
66 line = self.data.readline()
67
68 def __iter__(self):
69
70 return self
71
72 def next(self):
73
74 line = self.data.readline()
75
76 if line:
77 return line.rstrip('\r\n')
78 else:
79 raise StopIteration
80
81
82##
83## Stream of a single record
84##
85class SingleRecordStream(Stream):
86
87 def __init__(self, data, header=0):
88
89 Stream.__init__(self, data)
90
91 for i in range(header):
92 line = self.data.readline()
93
94 def __iter__(self):
95
96 return self
97
98 def next(self):
99
100 record = []
101 lines = self.data.readlines()
102
103 if lines:
104 for line in lines:
105 record.append(line.rstrip('\r\n'))
106 return record
107 else:
108 raise StopIteration
109
110
111##
112## Stream of records separated by blank lines
113##
114class BlankLineStream(Stream):
115
116 def __init__(self, data, header=0):
117
118 Stream.__init__(self, data)
119
120 for i in range(header):
121 line = self.data.readline()
122
123 def __iter__(self):
124
125 return self
126
127 def next(self):
128
129 record = []
130 line = self.data.readline()
131
132 if not line:
133 raise StopIteration
134
135 while line not in ['\n', '\r\n']:
136 record.append(line.rstrip('\r\n'))
137 line = self.data.readline()
138 if not line:
139 return record
140
141 return record
142
143
144##
145## Stream of records separated by a single line separator
146##
147class LineSeparatorStream(Stream):
148
149 def __init__(self, data, separator='\n', header=0):
150
151 Stream.__init__(self, data)
152
153 if separator[-1] != '\n':
154 separator = '%s\n' % (separator)
155
156 self.separator = separator
157
158 for i in range(header):
159 line = self.data.readline()
160
161 def __iter__(self):
162
163 return self
164
165 def next(self):
166
167 record = []
168 line = self.data.readline()
169
170 if not line:
171 raise StopIteration
172
173 while line != self.separator:
174 record.append(line.rstrip('\r\n'))
175 line = self.data.readline()
176 if not line:
177 return record
178
179 return record
180
Note: See TracBrowser for help on using the repository browser.