Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_message(self):
"""Make sure that prints the correct error"""
e = errors.ParseError(cause='error on line 10')
self.assertEqual('error on line 10', str(e))
def test_parse_invalid_date(self):
"""Test whether it raises an exception when a date is invalid"""
with self.assertRaisesRegex(ParseError, "date expected on line 4"):
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)),
"data/supybot/supybot_invalid_date.log"), 'r') as f:
parser = SupybotParser(f)
_ = [item for item in parser.parse()]
def find_activity_table(bs):
# The first table with 5 columns is the table of activity
tables = bs.find_all('table')
for tb in tables:
nheaders = len(tb.tr.find_all('th', recursive=False))
if nheaders == 5:
return tb
raise ParseError(cause="Table of bug activity not found.")
If the given XML is invalid or does not contains any bug, the
method will raise a ParseError exception.
:param raw_xml: XML string to parse
:returns: a generator of parsed bugs
:raises ParseError: raised when an error occurs parsing
the given XML stream
"""
bugs = xml_to_dict(raw_xml)
if 'bug' not in bugs:
cause = "No bugs found. XML stream seems to be invalid."
raise ParseError(cause=cause)
for bug in bugs['bug']:
yield bug
def _parse_supybot_timestamp(self, line):
"""Parse timestamp section"""
m = self.SUPYBOT_TIMESTAMP_REGEX.match(line)
if not m:
msg = "date expected on line %s" % (str(self.nline))
raise ParseError(cause=msg)
ts = m.group('ts')
msg = m.group('msg')
return ts, msg
childs = {}
for child in node:
childs.setdefault(child.tag, []).append(node_to_dict(child))
d.update(childs.items())
return d
purged_xml = remove_invalid_xml_chars(raw_xml)
try:
tree = xml.etree.ElementTree.fromstring(purged_xml)
except xml.etree.ElementTree.ParseError as e:
cause = "XML stream %s" % (str(e))
raise ParseError(cause=cause)
d = node_to_dict(tree)
return d
"""Parse a NNTP article.
This method parses a NNTP article stored in a string object
and returns an dictionary.
:param raw_article: NNTP article string
:returns: a dictionary of type `requests.structures.CaseInsensitiveDict`
:raises ParseError: when an error is found parsing the article
"""
try:
message = email.message_from_string(raw_article)
article = message_to_dict(message)
except UnicodeEncodeError as e:
raise ParseError(cause=str(e))
return article
def _parse_supybot_msg(self, line):
"""Parse message section"""
patterns = [(self.SUPYBOT_COMMENT_REGEX, self.TCOMMENT),
(self.SUPYBOT_COMMENT_ACTION_REGEX, self.TCOMMENT),
(self.SUPYBOT_SERVER_REGEX, self.TSERVER),
(self.SUPYBOT_BOT_REGEX, self.TCOMMENT)]
for p in patterns:
m = p[0].match(line)
if not m:
continue
return p[1], m.group('nick'), m.group('body').strip()
msg = "invalid message on line %s" % (str(self.nline))
raise ParseError(cause=msg)
return payload
# The function starts here
message = requests.structures.CaseInsensitiveDict()
if isinstance(msg, mailbox.mboxMessage):
message['unixfrom'] = msg.get_from()
else:
message['unixfrom'] = None
try:
for k, v in parse_headers(msg).items():
message[k] = v
message['body'] = parse_payload(msg)
except UnicodeError as e:
raise ParseError(cause=str(e))
return message