Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize Python 2 code to get ready for Python 3 #124

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions mail_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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'))
Expand All @@ -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)
Expand Down
28 changes: 17 additions & 11 deletions oauth.py
Original file line number Diff line number Diff line change
@@ -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]

Expand All @@ -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
Expand All @@ -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")
18 changes: 12 additions & 6 deletions tumble.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'

Expand Down Expand Up @@ -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
Expand All @@ -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'])
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tumblr_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down