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

Fixes for cli python3 #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions vspk/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)


Expand Down
19 changes: 9 additions & 10 deletions vspk/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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...
Expand Down
20 changes: 10 additions & 10 deletions vspk/cli/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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={}):
Expand All @@ -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 = []
Expand All @@ -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):
Expand All @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions vspk/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down