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

Add support for Python 3 #2

Merged
merged 1 commit into from
Jan 27, 2015
Merged
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
5 changes: 3 additions & 2 deletions samples/amazon_kclpy_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
express or implied. See the License for the specific language governing
permissions and limitations under the License.
'''
from __future__ import print_function
from amazon_kclpy import kcl
from glob import glob
import os, argparse, sys, samples
Expand Down Expand Up @@ -140,11 +141,11 @@ def get_kcl_app_command(java, multi_lang_daemon_class, properties, paths=[]):

# Print what the asked for
if args.print_classpath:
print get_kcl_classpath(args.properties, args.paths)
print(get_kcl_classpath(args.properties, args.paths))
elif args.print_command:
if args.java and args.properties:
multi_lang_daemon_class = 'com.amazonaws.services.kinesis.multilang.MultiLangDaemon'
print get_kcl_app_command(args.java, multi_lang_daemon_class, args.properties, paths=args.paths)
print(get_kcl_app_command(args.java, multi_lang_daemon_class, args.properties, paths=args.paths))
else:
sys.stderr.write("Must provide arguments: --java and --properties\n")
parser.print_usage()
Expand Down
9 changes: 5 additions & 4 deletions samples/sample_kclpy_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
express or implied. See the License for the specific language governing
permissions and limitations under the License.
'''
from __future__ import print_function
import sys, time, json, base64
from amazon_kclpy import kcl

Expand Down Expand Up @@ -59,7 +60,7 @@ def checkpoint(self, checkpointer, sequence_number=None):
A ShutdownException indicates that this record processor should be shutdown. This is due to
some failover event, e.g. another MultiLangDaemon has taken the lease for this shard.
'''
print 'Encountered shutdown execption, skipping checkpoint'
print('Encountered shutdown execption, skipping checkpoint')
return
elif 'ThrottlingException' == e.value:
'''
Expand All @@ -70,7 +71,7 @@ def checkpoint(self, checkpointer, sequence_number=None):
sys.stderr.write('Failed to checkpoint after {n} attempts, giving up.\n'.format(n=n))
return
else:
print 'Was throttled while checkpointing, will attempt again in {s} seconds'.format(s=self.SLEEP_SECONDS)
print('Was throttled while checkpointing, will attempt again in {s} seconds'.format(s=self.SLEEP_SECONDS))
elif 'InvalidStateException' == e.value:
sys.stderr.write('MultiLangDaemon reported an invalid state while checkpointing.\n')
else: # Some other error
Expand Down Expand Up @@ -146,10 +147,10 @@ def shutdown(self, checkpointer, reason):
# Checkpointing with no parameter will checkpoint at the
# largest sequence number reached by this processor on this
# shard id
print 'Was told to terminate, will attempt to checkpoint.'
print('Was told to terminate, will attempt to checkpoint.')
self.checkpoint(checkpointer, None)
else: # reason == 'ZOMBIE'
print 'Shutting down due to failover. Will not checkpoint.'
print('Shutting down due to failover. Will not checkpoint.')
except:
pass

Expand Down
15 changes: 8 additions & 7 deletions samples/sample_kinesis_wordputter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
express or implied. See the License for the specific language governing
permissions and limitations under the License.
'''
from __future__ import print_function
import sys, random, time, argparse
from boto import kinesis

Expand Down Expand Up @@ -46,10 +47,10 @@ def wait_for_stream(conn, stream_name):
SLEEP_TIME_SECONDS = 3
status = get_stream_status(conn, stream_name)
while status != 'ACTIVE':
print '{stream_name} has status: {status}, sleeping for {secs} seconds'.format(
print('{stream_name} has status: {status}, sleeping for {secs} seconds'.format(
stream_name = stream_name,
status = status,
secs = SLEEP_TIME_SECONDS)
secs = SLEEP_TIME_SECONDS))
time.sleep(SLEEP_TIME_SECONDS) # sleep for 3 seconds
status = get_stream_status(conn, stream_name)

Expand All @@ -69,7 +70,7 @@ def put_words_in_stream(conn, stream_name, words):
for w in words:
try:
conn.put_record(stream_name, w, w)
print "Put word: " + w + " into stream: " + stream_name
print("Put word: " + w + " into stream: " + stream_name)
except Exception as e:
sys.stderr.write("Encountered an exception while trying to put a word: "
+ w + " into stream: " + stream_name + " exception was: " + str(e))
Expand All @@ -93,7 +94,7 @@ def put_words_in_stream_periodically(conn, stream_name, words, period_seconds):
'''
while True:
put_words_in_stream(conn, stream_name, words)
print "Sleeping for {period_seconds} seconds".format(period_seconds=period_seconds)
print("Sleeping for {period_seconds} seconds".format(period_seconds=period_seconds))
time.sleep(period_seconds)

