diff --git a/vspk/cli/cli.py b/vspk/cli/cli.py index f7201a76..0cb0c2ed 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() @@ -132,8 +132,11 @@ 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 + from .commands import CLICommand CLICommand.execute(args) diff --git a/vspk/cli/commands.py b/vspk/cli/commands.py index 34ef2d05..d44734d0 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) @@ -73,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 @@ -141,7 +140,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 +163,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 +186,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 +278,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 +416,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 +436,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 list(attributes.items()): attribute = instance.get_attribute_infos(attribute_name) if attribute is None: @@ -449,7 +448,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..c62b1087 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 list(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..4e219152 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 list(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