Package linda :: Package parser :: Module rfc822parser
[hide private]

Source Code for Module linda.parser.rfc822parser

 1  from linda.debug import dprint 
 2   
3 -class RFC822Parser:
4 - def __init__(self, file):
5 self.file = file 6 self.data = [[]] 7 self.empty_stanzas = 0 8 self.read() 9 self.parse()
10
11 - def read(self):
12 try: 13 f = open(self.file) 14 except IOError, e: 15 raise IOError('failed to parse %s, due to %s.' % (self.file, e)) 16 tmp_counter = 0 17 for k in f: 18 if not k.strip(): 19 tmp_counter += 1 20 self.data.append([]) 21 continue 22 if k.startswith('#'): 23 continue 24 self.data[tmp_counter].append(k) 25 f.close() 26 for x in range(len(self.data) - 1, -1, -1): 27 if self.data[x] == []: 28 self.empty_stanzas += 1 29 del self.data[x] 30 self.concat() 31 dprint(_("Parsed array: %s") % self.data, 4)
32
33 - def validate(self, validtags):
34 to_del = [] 35 for stanza in range(len(self.data)): 36 seen = 0 37 for line in self.data[stanza]: 38 for data in validtags: 39 if line.startswith(data): 40 seen += 1 41 if seen < len(validtags): 42 to_del.insert(0, stanza) 43 for number in to_del: 44 del self.data[number] 45 dprint(_("Parsed array, after deleting: %s") % self.data, 4)
46
47 - def concat(self):
48 for x in self.data: 49 for y in range(len(x) - 1, 0, -1): 50 if x[y].startswith(' ') or x[y].startswith('\t'): 51 x[y-1] += x[y] 52 del x[y]
53
54 - def split(self):
55 tmp_data = [] 56 for x in self.data: 57 tmp_data.append({}) 58 for stanza in range(len(self.data)): 59 for line in self.data[stanza]: 60 preformatted_line = line 61 if line.endswith('\n'): 62 preformatted_line = line[:-1] 63 cur_line = preformatted_line.split(':', 1) 64 tmp_rhs = cur_line[1] 65 if tmp_rhs.startswith(' '): 66 tmp_rhs = cur_line[1][1:] 67 tmp_data[stanza][cur_line[0].lower()] = tmp_rhs 68 self.data = tmp_data 69 dprint(_("Parsed array, after converting: %s") % self.data, 4)
70
71 - def remove_signed(self):
72 if len(self.data) == 4: 73 for i in (3, 2, 0): 74 del self.data[i] 75 elif len(self.data) == 3: 76 for i in (2, 0): 77 del self.data[i] 78 reparse = [] 79 for line in self.data[0]: 80 if line.startswith('-----BEGIN PGP SIGNATURE'): 81 break 82 reparse.append(line) 83 self.data[0] = reparse
84
85 - def parse(self):
86 pass
87
88 - def __getitem__(self, item):
89 return self.data[item]
90
91 - def has_key(self, key):
92 return self.data.has_key(key)
93
94 - def keys(self):
95 return self.data.keys()
96