Skip to content

Commit 519c998

Browse files
author
cclauss
committed
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.
1 parent f234953 commit 519c998

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

mail_export.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
The script needs write permissions in /var/local to save the ID of the
1313
most recently mailed link. This ID is saved independently per user and tag.
1414
"""
15+
from __future__ import print_function
1516

1617
import os
1718
import re
@@ -64,7 +65,7 @@ def get_links(self):
6465
try:
6566
posts = json.loads(posts)
6667
except ValueError:
67-
print posts
68+
print(posts)
6869
return []
6970
return [
7071
p for p in posts['posts']
@@ -94,7 +95,7 @@ def run(self, options):
9495
self.latest = max(int(l['id']) for l in links) if not options.dry_run else None
9596

9697
if not self.recipients and not options.full:
97-
print body
98+
print(body)
9899
return
99100

100101
msg = MIMEText(body.encode('utf-8'))
@@ -105,7 +106,7 @@ def run(self, options):
105106
msg['To'] = ', '.join(self.recipients)
106107

107108
if options.full:
108-
print msg.as_string()
109+
print(msg.as_string())
109110
return
110111

111112
smtp = smtplib.SMTP(SMTP_SERVER)

oauth.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
from __future__ import print_function
12
import sys
23
import urlparse
34
import oauth2 as oauth
45
import urllib
56

7+
try:
8+
raw_input # Python 2
9+
except NameError:
10+
raw_input = input # Python 3
11+
612
consumer_key = sys.argv[1]
713
consumer_secret = sys.argv[2]
814

@@ -25,19 +31,19 @@
2531

2632
request_token = dict(urlparse.parse_qsl(content))
2733

28-
print "Request Token:"
29-
print " - oauth_token = %s" % request_token['oauth_token']
30-
print " - oauth_token_secret = %s\n" % request_token['oauth_token_secret']
34+
print("Request Token:")
35+
print(" - oauth_token = %s" % request_token['oauth_token'])
36+
print(" - oauth_token_secret = %s\n" % request_token['oauth_token_secret'])
3137

3238
# Step 2: Redirect to the provider. Since this is a CLI script we do not
3339
# redirect. In a web application you would redirect the user to the URL
3440
# below.
3541

36-
print "Go to the following link in your browser:"
37-
print "%s?%s\n" % (authorize_url, urllib.urlencode({
42+
print("Go to the following link in your browser:")
43+
print("%s?%s\n" % (authorize_url, urllib.urlencode({
3844
"oauth_token": request_token['oauth_token'],
3945
"oauth_callback": 'http://localhost/doctorstrange'
40-
}))
46+
})))
4147

4248
# After the user has granted access to you, the consumer, the provider will
4349
# redirect you to whatever URL you have told them to redirect to. You can
@@ -61,8 +67,8 @@
6167
resp, content = client.request(access_token_url, "POST")
6268
access_token = dict(urlparse.parse_qsl(content))
6369

64-
print "Access Token:"
65-
print " - oauth_token = %s" % access_token['oauth_token']
66-
print " - oauth_token_secret = %s" % access_token['oauth_token_secret']
67-
print
68-
print "You may now access protected resources using the access tokens above.\n"
70+
print("Access Token:")
71+
print(" - oauth_token = %s" % access_token['oauth_token'])
72+
print(" - oauth_token_secret = %s" % access_token['oauth_token_secret'])
73+
print()
74+
print("You may now access protected resources using the access tokens above.\n")

tumble.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- oauth2 (http://pypi.python.org/pypi/oauth2/)
3030
- httplib2 (http://pypi.python.org/pypi/httplib2/)
3131
"""
32+
from __future__ import print_function
3233

3334
import sys
3435
import os
@@ -44,6 +45,11 @@
4445
import oauth2 as oauth
4546
import feedparser
4647

48+
try:
49+
unicode # Python 2
50+
except NameError:
51+
unicode = str # Python 3
52+
4753
URL_FMT = 'http://api.tumblr.com/v2/blog/%s/post'
4854
CONFIG = '~/.config/tumblr'
4955

@@ -117,7 +123,7 @@ def post(self, entry):
117123
return dict(url=url, entry=entry, data=data)
118124

119125
for k in data:
120-
if type(data[k]) is unicode:
126+
if isinstance(data[k], unicode):
121127
data[k] = data[k].encode('utf-8')
122128

123129
# do the OAuth thing
@@ -127,9 +133,9 @@ def post(self, entry):
127133
try:
128134
headers, resp = client.request(url, method='POST', body=urllib.urlencode(data))
129135
resp = json.loads(resp)
130-
except ValueError, e:
136+
except ValueError as e:
131137
return 'error', 'json', resp
132-
except EnvironmentError, e:
138+
except EnvironmentError as e:
133139
return 'error', str(e)
134140
if resp['meta']['status'] in (200, 201):
135141
return op, str(resp['response']['id'])
@@ -141,12 +147,12 @@ def post(self, entry):
141147
try:
142148
opts, args = getopt.getopt(sys.argv[1:], 'hb:c:e:d')
143149
except getopt.GetoptError:
144-
print "Usage: %s [-b blog-name] [-c cred-file] [-e post-id] [-d]" % \
145-
sys.argv[0].split(os.sep)[-1]
150+
print("Usage: %s [-b blog-name] [-c cred-file] [-e post-id] [-d]" % \
151+
sys.argv[0].split(os.sep)[-1])
146152
sys.exit(1)
147153
for o, v in opts:
148154
if o == '-h':
149-
print __doc__.strip()
155+
print(__doc__.strip())
150156
sys.exit(0)
151157
if o == '-b':
152158
t.blog = v

tumblr_backup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
except ImportError:
4545
youtube_dl = None
4646

47+
try:
48+
long # Python 2
49+
except NameError:
50+
long = int # Python 3
51+
4752
# Format of displayed tags
4853
TAG_FMT = '#%s'
4954

0 commit comments

Comments
 (0)