From e7f94e4b3e5327c0bd455380a1dfa8cb14a7a46e Mon Sep 17 00:00:00 2001 From: Anatoly Kolpakov Date: Tue, 15 Dec 2020 18:59:59 +0000 Subject: [PATCH 1/3] fixes for python3 --- vspk/cli/cli.py | 8 ++++---- vspk/cli/commands.py | 16 ++++++++-------- vspk/cli/printer.py | 20 ++++++++++---------- vspk/cli/utils.py | 8 ++++---- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/vspk/cli/cli.py b/vspk/cli/cli.py index f7201a76..e43b56d5 100644 --- a/vspk/cli/cli.py +++ b/vspk/cli/cli.py @@ -42,9 +42,9 @@ def __call__(self, parser, namespace, values, option_string=None): for subparsers_action in subparsers_actions: - for choice, subparser in subparsers_action.choices.items(): - print("\n{}:\n{}".format(choice.upper(), "-" * (len(choice) + 1))) - print(subparser.format_help()) + for choice, subparser in list(subparsers_action.choices.items()): + print(("\n{}:\n{}".format(choice.upper(), "-" * (len(choice) + 1)))) + print((subparser.format_help())) parser.exit() @@ -133,7 +133,7 @@ def main(argv=sys.argv): args = parser.parse_args() - from commands import CLICommand + from .commands import CLICommand CLICommand.execute(args) diff --git a/vspk/cli/commands.py b/vspk/cli/commands.py index 34ef2d05..76d4d32a 100644 --- a/vspk/cli/commands.py +++ b/vspk/cli/commands.py @@ -141,7 +141,7 @@ def show(cls, args): try: (instance, connection) = instance.fetch() - except Exception, e: + except Exception as e: Printer.raise_error("Could not find '%s' with id '%s'. Activate verbose mode for more information:\n%s" % (name, args.id, e)) if not args.json: @@ -164,7 +164,7 @@ def create(cls, args): try: (instance, connection) = parent.create_child(instance) - except Exception, e: + except Exception as e: Printer.raise_error("Cannot create %s:\n%s" % (name, e)) if not args.json: @@ -187,14 +187,14 @@ def update(cls, args): try: (instance, connection) = instance.fetch() - except Exception, e: + except Exception as e: Printer.raise_error("Could not find '%s' with id '%s'. Activate verbose mode for more information:\n%s" % (name, args.id, e)) cls._fill_instance_with_attributes(instance, attributes) try: (instance, connection) = instance.save() - except Exception, e: + except Exception as e: Printer.raise_error("Cannot update %s:\n%s" % (name, e)) if not args.json: @@ -279,7 +279,7 @@ def delete(cls, args): try: (instance, connection) = instance.delete() - except Exception, e: + except Exception as e: Printer.raise_error("Could not delete '%s' with id '%s'. Activate verbose mode for more information:\n%s" % (name, args.id, e)) Printer.success("%s with ID=%s has been deleted" % (name, instance.id)) @@ -417,7 +417,7 @@ def _internal_assign(cls, args, method): try: (references, connection) = resource.assign(final_objects, object_class) - except Exception, e: + except Exception as e: Printer.raise_error('Cannot assign %s:\n%s' % (name, e)) if args.ids is None: @@ -437,7 +437,7 @@ def _fill_instance_with_attributes(cls, instance, attributes): The instance filled or throw an exception """ - for attribute_name, attribute_value in attributes.iteritems(): + for attribute_name, attribute_value in attributes.items(): attribute = instance.get_attribute_infos(attribute_name) if attribute is None: @@ -449,7 +449,7 @@ def _fill_instance_with_attributes(cls, instance, attributes): else: value = attribute.attribute_type(attribute_value) setattr(instance, attribute_name, value) - except Exception, e: + except Exception as e: Printer.raise_error("Attribute %s could not be set with value %s\n%s" % (attribute_name, attribute_value, e)) # TODO-CS: Remove validation when we will have all attribute information from Swagger... diff --git a/vspk/cli/printer.py b/vspk/cli/printer.py index e56309a6..94dc77d2 100644 --- a/vspk/cli/printer.py +++ b/vspk/cli/printer.py @@ -52,7 +52,7 @@ def colorprint(cls, message, color=""): message: the message to print """ - print(color + message + Style.RESET_ALL) + print((color + message + Style.RESET_ALL)) @classmethod def raise_error(cls, message): @@ -122,16 +122,16 @@ def json(cls, data, fields): print(data) elif isinstance(data, dict): - print(json.dumps(data, indent=4)) + print((json.dumps(data, indent=4))) elif isinstance(data, list): results = [] for obj in data: results.append(cls._object_to_dict(obj, fields)) - print(json.dumps(results, indent=4)) + print((json.dumps(results, indent=4))) else: - print(json.dumps(cls._object_to_dict(data, fields), indent=4)) + print((json.dumps(cls._object_to_dict(data, fields), indent=4))) @classmethod def tabulate(cls, data, fields, headers={}): @@ -142,10 +142,10 @@ def tabulate(cls, data, fields, headers={}): """ if isinstance(data, str): - print tabulate([[data]], tablefmt=Printer.TABULATE_FORMAT) + print(tabulate([[data]], tablefmt=Printer.TABULATE_FORMAT)) elif isinstance(data, dict): - print tabulate([data], headers=headers, tablefmt=Printer.TABULATE_FORMAT) + print(tabulate([data], headers=headers, tablefmt=Printer.TABULATE_FORMAT)) elif isinstance(data, list): results = [] @@ -156,12 +156,12 @@ def tabulate(cls, data, fields, headers={}): else: results.append([obj]) - print tabulate(results, headers=headers, tablefmt=Printer.TABULATE_FORMAT) + print(tabulate(results, headers=headers, tablefmt=Printer.TABULATE_FORMAT)) else: dictionary = cls._object_to_dict(data, fields) - result = [(key, value) for key, value in dictionary.iteritems()] - print tabulate(result, headers=headers, tablefmt=Printer.TABULATE_FORMAT) + result = [(key, value) for key, value in dictionary.items()] + print(tabulate(result, headers=headers, tablefmt=Printer.TABULATE_FORMAT)) @classmethod def _object_to_dict(cls, obj, fields=None): @@ -173,7 +173,7 @@ def _object_to_dict(cls, obj, fields=None): if fields is None or "ALL" in fields: return default_dict - known_fields = default_dict.keys() + known_fields = list(default_dict.keys()) ordered_dict = OrderedDict() for field in fields: diff --git a/vspk/cli/utils.py b/vspk/cli/utils.py index 64ce8182..e45ef2a6 100644 --- a/vspk/cli/utils.py +++ b/vspk/cli/utils.py @@ -59,8 +59,8 @@ def _clean_name(cls, string): "IPID": "IpID" } - rep = dict((re.escape(k), v) for k, v in rep.iteritems()) - pattern = re.compile("|".join(rep.keys())) + rep = dict((re.escape(k), v) for k, v in rep.items()) + pattern = re.compile("|".join(list(rep.keys()))) return pattern.sub(lambda m: rep[re.escape(m.group(0))], string) @classmethod @@ -174,7 +174,7 @@ def get_all_objects(self): """ Returns all objects available """ - resources = self._objects_mapping.keys() + resources = list(self._objects_mapping.keys()) resources = [Utils.get_entity_name_plural(name) for name in resources if name not in self._ignored_resources] return resources @@ -232,7 +232,7 @@ def get_parent(self, parent_infos, root_object): try: (parent, connection) = parent.fetch() - except Exception, ex: + except Exception as ex: Printer.raise_error("Failed fetching parent %s with uuid %s\n%s" % (name, uuid, ex)) return parent From 2c3090a65ffbee0379fecc3c3cabc55e29663d7b Mon Sep 17 00:00:00 2001 From: Anatoly Kolpakov Date: Wed, 16 Dec 2020 10:43:58 +0000 Subject: [PATCH 2/3] argparse --- vspk/cli/cli.py | 3 +++ vspk/cli/commands.py | 3 +-- vspk/cli/printer.py | 10 +++++----- vspk/cli/utils.py | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/vspk/cli/cli.py b/vspk/cli/cli.py index e43b56d5..0cb0c2ed 100644 --- a/vspk/cli/cli.py +++ b/vspk/cli/cli.py @@ -132,6 +132,9 @@ def main(argv=sys.argv): objects_parser.add_argument("-c", "--child", dest="child", help="Filter by children (ex: -c domain)") args = parser.parse_args() + if args.command == None: + parser.print_help() + exit(0) from .commands import CLICommand CLICommand.execute(args) diff --git a/vspk/cli/commands.py b/vspk/cli/commands.py index 76d4d32a..fce5e5cc 100644 --- a/vspk/cli/commands.py +++ b/vspk/cli/commands.py @@ -39,7 +39,6 @@ class CLICommand(object): @classmethod def execute(cls, args): """ Execute CLI command """ - func = getattr(cls, args.command) cls._check_arguments(args) func(args) @@ -437,7 +436,7 @@ def _fill_instance_with_attributes(cls, instance, attributes): The instance filled or throw an exception """ - for attribute_name, attribute_value in attributes.items(): + for attribute_name, attribute_value in list(attributes.items()): attribute = instance.get_attribute_infos(attribute_name) if attribute is None: diff --git a/vspk/cli/printer.py b/vspk/cli/printer.py index 94dc77d2..c62b1087 100644 --- a/vspk/cli/printer.py +++ b/vspk/cli/printer.py @@ -142,10 +142,10 @@ def tabulate(cls, data, fields, headers={}): """ if isinstance(data, str): - print(tabulate([[data]], tablefmt=Printer.TABULATE_FORMAT)) + print((tabulate([[data]], tablefmt=Printer.TABULATE_FORMAT))) elif isinstance(data, dict): - print(tabulate([data], headers=headers, tablefmt=Printer.TABULATE_FORMAT)) + print((tabulate([data], headers=headers, tablefmt=Printer.TABULATE_FORMAT))) elif isinstance(data, list): results = [] @@ -156,12 +156,12 @@ def tabulate(cls, data, fields, headers={}): else: results.append([obj]) - print(tabulate(results, headers=headers, tablefmt=Printer.TABULATE_FORMAT)) + print((tabulate(results, headers=headers, tablefmt=Printer.TABULATE_FORMAT))) else: dictionary = cls._object_to_dict(data, fields) - result = [(key, value) for key, value in dictionary.items()] - print(tabulate(result, headers=headers, tablefmt=Printer.TABULATE_FORMAT)) + result = [(key, value) for key, value in list(dictionary.items())] + print((tabulate(result, headers=headers, tablefmt=Printer.TABULATE_FORMAT))) @classmethod def _object_to_dict(cls, obj, fields=None): diff --git a/vspk/cli/utils.py b/vspk/cli/utils.py index e45ef2a6..4e219152 100644 --- a/vspk/cli/utils.py +++ b/vspk/cli/utils.py @@ -59,7 +59,7 @@ def _clean_name(cls, string): "IPID": "IpID" } - rep = dict((re.escape(k), v) for k, v in rep.items()) + rep = dict((re.escape(k), v) for k, v in list(rep.items())) pattern = re.compile("|".join(list(rep.keys()))) return pattern.sub(lambda m: rep[re.escape(m.group(0))], string) From 4495882c6bcbb1ef51b14b9f4dc7efe46476ff50 Mon Sep 17 00:00:00 2001 From: Anatoly Kolpakov Date: Wed, 16 Dec 2020 12:27:53 +0000 Subject: [PATCH 3/3] page_size fix --- vspk/cli/commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vspk/cli/commands.py b/vspk/cli/commands.py index fce5e5cc..d44734d0 100644 --- a/vspk/cli/commands.py +++ b/vspk/cli/commands.py @@ -72,7 +72,7 @@ def list(cls, args): Printer.raise_error(error_message) - if hasattr(args, 'page_size') and args.page_size > 0: + if hasattr(args, 'page_size') and args.page_size != None and args.page_size > 0: (fetcher, parent, objects) = fetcher.fetch(filter=args.filter, query_parameters=query_parameters, page=args.page, page_size=args.page_size) else: page = 0