diff --git a/src/gmv/blowfish.py b/src/gmv/blowfish.py index 34602089..b6f058bf 100644 --- a/src/gmv/blowfish.py +++ b/src/gmv/blowfish.py @@ -59,7 +59,7 @@ class Blowfish: DECRYPT = 1 # For _round() - _MODULUS = 2L ** 32 + _MODULUS = 2 ** 32 # CTR constants _BLOCK_SIZE = 8 @@ -528,23 +528,23 @@ def _round(self, xl): def _demo(heading, source, encrypted, decrypted): """demo method """ - print heading - print "\tSource: %(source)s" % { + print(heading) + print("\tSource: %(source)s" % { 'source': source, - } - print "\tEncrypted: %(encrypted)s" % { + }) + print("\tEncrypted: %(encrypted)s" % { 'encrypted': encrypted, - } - print "\tDecrypted: %(decrypted)s" % { + }) + print("\tDecrypted: %(decrypted)s" % { 'decrypted': decrypted, - } - print + }) + print() key = 'This is a test key' cipher = Blowfish(key) # Encryption processing - (xl, xr) = (123456L, 654321L) + (xl, xr) = (123456, 654321) (cl, cr) = cipher.cipher(xl, xr, cipher.ENCRYPT) (dl, dr) = cipher.cipher(cl, cr, cipher.DECRYPT) _demo("Testing encryption", (xl, xr), (cl, cr), (dl, dr)) @@ -564,7 +564,7 @@ def _demo(heading, source, encrypted, decrypted): _demo("Testing CTR logic", text, repr(crypted), decrypted) # Test speed - print "Testing speed" + print("Testing speed") test_strings = [''.join(("The quick brown fox jumps over the lazy dog", str(i),)) for i in xrange(1000)] n = 0 t1 = time.time() @@ -575,8 +575,8 @@ def _demo(heading, source, encrypted, decrypted): t2 = time.time() if t2 - t1 >= 5.0: break - print "%(count)i encryptions in %(time)0.1f seconds: %(throughput)0.1f enc/s" % { + print("%(count)i encryptions in %(time)0.1f seconds: %(throughput)0.1f enc/s" % { 'count': n, 'time': t2 - t1, 'throughput': n / (t2 - t1), - } + }) diff --git a/src/gmv/cmdline_utils.py b/src/gmv/cmdline_utils.py index e195cd76..7050f06c 100755 --- a/src/gmv/cmdline_utils.py +++ b/src/gmv/cmdline_utils.py @@ -75,7 +75,7 @@ def print_help(self, out=sys.stderr): super(CmdLineParser, self).print_help(out) if self.epilogue: #print >> out, '\n%s' % textwrap.fill(self.epilogue, 100, replace_whitespace = False) - print >> out, '\n%s' % self.epilogue + print('\n%s' % self.epilogue, file=out) out.flush() def show_usage(self, msg=None): @@ -97,7 +97,7 @@ def die_with_usage(self, msg=None, exit_code=2): The process exit code. Defaults to 2. """ if msg != None: - print >> sys.stderr, msg + print(msg, file=sys.stderr) self.print_help(sys.stderr) sys.exit(exit_code) diff --git a/src/gmv/conf/conf_helper.py b/src/gmv/conf/conf_helper.py index 7efc79a2..621fbfe3 100755 --- a/src/gmv/conf/conf_helper.py +++ b/src/gmv/conf/conf_helper.py @@ -101,10 +101,10 @@ def _get_value_from_command_line(self): for arg in sys.argv: if arg.lower() == the_s: i = sys.argv.index(arg) - #print "i = %d, val = %s\n"%(i,sys.argv[i]) + #print("i = %d, val = %s\n"%(i,sys.argv[i])) if len(sys.argv) <= i: # No more thing to read in the command line so quit - print "Resource: Commandline argument %s has no value\n" % (self._cli_arg) + print("Resource: Commandline argument %s has no value\n" % (self._cli_arg)) return None else: #print "i+1 = %d, val = %s\n"%(i+1,sys.argv[i+1]) @@ -243,7 +243,7 @@ def get_value_as_boolean(self): """ val = self.get_value() if val.lower() not in self._boolean_states: - raise ValueError, 'Not a boolean: %s' % val + raise ValueError( 'Not a boolean: %s' % val) return self._boolean_states[val.lower()] class MockConf(object): @@ -409,9 +409,9 @@ def _load_config(self, a_file=None): # memorize conf file path self._configuration_file_path = a_file - except Exception, exce: - print "Can't read the config file %s" % a_file - print "Current executing from dir = %s\n" % os.getcwd() + except Exception as exce: + print("Can't read the config file %s" % a_file) + print("Current executing from dir = %s\n" % os.getcwd()) raise exce def get_conf_file_path(self): @@ -639,7 +639,7 @@ def _replace_vars(self, a_str, location, lineno= - 1): dummy = res.get_value() else: dummy = self._sections[group][self.optionxform(option)] - except KeyError, _: #IGNORE:W0612 + except KeyError as _: #IGNORE:W0612 raise exceptions.SubstitutionError(lineno, location, "Property %s[%s] "\ "doesn't exist in this configuration file \n" \ % (group, option)) @@ -682,14 +682,14 @@ def getboolean(self, section, option, default=False, fail_if_missing=False): """getboolean value""" val = self.get(section, option, default, fail_if_missing) if val.lower() not in self._boolean_states: - raise ValueError, 'Not a boolean: %s' % val + raise ValueError('Not a boolean: %s' % val) return self._boolean_states[val.lower()] def get_boolean(self, section, option, default=False, fail_if_missing=False): """get_boolean value""" val = self.get(section, option, default, fail_if_missing) if val.lower() not in self._boolean_states: - raise ValueError, 'Not a boolean: %s' % val + raise ValueError('Not a boolean: %s' % val) return self._boolean_states[val.lower()] def get_list(self, section, option, default=None, fail_if_missing=False): @@ -701,7 +701,7 @@ def get_list(self, section, option, default=None, fail_if_missing=False): try: compiler = struct_parser.Compiler() return compiler.compile_list(val) - except struct_parser.CompilerError, err: + except struct_parser.CompilerError as err: raise exceptions.Error(err.message) def getlist(self, section, option, default=None, fail_if_missing=False): @@ -722,7 +722,7 @@ def get_dict(self, section, option, default=None, fail_if_missing=False): try: compiler = struct_parser.Compiler() return compiler.compile_dict(val) - except struct_parser.CompilerError, err: + except struct_parser.CompilerError as err: raise exceptions.Error(err.message) @classmethod diff --git a/src/gmv/conf/conf_tests.py b/src/gmv/conf/conf_tests.py index e932f231..a0e1cd02 100755 --- a/src/gmv/conf/conf_tests.py +++ b/src/gmv/conf/conf_tests.py @@ -265,7 +265,7 @@ def test_dict_error(self): try: self.conf.get_dict('GroupTestValueStruct', 'dict_error') - except Exception, err: + except Exception as err: self.assertEquals(err.message, "Expression \"{1:2,'v b': a\" cannot be converted as a dict.") return @@ -277,7 +277,7 @@ def test_list_error(self): try: the_list = self.conf.get_list('GroupTestValueStruct', 'list_error') print('the_list = %s\n' % (the_list)) - except Exception, err: + except Exception as err: self.assertEquals(err.message, 'Unsupported token (type: @, value : OP) (line=1,col=3).') return diff --git a/src/gmv/conf/utils/struct_parser.py b/src/gmv/conf/utils/struct_parser.py index 6b174147..641967ac 100755 --- a/src/gmv/conf/utils/struct_parser.py +++ b/src/gmv/conf/utils/struct_parser.py @@ -18,7 +18,10 @@ ''' import tokenize import token -import StringIO +try: + from StringIO import StringIO ## for Python 2 +except ImportError: + from io import StringIO ## for Python 3 class TokenizerError(Exception): """Base class for All exceptions""" @@ -109,7 +112,7 @@ def tokenize(self, a_program, a_eatable_token_types = ()): Raises: exception TokenizerError if the syntax of the aString string is incorrect """ - g_info = tokenize.generate_tokens(StringIO.StringIO(a_program).readline) # tokenize the string + g_info = tokenize.generate_tokens(StringIO(a_program).readline) # tokenize the string for toknum, tokval, tokbeg, tokend, tokline in g_info: if token.tok_name[toknum] not in a_eatable_token_types: @@ -266,7 +269,7 @@ def compile_list(self, a_to_compile_str): try: tokenizer = Tokenizer() tokenizer.tokenize(a_to_compile_str, self._tokens_to_ignore) - except tokenize.TokenError, err: + except tokenize.TokenError as err: #translate this error into something understandable. #It is because the bloody tokenizer counts the brackets @@ -287,7 +290,7 @@ def compile_dict(self, a_to_compile_str): try: tokenizer = Tokenizer() tokenizer.tokenize(a_to_compile_str, self._tokens_to_ignore) - except tokenize.TokenError, err: + except tokenize.TokenError as err: #translate this error into something understandable. #It is because the bloody tokenizer counts the brackets @@ -423,8 +426,8 @@ def _compile_litteral(self, a_tokenizer): #check if the string is unicode if len(the_token.value) >= 3 and the_token.value[:2] == "u'": #unicode string - #dummy = unicode(the_token.value[2:-1], 'utf_8') #decode from utf-8 encoding not necessary if read full utf-8 file - dummy = unicode(the_token.value[2:-1]) + #dummy = str(the_token.value[2:-1], 'utf_8') #decode from utf-8 encoding not necessary if read full utf-8 file + dummy = str(the_token.value[2:-1]) else: #ascii string # the value contains the quote or double quotes so remove them always diff --git a/src/gmv/conf/utils/struct_parser_tests.py b/src/gmv/conf/utils/struct_parser_tests.py index 14144af7..c4ae51b1 100755 --- a/src/gmv/conf/utils/struct_parser_tests.py +++ b/src/gmv/conf/utils/struct_parser_tests.py @@ -89,7 +89,7 @@ def test_list_error(self): try: compiler.compile_list(the_string) - except CompilerError, err: + except CompilerError as err: self.assertEqual(err.message, 'Expression " a ]" cannot be converted as a list.') def test_list_unicode_val(self): @@ -126,7 +126,7 @@ def test_list_error_2(self): try: compiler.compile_list(the_string) - except CompilerError, err: + except CompilerError as err: self.assertEqual(err.message, 'Unsupported token (type: @, value : OP) (line=1,col=3).') def test_simple_dict(self): @@ -148,7 +148,7 @@ def test_dict_error(self): try: compiler.compile_dict(the_string) - except CompilerError, err: + except CompilerError as err: self.assertEqual(err.message, 'Expression "{\'a\':1, b:2 " cannot be converted as a dict.') def test_dict_with_list(self): diff --git a/src/gmv/credential_utils.py b/src/gmv/credential_utils.py index dc08c7ed..b1a1257c 100755 --- a/src/gmv/credential_utils.py +++ b/src/gmv/credential_utils.py @@ -24,7 +24,7 @@ import json import base64 import urllib #for urlencode -import urllib2 +import urllib.request #for urlencode import os import getpass @@ -74,7 +74,7 @@ def get_secret_key(cls, a_filepath): else: secret = gmvault_utils.make_password() - fdesc = os.open(a_filepath, os.O_CREAT|os.O_WRONLY, 0600) + fdesc = os.open(a_filepath, os.O_CREAT|os.O_WRONLY, 0o600) try: the_bytes = os.write(fdesc, secret) finally: @@ -92,7 +92,7 @@ def store_passwd(cls, email, passwd): """ passwd_file = '%s/%s.passwd' % (gmvault_utils.get_home_dir_path(), email) - fdesc = os.open(passwd_file, os.O_CREAT|os.O_WRONLY, 0600) + fdesc = os.open(passwd_file, os.O_CREAT|os.O_WRONLY, 0o600) cipher = blowfish.Blowfish(cls.get_secret_key(cls.SECRET_FILEPATH % (gmvault_utils.get_home_dir_path()))) cipher.initCTR() @@ -153,7 +153,7 @@ def read_oauth2_tok_sec(cls, email): try: with open(user_oauth_file_path) as oauth_file: oauth_result = json.load(oauth_file) - except Exception, _: #pylint: disable-msg=W0703 + except Exception as _: #pylint: disable-msg=W0703 LOG.critical("Cannot read oauth credentials from %s. Force oauth credentials renewal." % user_oauth_file_path) LOG.critical("=== Exception traceback ===") LOG.critical(gmvault_utils.get_exception_traceback()) @@ -261,8 +261,8 @@ def _get_oauth2_acc_tok_from_ref_tok(cls, refresh_token): request_url = '%s/%s' % (account_base_url, 'o/oauth2/token') try: - response = urllib2.urlopen(request_url, urllib.urlencode(params)).read() - except Exception, err: #pylint: disable-msg=W0703 + response = urllib.request.urlopen(request_url, urllib.urlencode(params)).read() + except Exception as err: #pylint: disable-msg=W0703 LOG.critical("Error: Problems when trying to connect to Google oauth2 endpoint: %s.\n" % (request_url)) raise err @@ -300,8 +300,8 @@ def _get_authorization_tokens(cls, authorization_code): request_url = '%s/%s' % (account_base_url, 'o/oauth2/token') try: - response = urllib2.urlopen(request_url, urllib.urlencode(params)).read() - except Exception, err: #pylint: disable-msg=W0703 + response = urllib.request.urlopen(request_url, urllib.urlencode(params)).read() + except Exception as err: #pylint: disable-msg=W0703 LOG.critical("Error: Problems when trying to connect to Google oauth2 endpoint: %s." % (request_url)) raise err @@ -325,7 +325,7 @@ def _get_oauth2_tokens(cls, email, use_webbrowser = False, debug=False): if use_webbrowser: try: webbrowser.open(str(permission_url)) - except Exception, err: #pylint: disable-msg=W0703 + except Exception as err: #pylint: disable-msg=W0703 LOG.critical("Error: %s.\n" % (err) ) LOG.critical("=== Exception traceback ===") LOG.critical(gmvault_utils.get_exception_traceback()) diff --git a/src/gmv/gmv_cmd.py b/src/gmv/gmv_cmd.py index f245aada..46746752 100755 --- a/src/gmv/gmv_cmd.py +++ b/src/gmv/gmv_cmd.py @@ -456,7 +456,7 @@ def _parse_common_args(cls, options, parser, parsed_args, list_of_types = []): # port = int(options.port) else: port = options.port - except Exception, _: #pylint:disable=W0703 + except Exception as _: #pylint:disable=W0703 parser.error("--port option %s is not a number. Please check the port value" % (port)) # add port @@ -763,7 +763,7 @@ def run(self, args): #pylint:disable=R0912 on_error = False - except KeyboardInterrupt, _: + except KeyboardInterrupt as _: LOG.critical("\nCTRL-C. Stop all operations.\n") on_error = False except socket.error: @@ -773,7 +773,7 @@ def run(self, args): #pylint:disable=R0912 LOG.critical(gmvault_utils.get_exception_traceback()) LOG.critical("=== End of Exception traceback ===\n") die_with_usage = False - except imaplib.IMAP4.error, imap_err: + except imaplib.IMAP4.error as imap_err: #bad login or password if str(imap_err) in ['[AUTHENTICATIONFAILED] Invalid credentials (Failure)', \ '[ALERT] Web login required: http://support.google.com/'\ @@ -787,7 +787,7 @@ def run(self, args): #pylint:disable=R0912 LOG.critical("=== Exception traceback ===") LOG.critical(gmvault_utils.get_exception_traceback()) LOG.critical("=== End of Exception traceback ===\n") - except Exception, err: + except Exception as err: LOG.critical("Error: %s. \n" % (err) ) LOG.critical("=== Exception traceback ===") LOG.critical(gmvault_utils.get_exception_traceback()) diff --git a/src/gmv/gmvault.py b/src/gmv/gmvault.py index c127698e..2b55cfde 100755 --- a/src/gmv/gmvault.py +++ b/src/gmv/gmvault.py @@ -99,7 +99,7 @@ def handle_sync_imap_error(the_exception, the_id, error_report, src): try: #try to get the gmail_id curr = src.fetch(the_id, imap_utils.GIMAPFetcher.GET_GMAIL_ID) - except Exception, _: #pylint:disable-msg=W0703 + except Exception as _: #pylint:disable-msg=W0703 curr = None LOG.critical("Error when trying to get gmail id for message with imap id %s." % (the_id)) LOG.critical("Disconnect, wait for 10 sec then reconnect.") @@ -135,7 +135,7 @@ def handle_sync_imap_error(the_exception, the_id, error_report, src): #try to get the gmail_id LOG.critical("One more attempt. Trying to fetch the Gmail ID for %s" % (the_id) ) curr = src.fetch(the_id, imap_utils.GIMAPFetcher.GET_GMAIL_ID) - except Exception, _: #pylint:disable-msg=W0703 + except Exception as _: #pylint:disable-msg=W0703 curr = None if curr: @@ -179,7 +179,7 @@ def individual_fetch(self, imap_ids): try: single_data = self.src.fetch(the_id, self.request) new_data.update(single_data) - except Exception, error: + except Exception as error: handle_sync_imap_error(error, the_id, self.error_report, self.src) #do everything in this handler return new_data @@ -205,7 +205,7 @@ def next(self): return new_data - except imaplib.IMAP4.error, _: + except imaplib.IMAP4.error as _: new_data = self.individual_fetch(batch) return new_data @@ -331,7 +331,7 @@ def check_email_on_disk(cls, a_gstorer, a_id, a_dir = None): if a_dir: return a_gstorer.unbury_metadata(a_id, a_dir) - except ValueError, json_error: + except ValueError as json_error: LOG.exception("Cannot read file %s. Try to fetch the data again" % ('%s.meta' % (a_id)), json_error ) return None @@ -491,7 +491,7 @@ def _common_sync(self, a_timer, a_type, imap_req, compress, restart): try: new_data[the_id][imap_utils.GIMAPFetcher.GMAIL_LABELS] = \ imap_utils.decode_labels(new_data[the_id][imap_utils.GIMAPFetcher.GMAIL_LABELS]) - except KeyError, ke: + except KeyError as ke: LOG.info("KeyError, reason: %s. new_data[%s]=%s" % (str(ke), the_id, new_data.get(the_id))) # try to fetch it individually and replace current info if it fails then raise error. id_info = None @@ -499,7 +499,7 @@ def _common_sync(self, a_timer, a_type, imap_req, compress, restart): id_info = batch_fetcher.individual_fetch(the_id) new_data[the_id][imap_utils.GIMAPFetcher.GMAIL_LABELS] = \ imap_utils.decode_labels(id_info[imap_utils.GIMAPFetcher.GMAIL_LABELS]) - except Exception, err: + except Exception as err: LOG.debug("Error when trying to fetch again information for email id %s. id_info = %s. exception:(%s)" \ % (the_id, id_info, str(err))) LOG.info("Missing labels information for email id %s. Ignore it\n" % (the_id)) @@ -543,7 +543,7 @@ def _common_sync(self, a_timer, a_type, imap_req, compress, restart): #update local index id gid => index per directory to be thought out LOG.debug("Create and store email with imap id %s, gmail id %s." % (the_id, gid)) - except Exception, error: + except Exception as error: handle_sync_imap_error(error, the_id, self.error_report, self.src) #do everything in this handler nb_msgs_processed += 1 @@ -731,7 +731,7 @@ def get_gmails_ids_left_to_sync(self, op_type, imap_ids, imap_req):#pylint:disab LOG.critical("Restart from gmail id %s (imap id %s)." % (last_id, imap_id)) new_gmail_ids = imap_ids[last_id_index:] - except Exception, _: #ignore any exception and try to get all ids in case of problems. pylint:disable=W0703 + except Exception as _: #ignore any exception and try to get all ids in case of problems. pylint:disable=W0703 #element not in keys return current set of keys LOG.critical("Error: Cannot restore from last restore gmail id. It is not in Gmail."\ " Sync the complete list of gmail ids requested from Gmail.") @@ -866,7 +866,7 @@ def get_gmails_ids_left_to_restore(self, op_type, db_gmail_ids_info): keys = db_gmail_ids_info.keys() last_id_index = keys.index(last_id) LOG.critical("Restart from gmail id %s." % last_id) - except ValueError, _: + except ValueError as _: #element not in keys return current set of keys LOG.error("Cannot restore from last restore gmail id. It is not in the disk database.") @@ -1000,7 +1000,7 @@ def restore_chats(self, extra_labels = [], restart = False): #pylint:disable=W01 for ex_label in extra_labels: labels_to_apply[ex_label] = imap_id - except Exception, err: + except Exception as err: handle_restore_imap_error(err, gm_id, db_gmail_ids_info, self) #create the non existing labels and update existing labels @@ -1015,7 +1015,7 @@ def restore_chats(self, extra_labels = [], restart = False): #pylint:disable=W01 self.src.select_folder('ALLMAIL') #go to ALL MAIL to make STORE usable for label in labels_to_apply.keys(): self.src.apply_labels_to(labels_to_apply[label], [label]) - except Exception, err: + except Exception as err: LOG.error("Problem when applying labels %s to the following ids: %s" %(label, labels_to_apply[label]), err) if isinstance(err, imap_utils.LabelError) and err.ignore() == True: LOG.critical("Ignore labelling: %s" % (err)) @@ -1141,7 +1141,7 @@ def restore_emails(self, pivot_dir = None, extra_labels = [], restart = False): for ex_label in extra_labels: labels_to_apply[ex_label] = imap_id - except Exception, err: + except Exception as err: handle_restore_imap_error(err, gm_id, db_gmail_ids_info, self) #create the non existing labels and update existing labels @@ -1159,7 +1159,7 @@ def restore_emails(self, pivot_dir = None, extra_labels = [], restart = False): LOG.debug("Changed dir. Operation time = %s ms" % (the_timer.elapsed_ms())) for label in labels_to_apply.keys(): self.src.apply_labels_to(labels_to_apply[label], [label]) - except Exception, err: + except Exception as err: LOG.error("Problem when applying labels %s to the following ids: %s" %(label, labels_to_apply[label]), err) if isinstance(err, imap_utils.LabelError) and err.ignore() == True: LOG.critical("Ignore labelling: %s" % (err)) diff --git a/src/gmv/gmvault_db.py b/src/gmv/gmvault_db.py index c06407a2..5d9d2e48 100755 --- a/src/gmv/gmvault_db.py +++ b/src/gmv/gmvault_db.py @@ -25,7 +25,10 @@ import fnmatch import shutil import codecs -import StringIO +try: + from StringIO import StringIO ## for Python 2 +except ImportError: + from io import StringIO ## for Python 3 import gmv.blowfish as blowfish import gmv.log_utils as log_utils @@ -264,10 +267,10 @@ def parse_header_fields(cls, header_fields): try: encod = gmvault_utils.guess_encoding(tempo, use_encoding_list = False) u_tempo = unicode(tempo, encoding = encod) - except gmvault_utils.GuessEncoding, enc_err: + except gmvault_utils.GuessEncoding as enc_err: #it is already in unicode so ignore encoding u_tempo = tempo - except Exception, e: + except Exception as e: LOG.critical(e) LOG.critical("Warning: Guessed encoding = (%s). Ignore those characters" % (encod)) #try utf-8 diff --git a/src/gmv/gmvault_utils.py b/src/gmv/gmvault_utils.py index 9503f98a..e057a798 100755 --- a/src/gmv/gmvault_utils.py +++ b/src/gmv/gmvault_utils.py @@ -26,7 +26,11 @@ import fnmatch import functools -import StringIO +try: + from StringIO import StringIO ## for Python 2 +except ImportError: + from io import StringIO ## for Python 3 + import sys import traceback import random @@ -53,11 +57,11 @@ def __init__(self, func): def __call__(self, *args): try: return self.cache[args] - except KeyError: + except KeyError as e: value = self.func(*args) self.cache[args] = value return value - except TypeError: + except TypeError as e: # uncachable -- for instance, passing a list as an argument. # Better to not cache than to blow up entirely. return self.func(*args) @@ -115,7 +119,7 @@ def get_exception_traceback(): """ - the_file = StringIO.StringIO() + the_file = StringIO() exception_type, exception_value, exception_traceback = sys.exc_info() #IGNORE:W0702 traceback.print_exception(exception_type, exception_value, exception_traceback, file = the_file) return the_file.getvalue() @@ -409,9 +413,9 @@ def __rmgeneric(path, __func__): """ private function that is part of delete_all_under """ try: __func__(path) - #print 'Removed ', path - except OSError, (_, strerror): #IGNORE:W0612 - print """Error removing %(path)s, %(error)s """ % {'path' : path, 'error': strerror } + #print('Removed ', path) + except OSError as strerror: #IGNORE:W0612 + print("""Error removing %(path)s, %(error)s """ % {'path' : path, 'error': strerror }) def delete_all_under(path, delete_top_dir = False): """ delete all files and directories under path """ @@ -549,7 +553,7 @@ def convert_to_unicode(a_str): LOG.debug("Convert to %s" % (encoding)) u_str = unicode(a_str, encoding = encoding) #convert to unicode with given encoding - except Exception, e: + except Exception as e: LOG.debug("Exception: %s" % (e)) LOG.info("Warning: Guessed encoding = (%s). Ignore those characters" % (encoding if encoding else "Not defined")) #try utf-8 @@ -586,7 +590,7 @@ def convert_argv_to_unicode(a_str): u_str = a_str.decode(terminal_encoding) LOG.debug("unicode_escape val = %s." % (u_str.encode('unicode_escape'))) LOG.debug("raw unicode = %s." % (u_str)) - except Exception, err: + except Exception as err: LOG.error(err) get_exception_traceback() LOG.info("Convertion of %s from %s to a unicode failed. Will now convert to unicode using utf-8 encoding and ignoring errors (non utf-8 characters will be eaten)." % (a_str, terminal_encoding)) @@ -667,7 +671,7 @@ def _create_default_conf_file(home_conf_file): with open(home_conf_file, "w+") as f: f.write(gmvault_const.DEFAULT_CONF_FILE) return home_conf_file - except Exception, err: + except Exception as err: #catch all error and let run gmvault with defaults if needed LOG.critical("Ignore Error when trying to create conf file for defaults in %s:\n%s.\n" % (get_home_dir_path(), err)) LOG.debug("=== Exception traceback ===") diff --git a/src/gmv/imap_utils.py b/src/gmv/imap_utils.py index f30be16c..10f2d620 100755 --- a/src/gmv/imap_utils.py +++ b/src/gmv/imap_utils.py @@ -116,7 +116,7 @@ def reconnect(the_self, rec_nb_tries, total_nb_tries, rec_error, rec_sleep_time return - except Exception, ignored: + except Exception as ignored: # catch all errors and try as long as we have tries left LOG.exception(ignored) else: @@ -130,7 +130,7 @@ def wrapper(*args, **kwargs): #pylint:disable=C0111,R0912 while True: try: return the_func(*args, **kwargs) - except PushEmailError, p_err: + except PushEmailError as p_err: LOG.debug("error message = %s. traceback:%s" % (p_err, gmvault_utils.get_exception_traceback())) @@ -141,7 +141,7 @@ def wrapper(*args, **kwargs): #pylint:disable=C0111,R0912 reconnect(args[0], nb_tries, a_nb_tries, p_err, m_sleep_time) - except imaplib.IMAP4.abort, err: #abort is recoverable and error is not + except imaplib.IMAP4.abort as err: #abort is recoverable and error is not LOG.debug("IMAP (abort) error message = %s. traceback:%s" % (err, gmvault_utils.get_exception_traceback())) @@ -153,7 +153,7 @@ def wrapper(*args, **kwargs): #pylint:disable=C0111,R0912 # problem with this email, put it in quarantine reconnect(args[0], nb_tries, a_nb_tries, err, m_sleep_time) - except socket.error, sock_err: + except socket.error as sock_err: LOG.debug("error message = %s. traceback:%s" % (sock_err, gmvault_utils.get_exception_traceback())) if nb_tries[0] < a_nb_tries: @@ -163,7 +163,7 @@ def wrapper(*args, **kwargs): #pylint:disable=C0111,R0912 reconnect(args[0], nb_tries, a_nb_tries, sock_err, m_sleep_time) - except ssl.SSLError, ssl_err: + except ssl.SSLError as ssl_err: LOG.debug("error message = %s. traceback:%s" % (ssl_err, gmvault_utils.get_exception_traceback())) if nb_tries[0] < a_nb_tries: @@ -173,7 +173,7 @@ def wrapper(*args, **kwargs): #pylint:disable=C0111,R0912 reconnect(args[0], nb_tries, a_nb_tries, sock_err, m_sleep_time) - except imaplib.IMAP4.error, err: + except imaplib.IMAP4.error as err: #just trace it back for the moment LOG.debug("IMAP (normal) error message = %s. traceback:%s" % (err, gmvault_utils.get_exception_traceback())) @@ -196,11 +196,11 @@ class GIMAPFetcher(object): #pylint:disable=R0902,R0904 ''' IMAP Class reading the information ''' - GMAIL_EXTENSION = 'X-GM-EXT-1' # GMAIL capability - GMAIL_ALL = u'[Gmail]/All Mail' #GMAIL All Mail mailbox + GMAIL_EXTENSION = b'X-GM-EXT-1' # GMAIL capability + GMAIL_ALL = '[Gmail]/All Mail' #GMAIL All Mail mailbox - GENERIC_GMAIL_ALL = u'\\AllMail' # unlocalised GMAIL ALL - GENERIC_DRAFTS = u'\\Drafts' # unlocalised DRAFTS + GENERIC_GMAIL_ALL = '\\AllMail' # unlocalised GMAIL ALL + GENERIC_DRAFTS = '\\Drafts' # unlocalised DRAFTS GENERIC_GMAIL_CHATS = gmvault_const.GMAIL_UNLOCAL_CHATS # unlocalised Chats names FOLDER_NAMES = ['ALLMAIL', 'CHATS', 'DRAFTS'] @@ -291,6 +291,9 @@ def connect(self, go_to_current_folder = False): # connect with password or xoauth if self.credential['type'] == 'passwd': self.server.login(self.login, self.credential['value']) + print(self.server) + print(dir(self.server)) + print(self.server.list_folders()) elif self.credential['type'] == 'oauth2': #connect with oauth2 if self.once_connected: @@ -329,7 +332,7 @@ def disconnect(self): if self.server: try: self.server.logout() - except Exception, ignored: #ignored exception but still log it in log file if activated + except Exception as ignored: #ignored exception but still log it in log file if activated LOG.exception(ignored) self.server = None @@ -612,7 +615,7 @@ def create_gmail_labels(self, labels, existing_folders): raise Exception("Cannot create label %s: the directory %s cannot be created." % (lab, directory)) else: LOG.debug("============== ####### Created Labels (%s)." % (directory)) - except imaplib.IMAP4.error, error: + except imaplib.IMAP4.error as error: #log error in log file if it exists LOG.debug(gmvault_utils.get_exception_traceback()) if str(error).startswith("create failed: '[ALREADYEXISTS] Duplicate folder"): @@ -652,7 +655,7 @@ def apply_labels_to(self, imap_ids, labels): #+X-GM-LABELS.SILENT to have not returned data try: ret_code, data = self.server._imap.uid('STORE', id_list, '+X-GM-LABELS.SILENT', labels_str) #pylint: disable=W0212 - except imaplib.IMAP4.error, original_err: + except imaplib.IMAP4.error as original_err: LOG.info("Error in apply_labels_to. See exception traceback") LOG.debug(gmvault_utils.get_exception_traceback()) # try to add labels to each individual ids @@ -660,7 +663,7 @@ def apply_labels_to(self, imap_ids, labels): for the_id in imap_ids: try: ret_code, data = self.server._imap.uid('STORE', the_id, '+X-GM-LABELS.SILENT', labels_str) #pylint: disable=W0212 - except imaplib.IMAP4.error, store_err: + except imaplib.IMAP4.error as store_err: LOG.debug("Error when trying to apply labels %s to emails with imap_id %s. Error:%s" % (labels_str, the_id, store_err)) faulty_ids.append(the_id) @@ -678,7 +681,7 @@ def apply_labels_to(self, imap_ids, labels): for the_id in imap_ids: try: ret_code, data = self.server._imap.uid('STORE', the_id, '+X-GM-LABELS.SILENT', labels_str) #pylint: disable=W0212 - except imaplib.IMAP4.error, store_err: + except imaplib.IMAP4.error as store_err: LOG.debug("Error when trying to apply labels %s to emails with imap_id %s. Error:%s" % (labels_str, the_id, store_err)) faulty_ids.append(the_id) @@ -700,7 +703,7 @@ def delete_gmail_labels(self, labels, force_delete = False): and self.server.folder_exists(directory) ): #call server exists each time try: self.server.delete_folder(directory) - except imaplib.IMAP4.error, _: + except imaplib.IMAP4.error as _: LOG.debug(gmvault_utils.get_exception_traceback()) @@ -800,7 +803,7 @@ def push_data(self, a_folder, a_body, a_flags, a_internal_time): try: #a_body = self._clean_email_body(a_body) res = self.server.append(a_folder, a_body, a_flags, a_internal_time) - except imaplib.IMAP4.abort, err: + except imaplib.IMAP4.abort as err: # handle issue when there are invalid characters (This is do to the presence of null characters) if str(err).find("APPEND => Invalid character in literal") >= 0: LOG.critical("Invalid character detected. Try to clean the email and reconnect.") @@ -848,7 +851,7 @@ def deprecated_push_email(self, a_body, a_flags, a_internal_time, a_labels): try: res = self.server.append(u'[Google Mail]/All Mail', a_body, a_flags, a_internal_time) - except imaplib.IMAP4.abort, err: + except imaplib.IMAP4.abort as err: # handle issue when there are invalid characters (This is do to the presence of null characters) if str(err).find("APPEND => Invalid character in literal") >= 0: a_body = self._clean_email_body(a_body) diff --git a/src/gmv/mod_imap.py b/src/gmv/mod_imap.py index edbcb160..d181a940 100755 --- a/src/gmv/mod_imap.py +++ b/src/gmv/mod_imap.py @@ -24,7 +24,7 @@ import re import socket import ssl -import cStringIO +from io import StringIO import os import imaplib #for the exception @@ -154,12 +154,12 @@ def new_read(self, size): Call _intern_read that takes care of the compression """ - chunks = cStringIO.StringIO() #use cStringIO.cStringIO to avoir too much fragmentation + chunks = StringIO() #use cStringIO.cStringIO to avoir too much fragmentation read = 0 while read < size: try: data = self._intern_read(min(size-read, 16384)) #never ask more than 16384 because imaplib can do it - except ssl.SSLError, err: + except ssl.SSLError as err: print("************* SSLError received %s" % (err)) raise self.abort('Gmvault ssl socket error: EOF. Connection lost, reconnect.') read += len(data) @@ -173,7 +173,7 @@ def read(self, size): Call _intern_read that takes care of the compression """ - chunks = cStringIO.StringIO() #use cStringIO.cStringIO to avoir too much fragmentation + chunks = StringIO() #use cStringIO.cStringIO to avoir too much fragmentation read = 0 while read < size: data = self._intern_read(min(size-read, 16384)) #never ask more than 16384 because imaplib can do it @@ -202,7 +202,7 @@ def _intern_read(self, size): def readline(self): """Read line from remote.""" - line = cStringIO.StringIO() #use cStringIO to avoid memory fragmentation + line = StringIO() #use cStringIO to avoid memory fragmentation while 1: #make use of read that takes care of the compression #it could be simplified without compression diff --git a/src/gmv/test_utils.py b/src/gmv/test_utils.py index 274579e8..1655f5fc 100755 --- a/src/gmv/test_utils.py +++ b/src/gmv/test_utils.py @@ -435,7 +435,7 @@ def get_oauth_cred(email, cred_path): if len(oauth_result) == 2: token = oauth_result[0] secret = oauth_result[1] - except Exception, _: #pylint: disable-msg=W0703 + except Exception as _: #pylint: disable-msg=W0703 print("Cannot read oauth credentials from %s. Force oauth credentials renewal." % user_oauth_file_path) print("=== Exception traceback ===") print(gmvault_utils.get_exception_traceback()) diff --git a/src/gmv_cmd_tests.py b/src/gmv_cmd_tests.py index 358d566c..2e91a2f3 100755 --- a/src/gmv_cmd_tests.py +++ b/src/gmv_cmd_tests.py @@ -250,7 +250,7 @@ def zztest_cli_bad_server(self): gmvaulter.run(args) - except SystemExit, _: + except SystemExit as _: print("In Error success") def ztest_cli_bad_passwd(self): @@ -270,7 +270,7 @@ def ztest_cli_bad_passwd(self): gmvaulter.run(args) - except SystemExit, err: + except SystemExit as err: print("In Error success") def ztest_cli_bad_login(self): @@ -290,7 +290,7 @@ def ztest_cli_bad_login(self): gmvaulter.run(args) - except SystemExit, err: + except SystemExit as err: print("In Error success") @@ -307,10 +307,10 @@ def zztest_cli_host_error(self): try: _ = gmvaulter.parse_args() - except SystemExit, err: + except SystemExit as err: self.assertEquals(type(err), type(SystemExit())) self.assertEquals(err.code, 2) - except Exception, err: + except Exception as err: self.fail('unexpected exception: %s' % err) else: self.fail('SystemExit exception expected') @@ -339,9 +339,9 @@ def zztest_cli_(self): self.assertEquals(args['port'], 993) self.assertEquals(args['db-dir'],'./gmvault-db') - except SystemExit, err: + except SystemExit as err: self.fail("SystemExit Exception: %s" % err) - except Exception, err: + except Exception as err: self.fail('unexpected exception: %s' % err) def ztest_full_sync_gmv(self): diff --git a/src/gmvault_tests.py b/src/gmvault_tests.py index dc205179..ecf3ed8a 100755 --- a/src/gmvault_tests.py +++ b/src/gmvault_tests.py @@ -82,7 +82,7 @@ def ztest_gmvault_connect_error(self): try: gimap.connect() - except ssl.SSLError, err: + except ssl.SSLError as err: msg = str(err) @@ -285,7 +285,7 @@ def ztest_retrieve_gmail_ids(self): res = gimap.fetch(ids, [gimap.GMAIL_ID]) - self.assertEquals(res, {27362: {'X-GM-MSGID': 1147537963432096749L, 'SEQ': 14535}, 27363: {'X-GM-MSGID': 1147537994018957026L, 'SEQ': 14536}}) + self.assertEquals(res, {27362: {'X-GM-MSGID': 1147537963432096749, 'SEQ': 14535}, 27363: {'X-GM-MSGID': 1147537994018957026, 'SEQ': 14536}}) def ztest_retrieve_all_params(self): """ @@ -304,7 +304,7 @@ def ztest_retrieve_all_params(self): res = gimap.fetch(ids[0], [gimap.GMAIL_ID, gimap.EMAIL_BODY, gimap.GMAIL_THREAD_ID, gimap.GMAIL_LABELS]) - self.assertEquals(res[ids[0]][gimap.GMAIL_ID], 1147537963432096749L) + self.assertEquals(res[ids[0]][gimap.GMAIL_ID], 1147537963432096749) self.assertEquals(res[ids[0]][gimap.EMAIL_BODY], \ 'Message-ID: <6999505.1094377483218.JavaMail.wwwadm@chewbacca.ecmwf.int>\r\nDate: Sun, 5 Sep 2004 09:44:43 +0000 (GMT)\r\nFrom: Guillaume.Aubert@ecmwf.int\r\nReply-To: Guillaume.Aubert@ecmwf.int\r\nTo: aubert_guillaume@yahoo.fr\r\nSubject: Fwd: [Flickr] Guillaume Aubert wants you to see their photos\r\nMime-Version: 1.0\r\nContent-Type: text/plain; charset=us-ascii\r\nContent-Transfer-Encoding: 7bit\r\nX-Mailer: jwma\r\nStatus: RO\r\nX-Status: \r\nX-Keywords: \r\nX-UID: 1\r\n\r\n\r\n') #pylint:disable-msg=C0301 @@ -681,7 +681,7 @@ def ztest_fix_bug(self): credential = { 'type' : 'passwd', 'value': self.passwd} syncer = gmvault.GMVaulter(db_dir, 'imap.gmail.com', 993, self.login, credential, 'verySecRetKeY') - syncer._create_update_sync([142221L], compress = True) + syncer._create_update_sync([142221], compress = True) def test_check_flags(self): """ diff --git a/src/sandbox/chardet_test.py b/src/sandbox/chardet_test.py index 1b4bd964..55fab1a7 100644 --- a/src/sandbox/chardet_test.py +++ b/src/sandbox/chardet_test.py @@ -7,17 +7,17 @@ first_arg = sys.argv[1] #first_arg="réception" #first_arg="て感じでしょうか研" -print first_arg +print(first_arg) print("chardet = %s\n" % chardet.detect(first_arg)) res_char = chardet.detect(first_arg) -print type(first_arg) +print(type(first_arg)) first_arg_unicode = first_arg.decode(res_char['encoding']) -print first_arg_unicode -print type(first_arg_unicode) +print(first_arg_unicode) +print(type(first_arg_unicode)) utf8_arg = first_arg_unicode.encode("utf-8") -print type(utf8_arg) -print utf8_arg \ No newline at end of file +print(type(utf8_arg)) +print(utf8_arg) diff --git a/src/sandbox/pycrypto_test.py b/src/sandbox/pycrypto_test.py index bbf04eaf..bccedf1b 100644 --- a/src/sandbox/pycrypto_test.py +++ b/src/sandbox/pycrypto_test.py @@ -38,8 +38,8 @@ def _unpad(s): key="This is my key" enc = AESEncryptor(key) for secret in secrets: - print "Secret:", secret + print("Secret:", secret) encrypted = enc.encrypt(secret) - print "Encrypted secret:", encrypted - print "Clear Secret:" , enc.decrypt(encrypted) - print '-' *50 + print("Encrypted secret:", encrypted) + print("Clear Secret:" , enc.decrypt(encrypted)) + print('-' *50) diff --git a/src/sandbox/unicode_test.py b/src/sandbox/unicode_test.py index 22e64bc4..2cba93e8 100644 --- a/src/sandbox/unicode_test.py +++ b/src/sandbox/unicode_test.py @@ -12,12 +12,12 @@ def convert_to_utf8(a_str): """ """ if type(a_str) != type(u'a'): - #import chardet - #char_enc = chardet.detect(a_str) - #print("detected encoding = %s" % (char_enc)) - #print("system machine encoding = %s" % (sys.getdefaultencoding())) - #u_str = unicode(a_str, char_enc['encoding'], errors='ignore') - u_str = unicode(a_str, 'cp437', errors='ignore') + #import chardet + #char_enc = chardet.detect(a_str) + #print("detected encoding = %s" % (char_enc)) + #print("system machine encoding = %s" % (sys.getdefaultencoding())) + #u_str = unicode(a_str, char_enc['encoding'], errors='ignore') + u_str = unicode(a_str, 'cp437', errors='ignore') else: print("Already unicode do not convert") u_str = a_str