Move each_line() out of class
This commit is contained in:
parent
ce1f12c5d6
commit
3a878eb975
1 changed files with 20 additions and 16 deletions
|
@ -18,6 +18,24 @@ def decode(text: str):
|
||||||
except:
|
except:
|
||||||
return str(decoded[0])
|
return str(decoded[0])
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
class MessageState(enum.Enum):
|
class MessageState(enum.Enum):
|
||||||
EMPTY = 0
|
EMPTY = 0
|
||||||
HEADER = 1
|
HEADER = 1
|
||||||
|
@ -129,30 +147,16 @@ class Message(DatabaseTable):
|
||||||
def is_first_line(self):
|
def is_first_line(self):
|
||||||
return len(self.headers) == 1 and (self.body == '' or self.body is None)
|
return len(self.headers) == 1 and (self.body == '' or self.body is None)
|
||||||
|
|
||||||
@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
|
@staticmethod
|
||||||
def parse(text: str):
|
def parse(text: str):
|
||||||
message = Message()
|
message = Message()
|
||||||
|
|
||||||
for line in Message.each_line(text):
|
for line in each_line(text):
|
||||||
message.add(line)
|
message._parse_line(line)
|
||||||
|
|
||||||
return message
|
return message
|
||||||
|
|
Loading…
Add table
Reference in a new issue