Skip to content

Commit 79c6f6b

Browse files
authored
Merge pull request #7 from cambid/feat/python3
fix get_objects.py for python3
2 parents e326080 + 95f50f9 commit 79c6f6b

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

get_objects.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ def process_args_and_login(parser=None, client=None, showparameter=None, fields=
4040
debug = True
4141
if (debug or __debug__) and args.log_file is not None:
4242
try:
43-
log_file = open(args.log_file, "wb")
43+
log_file = open(args.log_file[0], "w")
4444
except IOError:
4545
debug_log("Could not open given log file for writing, sending debug information to stderr.")
4646
# open the output file if given one
47-
output_file = open(args.output[0], "wb") if args.output else None
47+
output_file = open(args.output[0], "w") if args.output else None
4848
output_file_format = args.format[0].lower()
4949
user_created = (args.user_created[0].lower() == "true") if args.user_created else True
5050

@@ -141,7 +141,7 @@ def export_json(data, fields, output_file, output_file_format, user_created, app
141141

142142
# getting the raw JSON data from the management server for commands such as show-...s
143143
def get_raw_data(param, payload=None, container_keys="objects", client=None):
144-
if isinstance(container_keys, basestring):
144+
if isinstance(container_keys, str):
145145
container_keys = [container_keys]
146146
# trying to log in to the management server
147147
if not payload:
@@ -213,7 +213,7 @@ def format_objects(data, fields_dict, output_file_format, user_created=True, dum
213213
res = {}
214214

215215
# this loop removes unnecessary dicts and lists and adds the good values to extracted_fields
216-
for key in flat.keys():
216+
for key in flat.copy().keys():
217217
try:
218218
if isinstance(flat[key], (list, dict)):
219219
del flat[key]
@@ -232,7 +232,7 @@ def format_objects(data, fields_dict, output_file_format, user_created=True, dum
232232
if "whitelist" in fields_dict:
233233
if len(fields_dict["whitelist"]) > 0:
234234
for pattern in fields_dict["whitelist"]:
235-
for key in flat.keys():
235+
for key in flat.copy().keys():
236236
# match keys with the given patterns
237237
if key_matches(key, pattern):
238238
res[key] = flat[key]
@@ -329,7 +329,7 @@ def format_objects(data, fields_dict, output_file_format, user_created=True, dum
329329
# searches for values that are matched with keys that match the regex
330330
def search_dict(dictionary, regex):
331331
result = []
332-
if isinstance(regex, basestring):
332+
if isinstance(regex, str):
333333
regex = re.compile(regex)
334334
for key in dictionary:
335335
if key_matches(key, regex):
@@ -385,8 +385,8 @@ def key_matches(key, pattern):
385385

386386
def key_matches(key, pattern):
387387
if pattern:
388-
if isinstance(pattern, basestring):
389-
return re.search(re.escape(pattern) if isinstance(pattern, unicode) else pattern, key)
388+
if isinstance(pattern, str):
389+
return re.search(pattern, key)
390390
else:
391391
return pattern.search(key)
392392
return False
@@ -476,7 +476,7 @@ def get_dict_len(d):
476476
# unravels a json tree into a dict with all the key-value pairs on the first level, with keys written as follows: 'k1.k2.7.k3' (7 for signifying the 8th element in the list k2)
477477
def flatten_json(jsondata):
478478
if isinstance(jsondata, dict):
479-
jdkeys = jsondata.keys()
479+
jdkeys = jsondata.copy().keys()
480480
for key in jdkeys:
481481
merge_flat_dicts(jsondata, flatten_json(jsondata[key]), key + '.')
482482
return jsondata
@@ -512,8 +512,8 @@ def flat_json_to_csv(jsondata, fields_order, print_column_names=True):
512512
for key in ordered_keys:
513513
var_to_append = ""
514514
if len(jsondata[key]) > i and jsondata[key][i] is not None:
515-
if isinstance(jsondata[key][i], unicode):
516-
var_to_append = jsondata[key][i].encode("utf-8")
515+
if isinstance(jsondata[key][i], str):
516+
var_to_append = jsondata[key][i]
517517
else:
518518
# var is int
519519
var_to_append = str(jsondata[key][i])
@@ -572,7 +572,7 @@ def get_fields_order_and_replace(fields, whitelist, translate):
572572
temp_keys.append(zfill_key(key))
573573
fields_order += sorted(temp_keys)
574574
# iterate over the list of remaining items in indexer, those that don't fit in a specific whitelisted field. general replacer
575-
for k, v in indexer.iteritems():
575+
for k, v in indexer.items():
576576
for i in range(len(fields_order)):
577577
if key_matches(fields_order[i], k):
578578
for sub_pair in translate[v][1]:
@@ -691,14 +691,14 @@ def get_username_and_password(username=None, password=None):
691691
debug_log("Trying to get username and password.")
692692
# getting username and password if nothing else worked
693693
if username is None:
694-
username = raw_input("Enter username: ")
694+
username = input("Enter username: ")
695695
if password is None:
696696
# getpass only works in a tty:
697697
if sys.stdin.isatty():
698698
password = getpass.getpass("Enter password: ")
699699
else:
700700
print("Attention! Your password will be shown on the screen!", file=sys.stderr)
701-
password = raw_input("Enter password: ")
701+
password = input("Enter password: ")
702702
return username, password
703703

704704

@@ -736,7 +736,7 @@ def order_data(self):
736736
uidlist = []
737737
result = []
738738
extras = []
739-
for k, v in self.treenodes_map.iteritems():
739+
for k, v in self.treenodes_map.items():
740740
# it has no parents -> it is a root of a tree
741741
if not v.parents:
742742
uidlist += v.postorder_traverse()

0 commit comments

Comments
 (0)