From 519c998f80463cb258fffcfef3de8e999353ef5f Mon Sep 17 00:00:00 2001 From: cclauss Date: Tue, 4 Dec 2018 23:53:44 +0100 Subject: [PATCH] Modernize Python 2 code to get ready for Python 3 * Use __print()__ function in both Python 2 and Python 3 * __print()__ is a function in Python 3. * Old style exceptions --> new style for Python 3 * Python 3 treats old style exceptions as syntax errors but new style exceptions work as expected in both Python 2 and Python 3. --- mail_export.py | 7 ++++--- oauth.py | 28 +++++++++++++++++----------- tumble.py | 18 ++++++++++++------ tumblr_backup.py | 5 +++++ 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/mail_export.py b/mail_export.py index 10f2f90..89e8e14 100755 --- a/mail_export.py +++ b/mail_export.py @@ -12,6 +12,7 @@ The script needs write permissions in /var/local to save the ID of the most recently mailed link. This ID is saved independently per user and tag. """ +from __future__ import print_function import os import re @@ -64,7 +65,7 @@ def get_links(self): try: posts = json.loads(posts) except ValueError: - print posts + print(posts) return [] return [ p for p in posts['posts'] @@ -94,7 +95,7 @@ def run(self, options): self.latest = max(int(l['id']) for l in links) if not options.dry_run else None if not self.recipients and not options.full: - print body + print(body) return msg = MIMEText(body.encode('utf-8')) @@ -105,7 +106,7 @@ def run(self, options): msg['To'] = ', '.join(self.recipients) if options.full: - print msg.as_string() + print(msg.as_string()) return smtp = smtplib.SMTP(SMTP_SERVER) diff --git a/oauth.py b/oauth.py index 7cabc29..625326c 100644 --- a/oauth.py +++ b/oauth.py @@ -1,8 +1,14 @@ +from __future__ import print_function import sys import urlparse import oauth2 as oauth import urllib +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + consumer_key = sys.argv[1] consumer_secret = sys.argv[2] @@ -25,19 +31,19 @@ request_token = dict(urlparse.parse_qsl(content)) -print "Request Token:" -print " - oauth_token = %s" % request_token['oauth_token'] -print " - oauth_token_secret = %s\n" % request_token['oauth_token_secret'] +print("Request Token:") +print(" - oauth_token = %s" % request_token['oauth_token']) +print(" - oauth_token_secret = %s\n" % request_token['oauth_token_secret']) # Step 2: Redirect to the provider. Since this is a CLI script we do not # redirect. In a web application you would redirect the user to the URL # below. -print "Go to the following link in your browser:" -print "%s?%s\n" % (authorize_url, urllib.urlencode({ +print("Go to the following link in your browser:") +print("%s?%s\n" % (authorize_url, urllib.urlencode({ "oauth_token": request_token['oauth_token'], "oauth_callback": 'http://localhost/doctorstrange' -})) +}))) # After the user has granted access to you, the consumer, the provider will # redirect you to whatever URL you have told them to redirect to. You can @@ -61,8 +67,8 @@ resp, content = client.request(access_token_url, "POST") access_token = dict(urlparse.parse_qsl(content)) -print "Access Token:" -print " - oauth_token = %s" % access_token['oauth_token'] -print " - oauth_token_secret = %s" % access_token['oauth_token_secret'] -print -print "You may now access protected resources using the access tokens above.\n" +print("Access Token:") +print(" - oauth_token = %s" % access_token['oauth_token']) +print(" - oauth_token_secret = %s" % access_token['oauth_token_secret']) +print() +print("You may now access protected resources using the access tokens above.\n") diff --git a/tumble.py b/tumble.py index 16b0854..1ed4041 100755 --- a/tumble.py +++ b/tumble.py @@ -29,6 +29,7 @@ - oauth2 (http://pypi.python.org/pypi/oauth2/) - httplib2 (http://pypi.python.org/pypi/httplib2/) """ +from __future__ import print_function import sys import os @@ -44,6 +45,11 @@ import oauth2 as oauth import feedparser +try: + unicode # Python 2 +except NameError: + unicode = str # Python 3 + URL_FMT = 'http://api.tumblr.com/v2/blog/%s/post' CONFIG = '~/.config/tumblr' @@ -117,7 +123,7 @@ def post(self, entry): return dict(url=url, entry=entry, data=data) for k in data: - if type(data[k]) is unicode: + if isinstance(data[k], unicode): data[k] = data[k].encode('utf-8') # do the OAuth thing @@ -127,9 +133,9 @@ def post(self, entry): try: headers, resp = client.request(url, method='POST', body=urllib.urlencode(data)) resp = json.loads(resp) - except ValueError, e: + except ValueError as e: return 'error', 'json', resp - except EnvironmentError, e: + except EnvironmentError as e: return 'error', str(e) if resp['meta']['status'] in (200, 201): return op, str(resp['response']['id']) @@ -141,12 +147,12 @@ def post(self, entry): try: opts, args = getopt.getopt(sys.argv[1:], 'hb:c:e:d') except getopt.GetoptError: - print "Usage: %s [-b blog-name] [-c cred-file] [-e post-id] [-d]" % \ - sys.argv[0].split(os.sep)[-1] + print("Usage: %s [-b blog-name] [-c cred-file] [-e post-id] [-d]" % \ + sys.argv[0].split(os.sep)[-1]) sys.exit(1) for o, v in opts: if o == '-h': - print __doc__.strip() + print(__doc__.strip()) sys.exit(0) if o == '-b': t.blog = v diff --git a/tumblr_backup.py b/tumblr_backup.py index 61547b3..66b8ac4 100755 --- a/tumblr_backup.py +++ b/tumblr_backup.py @@ -44,6 +44,11 @@ except ImportError: youtube_dl = None +try: + long # Python 2 +except NameError: + long = int # Python 3 + # Format of displayed tags TAG_FMT = '#%s'