if __name__ == '__main__':
Expand Down Expand Up @@ -123,12 +124,12 @@ def put_words_in_stream_periodically(conn, stream_name, words, period_seconds):
Getting a connection to Amazon Kinesis will require that you have your credentials available to
one of the standard credentials providers.
'''
print "Connecting to stream: {s} in {r}".format(s=stream_name, r=args.region)
print("Connecting to stream: {s} in {r}".format(s=stream_name, r=args.region))
conn = kinesis.connect_to_region(region_name = args.region)
try:
status = get_stream_status(conn, stream_name)
if 'DELETING' == status:
print 'The stream: {s} is being deleted, please rerun the script.'.format(s=stream_name)
print('The stream: {s} is being deleted, please rerun the script.'.format(s=stream_name))
sys.exit(1)
elif 'ACTIVE' != status:
wait_for_stream(conn, stream_name)
Expand All @@ -138,7 +139,7 @@ def put_words_in_stream_periodically(conn, stream_name, words, period_seconds):
wait_for_stream(conn, stream_name)
# Now the stream should exist
if len(args.words) == 0:
print 'No -w options provided. Waiting on input from STDIN'
print('No -w options provided. Waiting on input from STDIN')
words = [l.strip() for l in sys.stdin.readlines() if l.strip() != '']
else:
words = args.words
Expand Down
24 changes: 16 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@
express or implied. See the License for the specific language governing
permissions and limitations under the License.
"""
from __future__ import print_function
from setuptools import setup
import os, urllib, sys, glob
from setuptools import Command
from setuptools.command.install import install

try:
# Python 3
from urllib.request import urlretrieve
except ImportError:
# Python 2
from urllib import urlretrieve

'''
This script modifies the basic setuptools by adding some functionality to the standard
"install" command and by adding an additional command "download_jars" which
Expand Down Expand Up @@ -88,19 +96,19 @@ def download_file(self, url, dest):
'''
Downloads a file at the url to the destination.
'''
print 'Attempting to retrieve remote jar {url}'.format(url=url)
print('Attempting to retrieve remote jar {url}'.format(url=url))
try:
urllib.urlretrieve(url, dest)
print 'Saving {url} -> {dest}'.format(url=url, dest=dest)
urlretrieve(url, dest)
print('Saving {url} -> {dest}'.format(url=url, dest=dest))
except:
print 'Failed to retrieve {url}'.format(url=url)
print('Failed to retrieve {url}'.format(url=url))
return

def download_files(self):
for package in self.packages:
dest = os.path.join(self.destdir, self.package_destination(package[1], package[2]))
if os.path.isfile(dest):
print 'Skipping download of {dest}'.format(dest=dest)
print('Skipping download of {dest}'.format(dest=dest))
else:
url = self.package_url(package[0], package[1], package[2])
self.download_file(url, dest)
Expand All @@ -122,13 +130,13 @@ def run(self):
'''
downloader = MavenJarDownloader()
downloader.download_files()
print '''
print('''
Now you should run:

python setup.py install

Which will finish the installation.
'''
''')

class InstallThenCheckForJars(install):

Expand Down Expand Up @@ -175,7 +183,7 @@ def run(self):
install.run(self)
missing_jars = downloader.missing_jars()
if len(missing_jars) > 0:
print self.warning_string(missing_jars)
print(self.warning_string(missing_jars))

if __name__ == '__main__':
setup(
Expand Down