<commit_before>"""
	This module contains classes for managing incoming emails
"""

class IncomingMail:
	"""
		Single incoming email object. Extracts, text / html and attachments from the email
	"""
	def __init__(self, content):
		"""
			Parse the incoming mail content
		"""
		import email
		
		self.mail = email.message_from_string(content)
		self.text_content = ''
		self.html_content = ''
		self.attachments = []
		self.parse()

	def get_text_content(self):
		"""
			Returns the text parts of the email. If None, then HTML parts
		"""
		return self.text_content or self.html_content

	def get_charset(self, part):
		"""
			Guesses character set
		"""
		charset = part.get_content_charset()
		if not charset:
			import chardet
			charset = chardet.detect(str(part))['encoding']

		return charset
			
	def get_payload(self, part, charset):
		"""
			get utf-8 encoded part content
		"""
		try:
			return unicode(part.get_payload(decode=True),str(charset),"ignore").encode('utf8','replace')
		except LookupError, e:
			return part.get_payload()		

	def get_attachment(self, part, charset):
		"""
			Extracts an attachment
		"""
		self.attachments.append({
			'content-type': part.get_content_type(),
			'filename': part.get_filename(),
			'content': part.get_payload(decode=True)
		})
				
	def parse(self):
		"""
			Extracts text, html and attachments from the mail
		"""
		for part in self.mail.walk():
			self.process_part(part)

	def get_thread_id(self):
		"""
			Extracts thread id of the message between first [] 
			from the subject
		"""
		subject = self.mail.get('Subject', '')
		if '[' in subject and ']' in subject:
			return subject.split('[')[1].split(']')[0]

	def process_part(self, part):
		"""
			Process a single part of an email
		"""
		charset = self.get_charset(part)
		content_type = part.get_content_type()

		if content_type == 'text/plain':<commit_msg>Fix regarding support ticket to support ticket system email chaining issue<commit_after>"""
	This module contains classes for managing incoming emails
"""

class IncomingMail:
	"""
		Single incoming email object. Extracts, text / html and attachments from the email
	"""
	def __init__(self, content):
		"""
			Parse the incoming mail content
		"""
		import email
		
		self.mail = email.message_from_string(content)
		self.text_content = ''
		self.html_content = ''
		self.attachments = []
		self.parse()

	def get_text_content(self):
		"""
			Returns the text parts of the email. If None, then HTML parts
		"""
		return self.text_content or self.html_content

	def get_charset(self, part):
		"""
			Guesses character set
		"""
		charset = part.get_content_charset()
		if not charset:
			import chardet
			charset = chardet.detect(str(part))['encoding']

		return charset
			
	def get_payload(self, part, charset):
		"""
			get utf-8 encoded part content
		"""
		try:
			return unicode(part.get_payload(decode=True),str(charset),"ignore").encode('utf8','replace')
		except LookupError, e:
			return part.get_payload()		

	def get_attachment(self, part, charset):
		"""
			Extracts an attachment
		"""
		self.attachments.append({
			'content-type': part.get_content_type(),
			'filename': part.get_filename(),
			'content': part.get_payload(decode=True)
		})
				
	def parse(self):
		"""
			Extracts text, html and attachments from the mail
		"""
		for part in self.mail.walk():
			self.process_part(part)

	def get_thread_id(self):
		"""
			Extracts thread id of the message between first [] 
			from the subject
		"""
		import re
		subject = self.mail.get('Subject', '')

		return re.findall('(?<=\[)\w+', subject)


	def process_part(self, part):
		"""
			Process a single part of an email
		"""
		charset = self.get_charset(part)
		content_type = part.get_content_type()

		if content_type == 'text/plain':
{'fraction_non_alphanumeric': 0.13002114164904863, 'fraction_numerical': 0.0015856236786469344, 'mean_word_length': 3.779040404040404, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4924031', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1190, 'n_words': 326}