3535from email .header import decode_header
3636from unicodedata import normalize
3737
38- import six
39-
4038from mailparser .const import (
4139 ADDRESSES_HEADERS ,
4240 JUNK_PATTERN ,
@@ -90,52 +88,45 @@ def wrapper(*args, **kwargs):
9088@sanitize
9189def ported_string (raw_data , encoding = "utf-8" , errors = "ignore" ):
9290 """
93- Give as input raw data and output a str in Python 3
94- and unicode in Python 2.
91+ Give as input raw data and output a str in Python 3.
9592
9693 Args:
97- raw_data: Python 2 str, Python 3 bytes or str to porting
94+ raw_data: bytes or str to convert to str
9895 encoding: string giving the name of an encoding
99- errors: his specifies the treatment of characters
96+ errors: specifies the treatment of characters
10097 which are invalid in the input encoding
10198
10299 Returns:
103- str (Python 3) or unicode (Python 2)
100+ str
104101 """
105102
106103 if not raw_data :
107- return six . text_type ()
104+ return str ()
108105
109- if isinstance (raw_data , six . text_type ):
106+ if isinstance (raw_data , str ):
110107 return raw_data
111108
112- if six .PY2 :
113- try :
114- return six .text_type (raw_data , encoding , errors )
115- except LookupError :
116- return six .text_type (raw_data , "utf-8" , errors )
117-
118- if six .PY3 :
119- try :
120- return six .text_type (raw_data , encoding )
121- except (LookupError , UnicodeDecodeError ):
122- return six .text_type (raw_data , "utf-8" , errors )
109+ # raw_data is bytes, decode it
110+ try :
111+ return str (raw_data , encoding )
112+ except (LookupError , UnicodeDecodeError ):
113+ return str (raw_data , "utf-8" , errors )
123114
124115
125116def decode_header_part (header ):
126117 """
127- Given an raw header returns an decoded header
118+ Given a raw header returns a decoded header
128119
129120 Args:
130121 header (string): header to decode
131122
132123 Returns:
133- str (Python 3) or unicode (Python 2)
124+ str
134125 """
135126 if not header :
136- return six . text_type ()
127+ return str ()
137128
138- output = six . text_type ()
129+ output = str ()
139130
140131 try :
141132 for d , c in decode_header (header ):
@@ -151,10 +142,15 @@ def decode_header_part(header):
151142
152143
153144def ported_open (file_ ):
154- if six .PY2 :
155- return open (file_ )
156- elif six .PY3 :
157- return open (file_ , encoding = "utf-8" , errors = "ignore" )
145+ """Open a file with UTF-8 encoding and ignore errors.
146+
147+ Args:
148+ file_: path to the file to open
149+
150+ Returns:
151+ file object
152+ """
153+ return open (file_ , encoding = "utf-8" , errors = "ignore" )
158154
159155
160156def find_between (text , first_token , last_token ):
@@ -179,7 +175,7 @@ def fingerprints(data):
179175
180176 hashes = namedtuple ("Hashes" , "md5 sha1 sha256 sha512" )
181177
182- if not isinstance (data , six . binary_type ):
178+ if not isinstance (data , bytes ):
183179 data = data .encode ("utf-8" )
184180
185181 # md5
@@ -215,28 +211,19 @@ def msgconvert(email):
215211
216212 Returns:
217213 tuple with file path of mail converted and
218- standard output data (unicode Python 2, str Python 3 )
214+ standard output data (str)
219215 """
220216 log .debug ("Started converting Outlook email" )
221217 temph , temp = tempfile .mkstemp (prefix = "outlook_" )
222218 command = ["msgconvert" , "--outfile" , temp , email ]
223219
224220 try :
225- if six .PY2 :
226- with open (os .devnull , "w" ) as devnull :
227- out = subprocess .Popen (
228- command ,
229- stdin = subprocess .PIPE ,
230- stdout = subprocess .PIPE ,
231- stderr = devnull ,
232- )
233- elif six .PY3 :
234- out = subprocess .Popen (
235- command ,
236- stdin = subprocess .PIPE ,
237- stdout = subprocess .PIPE ,
238- stderr = subprocess .DEVNULL ,
239- )
221+ out = subprocess .Popen (
222+ command ,
223+ stdin = subprocess .PIPE ,
224+ stdout = subprocess .PIPE ,
225+ stderr = subprocess .DEVNULL ,
226+ )
240227
241228 except OSError as e :
242229 message = f"Check if 'msgconvert' tool is installed / { e !r} "
@@ -284,12 +271,9 @@ def parse_received(received):
284271 # otherwise we have one matching clause!
285272 log .debug ("Found one match for %s in %s" % (pattern .pattern , received ))
286273 match = matches [0 ].groupdict ()
287- if six .PY2 :
288- values_by_clause [match .keys ()[0 ]] = match .values ()[0 ]
289- elif six .PY3 :
290- key = list (match .keys ())[0 ]
291- value = list (match .values ())[0 ]
292- values_by_clause [key ] = value
274+ key = list (match .keys ())[0 ]
275+ value = list (match .values ())[0 ]
276+ values_by_clause [key ] = value
293277
294278 if len (values_by_clause ) == 0 :
295279 # we weren't able to match anything...
@@ -466,7 +450,7 @@ def get_to_domains(to=[], reply_to=[]):
466450 for i in to + reply_to :
467451 try :
468452 domains .add (i [1 ].split ("@" )[- 1 ].lower ().strip ())
469- except KeyError :
453+ except ( KeyError , IndexError ) :
470454 pass
471455
472456 return list (domains )
@@ -495,7 +479,7 @@ def get_header(message, name):
495479 return headers [0 ].strip ()
496480 # in this case return a list
497481 return headers
498- return six . text_type ()
482+ return str ()
499483
500484
501485def get_mail_keys (message , complete = True ):
0 commit comments