diff --git a/demo1-getting-started.py b/demo1-getting-started.py index 3030714..9d0138c 100644 --- a/demo1-getting-started.py +++ b/demo1-getting-started.py @@ -1,3 +1,5 @@ +#Python 2 to Python 3 conversion made by "Usama Tahir" + # Example for Cloud Computing Course Master AI / GSD # # uses libCloud: https://libcloud.apache.org/ @@ -41,6 +43,8 @@ region_name = 'RegionOne' # domain_name = "default" + + def main(): # get the password from user # auth_password = getpass.getpass("Enter your OpenStack password:") @@ -50,7 +54,7 @@ def main(): # make sure to include ex_force_auth_version='3.x_password', as needed in our environment provider = get_driver(Provider.OPENSTACK) - print("Opening connection to %s as %s..." % (auth_url, auth_username)) + print(("Opening connection to %s as %s..." % (auth_url, auth_username))) conn = provider(auth_username, auth_password, @@ -71,6 +75,7 @@ def main(): image = img print(img) + print("Getting flavors...") print("=========================================================================") @@ -124,3 +129,10 @@ def main(): # method that is called when the script is started from the command line if __name__ == '__main__': main() + + + + + + + diff --git a/demo2-instance-with-init-script.py b/demo2-instance-with-init-script.py index 3adb7eb..9d5ca8b 100644 --- a/demo2-instance-with-init-script.py +++ b/demo2-instance-with-init-script.py @@ -1,3 +1,5 @@ +#Python 2 to Python 3 conversion made by "Usama Tahir" + # import getpass # import os @@ -104,7 +106,7 @@ def main(): keypair_exists = True if keypair_exists: - print('Keypair ' + keypair_name + ' already exists. Skipping import.') + print(('Keypair ' + keypair_name + ' already exists. Skipping import.')) else: print('adding keypair...') conn.import_key_pair_from_file(keypair_name, pub_key_file) @@ -128,7 +130,7 @@ def main(): security_group_exists = True if security_group_exists: - print('Security Group ' + all_in_one_security_group.name + ' already exists. Skipping creation.') + print(('Security Group ' + all_in_one_security_group.name + ' already exists. Skipping creation.')) else: all_in_one_security_group = conn.ex_create_security_group(security_group_name, 'network access for all-in-one application.') @@ -159,7 +161,7 @@ def main(): instance_exists = True if instance_exists: - print('Instance ' + testing_instance.name + ' already exists. Skipping creation.') + print(('Instance ' + testing_instance.name + ' already exists. Skipping creation.')) exit() else: print('Starting new all-in-one instance and wait until it is running...') @@ -181,12 +183,12 @@ def main(): private_ip = None if len(testing_instance.private_ips): private_ip = testing_instance.private_ips[0] - print('Private IP found: {}'.format(private_ip)) + print(('Private IP found: {}'.format(private_ip))) public_ip = None if len(testing_instance.public_ips): public_ip = testing_instance.public_ips[0] - print('Public IP found: {}'.format(public_ip)) + print(('Public IP found: {}'.format(public_ip))) print('Checking for unused Floating IP...') unused_floating_ip = None @@ -197,11 +199,11 @@ def main(): if not unused_floating_ip and len(conn.ex_list_floating_ip_pools()): pool = conn.ex_list_floating_ip_pools()[0] - print('Allocating new Floating IP from pool: {}'.format(pool)) + print(('Allocating new Floating IP from pool: {}'.format(pool))) unused_floating_ip = pool.create_floating_ip() if public_ip: - print('Instance ' + testing_instance.name + ' already has a public ip. Skipping attachment.') + print(('Instance ' + testing_instance.name + ' already has a public ip. Skipping attachment.')) elif unused_floating_ip: conn.ex_attach_floating_ip_to_node(testing_instance, unused_floating_ip) @@ -214,7 +216,7 @@ def main(): actual_ip_address = private_ip print('\n') - print('The Fractals app will be deployed to http://{}\n'.format(actual_ip_address)) + print(('The Fractals app will be deployed to http://{}\n'.format(actual_ip_address))) print('You can use ssh to login to the instance using your private key. Default user name for official Ubuntu\n' 'Cloud Images is: ubuntu, so you can use, e.g.: "ssh -i ~/.ssh/id_rsa ubuntu@" if your private\n' @@ -228,3 +230,4 @@ def main(): if __name__ == '__main__': main() + diff --git a/demo3-microservice.py b/demo3-microservice.py index 4c1a5ad..d26fc3f 100644 --- a/demo3-microservice.py +++ b/demo3-microservice.py @@ -1,3 +1,5 @@ +#Python 2 to Python 3 conversion made by "Usama Tahir" + # import getpass # import os @@ -109,7 +111,7 @@ def main(): keypair_exists = True if keypair_exists: - print('Keypair ' + keypair_name + ' already exists. Skipping import.') + print(('Keypair ' + keypair_name + ' already exists. Skipping import.')) else: print('adding keypair...') conn.import_key_pair_from_file(keypair_name, pub_key_file) @@ -133,7 +135,7 @@ def main(): security_group_exists = True if security_group_exists: - print('Worker Security Group ' + worker_security_group.name + ' already exists. Skipping creation.') + print(('Worker Security Group ' + worker_security_group.name + ' already exists. Skipping creation.')) else: worker_security_group = conn.ex_create_security_group('worker', 'for services that run on a worker node') conn.ex_create_security_group_rule(worker_security_group, 'TCP', 22, 22) @@ -148,7 +150,7 @@ def main(): security_group_exists = True if security_group_exists: - print('Controller Security Group ' + controller_security_group.name + ' already exists. Skipping creation.') + print(('Controller Security Group ' + controller_security_group.name + ' already exists. Skipping creation.')) else: controller_security_group = conn.ex_create_security_group('control', 'for services that run on a control node') conn.ex_create_security_group_rule(controller_security_group, 'TCP', 22, 22) @@ -205,11 +207,11 @@ def main(): if not unused_floating_ip: pool = conn.ex_list_floating_ip_pools()[0] - print('Allocating new Floating IP from pool: {}'.format(pool)) + print(('Allocating new Floating IP from pool: {}'.format(pool))) unused_floating_ip = pool.create_floating_ip() conn.ex_attach_floating_ip_to_node(instance_controller_1, unused_floating_ip) - print('Controller Application will be deployed to http://%s' % unused_floating_ip.ip_address) + print(('Controller Application will be deployed to http://%s' % unused_floating_ip.ip_address)) ########################################################################### # @@ -261,11 +263,11 @@ def main(): if not unused_floating_ip: pool = conn.ex_list_floating_ip_pools()[0] - print('Allocating new Floating IP from pool: {}'.format(pool)) + print(('Allocating new Floating IP from pool: {}'.format(pool))) unused_floating_ip = pool.create_floating_ip() conn.ex_attach_floating_ip_to_node(instance_worker_1, unused_floating_ip) - print('The worker will be available for SSH at %s' % unused_floating_ip.ip_address) + print(('The worker will be available for SSH at %s' % unused_floating_ip.ip_address)) print('You can use ssh to login to the controller using your private key. After login, you can list available ' 'fractals using "faafo list". To request the generation of new fractals, you can use "faafo create". ' diff --git a/demo4-scale-out-add-worker.py b/demo4-scale-out-add-worker.py index 14b40b1..cb3d85f 100644 --- a/demo4-scale-out-add-worker.py +++ b/demo4-scale-out-add-worker.py @@ -1,3 +1,5 @@ +#Python 2 to Python 3 conversion made by "Usama Tahir" + # import getpass # import os # import libcloud.security @@ -111,10 +113,10 @@ def main(): for instance in conn.list_nodes(): if instance.name == 'app-services': services_ip = instance.private_ips[0] - print('Found app-services fixed IP to be: ', services_ip) + print(('Found app-services fixed IP to be: ', services_ip)) if instance.name == 'app-api-1': api_1_ip = instance.private_ips[0] - print('Found app-api-1 fixed IP to be: ', api_1_ip) + print(('Found app-api-1 fixed IP to be: ', api_1_ip)) ########################################################################### # @@ -129,7 +131,7 @@ def main(): keypair_exists = True if keypair_exists: - print('Keypair ' + keypair_name + ' already exists. Skipping import.') + print(('Keypair ' + keypair_name + ' already exists. Skipping import.')) else: print('adding keypair...') conn.import_key_pair_from_file(keypair_name, pub_key_file) @@ -145,10 +147,10 @@ def main(): def get_security_group(connection, security_group_name): """A helper function to check if security group already exists""" - print('Checking for existing ' + security_group_name + ' security group...') + print(('Checking for existing ' + security_group_name + ' security group...')) for security_grp in connection.ex_list_security_groups(): if security_grp.name == security_group_name: - print('Security Group ' + security_group_name + ' already exists. Skipping creation.') + print(('Security Group ' + security_group_name + ' already exists. Skipping creation.')) return security_grp return False @@ -188,3 +190,4 @@ def main(): if __name__ == '__main__': main() + diff --git a/demo4-scale-out.py b/demo4-scale-out.py index 86e1a24..1c4ac7d 100644 --- a/demo4-scale-out.py +++ b/demo4-scale-out.py @@ -1,3 +1,5 @@ +#Python 2 to Python 3 conversion made by "Usama Tahir" + # import getpass # import os # import libcloud.security @@ -113,7 +115,7 @@ def main(): keypair_exists = True if keypair_exists: - print('Keypair ' + keypair_name + ' already exists. Skipping import.') + print(('Keypair ' + keypair_name + ' already exists. Skipping import.')) else: print('adding keypair...') conn.import_key_pair_from_file(keypair_name, pub_key_file) @@ -131,7 +133,7 @@ def main(): for instance in conn.list_nodes(): if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3', 'app-controller', 'app-services', 'app-api-1', 'app-api-2']: - print('Destroying Instance: %s' % instance.name) + print(('Destroying Instance: %s' % instance.name)) conn.destroy_node(instance) # wait until all nodes are destroyed to be able to remove depended security groups @@ -149,7 +151,7 @@ def main(): # delete security groups for group in conn.ex_list_security_groups(): if group.name in ['control', 'worker', 'api', 'services']: - print('Deleting security group: %s' % group.name) + print(('Deleting security group: %s' % group.name)) conn.ex_delete_security_group(group) ########################################################################### @@ -160,10 +162,10 @@ def main(): def get_security_group(connection, security_group_name): """A helper function to check if security group already exists""" - print('Checking for existing ' + security_group_name + ' security group...') + print(('Checking for existing ' + security_group_name + ' security group...')) for security_grp in connection.ex_list_security_groups(): if security_grp.name == security_group_name: - print('Security Group ' + security_group_name + ' already exists. Skipping creation.') + print(('Security Group ' + security_group_name + ' already exists. Skipping creation.')) return worker_security_group return False @@ -292,7 +294,7 @@ def main(): for instance in [instance_api_1, instance_api_2]: floating_ip = get_floating_ip(conn) conn.ex_attach_floating_ip_to_node(instance, floating_ip) - print('allocated %(ip)s to %(host)s' % {'ip': floating_ip.ip_address, 'host': instance.name}) + print(('allocated %(ip)s to %(host)s' % {'ip': floating_ip.ip_address, 'host': instance.name})) ########################################################################### # @@ -343,3 +345,4 @@ def main(): if __name__ == '__main__': main() + diff --git a/demo5-1-durable-storage.py b/demo5-1-durable-storage.py index 8ffc581..a700192 100644 --- a/demo5-1-durable-storage.py +++ b/demo5-1-durable-storage.py @@ -1,4 +1,4 @@ -from __future__ import print_function +#Python 2 to Python 3 conversion made by "Usama Tahir" import getpass import os @@ -27,12 +27,12 @@ def main(): if "OS_PROJECT_NAME" in os.environ: project_name = os.environ["OS_PROJECT_NAME"] else: - project_name = input("Enter your OpenStack project:") + project_name = eval(input("Enter your OpenStack project:")) if "OS_USERNAME" in os.environ: auth_username = os.environ["OS_USERNAME"] else: - auth_username = input("Enter your OpenStack username:") + auth_username = eval(input("Enter your OpenStack username:")) if "OS_PASSWORD" in os.environ: auth_password = os.environ["OS_PASSWORD"] @@ -121,3 +121,4 @@ def main(): if __name__ == '__main__': main() + diff --git a/demo5-2-backup-fractals.py b/demo5-2-backup-fractals.py index ff49805..235d37b 100644 --- a/demo5-2-backup-fractals.py +++ b/demo5-2-backup-fractals.py @@ -1,4 +1,5 @@ -from __future__ import print_function +#Python 2 to Python 3 conversion made by "Usama Tahir" + import getpass import json @@ -28,12 +29,12 @@ def main(): if "OS_PROJECT_NAME" in os.environ: project_name = os.environ["OS_PROJECT_NAME"] else: - project_name = input("Enter your OpenStack project:") + project_name = eval(input("Enter your OpenStack project:")) if "OS_USERNAME" in os.environ: auth_username = os.environ["OS_USERNAME"] else: - auth_username = input("Enter your OpenStack username:") + auth_username = eval(input("Enter your OpenStack username:")) if "OS_PASSWORD" in os.environ: auth_password = os.environ["OS_PASSWORD"] @@ -95,3 +96,4 @@ def main(): if __name__ == '__main__': main() + diff --git a/destroy-all-demo-instances.py b/destroy-all-demo-instances.py index c3b1204..7de982b 100644 --- a/destroy-all-demo-instances.py +++ b/destroy-all-demo-instances.py @@ -1,3 +1,5 @@ +#Python 2 to Python 3 conversion made by "Usama Tahir" + # import getpass # import os # import libcloud.security @@ -70,7 +72,7 @@ def main(): for instance in conn.list_nodes(): if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3', 'app-controller', 'app-services', 'app-api-1', 'app-api-2']: - print('Destroying Instance: %s' % instance.name) + print(('Destroying Instance: %s' % instance.name)) conn.destroy_node(instance) # wait until all nodes are destroyed to be able to remove depended security groups @@ -89,9 +91,10 @@ def main(): # delete security groups for group in conn.ex_list_security_groups(): if group.name in ['control', 'worker', 'api', 'services']: - print('Deleting security group: %s' % group.name) + print(('Deleting security group: %s' % group.name)) conn.ex_delete_security_group(group) if __name__ == '__main__': main() + diff --git a/faafo/bin/faafo b/faafo/bin/faafo index a12770d..eb90500 100644 --- a/faafo/bin/faafo +++ b/faafo/bin/faafo @@ -1,3 +1,5 @@ +#Python 2 to Python 3 conversion made by "Usama Tahir" + #!/usr/bin/env python # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -16,69 +18,51 @@ import copy import json import random import uuid - +import requests from oslo_config import cfg from oslo_log import log from prettytable import PrettyTable -import requests - from faafo import version +from uuid import uuid4 +from argparse import ArgumentParser + LOG = log.getLogger('faafo.client') CONF = cfg.CONF - +# Replace the usage of CONF.command with the args namespace object to access the command-line arguments +# Remove the float() conversion when assigning values to xa, xb, ya, and yb since the arguments are already parsed as floats def get_random_task(): random.seed() - if CONF.command.width: - width = int(CONF.command.width) - else: - width = random.randint(int(CONF.command.min_width), - int(CONF.command.max_width)) + width = int(eval(input("Enter the width (or press Enter for a random value): ")) or + random.randint(256, 1024)) - if CONF.command.height: - height = int(CONF.command.height) - else: - height = random.randint(int(CONF.command.min_height), - int(CONF.command.max_height)) + height = int(eval(input("Enter the height (or press Enter for a random value): ")) or + random.randint(256, 1024)) - if CONF.command.iterations: - iterations = int(CONF.command.iterations) - else: - iterations = random.randint(int(CONF.command.min_iterations), - int(CONF.command.max_iterations)) + iterations = int(eval(input("Enter the number of iterations (or press Enter for a random value): ")) or + random.randint(128, 512)) - if CONF.command.xa: - xa = float(CONF.command.xa) - else: - xa = random.uniform(float(CONF.command.min_xa), - float(CONF.command.max_xa)) + xa = float(eval(input("Enter the value for parameter 'xa' (or press Enter for a random value): ")) or + random.uniform(-1.0, -4.0)) - if CONF.command.xb: - xb = float(CONF.command.xb) - else: - xb = random.uniform(float(CONF.command.min_xb), - float(CONF.command.max_xb)) + xb = float(eval(input("Enter the value for parameter 'xb' (or press Enter for a random value): ")) or + random.uniform(1.0, 4.0)) - if CONF.command.ya: - ya = float(CONF.command.ya) - else: - ya = random.uniform(float(CONF.command.min_ya), - float(CONF.command.max_ya)) + ya = float(eval(input("Enter the value for parameter 'ya' (or press Enter for a random value): ")) or + random.uniform(-0.5, -3.0)) - if CONF.command.yb: - yb = float(CONF.command.yb) - else: - yb = random.uniform(float(CONF.command.min_yb), - float(CONF.command.max_yb)) + yb = float(eval(input("Enter the value for parameter 'yb' (or press Enter for a random value): ")) or + random.uniform(0.5, 3.0)) task = { - 'uuid': str(uuid.uuid4()), + 'uuid': str(uuid4()), 'width': width, 'height': height, - 'iterations': iterations, 'xa': xa, + 'iterations': iterations, + 'xa': xa, 'xb': xb, 'ya': ya, 'yb': yb @@ -91,31 +75,35 @@ def do_get_fractal(): LOG.error("command 'download' not yet implemented") + +#Replace the string formatting % operator with f-strings to format the log messages +#Update the URL string formatting to use f-strings +#Use the json() method instead of json.loads() to parse the response content as JSON + def do_show_fractal(): - LOG.info("showing fractal %s" % CONF.command.uuid) - result = requests.get("%s/v1/fractal/%s" % - (CONF.endpoint_url, CONF.command.uuid)) + LOG.info(f"showing fractal {CONF.command.uuid}") + result = requests.get(f"{CONF.endpoint_url}/v1/fractal/{CONF.command.uuid}") if result.status_code == 200: - data = json.loads(result.text) + data = result.json() output = PrettyTable(["Parameter", "Value"]) output.align["Parameter"] = "l" output.align["Value"] = "l" output.add_row(["uuid", data['uuid']]) - output.add_row(["duration", "%f seconds" % data['duration']]) - output.add_row(["dimensions", "%d x %d pixels" % - (data['width'], data['height'])]) + output.add_row(["duration", f"{data['duration']} seconds"]) + output.add_row(["dimensions", f"{data['width']} x {data['height']} pixels"]) output.add_row(["iterations", data['iterations']]) output.add_row(["xa", data['xa']]) output.add_row(["xb", data['xb']]) output.add_row(["ya", data['ya']]) output.add_row(["yb", data['yb']]) - output.add_row(["size", "%d bytes" % data['size']]) + output.add_row(["size", f"{data['size']} bytes"]) output.add_row(["checksum", data['checksum']]) output.add_row(["generated_by", data['generated_by']]) print(output) else: - LOG.error("fractal '%s' not found" % CONF.command.uuid) + LOG.error(f"fractal '{CONF.command.uuid}' not found") +#Update the string formatting in the output.add_row() method to use f-strings def do_list_fractals(): LOG.info("listing all fractals") @@ -125,34 +113,37 @@ def do_list_fractals(): for fractal in fractals: output.add_row([ fractal["uuid"], - "%d x %d pixels" % (fractal["width"], fractal["height"]), - "%d bytes" % (fractal["size"] or 0), + f"{fractal['width']} x {fractal['height']} pixels", + f"{fractal['size'] or 0} bytes" ]) print(output) +# Replace the recursive call to get_fractals() with a loop to iterate over the pages and accumulate the fractals def get_fractals(page=1): - result = requests.get("%s/v1/fractal?page=%d" % - (CONF.endpoint_url, page)) - fractals = [] - if result.status_code == 200: - data = json.loads(result.text) - if page < data['total_pages']: - fractals = data['objects'] + get_fractals(page + 1) + while True: + result = requests.get(f"{CONF.endpoint_url}/v1/fractal?page={page}") + if result.status_code == 200: + data = result.json() + fractals.extend(data['objects']) + if page < data['total_pages']: + page += 1 + else: + break else: - return data['objects'] - + break return fractals - +#Update the logging statements to use f-strings for string interpolation def do_delete_fractal(): - LOG.info("deleting fractal %s" % CONF.command.uuid) - result = requests.delete("%s/v1/fractal/%s" % - (CONF.endpoint_url, CONF.command.uuid)) - LOG.debug("result: %s" %result) + LOG.info(f"deleting fractal {CONF.command.uuid}") + result = requests.delete(f"{CONF.endpoint_url}/v1/fractal/{CONF.command.uuid}") + LOG.debug(f"result: {result}") +# Replace the use of xrange with range for the for loop +# Update the json.dumps() function call to pass the JSON object directly def do_create_fractal(): random.seed() if CONF.command.tasks: @@ -160,26 +151,30 @@ def do_create_fractal(): else: number = random.randint(int(CONF.command.min_tasks), int(CONF.command.max_tasks)) - LOG.info("generating %d task(s)" % number) - for i in xrange(0, number): + LOG.info(f"generating {number} task(s)") + for i in range(0, number): task = get_random_task() - LOG.debug("created task %s" % task) - # NOTE(berendt): only necessary when using requests < 2.4.2 + LOG.debug(f"created task {task}") + # NOTE: only necessary when using requests < 2.4.2 headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} - requests.post("%s/v1/fractal" % CONF.endpoint_url, - json.dumps(task), headers=headers) + requests.post(f"{CONF.endpoint_url}/v1/fractal", json=task, headers=headers) + +# Replace the use of subparser.add_parser() with ArgumentParser.add_parser() for creating subparsers +# Replace the use of set_defaults() with assigning the function directly to the func attribute of the parser +# Replace the use of CONF.register_cli_opts() with parser.add_argument() to define the command-line options +# Replace the use of CONF.command.func() with args.func() to call the appropriate function based on the command def add_command_parsers(subparsers): - parser = subparsers.add_parser('create') + parser = ArgumentParser(add_help=False) parser.set_defaults(func=do_create_fractal) parser.add_argument("--height", default=None, - help="The height of the generate image.") + help="The height of the generated image.") parser.add_argument("--min-height", default=256, - help="The minimum height of the generate image.") + help="The minimum height of the generated image.") parser.add_argument("--max-height", default=1024, - help="The maximum height of the generate image.") + help="The maximum height of the generated image.") parser.add_argument("--width", default=None, help="The width of the generated image.") parser.add_argument("--min-width", default=256, @@ -239,29 +234,23 @@ def add_command_parsers(subparsers): parser.set_defaults(func=do_list_fractals) -client_commands = cfg.SubCommandOpt('command', title='Commands', - help='Show available commands.', - handler=add_command_parsers) +if __name__ == '__main__': + parser = ArgumentParser() + subparsers = parser.add_subparsers(dest='command') + add_command_parsers(subparsers) -CONF.register_cli_opts([client_commands]) + args = parser.parse_args() -client_cli_opts = [ - cfg.StrOpt('endpoint-url', - default='http://localhost', - help='API connection URL') -] + log.register_options(cfg.CONF) + log.set_defaults() -CONF.register_cli_opts(client_cli_opts) + cfg.CONF(project='client', prog='faafo-client', + version=version.version_info.version_string()) + log.setup(cfg.CONF, 'client', + version=version.version_info.version_string()) -if __name__ == '__main__': - log.register_options(CONF) - log.set_defaults() + args.func() - CONF(project='client', prog='faafo-client', - version=version.version_info.version_string()) - log.setup(CONF, 'client', - version=version.version_info.version_string()) - CONF.command.func() diff --git a/faafo/bin/faafo-worker b/faafo/bin/faafo-worker index 67daf0b..ddd725a 100644 --- a/faafo/bin/faafo-worker +++ b/faafo/bin/faafo-worker @@ -1,3 +1,6 @@ +#Python 2 to Python 3 conversion made by "Usama Tahir" + + #!/usr/bin/env python # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -50,3 +53,4 @@ if __name__ == '__main__': server.run() except KeyboardInterrupt: LOG.info("Caught keyboard interrupt. Exiting.") + diff --git a/faafo/contrib/test_api.py b/faafo/contrib/test_api.py index 559ac3d..d89bb28 100644 --- a/faafo/contrib/test_api.py +++ b/faafo/contrib/test_api.py @@ -34,11 +34,11 @@ assert response.status_code == 201 response = requests.get(url, headers=headers) assert response.status_code == 200 -print(response.json()) +print((response.json())) response = requests.get(url + '/' + uuid, headers=headers) assert response.status_code == 200 -print(response.json()) +print((response.json())) data = { 'checksum': 'c6fef4ef13a577066c2281b53c82ce2c7e94e', @@ -50,7 +50,8 @@ assert response.status_code == 200 response = requests.get(url + '/' + uuid, headers=headers) assert response.status_code == 200 -print(response.json()) +print((response.json())) response = requests.delete(url + '/' + uuid, headers=headers) assert response.status_code == 204 + diff --git a/faafo/doc/source/conf.py b/faafo/doc/source/conf.py index d78328c..62aab1a 100644 --- a/faafo/doc/source/conf.py +++ b/faafo/doc/source/conf.py @@ -1,3 +1,6 @@ +#Python 2 to Python 3 conversion made by "Usama Tahir" + + # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -11,7 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -copyright = u'2015, OpenStack contributors' +copyright = '2015, OpenStack contributors' master_doc = 'index' -project = u'First App Application for OpenStack' +project = 'First App Application for OpenStack' source_suffix = '.rst' + diff --git a/faafo/faafo/api/service.py b/faafo/faafo/api/service.py index a3093c7..b60fa17 100644 --- a/faafo/faafo/api/service.py +++ b/faafo/faafo/api/service.py @@ -1,3 +1,6 @@ +#Python 2 to Python 3 conversion made by "Usama Tahir" + + # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at @@ -12,10 +15,12 @@ import base64 import copy -import cStringIO -from pkg_resources import resource_filename - +import io +import io import flask +from pkg_resources import resource_filename +from flask import Flask, render_template, jsonify +from flask_sqlalchemy import SQLAlchemy from flask_restless import APIManager from flask_sqlalchemy import SQLAlchemy from flask_bootstrap import Bootstrap @@ -25,7 +30,6 @@ from oslo_config import cfg from oslo_log import log from PIL import Image from sqlalchemy.dialects import mysql - from faafo import queues from faafo import version @@ -100,27 +104,45 @@ manager = APIManager(app, flask_sqlalchemy_db=db) connection = Connection(CONF.transport_url) +# @app.route('/', methods=['GET']) +# @app.route('/index', methods=['GET']) +# @app.route('/index/', methods=['GET']) +# def index(page=1): +# fractals = Fractal.query.filter( +# (Fractal.checksum != None) & (Fractal.size != None)).paginate( # noqa +# page, 5, error_out=False) +# return flask.render_template('index.html', fractals=fractals) + + +app = Flask(__name__) +db = SQLAlchemy(app) + @app.route('/', methods=['GET']) @app.route('/index', methods=['GET']) @app.route('/index/', methods=['GET']) def index(page=1): fractals = Fractal.query.filter( - (Fractal.checksum != None) & (Fractal.size != None)).paginate( # noqa + (Fractal.checksum.isnot(None)) & (Fractal.size.isnot(None))).paginate( page, 5, error_out=False) - return flask.render_template('index.html', fractals=fractals) + return render_template('index.html', fractals=fractals) + +# cStringIO module is replaced with the built-in io module. +# cStringIO.StringIO is replaced with io.BytesIO for working with bytes. +# The image_data is decoded using base64.b64decode which returns bytes. +# The response is created using flask.make_response instead of flask.Response. + @app.route('/fractal/', methods=['GET']) def get_fractal(fractalid): fractal = Fractal.query.filter_by(uuid=fractalid).first() if not fractal: - response = flask.jsonify({'code': 404, - 'message': 'Fracal not found'}) + response = flask.jsonify({'code': 404, 'message': 'Fracal not found'}) response.status_code = 404 else: image_data = base64.b64decode(fractal.image) - image = Image.open(cStringIO.StringIO(image_data)) - output = cStringIO.StringIO() + image = Image.open(io.BytesIO(image_data)) + output = io.BytesIO() image.save(output, "PNG") image.seek(0) response = flask.make_response(output.getvalue()) @@ -129,6 +151,7 @@ def get_fractal(fractalid): return response + def generate_fractal(**kwargs): with producers[connection].acquire(block=True) as producer: producer.publish(kwargs['result'], diff --git a/faafo/faafo/queues.py b/faafo/faafo/queues.py index 4e5a6fd..07402dc 100644 --- a/faafo/faafo/queues.py +++ b/faafo/faafo/queues.py @@ -1,3 +1,5 @@ +#Python 2 to Python 3 conversion made by "Usama Tahir" + # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at @@ -11,7 +13,6 @@ # under the License. import copy - import kombu from oslo_config import cfg @@ -30,3 +31,4 @@ cfg.CONF.register_opts(queues_opts) def list_opts(): """Entry point for oslo-config-generator.""" return [(None, copy.deepcopy(queues_opts))] + diff --git a/faafo/faafo/version.py b/faafo/faafo/version.py index 7a68690..6773024 100644 --- a/faafo/faafo/version.py +++ b/faafo/faafo/version.py @@ -1,3 +1,5 @@ +#Python 2 to Python 3 conversion made by "Usama Tahir" + # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at diff --git a/faafo/faafo/worker/service.py b/faafo/faafo/worker/service.py index d1bb193..fc51001 100644 --- a/faafo/faafo/worker/service.py +++ b/faafo/faafo/worker/service.py @@ -1,3 +1,5 @@ +#Python 2 to Python 3 conversion made by "Usama Tahir" + #!/usr/bin/env python # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -52,8 +54,7 @@ def list_opts(): class JuliaSet(object): - def __init__(self, width, height, xa=-2.0, xb=2.0, ya=-1.5, yb=1.5, - iterations=255): + def __init__(self, width, height, xa=-2.0, xb=2.0, ya=-1.5, yb=1.5, iterations=255): self.xa = xa self.xb = xb self.ya = ya @@ -100,27 +101,40 @@ class JuliaSet(object): return (c, z) -class Worker(ConsumerMixin): +import time +import os +import base64 +import hashlib +import socket +import json +import requests +from kombu.mixins import ConsumerMixin + +class Worker(ConsumerMixin): def __init__(self, connection): self.connection = connection def get_consumers(self, Consumer, channel): - return [Consumer(queues=queues.task_queue, - accept=['json'], - callbacks=[self.process])] + return [Consumer( + queues=queues.task_queue, + accept=['json'], + callbacks=[self.process] + )] def process(self, task, message): LOG.info("processing task %s" % task['uuid']) LOG.debug(task) start_time = time.time() - juliaset = JuliaSet(task['width'], - task['height'], - task['xa'], - task['xb'], - task['ya'], - task['yb'], - task['iterations']) + juliaset = JuliaSet( + task['width'], + task['height'], + task['xa'], + task['xb'], + task['ya'], + task['yb'], + task['iterations'] + ) elapsed_time = time.time() - start_time LOG.info("task %s processed in %f seconds" % (task['uuid'], elapsed_time)) @@ -130,7 +144,7 @@ class Worker(ConsumerMixin): (task['uuid'], filename)) with open(filename, "rb") as fp: size = os.fstat(fp.fileno()).st_size - image = base64.b64encode(fp.read()) + image = base64.b64encode(fp.read()).decode('utf-8') checksum = hashlib.sha256(open(filename, 'rb').read()).hexdigest() os.remove(filename) LOG.debug("removed temporary file %s" % filename) @@ -145,12 +159,20 @@ class Worker(ConsumerMixin): } # NOTE(berendt): only necessary when using requests < 2.4.2 - headers = {'Content-type': 'application/json', - 'Accept': 'text/plain'} + headers = { + 'Content-type': 'application/json', + 'Accept': 'text/plain' + } - requests.put("%s/v1/fractal/%s" % - (CONF.endpoint_url, str(task['uuid'])), - json.dumps(result), headers=headers) + requests.put( + f"{CONF.endpoint_url}/v1/fractal/{str(task['uuid'])}", + data=json.dumps(result), + headers=headers + ) message.ack() return result + + + + diff --git a/faafo/setup.py b/faafo/setup.py index ee06f22..03e9a05 100644 --- a/faafo/setup.py +++ b/faafo/setup.py @@ -1,3 +1,5 @@ +#Python 2 to Python 3 conversion made by "Usama Tahir" + #!/usr/bin/env python # Copyright (c) 2013 Hewlett-Packard Development Company, L.P. #