Skip to content

Commit 1b6ceff

Browse files
author
Jeppe Toustrup
committed
Add support for Python 3
- Import print_function from __future__ and change all print statements to function calls. - Handle different import paths to urllib.urlretrieve/urllib.request.urlretrieve in setup.py
1 parent 5dc4d26 commit 1b6ceff

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

Diff for: samples/amazon_kclpy_helper.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
express or implied. See the License for the specific language governing
1414
permissions and limitations under the License.
1515
'''
16+
from __future__ import print_function
1617
from amazon_kclpy import kcl
1718
from glob import glob
1819
import os, argparse, sys, samples
@@ -140,11 +141,11 @@ def get_kcl_app_command(java, multi_lang_daemon_class, properties, paths=[]):
140141

141142
# Print what the asked for
142143
if args.print_classpath:
143-
print get_kcl_classpath(args.properties, args.paths)
144+
print(get_kcl_classpath(args.properties, args.paths))
144145
elif args.print_command:
145146
if args.java and args.properties:
146147
multi_lang_daemon_class = 'com.amazonaws.services.kinesis.multilang.MultiLangDaemon'
147-
print get_kcl_app_command(args.java, multi_lang_daemon_class, args.properties, paths=args.paths)
148+
print(get_kcl_app_command(args.java, multi_lang_daemon_class, args.properties, paths=args.paths))
148149
else:
149150
sys.stderr.write("Must provide arguments: --java and --properties\n")
150151
parser.print_usage()

Diff for: samples/sample_kclpy_app.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
express or implied. See the License for the specific language governing
1414
permissions and limitations under the License.
1515
'''
16+
from __future__ import print_function
1617
import sys, time, json, base64
1718
from amazon_kclpy import kcl
1819

@@ -59,7 +60,7 @@ def checkpoint(self, checkpointer, sequence_number=None):
5960
A ShutdownException indicates that this record processor should be shutdown. This is due to
6061
some failover event, e.g. another MultiLangDaemon has taken the lease for this shard.
6162
'''
62-
print 'Encountered shutdown execption, skipping checkpoint'
63+
print('Encountered shutdown execption, skipping checkpoint')
6364
return
6465
elif 'ThrottlingException' == e.value:
6566
'''
@@ -70,7 +71,7 @@ def checkpoint(self, checkpointer, sequence_number=None):
7071
sys.stderr.write('Failed to checkpoint after {n} attempts, giving up.\n'.format(n=n))
7172
return
7273
else:
73-
print 'Was throttled while checkpointing, will attempt again in {s} seconds'.format(s=self.SLEEP_SECONDS)
74+
print('Was throttled while checkpointing, will attempt again in {s} seconds'.format(s=self.SLEEP_SECONDS))
7475
elif 'InvalidStateException' == e.value:
7576
sys.stderr.write('MultiLangDaemon reported an invalid state while checkpointing.\n')
7677
else: # Some other error
@@ -146,10 +147,10 @@ def shutdown(self, checkpointer, reason):
146147
# Checkpointing with no parameter will checkpoint at the
147148
# largest sequence number reached by this processor on this
148149
# shard id
149-
print 'Was told to terminate, will attempt to checkpoint.'
150+
print('Was told to terminate, will attempt to checkpoint.')
150151
self.checkpoint(checkpointer, None)
151152
else: # reason == 'ZOMBIE'
152-
print 'Shutting down due to failover. Will not checkpoint.'
153+
print('Shutting down due to failover. Will not checkpoint.')
153154
except:
154155
pass
155156

Diff for: samples/sample_kinesis_wordputter.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
express or implied. See the License for the specific language governing
1414
permissions and limitations under the License.
1515
'''
16+
from __future__ import print_function
1617
import sys, random, time, argparse
1718
from boto import kinesis
1819

@@ -46,10 +47,10 @@ def wait_for_stream(conn, stream_name):
4647
SLEEP_TIME_SECONDS = 3
4748
status = get_stream_status(conn, stream_name)
4849
while status != 'ACTIVE':
49-
print '{stream_name} has status: {status}, sleeping for {secs} seconds'.format(
50+
print('{stream_name} has status: {status}, sleeping for {secs} seconds'.format(
5051
stream_name = stream_name,
5152
status = status,
52-
secs = SLEEP_TIME_SECONDS)
53+
secs = SLEEP_TIME_SECONDS))
5354
time.sleep(SLEEP_TIME_SECONDS) # sleep for 3 seconds
5455
status = get_stream_status(conn, stream_name)
5556

@@ -69,7 +70,7 @@ def put_words_in_stream(conn, stream_name, words):
6970
for w in words:
7071
try:
7172
conn.put_record(stream_name, w, w)
72-
print "Put word: " + w + " into stream: " + stream_name
73+
print("Put word: " + w + " into stream: " + stream_name)
7374
except Exception as e:
7475
sys.stderr.write("Encountered an exception while trying to put a word: "
7576
+ w + " into stream: " + stream_name + " exception was: " + str(e))
@@ -93,7 +94,7 @@ def put_words_in_stream_periodically(conn, stream_name, words, period_seconds):
9394
'''
9495
while True:
9596
put_words_in_stream(conn, stream_name, words)
96-
print "Sleeping for {period_seconds} seconds".format(period_seconds=period_seconds)
97+
print("Sleeping for {period_seconds} seconds".format(period_seconds=period_seconds))
9798
time.sleep(period_seconds)
9899

99100
if __name__ == '__main__':
@@ -123,12 +124,12 @@ def put_words_in_stream_periodically(conn, stream_name, words, period_seconds):
123124
Getting a connection to Amazon Kinesis will require that you have your credentials available to
124125
one of the standard credentials providers.
125126
'''
126-
print "Connecting to stream: {s} in {r}".format(s=stream_name, r=args.region)
127+
print("Connecting to stream: {s} in {r}".format(s=stream_name, r=args.region))
127128
conn = kinesis.connect_to_region(region_name = args.region)
128129
try:
129130
status = get_stream_status(conn, stream_name)
130131
if 'DELETING' == status:
131-
print 'The stream: {s} is being deleted, please rerun the script.'.format(s=stream_name)
132+
print('The stream: {s} is being deleted, please rerun the script.'.format(s=stream_name))
132133
sys.exit(1)
133134
elif 'ACTIVE' != status:
134135
wait_for_stream(conn, stream_name)
@@ -138,7 +139,7 @@ def put_words_in_stream_periodically(conn, stream_name, words, period_seconds):
138139
wait_for_stream(conn, stream_name)
139140
# Now the stream should exist
140141
if len(args.words) == 0:
141-
print 'No -w options provided. Waiting on input from STDIN'
142+
print('No -w options provided. Waiting on input from STDIN')
142143
words = [l.strip() for l in sys.stdin.readlines() if l.strip() != '']
143144
else:
144145
words = args.words

Diff for: setup.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@
1212
express or implied. See the License for the specific language governing
1313
permissions and limitations under the License.
1414
"""
15+
from __future__ import print_function
1516
from setuptools import setup
1617
import os, urllib, sys, glob
1718
from setuptools import Command
1819
from setuptools.command.install import install
1920

21+
try:
22+
# Python 3
23+
from urllib.request import urlretrieve
24+
except ImportError:
25+
# Python 2
26+
from urllib import urlretrieve
27+
2028
'''
2129
This script modifies the basic setuptools by adding some functionality to the standard
2230
"install" command and by adding an additional command "download_jars" which
@@ -88,19 +96,19 @@ def download_file(self, url, dest):
8896
'''
8997
Downloads a file at the url to the destination.
9098
'''
91-
print 'Attempting to retrieve remote jar {url}'.format(url=url)
99+
print('Attempting to retrieve remote jar {url}'.format(url=url))
92100
try:
93-
urllib.urlretrieve(url, dest)
94-
print 'Saving {url} -> {dest}'.format(url=url, dest=dest)
101+
urlretrieve(url, dest)
102+
print('Saving {url} -> {dest}'.format(url=url, dest=dest))
95103
except:
96-
print 'Failed to retrieve {url}'.format(url=url)
104+
print('Failed to retrieve {url}'.format(url=url))
97105
return
98106

99107
def download_files(self):
100108
for package in self.packages:
101109
dest = os.path.join(self.destdir, self.package_destination(package[1], package[2]))
102110
if os.path.isfile(dest):
103-
print 'Skipping download of {dest}'.format(dest=dest)
111+
print('Skipping download of {dest}'.format(dest=dest))
104112
else:
105113
url = self.package_url(package[0], package[1], package[2])
106114
self.download_file(url, dest)
@@ -122,13 +130,13 @@ def run(self):
122130
'''
123131
downloader = MavenJarDownloader()
124132
downloader.download_files()
125-
print '''
133+
print('''
126134
Now you should run:
127135
128136
python setup.py install
129137
130138
Which will finish the installation.
131-
'''
139+
''')
132140

133141
class InstallThenCheckForJars(install):
134142

@@ -175,7 +183,7 @@ def run(self):
175183
install.run(self)
176184
missing_jars = downloader.missing_jars()
177185
if len(missing_jars) > 0:
178-
print self.warning_string(missing_jars)
186+
print(self.warning_string(missing_jars))
179187

180188
if __name__ == '__main__':
181189
setup(

0 commit comments

Comments
 (0)