Add static method for parsing single mbox messages
This commit is contained in:
parent
240a79e3ba
commit
af9e264ff9
1 changed files with 28 additions and 0 deletions
|
@ -99,6 +99,34 @@ class MBoxMessage():
|
|||
def is_first_line(self):
|
||||
return len(self.headers) == 1 and self.body == ''
|
||||
|
||||
@staticmethod
|
||||
def each_line(text: str):
|
||||
start = 0
|
||||
end = len(text)
|
||||
|
||||
while True:
|
||||
try:
|
||||
index = text.index('\n', start, end)
|
||||
|
||||
yield text[start:index+1]
|
||||
|
||||
start = index + 1
|
||||
|
||||
if start == end:
|
||||
break
|
||||
except ValueError:
|
||||
yield text[start:end]
|
||||
break
|
||||
|
||||
@staticmethod
|
||||
def parse(text: str):
|
||||
message = MBoxMessage()
|
||||
|
||||
for line in MBoxMessage.each_line(text):
|
||||
message.add(line)
|
||||
|
||||
return message
|
||||
|
||||
class MBoxReader():
|
||||
__slots__ = 'path', 'fh', 'line', 'buf', 'message',
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue